import { DomainConflictError } from '../../../common/errors/domain-conflict.error';
type EquipmentInput = Record<string, any>;
function object(input: unknown, code: string): asserts input is EquipmentInput {
if (!input || typeof input !== 'object' || Array.isArray(input)) throw new DomainConflictError(code);
}
function text(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);
}
function optionalText(value: unknown, max: number, code: string): void {
if (value !== undefined && value !== null) {
if (typeof value !== 'string' || value.length > max) throw new DomainConflictError(code);
}
}
function scope(input: unknown, code: string): asserts input is EquipmentInput {
object(input, code);
text(input.organizationId, 200, code);
text(input.locationId, 200, code);
}
function optionalDate(value: unknown, code: string): void {
if (value === undefined || value === null) return;
if (typeof value !== 'string' || value.length > 100 || Number.isNaN(Date.parse(value))) throw new DomainConflictError(code);
}
export function assertEquipmentCreateInput(input: unknown): asserts input is EquipmentInput {
const code = 'equipment.input_invalid';
scope(input, code);
text(input.name, 300, code);
text(input.equipmentType, 100, code);
optionalText(input.serialNumber, 250, code);
optionalText(input.manufacturer, 250, code);
optionalText(input.model, 250, code);
optionalText(input.notes, 4000, code);
optionalText(input.areaId, 200, code);
optionalDate(input.installedAt, code);
}
export function assertEquipmentStatusInput(input: unknown): asserts input is EquipmentInput {
const code = 'equipment.input_invalid';
scope(input, code);
text(input.equipmentId, 200, code);
if (!['active', 'out_of_service', 'maintenance', 'retired'].includes(String(input.status))) throw new DomainConflictError(code);
}
export function assertEquipmentMaintenanceInput(input: unknown): asserts input is EquipmentInput {
const code = 'equipment.input_invalid';
scope(input, code);
text(input.equipmentId, 200, code);
text(input.maintenanceType, 100, code);
text(input.description, 4000, code);
optionalDate(input.performedAt, code);
optionalDate(input.dueAt, code);
optionalText(input.employeeId, 200, code);
}
