import type { DispatchChecklistItem, DispatchComment, DispatchDetail, DispatchSignoff, DispatchToVendorPayload, DispatchWorkOrderRef, UpdateDispatchPayload, } from "@/domain/work-orders/types/work-order"; function asRecord(raw: unknown): Record { return typeof raw === "object" && raw !== null ? (raw as Record) : {}; } function readId(record: Record): string | number { const value = record.id ?? record.Id; if (typeof value === "string" || typeof value === "number") { return value; } return ""; } function readString(record: Record, ...keys: string[]): string { for (const key of keys) { const value = record[key]; if (typeof value === "string") { return value; } if (typeof value === "number") { return String(value); } } return ""; } function readNumber(record: Record, ...keys: string[]): number | null { for (const key of keys) { const value = record[key]; if (typeof value === "number") { return value; } if (typeof value === "string" && value !== "") { const parsed = Number(value); if (!Number.isNaN(parsed)) { return parsed; } } } return null; } function readBool(record: Record, ...keys: string[]): boolean { for (const key of keys) { const value = record[key]; if (typeof value === "boolean") { return value; } } return false; } export function mapDispatchChecklistItem(raw: unknown): DispatchChecklistItem { const item = asRecord(raw); return { id: readId(item), itemText: readString(item, "itemText", "ItemText"), isCompleted: readBool(item, "isCompleted", "IsCompleted"), completedBy: readString(item, "completedBy", "CompletedBy") || null, completedAt: readString(item, "completedAt", "CompletedAt") || null, }; } export function mapDispatchSignoff(raw: unknown): DispatchSignoff { const item = asRecord(raw); return { id: readId(item), signoffType: readString(item, "signoffType", "SignoffType"), name: readString(item, "name", "Name"), signature: readString(item, "signature", "Signature"), signatureMethod: readString(item, "signatureMethod", "SignatureMethod"), signedAt: readString(item, "signedAt", "SignedAt"), }; } export function mapDispatchComment(raw: unknown): DispatchComment { const item = asRecord(raw); return { id: readId(item), createdDate: readString(item, "createdDate", "CreatedDate"), commenttext: readString(item, "commenttext", "Commenttext", "text", "Text"), userName: readString(item, "userName", "UserName"), commenter: readString(item, "commenter", "Commenter") || null, }; } export function mapDispatchWorkOrderRef(raw: unknown): DispatchWorkOrderRef { const item = asRecord(raw); return { id: readId(item), woNumber: readString(item, "woNumber", "WoNumber", "WONumber", "number", "Number"), }; } export function mapDispatchDetail(raw: unknown): DispatchDetail { const item = asRecord(raw); const checklistItems = item.checklistItems ?? item.ChecklistItems; const signoffs = item.signoffs ?? item.Signoffs; const comments = item.comments ?? item.Comments; const workOrders = item.workOrders ?? item.WorkOrders; return { id: readId(item), dispatchNumber: readString(item, "dispatchNumber", "DispatchNumber"), poNumber: readString(item, "poNumber", "PoNumber", "PONumber"), vendorName: readString(item, "vendorName", "VendorName"), vendorEmail: readString(item, "vendorEmail", "VendorEmail"), woNumber: readString(item, "woNumber", "WoNumber"), status: readString(item, "status", "Status"), nteAmount: readNumber(item, "nteAmount", "NTEAmount"), scheduledDate: readString(item, "scheduledDate", "ScheduledDate"), estimatedArrivalAt: readString(item, "estimatedArrivalAt", "EstimatedArrivalAt"), etaManualOverride: readBool(item, "etaManualOverride", "EtaManualOverride"), completedDate: readString(item, "completedDate", "CompletedDate"), description: readString(item, "description", "Description"), dispatchedAt: readString(item, "dispatchedAt", "DispatchedAt"), acknowledgedAt: readString(item, "acknowledgedAt", "AcknowledgedAt"), verifiedAt: readString(item, "verifiedAt", "VerifiedAt"), verifiedBy: readString(item, "verifiedBy", "VerifiedBy"), checklistItems: Array.isArray(checklistItems) ? checklistItems.map(mapDispatchChecklistItem) : [], signoffs: Array.isArray(signoffs) ? signoffs.map(mapDispatchSignoff) : [], comments: Array.isArray(comments) ? comments.map(mapDispatchComment) : [], workOrders: Array.isArray(workOrders) ? workOrders.map(mapDispatchWorkOrderRef) : [], }; } export function mapDispatchToVendorPayload( payload: DispatchToVendorPayload, ): Record { return { WorkOrderIds: payload.workOrderIds, VendorIds: payload.vendorIds, NTEAmount: payload.nteAmount, ScheduledDate: payload.scheduledDate, EstimatedArrivalAt: payload.estimatedArrivalAt, EtaManualOverride: payload.etaManualOverride ?? false, Description: payload.description, TaskListTemplateId: payload.taskListTemplateId, CustomChecklistItems: payload.customChecklistItems, }; } export function mapUpdateDispatchPayload(payload: UpdateDispatchPayload): Record { return { Id: payload.id, Status: payload.status, NTEAmount: payload.nteAmount, ScheduledDate: payload.scheduledDate, EstimatedArrivalAt: payload.estimatedArrivalAt, EtaManualOverride: payload.etaManualOverride ?? false, CompletedDate: payload.completedDate, Description: payload.description, }; }