refactor(api): extract dispo management support
This commit is contained in:
@@ -1,11 +1,17 @@
|
||||
import {
|
||||
DispoStagedRecordType,
|
||||
ImportBatchStatus,
|
||||
StagedRecordStatus,
|
||||
} from "@capakraken/db";
|
||||
import { commitDispoImportBatch } from "@capakraken/application";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { createAuditEntry } from "../lib/audit.js";
|
||||
import {
|
||||
assertImportBatchCancelable,
|
||||
buildCancelledImportBatchUpdateData,
|
||||
buildDispoImportCommitAuditSummary,
|
||||
buildResolvedStagedRecordUpdateData,
|
||||
resolveDispoStagedRecordStoreKey,
|
||||
type DispoStagedRecordStores,
|
||||
} from "./dispo-management-support.js";
|
||||
|
||||
type AuditDb = Parameters<typeof createAuditEntry>[0]["db"];
|
||||
type CommitDb = Parameters<typeof commitDispoImportBatch>[0];
|
||||
@@ -24,21 +30,11 @@ export async function cancelImportBatch(
|
||||
if (!batch) {
|
||||
throw new TRPCError({ code: "NOT_FOUND", message: `Import batch "${input.id}" not found` });
|
||||
}
|
||||
|
||||
const terminalStatuses: ImportBatchStatus[] = [
|
||||
ImportBatchStatus.COMMITTED,
|
||||
ImportBatchStatus.CANCELLED,
|
||||
];
|
||||
if (terminalStatuses.includes(batch.status)) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: `Cannot cancel batch in status "${batch.status}"`,
|
||||
});
|
||||
}
|
||||
assertImportBatchCancelable({ id: input.id, status: batch.status });
|
||||
|
||||
const cancelled = await db.importBatch.update({
|
||||
where: { id: input.id },
|
||||
data: { status: ImportBatchStatus.CANCELLED },
|
||||
data: buildCancelledImportBatchUpdateData(),
|
||||
});
|
||||
|
||||
void createAuditEntry({
|
||||
@@ -56,66 +52,14 @@ export async function cancelImportBatch(
|
||||
}
|
||||
|
||||
export async function resolveStagedRecord(
|
||||
db: {
|
||||
stagedResource: { update: Function };
|
||||
stagedClient: { update: Function };
|
||||
stagedProject: { update: Function };
|
||||
stagedAssignment: { update: Function };
|
||||
stagedVacation: { update: Function };
|
||||
stagedAvailabilityRule: { update: Function };
|
||||
stagedUnresolvedRecord: { update: Function };
|
||||
},
|
||||
db: DispoStagedRecordStores,
|
||||
input: { action: "APPROVE" | "REJECT" | "SKIP"; id: string; recordType: DispoStagedRecordType },
|
||||
) {
|
||||
const statusMap: Record<string, StagedRecordStatus> = {
|
||||
APPROVE: StagedRecordStatus.APPROVED,
|
||||
REJECT: StagedRecordStatus.REJECTED,
|
||||
SKIP: StagedRecordStatus.REJECTED,
|
||||
};
|
||||
const nextStatus = statusMap[input.action]!;
|
||||
|
||||
switch (input.recordType) {
|
||||
case DispoStagedRecordType.RESOURCE:
|
||||
return db.stagedResource.update({
|
||||
where: { id: input.id },
|
||||
data: { status: nextStatus },
|
||||
});
|
||||
case DispoStagedRecordType.CLIENT:
|
||||
return db.stagedClient.update({
|
||||
where: { id: input.id },
|
||||
data: { status: nextStatus },
|
||||
});
|
||||
case DispoStagedRecordType.PROJECT:
|
||||
return db.stagedProject.update({
|
||||
where: { id: input.id },
|
||||
data: { status: nextStatus },
|
||||
});
|
||||
case DispoStagedRecordType.ASSIGNMENT:
|
||||
return db.stagedAssignment.update({
|
||||
where: { id: input.id },
|
||||
data: { status: nextStatus },
|
||||
});
|
||||
case DispoStagedRecordType.VACATION:
|
||||
return db.stagedVacation.update({
|
||||
where: { id: input.id },
|
||||
data: { status: nextStatus },
|
||||
});
|
||||
case DispoStagedRecordType.AVAILABILITY_RULE:
|
||||
return db.stagedAvailabilityRule.update({
|
||||
where: { id: input.id },
|
||||
data: { status: nextStatus },
|
||||
});
|
||||
case DispoStagedRecordType.UNRESOLVED:
|
||||
return db.stagedUnresolvedRecord.update({
|
||||
where: { id: input.id },
|
||||
data: { status: nextStatus },
|
||||
});
|
||||
default:
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: `Unknown record type: ${input.recordType as string}`,
|
||||
});
|
||||
}
|
||||
const storeKey = resolveDispoStagedRecordStoreKey(input.recordType);
|
||||
return db[storeKey].update({
|
||||
where: { id: input.id },
|
||||
data: buildResolvedStagedRecordUpdateData(input.action),
|
||||
});
|
||||
}
|
||||
|
||||
export async function commitImportBatch(
|
||||
@@ -140,7 +84,7 @@ export async function commitImportBatch(
|
||||
entityId: input.importBatchId,
|
||||
entityName: input.importBatchId,
|
||||
action: "IMPORT",
|
||||
summary: `Committed import batch (${JSON.stringify(counts)})`,
|
||||
summary: buildDispoImportCommitAuditSummary(counts),
|
||||
after: counts,
|
||||
source: "ui",
|
||||
...(input.userId !== undefined ? { userId: input.userId } : {}),
|
||||
|
||||
Reference in New Issue
Block a user