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:
Sven laptop
2026-09-24 20:18:32 +02:00
parent 2ae1720383
commit 7d55580d87
44 changed files with 7033 additions and 5343 deletions
+102
View File
@@ -0,0 +1,102 @@
const { store } = require("../mock/store");
const { checkBusConflict } = require("../lib/schedule");
const { reportError } = require("../lib/report-error");
// Fleet repository methods (DEV_MOCK mode).
async function listFleetBuses(req) {
try {
const buses = store.fleetBuses.slice().sort((a, b) => a.id - b.id);
return {
statusCode: 200,
body: {
buses: buses.map((b) => ({
...b,
available: !checkBusConflict(
b.id,
new Date().toISOString(),
new Date(Date.now() + 24 * 60 * 60 * 1000).toISOString(),
[],
store.schedules || [],
),
})),
},
};
} 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 createFleetBus(req) {
try {
const { name, seatCapacity, licensePlate } = req.body;
if (!name || seatCapacity === undefined || !licensePlate) {
return { statusCode: 400, body: { error: "missing_required_fields" } };
}
const bus = {
id: store.nextFleetBusId++,
name,
seatCapacity: Number(seatCapacity),
licensePlate,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
};
store.fleetBuses.push(bus);
return { statusCode: 201, body: { bus } };
} 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 updateFleetBus(req) {
try {
const { busId } = req.params;
const { name, seatCapacity, licensePlate } = req.body;
if (!busId || !Number.isFinite(Number(busId))) {
return { statusCode: 400, body: { error: "invalid_bus_id" } };
}
const bus = store.fleetBuses.find(
(b) => Number(b.id) === Number(busId),
);
if (!bus) return { statusCode: 404, body: { error: "bus_not_found" } };
if (name !== undefined) bus.name = name;
if (seatCapacity !== undefined) bus.seatCapacity = Number(seatCapacity);
if (licensePlate !== undefined) bus.licensePlate = licensePlate;
bus.updatedAt = new Date().toISOString();
return { statusCode: 200, body: { bus } };
} 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 deleteFleetBus(req) {
try {
const { busId } = req.params;
if (!busId || !Number.isFinite(Number(busId))) {
return { statusCode: 400, body: { error: "invalid_bus_id" } };
}
const idx = store.fleetBuses.findIndex(
(b) => Number(b.id) === Number(busId),
);
if (idx === -1) return { statusCode: 404, body: { error: "bus_not_found" } };
const deleted = store.fleetBuses.splice(idx, 1)[0];
return { statusCode: 200, body: { success: true, bus: deleted } };
} 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 = { listFleetBuses, createFleetBus, updateFleetBus, deleteFleetBus };