Files
KIS-TOiR/server/src/modules/shared/record-mappers.ts

86 lines
2.5 KiB
TypeScript

function decimalToString(value: unknown): string | null {
if (value === null || value === undefined) {
return null;
}
return String(value);
}
function isoDate(value: unknown): string | null {
if (!value) {
return null;
}
const date = value instanceof Date ? value : new Date(String(value));
return Number.isNaN(date.getTime()) ? null : date.toISOString();
}
export function mapEquipment(record: any) {
return {
id: record.id,
name: record.name,
serialNumber: record.serialNumber,
inventoryNumber: record.inventoryNumber,
equipmentType: record.equipmentType,
dateOfInspection: isoDate(record.dateOfInspection),
periodicityTO: record.periodicityTO,
location: record.location ?? null,
status: record.status,
commissionedAt: isoDate(record.commissionedAt),
totalEngineHours: decimalToString(record.totalEngineHours),
engineHoursSinceLastRepair: decimalToString(record.engineHoursSinceLastRepair),
lastRepairAt: isoDate(record.lastRepairAt),
notes: record.notes ?? null,
workAsPartOf: record.workAsPartOf ?? null,
fuelConsumed: record.fuelConsumed ?? null,
};
}
export function mapEmployee(record: any, shallow = false): any {
return {
id: record.code,
code: record.code,
fullName: record.fullName,
role: record.role,
position: record.position,
bossCode: record.bossCode ?? record.boss?.code ?? null,
boss: shallow || !record.boss ? null : mapEmployee(record.boss, true),
subordinates: shallow
? []
: Array.isArray(record.subordinates)
? record.subordinates.map((item: any) => mapEmployee(item, true))
: [],
price: record.price ?? null,
phoneNumber: record.phoneNumber ?? null,
};
}
export function mapPart(record: any) {
return {
id: record.id,
name: record.name,
categories: record.categories ?? null,
price: record.price ?? null,
description: record.description ?? null,
serialNumber: record.serialNumber ?? null,
};
}
export function mapCategoryResource(record: any) {
return {
id: record.id,
partId: record.partId ?? record.part?.id ?? null,
employeeCode: record.employeeCode ?? record.employee?.code ?? null,
part: record.part ? mapPart(record.part) : null,
employee: record.employee ? mapEmployee(record.employee, true) : null,
};
}
export function mapPriceList(costOfWorkingHours: number | null | undefined, partPrice: number | null | undefined) {
return {
id: 'price-list',
costOfWorkingHours: costOfWorkingHours ?? 0,
partPrice: partPrice ?? 0,
};
}