Refactor admin procedures into separate files
Extract admin procedures from router.ts into dedicated files under procedures/admin/ with consolidated exports via _routes.ts. Adds shared helper functions for response transformation and includes race condition fixes via transaction-scoped existence checks. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
36
apps/api-server/src/procedures/admin/orgs/delete.ts
Normal file
36
apps/api-server/src/procedures/admin/orgs/delete.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* admin.orgs.delete - Delete organization and all related records
|
||||
*/
|
||||
|
||||
import { ORPCError } from "@orpc/server";
|
||||
import { authMiddleware, os, superuserMiddleware } from "../../base.js";
|
||||
|
||||
export const adminOrgsDelete = os.admin.orgs.delete
|
||||
.use(authMiddleware)
|
||||
.use(superuserMiddleware)
|
||||
.handler(async ({ input, context }) => {
|
||||
const { slug } = input;
|
||||
|
||||
// Delete org and related records in transaction
|
||||
await context.db.transaction().execute(async (trx) => {
|
||||
const org = await trx
|
||||
.selectFrom("orgs")
|
||||
.where("slug", "=", slug)
|
||||
.select(["id"])
|
||||
.executeTakeFirst();
|
||||
if (!org) {
|
||||
throw new ORPCError("NOT_FOUND", { message: "Organization not found" });
|
||||
}
|
||||
|
||||
await trx
|
||||
.deleteFrom("org_invites")
|
||||
.where("org_id", "=", org.id)
|
||||
.execute();
|
||||
await trx.deleteFrom("org_sites").where("org_id", "=", org.id).execute();
|
||||
await trx
|
||||
.deleteFrom("org_members")
|
||||
.where("org_id", "=", org.id)
|
||||
.execute();
|
||||
await trx.deleteFrom("orgs").where("id", "=", org.id).execute();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user