Refactor transit_api into a repo/routes module split
Split the 5,441-line index.js into an 86-line bootstrap, thin domain
route modules, shared lib/ helpers, and a repo/ layer that isolates the
DEV_MOCK branches (repo/*.mock.js). Repo methods return {statusCode, body};
routes are one-line wrappers. Pin search_path to "transit.bus" and
document the /bus/ prefix with legacy root aliases (Deprecation header).
Parity verified: 57/57 byte-identical on the stub differential harness
plus 24/24 live read-only responses vs the original against real
PostgreSQL. The only intentional behavior change: mock reorder/delete
bus-line endpoints no longer require a live DB (previously 500 in
DEV_MOCK=1 without PG). DEV_MOCK gate and node --check clean.
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
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 };
|
||||
Reference in New Issue
Block a user