@@ -394,6 +394,18 @@ const getJobMaxSpeedKmh = (row) => {
394394 return speed ;
395395} ;
396396
397+ const parseRouteJobDetails = ( row ) => {
398+ const rawPayload = parseWebhookRawPayload ( row . raw ) ;
399+ const rawData = rawPayload && typeof rawPayload === 'object' ? rawPayload . data || { } : { } ;
400+
401+ return {
402+ cargoName : rawData . cargo_name ? String ( rawData . cargo_name ) . trim ( ) : null ,
403+ publicUrl : rawData . public_url || ( row . job_id ? `https://hub.truckyapp.com/job/${ row . job_id } ` : null ) ,
404+ startedAt : rawData . started_at || null ,
405+ market : row . market || rawData . market || null ,
406+ } ;
407+ } ;
408+
397409const parseRouteFromRaw = ( row ) => {
398410 const rawPayload = parseWebhookRawPayload ( row . raw ) ;
399411 const rawData = rawPayload && typeof rawPayload === 'object' ? rawPayload . data || { } : { } ;
@@ -668,8 +680,11 @@ const buildRoutesPayload = async ({
668680 const select = [
669681 'job_id' ,
670682 'company_id' ,
683+ 'driver_id' ,
671684 'source_city_id' ,
672685 'destination_city_id' ,
686+ 'cargo_id' ,
687+ 'market' ,
673688 'planned_distance_km' ,
674689 'driven_distance_km' ,
675690 'real_driven_distance_km' ,
@@ -696,7 +711,8 @@ const buildRoutesPayload = async ({
696711 company,
697712 totalDistanceKm : 0 ,
698713 totalJobs : 0 ,
699- routes : new Map ( ) ,
714+ uniqueRouteKeys : new Set ( ) ,
715+ routes : [ ] ,
700716 } ) ;
701717 }
702718
@@ -706,38 +722,40 @@ const buildRoutesPayload = async ({
706722 if ( ! groupedRoutes . has ( companyId ) ) continue ;
707723
708724 const route = parseRouteFromRaw ( row ) ;
725+ const driver = parseDriverFromRaw ( row ) ;
726+ const jobDetails = parseRouteJobDetails ( row ) ;
709727 const distanceKm = getJobDistanceKm ( row ) ;
710728 const plannedDistanceKm = getJobPlannedDistanceKm ( row ) ;
711729 const maxSpeedKmh = getJobMaxSpeedKmh ( row ) ;
712730 const group = groupedRoutes . get ( companyId ) ;
731+ const updatedAt = row . updated_at || row . created_at || null ;
713732
714733 group . totalDistanceKm += distanceKm ;
715734 group . totalJobs += 1 ;
735+ group . uniqueRouteKeys . add ( route . routeKey ) ;
716736 routeKeys . add ( route . routeKey ) ;
717-
718- if ( ! group . routes . has ( route . routeKey ) ) {
719- group . routes . set ( route . routeKey , {
720- routeKey : route . routeKey ,
721- source : route . source ,
722- destination : route . destination ,
723- totalDistanceKm : 0 ,
724- totalPlannedDistanceKm : 0 ,
725- maxSpeedKmh : null ,
726- totalJobs : 0 ,
727- lastJobAt : row . updated_at || row . created_at || null ,
728- } ) ;
729- }
730-
731- const routeEntry = group . routes . get ( route . routeKey ) ;
732- routeEntry . totalDistanceKm += distanceKm ;
733- routeEntry . totalPlannedDistanceKm += plannedDistanceKm ;
734- routeEntry . maxSpeedKmh = maxSpeedKmh == null
735- ? routeEntry . maxSpeedKmh
736- : Math . max ( routeEntry . maxSpeedKmh == null ? 0 : routeEntry . maxSpeedKmh , maxSpeedKmh ) ;
737- routeEntry . totalJobs += 1 ;
738- if ( ( row . updated_at || row . created_at || '' ) > ( routeEntry . lastJobAt || '' ) ) {
739- routeEntry . lastJobAt = row . updated_at || row . created_at || null ;
740- }
737+ group . routes . push ( {
738+ jobId : Number ( row . job_id || 0 ) ,
739+ routeKey : route . routeKey ,
740+ source : route . source ,
741+ destination : route . destination ,
742+ driver : Number . isFinite ( driver . id ) && driver . id > 0 ? driver : null ,
743+ cargoName : jobDetails . cargoName || ( row . cargo_id ? String ( row . cargo_id ) . trim ( ) : null ) ,
744+ market : jobDetails . market ,
745+ publicUrl : jobDetails . publicUrl ,
746+ startedAt : jobDetails . startedAt ,
747+ createdAt : row . created_at || null ,
748+ updatedAt,
749+ lastJobAt : updatedAt ,
750+ status : row . status || null ,
751+ eventType : row . event_type || null ,
752+ distanceKm : Math . floor ( distanceKm ) ,
753+ plannedDistanceKm : Math . floor ( plannedDistanceKm ) ,
754+ totalDistanceKm : Math . floor ( distanceKm ) ,
755+ totalPlannedDistanceKm : Math . floor ( plannedDistanceKm ) ,
756+ maxSpeedKmh : maxSpeedKmh == null ? null : Math . round ( maxSpeedKmh * 100 ) / 100 ,
757+ totalJobs : 1 ,
758+ } ) ;
741759 }
742760
743761 const routeMetadataMap = await fetchRouteMetadata ( Array . from ( routeKeys ) , includeCoordinates ) ;
@@ -748,16 +766,14 @@ const buildRoutesPayload = async ({
748766 stats : {
749767 totalDistanceKm : Math . floor ( entry . totalDistanceKm ) ,
750768 totalJobs : entry . totalJobs ,
751- totalRoutes : entry . routes . size ,
769+ totalRoutes : entry . uniqueRouteKeys . size ,
770+ totalRouteEntries : entry . routes . length ,
752771 } ,
753- routes : [ ... entry . routes . values ( ) ]
772+ routes : entry . routes
754773 . map ( ( route ) => {
755774 const metadata = routeMetadataMap . get ( route . routeKey ) || null ;
756775 return {
757776 ...route ,
758- totalDistanceKm : Math . floor ( route . totalDistanceKm ) ,
759- totalPlannedDistanceKm : Math . floor ( route . totalPlannedDistanceKm ) ,
760- maxSpeedKmh : route . maxSpeedKmh == null ? null : Math . round ( route . maxSpeedKmh * 100 ) / 100 ,
761777 routeCache : metadata ? {
762778 distanceMeters : metadata . distanceMeters ,
763779 durationSeconds : metadata . durationSeconds ,
0 commit comments