import "server-only"; import { eq } from "drizzle-orm"; import type { NodePgDatabase } from "drizzle-orm/node-postgres"; import { db } from "@/db/client"; import { editorialAccounts, usages } from "@/db/schema"; import type * as schema from "@/db/schema"; import { writeAudit } from "@/modules/audit/write"; export class CollabAdminError extends Error { readonly name = "CollabAdminError"; constructor( readonly code: "USAGE_NOT_FOUND" | "USAGE_NOT_EDITABLE" | "INVALID_PRICE", ) { super(code); } } export type CollabAdminRepository = { getUsageForCorrection(usageId: string): Promise<{ id: string; editorialAccountId: string; billingStatus: "open" | "billed" | "reopened" | "voided"; collab: boolean; priceCents: number; currentPriceCents: number | null; } | null>; updateUsageCollab(usageId: string, collab: boolean, priceCents: number): Promise; }; /** * Admin correction of the collab flag on open usages, so wrong entries can be * fixed before the Nutzungsnachweis is finalized and sent. Marking collab sets * the price to 0; unmarking restores the editorial's current price. */ export function createCollabAdminService({ repo }: { repo: CollabAdminRepository }) { return { async setUsageCollab(input: { usageId: string; collab: boolean; adminUserId: string; }): Promise<{ priceCents: number }> { const usage = await repo.getUsageForCorrection(input.usageId); if (!usage) throw new CollabAdminError("USAGE_NOT_FOUND"); if (usage.billingStatus !== "open" && usage.billingStatus !== "reopened") { throw new CollabAdminError("USAGE_NOT_EDITABLE"); } let priceCents: number; if (input.collab) { priceCents = 0; } else { if ( !Number.isSafeInteger(usage.currentPriceCents) || (usage.currentPriceCents ?? 0) <= 0 ) { throw new CollabAdminError("INVALID_PRICE"); } priceCents = usage.currentPriceCents!; } await repo.updateUsageCollab(input.usageId, input.collab, priceCents); try { await writeAudit({ type: "usage_collab_updated", actorUserId: input.adminUserId, actorType: "admin", entityType: "usage", entityId: input.usageId, metadata: { from: { collab: usage.collab, priceCents: usage.priceCents }, to: { collab: input.collab, priceCents }, }, }); } catch { // Audit is observability, not a transaction participant. } return { priceCents }; }, }; } export function createCollabAdminRepository( database: NodePgDatabase, ): CollabAdminRepository { return { async getUsageForCorrection(usageId) { const [row] = await database .select({ id: usages.id, editorialAccountId: usages.editorialAccountId, billingStatus: usages.billingStatus, collab: usages.collab, priceCents: usages.priceCents, currentPriceCents: editorialAccounts.currentPriceCents, }) .from(usages) .innerJoin(editorialAccounts, eq(usages.editorialAccountId, editorialAccounts.id)) .where(eq(usages.id, usageId)) .limit(1); return row ?? null; }, async updateUsageCollab(usageId, collab, priceCents) { await database .update(usages) .set({ collab, priceCents }) .where(eq(usages.id, usageId)); }, }; } export const collabAdminService = createCollabAdminService({ repo: createCollabAdminRepository(db), }); export const setUsageCollab = collabAdminService.setUsageCollab;