33 lines
934 B
TypeScript
33 lines
934 B
TypeScript
import { fail, type HousekeepingResult, ok } from "../contracts";
|
|
import type { HousekeepingCommand } from "./registry";
|
|
|
|
export interface HousekeepingCommandConfirmation {
|
|
commandId: string;
|
|
risk: "safe" | "sensitive";
|
|
requiresReason: boolean;
|
|
reason?: string;
|
|
}
|
|
|
|
export function confirmHousekeepingCommand<I, O>(
|
|
command: HousekeepingCommand<I, O>,
|
|
reason: string | undefined,
|
|
correlationId: string,
|
|
): HousekeepingResult<HousekeepingCommandConfirmation> {
|
|
const normalizedReason = reason?.normalize("NFC").trim() || undefined;
|
|
if (command.requiresReason && !normalizedReason) {
|
|
return fail("VALIDATION", "errors.housekeeping.validation", correlationId, {
|
|
reason: ["errors.validation.required"],
|
|
});
|
|
}
|
|
|
|
return ok(
|
|
{
|
|
commandId: command.id,
|
|
risk: command.risk,
|
|
requiresReason: command.requiresReason,
|
|
...(normalizedReason === undefined ? {} : { reason: normalizedReason }),
|
|
},
|
|
correlationId,
|
|
);
|
|
}
|