refactor(api): split allocation assignment mutations
This commit is contained in:
@@ -0,0 +1,318 @@
|
|||||||
|
import {
|
||||||
|
buildSplitAllocationReadModel,
|
||||||
|
createAssignment,
|
||||||
|
createDemandRequirement,
|
||||||
|
deleteAllocationEntry,
|
||||||
|
deleteAssignment,
|
||||||
|
loadAllocationEntry,
|
||||||
|
updateAllocationEntry,
|
||||||
|
updateAssignment,
|
||||||
|
} from "@capakraken/application";
|
||||||
|
import type { PrismaClient } from "@capakraken/db";
|
||||||
|
import {
|
||||||
|
AllocationStatus,
|
||||||
|
CreateAllocationSchema,
|
||||||
|
UpdateAllocationSchema,
|
||||||
|
} from "@capakraken/shared";
|
||||||
|
import { TRPCError } from "@trpc/server";
|
||||||
|
import { z } from "zod";
|
||||||
|
import { findUniqueOrThrow } from "../db/helpers.js";
|
||||||
|
import { ASSIGNMENT_INCLUDE, toIsoDate } from "./allocation-shared.js";
|
||||||
|
import {
|
||||||
|
findAllocationEntryOrNull,
|
||||||
|
toAssignmentUpdateInput,
|
||||||
|
toDemandRequirementUpdateInput,
|
||||||
|
} from "./allocation-support.js";
|
||||||
|
|
||||||
|
type AllocationMutationDb = Pick<
|
||||||
|
PrismaClient,
|
||||||
|
"$transaction" | "assignment" | "demandRequirement" | "resource" | "auditLog"
|
||||||
|
>;
|
||||||
|
|
||||||
|
type CreateAllocationInput = z.infer<typeof CreateAllocationSchema>;
|
||||||
|
type UpdateAllocationInput = z.infer<typeof UpdateAllocationSchema>;
|
||||||
|
|
||||||
|
type EnsureAssignmentInput = {
|
||||||
|
resourceId: string;
|
||||||
|
projectId: string;
|
||||||
|
startDate: Date;
|
||||||
|
endDate: Date;
|
||||||
|
hoursPerDay: number;
|
||||||
|
role?: string | undefined;
|
||||||
|
};
|
||||||
|
|
||||||
|
export async function createAllocationReadModelEntry(
|
||||||
|
tx: Parameters<typeof createAssignment>[0],
|
||||||
|
input: CreateAllocationInput,
|
||||||
|
) {
|
||||||
|
if (!input.resourceId) {
|
||||||
|
const demandRequirement = await createDemandRequirement(
|
||||||
|
tx as Parameters<typeof createDemandRequirement>[0],
|
||||||
|
{
|
||||||
|
projectId: input.projectId,
|
||||||
|
startDate: input.startDate,
|
||||||
|
endDate: input.endDate,
|
||||||
|
hoursPerDay: input.hoursPerDay,
|
||||||
|
percentage: input.percentage,
|
||||||
|
role: input.role,
|
||||||
|
roleId: input.roleId,
|
||||||
|
headcount: input.headcount,
|
||||||
|
status: input.status,
|
||||||
|
metadata: input.metadata,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
return buildSplitAllocationReadModel({
|
||||||
|
demandRequirements: [demandRequirement],
|
||||||
|
assignments: [],
|
||||||
|
}).allocations[0]!;
|
||||||
|
}
|
||||||
|
|
||||||
|
const assignment = await createAssignment(
|
||||||
|
tx,
|
||||||
|
{
|
||||||
|
resourceId: input.resourceId,
|
||||||
|
projectId: input.projectId,
|
||||||
|
startDate: input.startDate,
|
||||||
|
endDate: input.endDate,
|
||||||
|
hoursPerDay: input.hoursPerDay,
|
||||||
|
percentage: input.percentage,
|
||||||
|
role: input.role,
|
||||||
|
roleId: input.roleId,
|
||||||
|
status: input.status,
|
||||||
|
metadata: input.metadata,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
return buildSplitAllocationReadModel({
|
||||||
|
demandRequirements: [],
|
||||||
|
assignments: [assignment],
|
||||||
|
}).allocations[0]!;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function ensureAssignmentRecord(
|
||||||
|
db: Pick<PrismaClient, "$transaction" | "assignment" | "resource">,
|
||||||
|
input: EnsureAssignmentInput,
|
||||||
|
) {
|
||||||
|
const existing = (await db.assignment.findMany({
|
||||||
|
where: {
|
||||||
|
resourceId: input.resourceId,
|
||||||
|
projectId: input.projectId,
|
||||||
|
},
|
||||||
|
include: ASSIGNMENT_INCLUDE,
|
||||||
|
orderBy: { startDate: "asc" },
|
||||||
|
})).find((assignment) => (
|
||||||
|
toIsoDate(assignment.startDate) === toIsoDate(input.startDate)
|
||||||
|
&& toIsoDate(assignment.endDate) === toIsoDate(input.endDate)
|
||||||
|
));
|
||||||
|
|
||||||
|
if (existing) {
|
||||||
|
if (existing.status !== AllocationStatus.CANCELLED) {
|
||||||
|
throw new TRPCError({
|
||||||
|
code: "CONFLICT",
|
||||||
|
message: `An allocation already exists for this resource/project/dates with status ${existing.status}.`,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const resource = await db.resource.findUnique({
|
||||||
|
where: { id: input.resourceId },
|
||||||
|
select: { lcrCents: true },
|
||||||
|
});
|
||||||
|
if (!resource) {
|
||||||
|
throw new TRPCError({ code: "NOT_FOUND", message: "Resource not found" });
|
||||||
|
}
|
||||||
|
|
||||||
|
const updated = await db.$transaction(async (tx) => updateAssignment(
|
||||||
|
tx as Parameters<typeof updateAssignment>[0],
|
||||||
|
existing.id,
|
||||||
|
{
|
||||||
|
status: AllocationStatus.PROPOSED,
|
||||||
|
hoursPerDay: input.hoursPerDay,
|
||||||
|
percentage: (input.hoursPerDay / 8) * 100,
|
||||||
|
dailyCostCents: Math.round(resource.lcrCents * input.hoursPerDay),
|
||||||
|
...(input.role ? { role: input.role } : {}),
|
||||||
|
},
|
||||||
|
));
|
||||||
|
|
||||||
|
return { assignment: updated, action: "reactivated" as const };
|
||||||
|
}
|
||||||
|
|
||||||
|
const assignment = await db.$transaction(async (tx) => createAssignment(
|
||||||
|
tx as Parameters<typeof createAssignment>[0],
|
||||||
|
{
|
||||||
|
resourceId: input.resourceId,
|
||||||
|
projectId: input.projectId,
|
||||||
|
startDate: input.startDate,
|
||||||
|
endDate: input.endDate,
|
||||||
|
hoursPerDay: input.hoursPerDay,
|
||||||
|
percentage: (input.hoursPerDay / 8) * 100,
|
||||||
|
status: AllocationStatus.PROPOSED,
|
||||||
|
metadata: {},
|
||||||
|
...(input.role ? { role: input.role } : {}),
|
||||||
|
},
|
||||||
|
));
|
||||||
|
|
||||||
|
return { assignment, action: "created" as const };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function updateAllocationWithAudit(
|
||||||
|
db: AllocationMutationDb,
|
||||||
|
id: string,
|
||||||
|
data: UpdateAllocationInput,
|
||||||
|
) {
|
||||||
|
const existing = await loadAllocationEntry(db, id);
|
||||||
|
const updated = await db.$transaction(async (tx) => {
|
||||||
|
const { allocation: updatedAllocation } = await updateAllocationEntry(
|
||||||
|
tx as Parameters<typeof updateAllocationEntry>[0],
|
||||||
|
{
|
||||||
|
id,
|
||||||
|
demandRequirementUpdate:
|
||||||
|
existing.kind === "assignment" ? {} : toDemandRequirementUpdateInput(data),
|
||||||
|
assignmentUpdate:
|
||||||
|
existing.kind === "demand" ? {} : toAssignmentUpdateInput(data),
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
await tx.auditLog.create({
|
||||||
|
data: {
|
||||||
|
entityType: "Allocation",
|
||||||
|
entityId: id,
|
||||||
|
action: "UPDATE",
|
||||||
|
changes: {
|
||||||
|
before: existing.entry,
|
||||||
|
after: updatedAllocation,
|
||||||
|
} as unknown as import("@capakraken/db").Prisma.InputJsonValue,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return updatedAllocation;
|
||||||
|
});
|
||||||
|
|
||||||
|
return { existing, updated };
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteAssignmentWithAudit(
|
||||||
|
db: AllocationMutationDb,
|
||||||
|
id: string,
|
||||||
|
) {
|
||||||
|
const existing = await findUniqueOrThrow(
|
||||||
|
db.assignment.findUnique({
|
||||||
|
where: { id },
|
||||||
|
include: ASSIGNMENT_INCLUDE,
|
||||||
|
}),
|
||||||
|
"Assignment",
|
||||||
|
);
|
||||||
|
|
||||||
|
await db.$transaction(async (tx) => {
|
||||||
|
await deleteAssignment(
|
||||||
|
tx as Parameters<typeof deleteAssignment>[0],
|
||||||
|
id,
|
||||||
|
);
|
||||||
|
|
||||||
|
await tx.auditLog.create({
|
||||||
|
data: {
|
||||||
|
entityType: "Assignment",
|
||||||
|
entityId: id,
|
||||||
|
action: "DELETE",
|
||||||
|
changes: { before: existing } as unknown as import("@capakraken/db").Prisma.InputJsonValue,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return existing;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function deleteAllocationWithAudit(
|
||||||
|
db: AllocationMutationDb,
|
||||||
|
id: string,
|
||||||
|
) {
|
||||||
|
const existing = await loadAllocationEntry(db, id);
|
||||||
|
|
||||||
|
await db.$transaction(async (tx) => {
|
||||||
|
await deleteAllocationEntry(
|
||||||
|
tx as Parameters<typeof deleteAllocationEntry>[0],
|
||||||
|
existing,
|
||||||
|
);
|
||||||
|
|
||||||
|
await tx.auditLog.create({
|
||||||
|
data: {
|
||||||
|
entityType: "Allocation",
|
||||||
|
entityId: id,
|
||||||
|
action: "DELETE",
|
||||||
|
changes: { before: existing.entry } as unknown as import("@capakraken/db").Prisma.InputJsonValue,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return existing;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function batchDeleteAllocationsWithAudit(
|
||||||
|
db: AllocationMutationDb,
|
||||||
|
ids: string[],
|
||||||
|
) {
|
||||||
|
const existing = (
|
||||||
|
await Promise.all(ids.map(async (id) => findAllocationEntryOrNull(db, id)))
|
||||||
|
).filter((entry): entry is NonNullable<typeof entry> => Boolean(entry));
|
||||||
|
|
||||||
|
await db.$transaction(async (tx) => {
|
||||||
|
for (const allocation of existing) {
|
||||||
|
await deleteAllocationEntry(
|
||||||
|
tx as Parameters<typeof deleteAllocationEntry>[0],
|
||||||
|
allocation,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
await tx.auditLog.create({
|
||||||
|
data: {
|
||||||
|
entityType: "Allocation",
|
||||||
|
entityId: ids.join(","),
|
||||||
|
action: "DELETE",
|
||||||
|
changes: {
|
||||||
|
before: existing.map((allocation) => ({
|
||||||
|
id: allocation.entry.id,
|
||||||
|
projectId: allocation.projectId,
|
||||||
|
})),
|
||||||
|
} as unknown as import("@capakraken/db").Prisma.InputJsonValue,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return existing;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function batchUpdateAllocationStatusWithAudit(
|
||||||
|
db: AllocationMutationDb,
|
||||||
|
input: {
|
||||||
|
ids: string[];
|
||||||
|
status: AllocationStatus;
|
||||||
|
},
|
||||||
|
) {
|
||||||
|
const updated = await db.$transaction(async (tx) => {
|
||||||
|
const updatedAllocations = await Promise.all(
|
||||||
|
input.ids.map(async (id) => (
|
||||||
|
await updateAllocationEntry(
|
||||||
|
tx as Parameters<typeof updateAllocationEntry>[0],
|
||||||
|
{
|
||||||
|
id,
|
||||||
|
demandRequirementUpdate: { status: input.status },
|
||||||
|
assignmentUpdate: { status: input.status },
|
||||||
|
},
|
||||||
|
)
|
||||||
|
).allocation),
|
||||||
|
);
|
||||||
|
|
||||||
|
return updatedAllocations;
|
||||||
|
});
|
||||||
|
|
||||||
|
await db.auditLog.create({
|
||||||
|
data: {
|
||||||
|
entityType: "Allocation",
|
||||||
|
entityId: input.ids.join(","),
|
||||||
|
action: "UPDATE",
|
||||||
|
changes: { after: { status: input.status, ids: input.ids } },
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
return updated;
|
||||||
|
}
|
||||||
@@ -1,11 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
buildSplitAllocationReadModel,
|
|
||||||
createAssignment,
|
createAssignment,
|
||||||
createDemandRequirement,
|
|
||||||
deleteAllocationEntry,
|
|
||||||
deleteAssignment,
|
|
||||||
loadAllocationEntry,
|
|
||||||
updateAllocationEntry,
|
|
||||||
updateAssignment,
|
updateAssignment,
|
||||||
} from "@capakraken/application";
|
} from "@capakraken/application";
|
||||||
import {
|
import {
|
||||||
@@ -16,7 +10,6 @@ import {
|
|||||||
UpdateAllocationSchema,
|
UpdateAllocationSchema,
|
||||||
UpdateAssignmentSchema,
|
UpdateAssignmentSchema,
|
||||||
} from "@capakraken/shared";
|
} from "@capakraken/shared";
|
||||||
import { TRPCError } from "@trpc/server";
|
|
||||||
import { z } from "zod";
|
import { z } from "zod";
|
||||||
import { findUniqueOrThrow } from "../db/helpers.js";
|
import { findUniqueOrThrow } from "../db/helpers.js";
|
||||||
import {
|
import {
|
||||||
@@ -27,14 +20,14 @@ import {
|
|||||||
publishBatchAllocationStatusUpdates,
|
publishBatchAllocationStatusUpdates,
|
||||||
} from "./allocation-assignment-effects.js";
|
} from "./allocation-assignment-effects.js";
|
||||||
import {
|
import {
|
||||||
ASSIGNMENT_INCLUDE,
|
batchDeleteAllocationsWithAudit,
|
||||||
toIsoDate,
|
batchUpdateAllocationStatusWithAudit,
|
||||||
} from "./allocation-shared.js";
|
createAllocationReadModelEntry,
|
||||||
import {
|
deleteAllocationWithAudit,
|
||||||
findAllocationEntryOrNull,
|
deleteAssignmentWithAudit,
|
||||||
toAssignmentUpdateInput,
|
ensureAssignmentRecord,
|
||||||
toDemandRequirementUpdateInput,
|
updateAllocationWithAudit,
|
||||||
} from "./allocation-support.js";
|
} from "./allocation-assignment-mutations.js";
|
||||||
import {
|
import {
|
||||||
managerProcedure,
|
managerProcedure,
|
||||||
requirePermission,
|
requirePermission,
|
||||||
@@ -45,51 +38,11 @@ export const allocationAssignmentProcedures = {
|
|||||||
.input(CreateAllocationSchema)
|
.input(CreateAllocationSchema)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(async ({ ctx, input }) => {
|
||||||
requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
|
requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
|
||||||
const allocation = await ctx.db.$transaction(async (tx) => {
|
const allocation = await ctx.db.$transaction(async (tx) =>
|
||||||
if (!input.resourceId) {
|
createAllocationReadModelEntry(
|
||||||
const demandRequirement = await createDemandRequirement(
|
tx as Parameters<typeof createAssignment>[0],
|
||||||
tx as unknown as Parameters<typeof createDemandRequirement>[0],
|
input,
|
||||||
{
|
));
|
||||||
projectId: input.projectId,
|
|
||||||
startDate: input.startDate,
|
|
||||||
endDate: input.endDate,
|
|
||||||
hoursPerDay: input.hoursPerDay,
|
|
||||||
percentage: input.percentage,
|
|
||||||
role: input.role,
|
|
||||||
roleId: input.roleId,
|
|
||||||
headcount: input.headcount,
|
|
||||||
status: input.status,
|
|
||||||
metadata: input.metadata,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
return buildSplitAllocationReadModel({
|
|
||||||
demandRequirements: [demandRequirement],
|
|
||||||
assignments: [],
|
|
||||||
}).allocations[0]!;
|
|
||||||
}
|
|
||||||
|
|
||||||
const assignment = await createAssignment(
|
|
||||||
tx as unknown as Parameters<typeof createAssignment>[0],
|
|
||||||
{
|
|
||||||
resourceId: input.resourceId,
|
|
||||||
projectId: input.projectId,
|
|
||||||
startDate: input.startDate,
|
|
||||||
endDate: input.endDate,
|
|
||||||
hoursPerDay: input.hoursPerDay,
|
|
||||||
percentage: input.percentage,
|
|
||||||
role: input.role,
|
|
||||||
roleId: input.roleId,
|
|
||||||
status: input.status,
|
|
||||||
metadata: input.metadata,
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
return buildSplitAllocationReadModel({
|
|
||||||
demandRequirements: [],
|
|
||||||
assignments: [assignment],
|
|
||||||
}).allocations[0]!;
|
|
||||||
});
|
|
||||||
|
|
||||||
publishAllocationCreated(ctx.db, {
|
publishAllocationCreated(ctx.db, {
|
||||||
id: allocation.id,
|
id: allocation.id,
|
||||||
@@ -131,78 +84,25 @@ export const allocationAssignmentProcedures = {
|
|||||||
}))
|
}))
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(async ({ ctx, input }) => {
|
||||||
requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
|
requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
|
||||||
|
const result = await ensureAssignmentRecord(ctx.db, input);
|
||||||
|
|
||||||
const existing = (await ctx.db.assignment.findMany({
|
if (result.action === "reactivated") {
|
||||||
where: {
|
|
||||||
resourceId: input.resourceId,
|
|
||||||
projectId: input.projectId,
|
|
||||||
},
|
|
||||||
include: ASSIGNMENT_INCLUDE,
|
|
||||||
orderBy: { startDate: "asc" },
|
|
||||||
})).find((assignment) => (
|
|
||||||
toIsoDate(assignment.startDate) === toIsoDate(input.startDate)
|
|
||||||
&& toIsoDate(assignment.endDate) === toIsoDate(input.endDate)
|
|
||||||
));
|
|
||||||
|
|
||||||
if (existing) {
|
|
||||||
if (existing.status !== AllocationStatus.CANCELLED) {
|
|
||||||
throw new TRPCError({
|
|
||||||
code: "CONFLICT",
|
|
||||||
message: `An allocation already exists for this resource/project/dates with status ${existing.status}.`,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const resource = await ctx.db.resource.findUnique({
|
|
||||||
where: { id: input.resourceId },
|
|
||||||
select: { lcrCents: true },
|
|
||||||
});
|
|
||||||
if (!resource) {
|
|
||||||
throw new TRPCError({ code: "NOT_FOUND", message: "Resource not found" });
|
|
||||||
}
|
|
||||||
|
|
||||||
const updated = await ctx.db.$transaction(async (tx) => updateAssignment(
|
|
||||||
tx as unknown as Parameters<typeof updateAssignment>[0],
|
|
||||||
existing.id,
|
|
||||||
{
|
|
||||||
status: AllocationStatus.PROPOSED,
|
|
||||||
hoursPerDay: input.hoursPerDay,
|
|
||||||
percentage: (input.hoursPerDay / 8) * 100,
|
|
||||||
dailyCostCents: Math.round(resource.lcrCents * input.hoursPerDay),
|
|
||||||
...(input.role ? { role: input.role } : {}),
|
|
||||||
},
|
|
||||||
));
|
|
||||||
|
|
||||||
publishAllocationUpdated(ctx.db, {
|
publishAllocationUpdated(ctx.db, {
|
||||||
id: updated.id,
|
id: result.assignment.id,
|
||||||
projectId: updated.projectId,
|
projectId: result.assignment.projectId,
|
||||||
resourceId: updated.resourceId,
|
resourceId: result.assignment.resourceId,
|
||||||
}, { dispatchWebhook: true });
|
}, { dispatchWebhook: true });
|
||||||
|
|
||||||
return { assignment: updated, action: "reactivated" as const };
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
const assignment = await ctx.db.$transaction(async (tx) => createAssignment(
|
|
||||||
tx as unknown as Parameters<typeof createAssignment>[0],
|
|
||||||
{
|
|
||||||
resourceId: input.resourceId,
|
|
||||||
projectId: input.projectId,
|
|
||||||
startDate: input.startDate,
|
|
||||||
endDate: input.endDate,
|
|
||||||
hoursPerDay: input.hoursPerDay,
|
|
||||||
percentage: (input.hoursPerDay / 8) * 100,
|
|
||||||
status: AllocationStatus.PROPOSED,
|
|
||||||
metadata: {},
|
|
||||||
...(input.role ? { role: input.role } : {}),
|
|
||||||
},
|
|
||||||
));
|
|
||||||
|
|
||||||
publishAllocationCreated(ctx.db, {
|
publishAllocationCreated(ctx.db, {
|
||||||
id: assignment.id,
|
id: result.assignment.id,
|
||||||
projectId: assignment.projectId,
|
projectId: result.assignment.projectId,
|
||||||
resourceId: assignment.resourceId,
|
resourceId: result.assignment.resourceId,
|
||||||
});
|
});
|
||||||
|
|
||||||
return { assignment, action: "created" as const };
|
return result;
|
||||||
}),
|
}),
|
||||||
|
|
||||||
updateAssignment: managerProcedure
|
updateAssignment: managerProcedure
|
||||||
@@ -239,34 +139,7 @@ export const allocationAssignmentProcedures = {
|
|||||||
.input(z.object({ id: z.string(), data: UpdateAllocationSchema }))
|
.input(z.object({ id: z.string(), data: UpdateAllocationSchema }))
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(async ({ ctx, input }) => {
|
||||||
requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
|
requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
|
||||||
const existing = await loadAllocationEntry(ctx.db, input.id);
|
const { existing, updated } = await updateAllocationWithAudit(ctx.db, input.id, input.data);
|
||||||
|
|
||||||
const updated = await ctx.db.$transaction(async (tx) => {
|
|
||||||
const { allocation: updatedAllocation } = await updateAllocationEntry(
|
|
||||||
tx as unknown as Parameters<typeof updateAllocationEntry>[0],
|
|
||||||
{
|
|
||||||
id: input.id,
|
|
||||||
demandRequirementUpdate:
|
|
||||||
existing.kind === "assignment" ? {} : toDemandRequirementUpdateInput(input.data),
|
|
||||||
assignmentUpdate:
|
|
||||||
existing.kind === "demand" ? {} : toAssignmentUpdateInput(input.data),
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
await tx.auditLog.create({
|
|
||||||
data: {
|
|
||||||
entityType: "Allocation",
|
|
||||||
entityId: input.id,
|
|
||||||
action: "UPDATE",
|
|
||||||
changes: {
|
|
||||||
before: existing.entry,
|
|
||||||
after: updatedAllocation,
|
|
||||||
} as unknown as import("@capakraken/db").Prisma.InputJsonValue,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
return updatedAllocation;
|
|
||||||
});
|
|
||||||
|
|
||||||
const affectedResourceIds = [existing.entry.resourceId, updated.resourceId].filter(
|
const affectedResourceIds = [existing.entry.resourceId, updated.resourceId].filter(
|
||||||
(resourceId): resourceId is string => Boolean(resourceId),
|
(resourceId): resourceId is string => Boolean(resourceId),
|
||||||
@@ -286,30 +159,7 @@ export const allocationAssignmentProcedures = {
|
|||||||
.input(z.object({ id: z.string() }))
|
.input(z.object({ id: z.string() }))
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(async ({ ctx, input }) => {
|
||||||
requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
|
requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
|
||||||
|
const existing = await deleteAssignmentWithAudit(ctx.db, input.id);
|
||||||
const existing = await findUniqueOrThrow(
|
|
||||||
ctx.db.assignment.findUnique({
|
|
||||||
where: { id: input.id },
|
|
||||||
include: ASSIGNMENT_INCLUDE,
|
|
||||||
}),
|
|
||||||
"Assignment",
|
|
||||||
);
|
|
||||||
|
|
||||||
await ctx.db.$transaction(async (tx) => {
|
|
||||||
await deleteAssignment(
|
|
||||||
tx as unknown as Parameters<typeof deleteAssignment>[0],
|
|
||||||
input.id,
|
|
||||||
);
|
|
||||||
|
|
||||||
await tx.auditLog.create({
|
|
||||||
data: {
|
|
||||||
entityType: "Assignment",
|
|
||||||
entityId: input.id,
|
|
||||||
action: "DELETE",
|
|
||||||
changes: { before: existing } as unknown as import("@capakraken/db").Prisma.InputJsonValue,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
publishAllocationDeleted(ctx.db, {
|
publishAllocationDeleted(ctx.db, {
|
||||||
id: existing.id,
|
id: existing.id,
|
||||||
@@ -324,23 +174,7 @@ export const allocationAssignmentProcedures = {
|
|||||||
.input(z.object({ id: z.string() }))
|
.input(z.object({ id: z.string() }))
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(async ({ ctx, input }) => {
|
||||||
requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
|
requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
|
||||||
const existing = await loadAllocationEntry(ctx.db, input.id);
|
const existing = await deleteAllocationWithAudit(ctx.db, input.id);
|
||||||
|
|
||||||
await ctx.db.$transaction(async (tx) => {
|
|
||||||
await deleteAllocationEntry(
|
|
||||||
tx as unknown as Parameters<typeof deleteAllocationEntry>[0],
|
|
||||||
existing,
|
|
||||||
);
|
|
||||||
|
|
||||||
await tx.auditLog.create({
|
|
||||||
data: {
|
|
||||||
entityType: "Allocation",
|
|
||||||
entityId: input.id,
|
|
||||||
action: "DELETE",
|
|
||||||
changes: { before: existing.entry } as unknown as import("@capakraken/db").Prisma.InputJsonValue,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
publishAllocationDeleted(ctx.db, {
|
publishAllocationDeleted(ctx.db, {
|
||||||
id: existing.entry.id,
|
id: existing.entry.id,
|
||||||
@@ -355,31 +189,7 @@ export const allocationAssignmentProcedures = {
|
|||||||
.input(z.object({ ids: z.array(z.string()).min(1).max(100) }))
|
.input(z.object({ ids: z.array(z.string()).min(1).max(100) }))
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(async ({ ctx, input }) => {
|
||||||
requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
|
requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
|
||||||
const existing = (
|
const existing = await batchDeleteAllocationsWithAudit(ctx.db, input.ids);
|
||||||
await Promise.all(input.ids.map(async (id) => findAllocationEntryOrNull(ctx.db, id)))
|
|
||||||
).filter((entry): entry is NonNullable<typeof entry> => Boolean(entry));
|
|
||||||
|
|
||||||
await ctx.db.$transaction(async (tx) => {
|
|
||||||
for (const allocation of existing) {
|
|
||||||
await deleteAllocationEntry(
|
|
||||||
tx as unknown as Parameters<typeof deleteAllocationEntry>[0],
|
|
||||||
allocation,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
await tx.auditLog.create({
|
|
||||||
data: {
|
|
||||||
entityType: "Allocation",
|
|
||||||
entityId: input.ids.join(","),
|
|
||||||
action: "DELETE",
|
|
||||||
changes: {
|
|
||||||
before: existing.map((allocation) => ({
|
|
||||||
id: allocation.entry.id,
|
|
||||||
projectId: allocation.projectId,
|
|
||||||
})),
|
|
||||||
} as unknown as import("@capakraken/db").Prisma.InputJsonValue,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
publishBatchAllocationDeletes(ctx.db, existing.map((allocation) => ({
|
publishBatchAllocationDeletes(ctx.db, existing.map((allocation) => ({
|
||||||
id: allocation.entry.id,
|
id: allocation.entry.id,
|
||||||
@@ -399,33 +209,7 @@ export const allocationAssignmentProcedures = {
|
|||||||
)
|
)
|
||||||
.mutation(async ({ ctx, input }) => {
|
.mutation(async ({ ctx, input }) => {
|
||||||
requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
|
requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
|
||||||
const updated = await ctx.db.$transaction(async (tx) => {
|
const updated = await batchUpdateAllocationStatusWithAudit(ctx.db, input);
|
||||||
const updatedAllocations = await Promise.all(
|
|
||||||
input.ids.map(async (id) =>
|
|
||||||
(
|
|
||||||
await updateAllocationEntry(
|
|
||||||
tx as unknown as Parameters<typeof updateAllocationEntry>[0],
|
|
||||||
{
|
|
||||||
id,
|
|
||||||
demandRequirementUpdate: { status: input.status },
|
|
||||||
assignmentUpdate: { status: input.status },
|
|
||||||
},
|
|
||||||
)
|
|
||||||
).allocation,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
return updatedAllocations;
|
|
||||||
});
|
|
||||||
|
|
||||||
await ctx.db.auditLog.create({
|
|
||||||
data: {
|
|
||||||
entityType: "Allocation",
|
|
||||||
entityId: input.ids.join(","),
|
|
||||||
action: "UPDATE",
|
|
||||||
changes: { after: { status: input.status, ids: input.ids } },
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
publishBatchAllocationStatusUpdates(ctx.db, updated.map((allocation) => ({
|
publishBatchAllocationStatusUpdates(ctx.db, updated.map((allocation) => ({
|
||||||
id: allocation.id,
|
id: allocation.id,
|
||||||
|
|||||||
Reference in New Issue
Block a user