refactor(api): extract timeline shift mutation routing
This commit is contained in:
@@ -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,
|
||||
};
|
||||
}
|
||||
@@ -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,
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user