refactor(api): extract timeline allocation assignment procedures
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import {
|
||||
buildSplitAllocationReadModel,
|
||||
createAssignment,
|
||||
} from "@capakraken/application";
|
||||
import type { PrismaClient } from "@capakraken/db";
|
||||
import { emitAllocationCreated } from "../sse/event-bus.js";
|
||||
import {
|
||||
assertTimelineDateRangeValid,
|
||||
validateTimelineAllocationDateRanges,
|
||||
} from "./timeline-allocation-mutation-support.js";
|
||||
import { buildTimelineQuickAssignAssignmentInput } from "./timeline-allocation-quick-assign-support.js";
|
||||
|
||||
export async function createTimelineQuickAssignment(
|
||||
db: PrismaClient,
|
||||
input: Parameters<typeof buildTimelineQuickAssignAssignmentInput>[0],
|
||||
) {
|
||||
assertTimelineDateRangeValid(input.startDate, input.endDate);
|
||||
|
||||
const allocation = await db.$transaction(async (tx) => {
|
||||
const assignment = await createAssignment(
|
||||
tx as unknown as Parameters<typeof createAssignment>[0],
|
||||
buildTimelineQuickAssignAssignmentInput(input),
|
||||
);
|
||||
|
||||
return buildSplitAllocationReadModel({
|
||||
demandRequirements: [],
|
||||
assignments: [assignment],
|
||||
}).allocations[0]!;
|
||||
});
|
||||
|
||||
emitAllocationCreated({
|
||||
id: allocation.id,
|
||||
projectId: allocation.projectId,
|
||||
resourceId: allocation.resourceId,
|
||||
});
|
||||
|
||||
return allocation;
|
||||
}
|
||||
|
||||
export async function createTimelineBatchQuickAssignments(
|
||||
db: PrismaClient,
|
||||
input: {
|
||||
assignments: Array<Parameters<typeof buildTimelineQuickAssignAssignmentInput>[0]>;
|
||||
},
|
||||
) {
|
||||
validateTimelineAllocationDateRanges(input.assignments);
|
||||
|
||||
const results = await db.$transaction(async (tx) => {
|
||||
const created = [];
|
||||
for (const assignment of input.assignments) {
|
||||
const createdAssignment = await createAssignment(
|
||||
tx as unknown as Parameters<typeof createAssignment>[0],
|
||||
buildTimelineQuickAssignAssignmentInput(assignment),
|
||||
);
|
||||
created.push(createdAssignment);
|
||||
}
|
||||
return created;
|
||||
});
|
||||
|
||||
for (const assignment of results) {
|
||||
emitAllocationCreated({
|
||||
id: assignment.id,
|
||||
projectId: assignment.projectId,
|
||||
resourceId: assignment.resourceId,
|
||||
});
|
||||
}
|
||||
|
||||
return { count: results.length };
|
||||
}
|
||||
@@ -5,8 +5,8 @@ import { managerProcedure, requirePermission } from "../trpc.js";
|
||||
import {
|
||||
createTimelineBatchQuickAssignments,
|
||||
createTimelineQuickAssignment,
|
||||
shiftTimelineAllocations,
|
||||
} from "./timeline-allocation-procedure-support.js";
|
||||
} from "./timeline-allocation-assignment-procedure-support.js";
|
||||
import { shiftTimelineAllocations } from "./timeline-allocation-procedure-support.js";
|
||||
import {
|
||||
UpdateAllocationHoursSchema,
|
||||
timelineBatchQuickAssignInputSchema,
|
||||
|
||||
@@ -1,77 +1,7 @@
|
||||
import {
|
||||
buildSplitAllocationReadModel,
|
||||
createAssignment,
|
||||
} from "@capakraken/application";
|
||||
import type { PrismaClient } from "@capakraken/db";
|
||||
import {
|
||||
emitAllocationCreated,
|
||||
emitAllocationUpdated,
|
||||
} from "../sse/event-bus.js";
|
||||
import {
|
||||
assertTimelineDateRangeValid,
|
||||
validateTimelineAllocationDateRanges,
|
||||
} from "./timeline-allocation-mutation-support.js";
|
||||
import { buildTimelineQuickAssignAssignmentInput } from "./timeline-allocation-quick-assign-support.js";
|
||||
import { emitAllocationUpdated } from "../sse/event-bus.js";
|
||||
import { applyTimelineBatchAllocationShift } from "./timeline-allocation-shift-support.js";
|
||||
|
||||
export async function createTimelineQuickAssignment(
|
||||
db: PrismaClient,
|
||||
input: Parameters<typeof buildTimelineQuickAssignAssignmentInput>[0],
|
||||
) {
|
||||
assertTimelineDateRangeValid(input.startDate, input.endDate);
|
||||
|
||||
const allocation = await db.$transaction(async (tx) => {
|
||||
const assignment = await createAssignment(
|
||||
tx as unknown as Parameters<typeof createAssignment>[0],
|
||||
buildTimelineQuickAssignAssignmentInput(input),
|
||||
);
|
||||
|
||||
return buildSplitAllocationReadModel({
|
||||
demandRequirements: [],
|
||||
assignments: [assignment],
|
||||
}).allocations[0]!;
|
||||
});
|
||||
|
||||
emitAllocationCreated({
|
||||
id: allocation.id,
|
||||
projectId: allocation.projectId,
|
||||
resourceId: allocation.resourceId,
|
||||
});
|
||||
|
||||
return allocation;
|
||||
}
|
||||
|
||||
export async function createTimelineBatchQuickAssignments(
|
||||
db: PrismaClient,
|
||||
input: {
|
||||
assignments: Array<Parameters<typeof buildTimelineQuickAssignAssignmentInput>[0]>;
|
||||
},
|
||||
) {
|
||||
validateTimelineAllocationDateRanges(input.assignments);
|
||||
|
||||
const results = await db.$transaction(async (tx) => {
|
||||
const created = [];
|
||||
for (const assignment of input.assignments) {
|
||||
const createdAssignment = await createAssignment(
|
||||
tx as unknown as Parameters<typeof createAssignment>[0],
|
||||
buildTimelineQuickAssignAssignmentInput(assignment),
|
||||
);
|
||||
created.push(createdAssignment);
|
||||
}
|
||||
return created;
|
||||
});
|
||||
|
||||
for (const assignment of results) {
|
||||
emitAllocationCreated({
|
||||
id: assignment.id,
|
||||
projectId: assignment.projectId,
|
||||
resourceId: assignment.resourceId,
|
||||
});
|
||||
}
|
||||
|
||||
return { count: results.length };
|
||||
}
|
||||
|
||||
export async function shiftTimelineAllocations(
|
||||
db: PrismaClient,
|
||||
input: Omit<Parameters<typeof applyTimelineBatchAllocationShift>[0], "db">,
|
||||
|
||||
Reference in New Issue
Block a user