import { DomainConflictError } from '../../../common/errors/domain-conflict.error';
export type AccountingInputRecord = Record<string, unknown>;
export function assertAccountingInputObject(input: unknown, code: string): asserts input is AccountingInputRecord {
if (!input || typeof input !== 'object' || Array.isArray(input)) {
throw new DomainConflictError(code);
}
}
export function assertAccountingScope(
input: unknown,
code: string,
options: { locationRequired?: boolean } = {},
): asserts input is AccountingInputRecord {
assertAccountingInputObject(input, code);
const candidate = input;
assertAccountingText(candidate.organizationId, 200, code);
if (options.locationRequired === false) {
if (candidate.locationId !== undefined && candidate.locationId !== null) {
assertAccountingText(candidate.locationId, 200, code);
}
} else {
assertAccountingText(candidate.locationId, 200, code);
}
}
export function assertAccountingText(value: unknown, max: number, code: string): asserts value is string {
if (typeof value !== 'string' || value.trim().length === 0 || value.length > max) {
throw new DomainConflictError(code);
}
}
export function assertOptionalAccountingText(value: unknown, max: number, code: string): void {
if (value !== undefined && value !== null) assertAccountingText(value, max, code);
}
export function assertAccountingRecord(value: unknown, code: string): asserts value is AccountingInputRecord {
if (!value || typeof value !== 'object' || Array.isArray(value)) {
throw new DomainConflictError(code);
}
}
export function assertAccountingDate(value: unknown, code: string): asserts value is string {
assertAccountingText(value, 10, code);
if (!/^\d{4}-\d{2}-\d{2}$/.test(value)) throw new DomainConflictError(code);
const parsed = new Date(`${value}T00:00:00.000Z`);
if (Number.isNaN(parsed.getTime()) || parsed.toISOString().slice(0, 10) !== value) {
throw new DomainConflictError(code);
}
}
export function assertAccountingEnum(value: unknown, allowed: readonly string[], code: string): void {
if (typeof value !== 'string' || !allowed.includes(value)) throw new DomainConflictError(code);
}
export function assertAccountingSafeInteger(value: unknown, code: string, min = 0, max = 1_000_000): void {
if (!Number.isSafeInteger(value) || Number(value) < min || Number(value) > max) {
throw new DomainConflictError(code);
}
}
export function assertAccountingBoolean(value: unknown, code: string): void {
if (typeof value !== 'boolean') throw new DomainConflictError(code);
}
export function assertAccountingCurrency(value: unknown, code: string): void {
if (typeof value !== 'string' || !/^[A-Z]{3}$/.test(value)) throw new DomainConflictError(code);
}
