Dispatch shows wrong map pin and drive time when inspection address text is correct

Objective

  • Fix dispatch map pins and drive-time forecasts so they reflect the actual inspection location when the address text shown in dispatch is already correct.
  • Give schedulers and dispatch staff trustworthy travel context between inspections—wrong GPS coordinates lead to incorrect Mapbox routes, inflated or deflated drive times, and poor scheduling decisions.
  • Prevent split-brain location data on inspections where the work order property looks correct but calendar/dispatch routing uses stale coordinates.

Background

  • Reported example: Work order 1008598200 — the inspection address displays correctly in dispatch, but the map pin sits at incorrect GPS coordinates, producing a wrong drive-time forecast to/from neighboring jobs on that inspector's day.
  • Dispatch loads calendar events and builds Mapbox driving routes from each stop's lat/lng (attik-frontend/src/app/tools/dispatch/dispatchContext.tsxcleanSearchCoordscallDirectionsServer). Drive time is computed from coordinates, not from the address string.
  • The calendar API mixes two fields on the same inspection document (attik-backend/src/routes/calendar.ts):
  • Address labelinspection.property.address (fallback inspection.address)
  • Map pin / routing coords → top-level inspection.lat / inspection.lng
  • Inspections store coordinates in two places (attik-backend/src/models/inspectionSchema.ts):
  • Top-level lat / lng — used by dispatch, calendar, reschedule preview
  • Nested property.lat / property.lng — used by work order map, scheduler, pricing
  • On create, both are set together (attik-backend/src/util/functions/inspection/createInspection.ts). On property edit from the work order, only property is PATCHed (attik-frontend/src/app/tools/inspections/[id]/components/PropertySectionWrapper.tsx sends { property: propertyData }). Backend updates property.lat / property.lng but does not mirror to top-level unless lat / lng are sent separately (attik-backend/src/routes/inspection.ts).
  • Diagnostic split: If the work order static map (uses property.lat) looks correct but the dispatch pin is wrong → likely stale top-level coordinates after a property/address correction. If both are wrong → bad geocoding at creation or bad coords from import (ISN/Spectora).

Root cause hypothesis (code trace)

Primary: duplicate coordinate fields drift after property/address updates.

  1. User corrects address on work order → property.lat / property.lng update with new geocode.
  2. Top-level inspection.lat / inspection.lng remain at original scheduling/import values.
  3. Calendar aggregation projects $inspection.lat / $inspection.lng into dispatch events while address text comes from property.address.
  4. Mapbox route legs in cleanSearchCoords use the stale top-level coords → wrong pin + wrong drive time despite correct address tooltip.

Secondary causes to rule out on example job:

  • Bad geocoding at initial scheduler entry (Mapbox wrong match).
  • ISN historical import using incorrect latitude / longitude from source order (attik-backend/src/util/functions/isn/importIsnHistoricalOrder.ts sets both levels the same at import—less likely unless address edited post-import).
  • Missing/zero coords (lat/lng = 0) skipped in routing—usually omits pin rather than wrong location.

Scope

Backend (attik-backend)

  • src/routes/calendar.ts — calendar event projection uses top-level lat/lng only; consider $ifNull fallback to property.lat / property.lng for legacy/drifted records.
  • src/routes/inspection.ts — when property.lat / property.lng (or full property address) update, mirror coords (and optionally address) to top-level inspection.lat / inspection.lng / inspection.address.
  • src/models/inspectionSchema.ts — document single source of truth or pre-save sync if appropriate.
  • src/routes/schedule.ts — optimal-slots path also references $inspection.lat; verify parity after fix.
  • Optional one-time backfill for inspections where property.lat/lng ≠ top-level lat/lng (or property has coords and top-level is 0).

Frontend (attik-frontend)

  • src/app/tools/inspections/[id]/components/PropertySectionWrapper.tsx — property save sends only property; may also send top-level lat/lng as belt-and-suspenders until backend sync exists.
  • src/app/tools/dispatch/dispatchContext.tsx and src/components/scheduling/dispatchScheduling/cleanSearchCoords.ts — verify routing uses corrected calendar payload (no client-side workaround needed if backend is fixed).
  • src/components/scheduling/DispatchSection.tsx — scheduler dispatch section uses same calendar + directions pattern; confirm fix applies there too.
  • src/app/tools/inspections/[id]/components/RescheduleJobModal.tsx — uses top-level inspection.lat/lng; should benefit from backend sync.

Verification on example job

  • For inspection 1008598200, compare top-level vs property lat/lng in DB/API before and after fix.
  • Confirm dispatch pin, Mapbox route, and drive-time blocks align with work order map for that job's scheduled day.

Out of scope (v1)

  • Persisting/caching Mapbox directions legs globally (see ATT-819).
  • Dispatch optimization / Mapbox cost layer (ATT-1821).
  • Re-geocoding every inspection in bulk without drift detection.

Done when

  • [ ] Dev confirms on 1008598200 (or repro case) whether top-level vs property coords diverge.
  • [ ] Property/address updates keep top-level and property coordinates in sync going forward.
  • [ ] Calendar/dispatch events use correct coords (fallback or synced source) so map pin matches address.
  • [ ] Drive-time legs between inspections on the same day reflect actual travel distance for corrected jobs.
  • [ ] Scheduler dispatch section and tools/dispatch page behave consistently.
  • [ ] Optional backfill script or migration documented/run for existing drifted inspections.
  • [ ] Test coverage for property PATCH updating both coordinate locations.

References

  • Example work order: https://www.attik.ai/inspections/1008598200
  • Related: ATT-819 (persist drive times), ATT-1048 (reschedule dispatch context)
  • attik-backend/src/routes/calendar.ts
  • attik-backend/src/routes/inspection.ts
  • attik-frontend/src/app/tools/dispatch/dispatchContext.tsx
  • attik-frontend/src/components/scheduling/dispatchScheduling/cleanSearchCoords.ts

Please authenticate to join the conversation.

Upvoters
Status

Triage

Board
🏠

Main App

Date

About 1 hour ago

Author

Linear

Subscribe to post

Get notified by email when there are changes.