Files
The-transit-Project/transit_api/repo/stops.mock.js
T
Sven laptop 7d55580d87 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.
2026-09-24 20:18:32 +02:00

96 lines
3.3 KiB
JavaScript

const { store } = require("../mock/store");
const { reportError } = require("../lib/report-error");
// Stop repository methods (DEV_MOCK mode). Each mirrors the original handler's
// `if (DEV_MOCK) { ... }` branch verbatim and returns { statusCode, body }.
async function listStops(req) {
try {
return {
statusCode: 200,
body: {
stops: store.stops.slice().sort((a, b) => a.id - b.id),
},
};
} 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 createStop(req) {
try {
const { name, latitude, longitude } = req.body;
if (!name || latitude === undefined || longitude === undefined) {
return { statusCode: 400, body: { error: "missing_required_fields" } };
}
const stop = {
id: store.nextStopId++,
name,
latitude: parseFloat(latitude),
longitude: parseFloat(longitude),
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
};
store.stops.push(stop);
return { statusCode: 201, body: { stop } };
} 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 deleteStop(req) {
try {
const { stopId } = req.params;
if (!stopId || !Number.isFinite(Number(stopId))) {
return { statusCode: 400, body: { error: "invalid_stop_id" } };
}
const index = store.stops.findIndex(
(s) => Number(s.id) === Number(stopId),
);
if (index === -1) {
return { statusCode: 404, body: { error: "stop_not_found" } };
}
const deletedStop = store.stops.splice(index, 1)[0];
return { statusCode: 200, body: { success: true, stop: deletedStop } };
} catch (err) {
console.error(err);
reportError({ partofservice: `${req.method} ${req.originalUrl}`, error: err.stack || err.message });
if (err.code === "23503") {
return { statusCode: 409, body: { error: "foreign_key_violation", message: "This stop is referenced by route_stops" } };
}
return { statusCode: 500, body: { error: "internal_error", details: err.message } };
}
}
async function updateStop(req) {
try {
const { stopId } = req.params;
const { name, latitude, longitude } = req.body;
if (!stopId || !Number.isFinite(Number(stopId))) {
return { statusCode: 400, body: { error: "invalid_stop_id" } };
}
const stop = store.stops.find((s) => Number(s.id) === Number(stopId));
if (!stop) return { statusCode: 404, body: { error: "stop_not_found" } };
if (name !== undefined) stop.name = name;
if (latitude !== undefined) stop.latitude = parseFloat(latitude);
if (longitude !== undefined) stop.longitude = parseFloat(longitude);
stop.updatedAt = new Date().toISOString();
return { statusCode: 200, body: { stop } };
} 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 = { listStops, createStop, updateStop, deleteStop };