7d55580d87
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.
109 lines
2.9 KiB
JavaScript
109 lines
2.9 KiB
JavaScript
const db = require("../db");
|
|
const { reportError } = require("../lib/report-error");
|
|
|
|
// Per-user driving route repository methods (PostgreSQL mode; no DEV_MOCK
|
|
// branch in the original handlers, so this is used in both modes).
|
|
|
|
async function getRoute(req) {
|
|
try {
|
|
const { routeId } = req.params;
|
|
const userId = req.user.userId;
|
|
|
|
const result = await db.query(
|
|
`
|
|
SELECT
|
|
r.id,
|
|
r.start_time,
|
|
r.end_time,
|
|
r.status,
|
|
bl.id as line_id,
|
|
bl.line_number,
|
|
bl.start_stop,
|
|
bl.end_stop,
|
|
bl.estimated_duration_minutes,
|
|
bl.description,
|
|
bt.id as bus_type_id,
|
|
bt.name as bus_type,
|
|
bt.seat_capacity,
|
|
bt.license_plate
|
|
FROM routes r
|
|
JOIN bus_lines bl ON r.bus_line_id = bl.id
|
|
JOIN bus_types bt ON r.bus_type_id = bt.id
|
|
WHERE r.id = $1 AND r.user_id = $2
|
|
`,
|
|
[routeId, userId],
|
|
);
|
|
|
|
if (result.rows.length === 0) {
|
|
return { statusCode: 404, body: { error: "route_not_found" } };
|
|
}
|
|
|
|
const row = result.rows[0];
|
|
const route = {
|
|
id: row.id,
|
|
startTime: row.start_time,
|
|
endTime: row.end_time,
|
|
status: row.status,
|
|
busLine: {
|
|
id: row.line_id,
|
|
lineNumber: row.line_number,
|
|
startStop: row.start_stop,
|
|
endStop: row.end_stop,
|
|
estimatedDuration: row.estimated_duration_minutes,
|
|
description: row.description,
|
|
},
|
|
busType: {
|
|
id: row.bus_type_id,
|
|
name: row.bus_type,
|
|
seatCapacity: row.seat_capacity,
|
|
licensePlate: row.license_plate,
|
|
},
|
|
};
|
|
|
|
return { statusCode: 200, body: { route } };
|
|
} 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 } };
|
|
}
|
|
}
|
|
|
|
async function updateRouteStatus(req) {
|
|
try {
|
|
const { routeId } = req.params;
|
|
const { status } = req.body;
|
|
const userId = req.user.userId;
|
|
|
|
const validStatuses = [
|
|
"scheduled",
|
|
"in_progress",
|
|
"completed",
|
|
"cancelled",
|
|
];
|
|
if (!validStatuses.includes(status)) {
|
|
return { statusCode: 400, body: { error: "invalid_status" } };
|
|
}
|
|
|
|
const result = await db.query(
|
|
`
|
|
UPDATE routes
|
|
SET status = $1, updated_at = NOW()
|
|
WHERE id = $2 AND user_id = $3
|
|
RETURNING *
|
|
`,
|
|
[status, routeId, userId],
|
|
);
|
|
|
|
if (result.rows.length === 0) {
|
|
return { statusCode: 404, body: { error: "route_not_found" } };
|
|
}
|
|
|
|
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 = { getRoute, updateRouteStatus }; |