feat(admin): hard-delete resources (admin-only)

Adds a transactional hard-delete procedure behind adminProcedure that
removes a resource's assignments and vacations first, then the record
itself, and writes an audit log entry.  The ResourceModal exposes a
"Delete Resource" button (edit mode, ADMIN role only) with an inline
confirm step before the mutation fires.

Co-Authored-By: claude-flow <ruv@ruv.net>
This commit is contained in:
2026-04-03 15:23:30 +02:00
parent 3979d342c8
commit 0d0707264d
2 changed files with 93 additions and 15 deletions
+31 -1
View File
@@ -3,7 +3,7 @@ import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { findUniqueOrThrow } from "../db/helpers.js";
import { ROLE_BRIEF_SELECT } from "../db/selects.js";
import { managerProcedure, requirePermission } from "../trpc.js";
import { adminProcedure, managerProcedure, requirePermission } from "../trpc.js";
import { assertBlueprintDynamicFields } from "./blueprint-validation.js";
export const resourceMutationProcedures = {
@@ -259,4 +259,34 @@ export const resourceMutationProcedures = {
return { updated: input.ids.length };
}),
hardDelete: adminProcedure
.input(z.object({ id: z.string() }))
.mutation(async ({ ctx, input }) => {
const resource = await ctx.db.resource.findUnique({
where: { id: input.id },
select: { id: true, displayName: true, eid: true },
});
if (!resource) {
throw new TRPCError({ code: "NOT_FOUND", message: "Resource not found" });
}
await ctx.db.$transaction([
ctx.db.assignment.deleteMany({ where: { resourceId: input.id } }),
ctx.db.vacation.deleteMany({ where: { resourceId: input.id } }),
ctx.db.resource.delete({ where: { id: input.id } }),
]);
await ctx.db.auditLog.create({
data: {
entityType: "Resource",
entityId: input.id,
action: "DELETE",
userId: ctx.dbUser?.id,
changes: { before: resource } as unknown as import("@capakraken/db").Prisma.InputJsonValue,
},
});
return { deleted: true };
}),
};