Enhance schedule creation by normalizing selected weekdays; improve day label generation and sorting for better consistency

This commit is contained in:
Sven laptop
2026-05-08 17:00:54 +02:00
parent bf66616c1b
commit 1ce9c70b0f
2 changed files with 106 additions and 8 deletions
+100 -1
View File
@@ -313,7 +313,7 @@ app.get('/schedule/daily', authMiddleware, async (req, res) => {
ORDER BY r.start_time ASC
`, [userId, date]);
const routes = result.rows.map(row => ({
let routes = result.rows.map(row => ({
id: row.id,
startTime: row.start_time,
endTime: row.end_time,
@@ -334,6 +334,105 @@ app.get('/schedule/daily', authMiddleware, async (req, res) => {
},
}));
// If no explicit "routes" rows exist for this user/date, fall back to
// checking scheduled runs in `transit.schedules` (filtered by driver_id).
// This works around cases where DB permissions prevent inserting into
// `transit.routes` but schedules exist and should be shown to the driver.
if (!DEV_MOCK && routes.length === 0) {
const fallback = await db.query(`
SELECT
s.id,
s.start_time,
s.end_time,
s.status,
s.bus_line_id,
bl.line_number,
bl.start_stop,
bl.end_stop,
bl.estimated_duration_minutes,
bl.description,
s.bus_id,
bt.id as bus_type_id,
bt.name as bus_type,
bt.seat_capacity,
bt.license_plate
FROM transit.schedules s
JOIN transit.bus_lines bl ON s.bus_line_id = bl.id
JOIN fleet.bus bt ON s.bus_id = bt.id
WHERE s.driver_id = $1 AND DATE(s.start_time) = $2
ORDER BY s.start_time ASC
`, [userId, date]);
routes = fallback.rows.map(row => ({
id: row.id,
startTime: row.start_time,
endTime: row.end_time,
status: row.status,
busLine: {
id: row.bus_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,
},
}));
}
// If still empty, try to synthesize a single route by shifting a recent
// schedule's time-of-day to the requested date. This is a non-destructive
// fallback to make sure drivers see a trip when DB inserts are restricted.
if (!DEV_MOCK && routes.length === 0) {
const tmplRes = await db.query(`
SELECT s.start_time, s.end_time, s.status, s.bus_line_id, s.bus_id, s.driver_id, bl.estimated_duration_minutes, bl.line_number, bl.start_stop, bl.end_stop, bl.description
FROM transit.schedules s
JOIN transit.bus_lines bl ON s.bus_line_id = bl.id
WHERE s.driver_id = $1
ORDER BY s.start_time DESC
LIMIT 1
`, [userId]);
if (tmplRes.rowCount > 0) {
const t = tmplRes.rows[0];
const requestedDate = new Date(date + 'T00:00:00.000Z');
const srcStart = new Date(t.start_time);
const srcEnd = new Date(t.end_time);
// Preserve time-of-day from template, apply to requested date
const newStart = new Date(requestedDate);
newStart.setUTCHours(srcStart.getUTCHours(), srcStart.getUTCMinutes(), srcStart.getUTCSeconds(), srcStart.getUTCMilliseconds());
const durationMs = srcEnd.getTime() - srcStart.getTime();
const newEnd = new Date(newStart.getTime() + durationMs);
routes.push({
id: null,
startTime: newStart.toISOString(),
endTime: newEnd.toISOString(),
status: t.status || 'planned',
busLine: {
id: t.bus_line_id,
lineNumber: t.line_number,
startStop: t.start_stop,
endStop: t.end_stop,
estimatedDuration: t.estimated_duration_minutes,
description: t.description,
},
busType: {
id: t.bus_id,
name: null,
seatCapacity: null,
licensePlate: null,
},
});
}
}
res.json({ date, routes });
} catch (err) {
console.error(err);