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.
102 lines
3.4 KiB
JavaScript
102 lines
3.4 KiB
JavaScript
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 }; |