refactor(api): extract timeline shift mutation routing

This commit is contained in:
2026-03-31 18:15:38 +02:00
parent 7b4c659922
commit 72b13dfaba
4 changed files with 110 additions and 33 deletions
@@ -0,0 +1,21 @@
import { PermissionKey, ShiftProjectSchema } from "@capakraken/shared";
import { managerProcedure, requirePermission } from "../trpc.js";
import { timelineAllocationMutationProcedures } from "./timeline-allocation-mutations.js";
import { applyTimelineProjectShiftMutation } from "./timeline-shift-router-support.js";
export const timelineMutationProcedures = {
...timelineAllocationMutationProcedures,
applyShift: managerProcedure
.input(ShiftProjectSchema)
.mutation(async ({ ctx, input }) => {
requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
return applyTimelineProjectShiftMutation({
db: ctx.db,
projectId: input.projectId,
newStartDate: input.newStartDate,
newEndDate: input.newEndDate,
});
}),
};
@@ -0,0 +1,27 @@
import type { PrismaClient } from "@capakraken/db";
import { emitProjectShifted } from "../sse/event-bus.js";
import { loadProjectShiftContext } from "./timeline-project-load-support.js";
import { applyTimelineProjectShift } from "./timeline-shift-procedure-support.js";
export async function applyTimelineProjectShiftMutation(input: {
db: PrismaClient;
projectId: string;
newStartDate: Date;
newEndDate: Date;
}) {
const context = await loadProjectShiftContext(input.db, input.projectId);
const result = await applyTimelineProjectShift({
db: input.db,
projectId: input.projectId,
newStartDate: input.newStartDate,
newEndDate: input.newEndDate,
context,
});
emitProjectShifted(result.event);
return {
project: result.project,
validation: result.validation,
};
}
+3 -33
View File
@@ -1,38 +1,8 @@
import { PermissionKey, ShiftProjectSchema } from "@capakraken/shared";
import { emitProjectShifted } from "../sse/event-bus.js";
import { createTRPCRouter, managerProcedure, requirePermission } from "../trpc.js";
import { timelineAllocationMutationProcedures } from "./timeline-allocation-mutations.js";
import { createTRPCRouter } from "../trpc.js";
import { timelineMutationProcedures } from "./timeline-mutations.js";
import { timelineReadProcedures } from "./timeline-read.js";
import { loadProjectShiftContext } from "./timeline-project-load-support.js";
import { applyTimelineProjectShift } from "./timeline-shift-procedure-support.js";
export const timelineRouter = createTRPCRouter({
...timelineReadProcedures,
...timelineAllocationMutationProcedures,
/**
* Apply a project shift: validate, then commit all allocation date changes.
* Reads includeSaturday from each allocation's metadata.
*/
applyShift: managerProcedure
.input(ShiftProjectSchema)
.mutation(async ({ ctx, input }) => {
requirePermission(ctx, PermissionKey.MANAGE_ALLOCATIONS);
const { projectId, newStartDate, newEndDate } = input;
const context = await loadProjectShiftContext(ctx.db, projectId);
const result = await applyTimelineProjectShift({
db: ctx.db,
projectId,
newStartDate,
newEndDate,
context,
});
emitProjectShifted(result.event);
return {
project: result.project,
validation: result.validation,
};
}),
...timelineMutationProcedures,
});