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:
50
apps/api-server/src/procedures/admin/orgs/update.ts
Normal file
50
apps/api-server/src/procedures/admin/orgs/update.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* admin.orgs.update - Update organization
|
||||
*/
|
||||
|
||||
import { ORPCError } from "@orpc/server";
|
||||
import { authMiddleware, os, superuserMiddleware } from "../../base.js";
|
||||
|
||||
export const adminOrgsUpdate = os.admin.orgs.update
|
||||
.use(authMiddleware)
|
||||
.use(superuserMiddleware)
|
||||
.handler(async ({ input, context }) => {
|
||||
const { slug, displayName, logoUrl } = input;
|
||||
|
||||
// Check if there are actual updates to make
|
||||
if (displayName === undefined && logoUrl === undefined) {
|
||||
// Verify org exists even for no-op
|
||||
const org = await context.db
|
||||
.selectFrom("orgs")
|
||||
.where("slug", "=", slug)
|
||||
.select(["id"])
|
||||
.executeTakeFirst();
|
||||
if (!org) {
|
||||
throw new ORPCError("NOT_FOUND", { message: "Organization not found" });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const updates: Partial<{
|
||||
display_name: string;
|
||||
logo_url: string | null;
|
||||
updated_at: Date;
|
||||
}> = { updated_at: new Date() };
|
||||
|
||||
if (displayName !== undefined) {
|
||||
updates.display_name = displayName;
|
||||
}
|
||||
if (logoUrl !== undefined) {
|
||||
updates.logo_url = logoUrl || null;
|
||||
}
|
||||
|
||||
const result = await context.db
|
||||
.updateTable("orgs")
|
||||
.set(updates)
|
||||
.where("slug", "=", slug)
|
||||
.executeTakeFirst();
|
||||
|
||||
if (!result.numUpdatedRows || result.numUpdatedRows === 0n) {
|
||||
throw new ORPCError("NOT_FOUND", { message: "Organization not found" });
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user