refactor(api): extract dispo management support
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
import {
|
||||
DispoStagedRecordType,
|
||||
ImportBatchStatus,
|
||||
StagedRecordStatus,
|
||||
} from "@capakraken/db";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
|
||||
type StagedRecordAction = "APPROVE" | "REJECT" | "SKIP";
|
||||
|
||||
export const terminalImportBatchStatuses: readonly ImportBatchStatus[] = [
|
||||
ImportBatchStatus.COMMITTED,
|
||||
ImportBatchStatus.CANCELLED,
|
||||
];
|
||||
|
||||
export function assertImportBatchCancelable(input: {
|
||||
id: string;
|
||||
status: ImportBatchStatus;
|
||||
}): void {
|
||||
if (terminalImportBatchStatuses.includes(input.status)) {
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: `Cannot cancel batch in status "${input.status}"`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function resolveDispoStagedRecordStatus(
|
||||
action: StagedRecordAction,
|
||||
): StagedRecordStatus {
|
||||
switch (action) {
|
||||
case "APPROVE":
|
||||
return StagedRecordStatus.APPROVED;
|
||||
case "REJECT":
|
||||
case "SKIP":
|
||||
return StagedRecordStatus.REJECTED;
|
||||
}
|
||||
}
|
||||
|
||||
export function resolveDispoStagedRecordStoreKey(
|
||||
recordType: DispoStagedRecordType,
|
||||
): keyof DispoStagedRecordStores {
|
||||
switch (recordType) {
|
||||
case DispoStagedRecordType.RESOURCE:
|
||||
return "stagedResource";
|
||||
case DispoStagedRecordType.CLIENT:
|
||||
return "stagedClient";
|
||||
case DispoStagedRecordType.PROJECT:
|
||||
return "stagedProject";
|
||||
case DispoStagedRecordType.ASSIGNMENT:
|
||||
return "stagedAssignment";
|
||||
case DispoStagedRecordType.VACATION:
|
||||
return "stagedVacation";
|
||||
case DispoStagedRecordType.AVAILABILITY_RULE:
|
||||
return "stagedAvailabilityRule";
|
||||
case DispoStagedRecordType.UNRESOLVED:
|
||||
return "stagedUnresolvedRecord";
|
||||
default:
|
||||
throw new TRPCError({
|
||||
code: "BAD_REQUEST",
|
||||
message: `Unknown record type: ${recordType as string}`,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export type DispoStagedRecordStores = {
|
||||
stagedResource: { update: Function };
|
||||
stagedClient: { update: Function };
|
||||
stagedProject: { update: Function };
|
||||
stagedAssignment: { update: Function };
|
||||
stagedVacation: { update: Function };
|
||||
stagedAvailabilityRule: { update: Function };
|
||||
stagedUnresolvedRecord: { update: Function };
|
||||
};
|
||||
|
||||
export function buildCancelledImportBatchUpdateData() {
|
||||
return { status: ImportBatchStatus.CANCELLED } as const;
|
||||
}
|
||||
|
||||
export function buildResolvedStagedRecordUpdateData(
|
||||
action: StagedRecordAction,
|
||||
) {
|
||||
return { status: resolveDispoStagedRecordStatus(action) } as const;
|
||||
}
|
||||
|
||||
export function buildDispoImportCommitAuditSummary(
|
||||
counts: Record<string, unknown>,
|
||||
): string {
|
||||
return `Committed import batch (${JSON.stringify(counts)})`;
|
||||
}
|
||||
Reference in New Issue
Block a user