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.
88 lines
2.5 KiB
JavaScript
88 lines
2.5 KiB
JavaScript
const db = require("../db");
|
|
const { parseWeekdaysValue, checkDriverConflict } = require("../lib/schedule");
|
|
const { reportError } = require("../lib/report-error");
|
|
|
|
// Driver repository methods (PostgreSQL mode).
|
|
|
|
async function listDrivers(req) {
|
|
try {
|
|
const result = await db.query(`
|
|
SELECT id, firstname, lastname, email, phone, job, joined, status
|
|
FROM workers.employees
|
|
WHERE status = 'active'
|
|
ORDER BY firstname, lastname ASC
|
|
`);
|
|
|
|
// Get all schedules to check for conflicts
|
|
const schedulesResult = await db.query(
|
|
`SELECT * FROM schedules WHERE status IN ('planned', 'active')`,
|
|
);
|
|
const schedules = schedulesResult.rows.map((row) => ({
|
|
...row,
|
|
weekdays: parseWeekdaysValue(row.weekdays),
|
|
}));
|
|
|
|
return {
|
|
statusCode: 200,
|
|
body: {
|
|
drivers: result.rows.map((row) => {
|
|
const driverId = row.id;
|
|
const now = new Date();
|
|
const tomorrow = new Date(now);
|
|
tomorrow.setDate(tomorrow.getDate() + 1);
|
|
const isAvailable = !checkDriverConflict(
|
|
driverId,
|
|
now.toISOString(),
|
|
tomorrow.toISOString(),
|
|
[],
|
|
schedules,
|
|
);
|
|
|
|
return {
|
|
id: row.id,
|
|
name: (
|
|
String(row.firstname || "") +
|
|
" " +
|
|
String(row.lastname || "")
|
|
).trim(),
|
|
email: row.email,
|
|
phone: row.phone,
|
|
job: row.job,
|
|
joined: row.joined,
|
|
status: row.status,
|
|
available: isAvailable,
|
|
};
|
|
}),
|
|
},
|
|
};
|
|
} 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 listShiftDrivers(req) {
|
|
try {
|
|
const result = await db.query(`
|
|
SELECT id, usercode
|
|
FROM workers.users
|
|
ORDER BY usercode ASC
|
|
`);
|
|
return {
|
|
statusCode: 200,
|
|
body: {
|
|
drivers: result.rows.map((row) => ({
|
|
id: Number(row.id),
|
|
name: String(row.usercode),
|
|
})),
|
|
},
|
|
};
|
|
} catch (err) {
|
|
console.error("GET /admin/shift-drivers error:", err);
|
|
reportError({ partofservice: "GET /admin/shift-drivers", error: err.stack || err.message });
|
|
return { statusCode: 500, body: { error: "internal_error" } };
|
|
}
|
|
}
|
|
|
|
module.exports = { listDrivers, listShiftDrivers }; |