const db = require("../db"); const { reportError } = require("../lib/report-error"); // Navigation repository methods (PostgreSQL mode; no DEV_MOCK branch). async function saveNavigationRoute(req) { try { const { routeId, startLat, startLon, endLat, endLon, totalDistance, totalDuration, geojson, } = req.body; if ( !routeId || !startLat || !startLon || !endLat || !endLon || !totalDistance || !totalDuration ) { return { statusCode: 400, body: { error: "missing_required_fields" } }; } const result = await db.query( ` INSERT INTO navigation_routes (route_id, start_lat, start_lon, end_lat, end_lon, total_distance_meters, total_duration_seconds, polyline_geojson) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) ON CONFLICT (route_id) DO UPDATE SET start_lat = $2, start_lon = $3, end_lat = $4, end_lon = $5, total_distance_meters = $6, total_duration_seconds = $7, polyline_geojson = $8 RETURNING * `, [ routeId, startLat, startLon, endLat, endLon, totalDistance, totalDuration, JSON.stringify(geojson), ], ); return { statusCode: 200, body: { route: result.rows[0] } }; } catch (err) { console.error(err); reportError({ partofservice: `${req.method} ${req.originalUrl}`, error: err.stack || err.message }); return { statusCode: 500, body: { error: "internal_error", details: err.message } }; } } module.exports = { saveNavigationRoute };