diff --git a/src/schemas/team.ts b/src/schemas/team.ts index f480166..0ce06d9 100644 --- a/src/schemas/team.ts +++ b/src/schemas/team.ts @@ -527,6 +527,9 @@ function mapQuotation(item: QuotationWithRelations) { } function mapLocalOrder(item: OrderWithRelations) { + const progress = orderLocationProgress(item.status) + const locationLatitude = interpolateCoordinate(item.sourceLatitude, item.destinationLatitude, progress) + const locationLongitude = interpolateCoordinate(item.sourceLongitude, item.destinationLongitude, progress) return { uuid: item.uuid, quotationUuid: item.quotation?.uuid ?? null, @@ -549,10 +552,48 @@ function mapLocalOrder(item: OrderWithRelations) { updatedAt: item.updatedAt.toISOString(), notes: item.notes ?? '', orderLines: [], - stages: [], + stages: [ + { + uuid: `${item.uuid}-route`, + name: item.name, + sequence: 1, + stageType: 'route', + transportType: item.quotation?.transportTypeCode ?? null, + sourceLocationName: item.sourceLocationName, + sourceLatitude: item.sourceLatitude, + sourceLongitude: item.sourceLongitude, + destinationLocationName: item.destinationLocationName, + destinationLatitude: item.destinationLatitude, + destinationLongitude: item.destinationLongitude, + locationName: orderLocationName(item, progress), + locationLatitude, + locationLongitude, + selectedCompany: null, + trips: [], + }, + ], } } +function orderLocationProgress(status: string | null): number { + const normalized = (status ?? '').toLowerCase().replaceAll('-', '_').trim() + if (normalized === 'delivered' || normalized === 'completed' || normalized === 'done') return 1 + if (normalized === 'in_transit' || normalized === 'active') return 0.55 + if (normalized === 'processing' || normalized === 'accepted') return 0.25 + return 0.05 +} + +function interpolateCoordinate(start: number | null, end: number | null, progress: number): number | null { + if (start === null || end === null) return start ?? end + return start + (end - start) * progress +} + +function orderLocationName(item: OrderWithRelations, progress: number): string | null { + if (progress >= 1) return item.destinationLocationName ?? 'Груз доставлен' + if (progress <= 0.05) return item.sourceLocationName ?? 'Груз принят' + return 'Груз в пути' +} + function requiredString(value: unknown, label: string): string { const next = normalizeString(value) if (!next) {