feat(organization): prevent deletion of last owned organization

This commit is contained in:
Mauricio Siu
2025-03-02 22:32:56 -06:00
parent 4da4e1c17d
commit 5db7508530

View File

@@ -133,6 +133,18 @@ export const organizationRouter = createTRPCRouter({
});
}
const ownerOrgs = await db.query.organization.findMany({
where: eq(organization.ownerId, ctx.user.id),
});
if (ownerOrgs.length <= 1) {
throw new TRPCError({
code: "FORBIDDEN",
message:
"You must maintain at least one organization where you are the owner",
});
}
const result = await db
.delete(organization)
.where(eq(organization.id, input.organizationId));