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:
@@ -0,0 +1,184 @@
|
||||
# Bus API `/bus/` Prefix Refactor — Agent Instructions
|
||||
|
||||
**Goal:** Every bus endpoint served by `transit_api` lives under a `/bus/` route prefix
|
||||
(e.g. `/bus/schedules`, `/bus/admin/stops`), and the `admin_ui` talks only to the
|
||||
prefixed endpoints.
|
||||
|
||||
**Scope:** `transit_api/index.js` (server) + `admin_ui` (client). Do NOT touch
|
||||
`auth_api`, `employee_api`, `error_log_api`.
|
||||
|
||||
---
|
||||
|
||||
## 1. Canonical paths / non-negotiable rules
|
||||
|
||||
1. `/health` stays **unprefixed** and stays registered directly on `app`. The
|
||||
`monitoring_ui` latency graphs poll `transit_api` at `/health` and must never be
|
||||
affected.
|
||||
2. Every other existing route gets a `/bus/...` canonical form. Full list (from
|
||||
`transit_api/index.js`):
|
||||
|
||||
| Method | Current | New |
|
||||
| --- | --- | --- |
|
||||
| GET | `/schedule/daily` | `/bus/schedule/daily` |
|
||||
| GET | `/driver/schedules/daily` | `/bus/driver/schedules/daily` |
|
||||
| GET | `/routes/:routeId` | `/bus/routes/:routeId` |
|
||||
| GET | `/bus-lines/:busLineId/stops` | `/bus/bus-lines/:busLineId/stops` |
|
||||
| GET/POST | `/admin/stops` | `/bus/admin/stops` |
|
||||
| GET/POST/PATCH | `/admin/bus-lines` and `/admin/bus-lines/:busLineId` | `/bus/admin/...` |
|
||||
| GET/POST/PATCH/DELETE | `/admin/fleet/buses*` | `/bus/admin/fleet/buses*` |
|
||||
| GET | `/admin/bus-lines/:busLineId/stops` | `/bus/admin/bus-lines/:busLineId/stops` |
|
||||
| POST | `/admin/bus-lines/:busLineId/stops` | `/bus/admin/...` (same) |
|
||||
| POST | `/admin/bus-lines/:busLineId/stops/reorder` | `/bus/admin/.../reorder` |
|
||||
| DELETE | `/admin/stops/:stopId` | `/bus/admin/stops/:stopId` |
|
||||
| PATCH | `/admin/stops/:stopId` | `/bus/admin/stops/:stopId` |
|
||||
| DELETE | `/admin/bus-lines/:busLineId` | `/bus/admin/bus-lines/:busLineId` |
|
||||
| DELETE | `/admin/bus-lines/:busLineId/stops/:routeStopId` | `/bus/...` |
|
||||
| POST | `/navigation/route` | `/bus/navigation/route` |
|
||||
| POST | `/gps/track` | `/bus/gps/track` |
|
||||
| GET | `/gps/route/:routeId` | `/bus/gps/route/:routeId` |
|
||||
| PATCH | `/routes/:routeId/status` | `/bus/routes/:routeId/status` |
|
||||
| PATCH | `/driver/weekly-schedules/:scheduleId/status` | `/bus/driver/...` |
|
||||
| PATCH | `/driver/schedules/:scheduleId/status` | `/bus/driver/...` |
|
||||
| GET | `/admin/drivers` | `/bus/admin/drivers` |
|
||||
| GET | `/admin/shift-drivers` | `/bus/admin/shift-drivers` |
|
||||
| GET/POST/PUT/DELETE | `/admin/weekly-schedules*` | `/bus/admin/weekly-schedules*` |
|
||||
| GET/POST/PUT/DELETE | `/admin/schedules*` | `/bus/admin/schedules*` |
|
||||
| POST | `/admin/schedules/bulk` | `/bus/admin/schedules/bulk` |
|
||||
| GET/POST/... | `/schedules`, `/schedules/overview`, `/schedules/recurring`, `/schedules/:scheduleId`, `/schedules/:scheduleId/stops` | `/bus/...` |
|
||||
| POST | `/driver/schedules/:scheduleId/stops/:stopOrder/passed` | `/bus/...` |
|
||||
| GET | `/public/schedules/:scheduleId/progress` | `/bus/public/schedules/:scheduleId/progress` |
|
||||
| POST | `/admin/check-driver-conflicts` | `/bus/admin/check-driver-conflicts` |
|
||||
| POST | `/admin/assign-schedules` | `/bus/admin/assign-schedules` |
|
||||
|
||||
3. **Backward compatibility is mandatory.** The legacy Windows XP driver
|
||||
(`windows_xp_driver_ui/driver_xp.c:787`, `:828`) hardcodes
|
||||
`:5001/schedule/daily` and `:5001/bus-lines/%d/stops`. Until that client is
|
||||
retired, the old root paths MUST keep working as deprecated aliases. Implement
|
||||
them with the double-mount technique in section 2 so there is only ONE route
|
||||
definition per endpoint (no duplicate handlers).
|
||||
|
||||
---
|
||||
|
||||
## 2. Server change — `transit_api/index.js` (Express Router double-mount)
|
||||
|
||||
This keeps one handler per endpoint while exposing both `/bus/...` (canonical) and
|
||||
the legacy root path (deprecated alias).
|
||||
|
||||
1. **Keep** `/health` registered on `app` and **keep** the global pieces
|
||||
(`express.json()`, `cors()`, and the `res.json` error-reporting middleware at
|
||||
the top) untouched.
|
||||
2. Insert this line after the `/health` handler (after line ~82):
|
||||
|
||||
```js
|
||||
const busRouter = express.Router();
|
||||
```
|
||||
|
||||
3. Change the method calls of **every route except `/health`** from `app.` to
|
||||
`busRouter.`:
|
||||
- Find all occurrences of `app.get(`, `app.post(`, `app.put(`, `app.patch(`,
|
||||
`app.delete(` in `transit_api/index.js`.
|
||||
- Change the one for `/health` back to `app.get(` if your replace touches it —
|
||||
`/health` must stay on `app`.
|
||||
- Every other occurrence becomes `busRouter.get(`, `busRouter.post(`, etc.
|
||||
|
||||
4. Mount the router **twice** — after the last route definition (end of file):
|
||||
|
||||
```js
|
||||
// Canonical bus routes.
|
||||
app.use("/bus", busRouter);
|
||||
|
||||
// Legacy aliases for old clients (Windows XP driver). Deprecated: remove only
|
||||
// after the driver is retired.
|
||||
app.use("/", (req, res, next) => {
|
||||
res.set("Deprecation", 'true; date="2030-01-01"');
|
||||
next();
|
||||
}, busRouter);
|
||||
```
|
||||
|
||||
Result: `/bus/admin/stops` and `/admin/stops` both work, `app` stays in charge
|
||||
of `/health`, and there is still exactly one handler per path pattern.
|
||||
|
||||
5. Do NOT add a `/bus/health`. Do NOT move the `DEV_MOCK` branches — they are
|
||||
inside the handlers and move along automatically.
|
||||
6. When a train API is built later, register a separate
|
||||
`const trainRouter = express.Router()` and mount it at `/train`. Bus and train
|
||||
must never share the router instance.
|
||||
|
||||
---
|
||||
|
||||
## 3. Client change — `admin_ui/src/api.js`
|
||||
|
||||
All transit calls in the admin UI go through `transitClient(token)` in
|
||||
`admin_ui/src/api.js:64`. Prefix once, centrally:
|
||||
|
||||
1. Change the `baseURL` of the transit axios client:
|
||||
|
||||
```js
|
||||
export const transitClient = (token) => {
|
||||
const client = axios.create({
|
||||
baseURL: `${TRANSIT_API}/bus`,
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
...
|
||||
```
|
||||
|
||||
2. Verify every call site uses `transitClient`:
|
||||
- `BusLinesManager.jsx` → `/admin/bus-lines`, `/admin/stops` ✓
|
||||
- `StopsManager.jsx` → `/admin/stops` ✓
|
||||
- `FleetManager.jsx` → `/admin/fleet/buses` ✓
|
||||
- `LinkStop.jsx` → `/bus-lines/:id/stops`, `/admin/bus-lines/...`, `/admin/stops` ✓
|
||||
- `ScheduleMaker.jsx`, `WeeklyScheduleEditor.jsx` → `/admin/weekly-schedules*`,
|
||||
`/admin/bus-lines`, `/admin/schedules` ✓
|
||||
- `FixedScheduleEditor.jsx` → `/admin/schedules*`, `/admin/check-driver-conflicts`,
|
||||
`/admin/drivers` ✓
|
||||
- `Overview.jsx` → `/admin/stops`, `/admin/bus-lines`, `/admin/fleet/buses`,
|
||||
`/admin/shift-drivers` ✓
|
||||
- `TimetableDisplay.jsx` → `/schedules/overview`, `/admin/bus-lines/:id/stops` ✓
|
||||
- **Do NOT prefix** anything using `errorLogClient` or `authClient`
|
||||
(`ErrorLogViewer.jsx` `/error_log`, `Login.jsx` `/login` + `/me`). Those
|
||||
services are unchanged.
|
||||
|
||||
3. There are no `/health` calls on `transitClient`, so nothing breaks by adding the
|
||||
prefix.
|
||||
|
||||
---
|
||||
|
||||
## 4. Other consumers — check, do not silently break
|
||||
|
||||
| Consumer | Effect | Action |
|
||||
| --- | --- | --- |
|
||||
| `monitoring_ui` latency polls | hits `/health` | unaffected (rule 1) |
|
||||
| `monitoring_ui` Request Lab | user-entered paths | note in docs that bus paths now need `/bus/` |
|
||||
| `windows_xp_driver_ui/driver_xp.c` | hardcodes `/schedule/daily`, `/bus-lines/:id/stops` | unaffected (legacy aliases from section 2) |
|
||||
| `monitoring_ui/docs/transit_api/README.md`, `SCHEDULE_API.md`, `admin_ui/README.md` | `curl` examples use root paths | update examples to `/bus/...` |
|
||||
|
||||
---
|
||||
|
||||
## 5. Verification (must all pass before finishing)
|
||||
|
||||
1. `node --check transit_api/index.js` — syntax only, first gate.
|
||||
2. Start locally with mock mode:
|
||||
```bash
|
||||
ADMIN_USER_CODES=42 DEV_MOCK=1 JWT_SECRET=devsecret npm --prefix ./transit_api start
|
||||
```
|
||||
3. `curl -i http://localhost:5001/bus/schedules` → **200** (mock, unpaged) and
|
||||
response has no `Deprecation` header.
|
||||
4. `curl -i http://localhost:5001/schedules` → **200** with a
|
||||
`Deprecation: true; date="2030-01-01"` header (legacy alias intact).
|
||||
5. `curl -i http://localhost:5001/bus/health` → **404**; `curl http://localhost:5001/health`
|
||||
→ **200** `{status:"ok"}`.
|
||||
6. `curl -i http://localhost:5001/bus/admin/stops` → **401** `missing_token`
|
||||
(proves the prefixed route exists and auth still applies).
|
||||
7. `npm --prefix admin_ui run build` → succeeds. Grep the built chunk to confirm
|
||||
requests target `/bus/admin/...` (or visually run the UI and log in).
|
||||
8. Grep `warning`: no call site in `admin_ui/src` should pass a root transit path
|
||||
that was missed; run the transit call pattern again and confirm all are under
|
||||
`transitClient`.
|
||||
|
||||
## 6. Definition of done
|
||||
|
||||
- All 46 non-`/health` routes exist under `/bus/` (canonical) with legacy aliases
|
||||
at root (deprecated).
|
||||
- `admin_ui` issues every transit request under `/bus/`.
|
||||
- `node --check` and `admin_ui` build pass; manual curl checks above pass.
|
||||
- This task is complete only when the agent has run section 5 and reported results.
|
||||
@@ -0,0 +1,320 @@
|
||||
# Phase 3 — `transit_api` Module Split — Agent Instructions
|
||||
|
||||
**Goal:** Break the 5,441-line `transit_api/index.js` into a bootstrap file + domain
|
||||
route modules + shared libs, and eliminate the double-implementation (DB vs
|
||||
`DEV_MOCK`) via a repository layer. **No behavior change. No endpoint changes.**
|
||||
|
||||
**Prerequisite:** the `/bus/` prefix refactor is already applied
|
||||
(`const busRouter = express.Router()` at index.js:99, double-mount `/bus` + `/`
|
||||
with `Deprecation` at the end of the file). Do not re-create it.
|
||||
|
||||
## 1. Rules
|
||||
|
||||
1. Pure code moves only. Every endpoint path, JSON shape, error code, and
|
||||
`DEV_MOCK` behavior must stay byte-for-byte identical to today.
|
||||
2. `node --check <file>` must pass for every new/edited file.
|
||||
3. The `app` remains the owner of the global middleware and the two mounts:
|
||||
- `app.use((req,res,next)=>…)` — the `res.json` error-reporting wrapper (index.js:54–71)
|
||||
- The double-mount `app.use('/bus', busRouter)` + `app.use('/', deprecMw, busRouter)`
|
||||
- `app.use('/health', healthRouter)` (unprefixed, per /bus/ refactor rule 1)
|
||||
4. All SQL keeps unqualified table names (resolution via `search_path="transit.bus"`).
|
||||
5. One module per endpoint cluster (map below). Each route module ends with
|
||||
`module.exports = router;` (an `express.Router()`).
|
||||
6. After finishing **each** stage you must boot mock mode and curl the touched
|
||||
endpoints (section 8).
|
||||
|
||||
## 2. Target layout
|
||||
|
||||
```
|
||||
transit_api/
|
||||
├── index.js → bootstrap (~120 lines)
|
||||
├── db.js (unchanged)
|
||||
├── middleware.js → add adminMiddleware (moved from index.js:282)
|
||||
├── lib/
|
||||
│ ├── env.js → DEV_MOCK, ERROR_LOG_API_URL
|
||||
│ ├── log.js → log()
|
||||
│ ├── errors.js → friendlyErrorMessage()
|
||||
│ ├── report-error.js → reportError()
|
||||
│ ├── validate.js → isValidDateKey()
|
||||
│ ├── schedule.js → the ~20 pure schedule/conflict helpers (section 4)
|
||||
│ └── schema.js → ensureScheduleStopProgressTable()
|
||||
├── mock/
|
||||
│ └── store.js → singleton _mock object + seed block (index.js:85–280)
|
||||
├── repo/
|
||||
│ └── index.js → DEV_MOCK ? mockRepo : pgRepo (Stage D)
|
||||
└── routes/
|
||||
├── health.js ├── stops.js ├── lines.js
|
||||
├── fleet.js ├── drivers.js ├── daily.js
|
||||
├── routes.js ├── navigation.js ├── gps.js
|
||||
├── schedules.js ├── weekly.js └── adminSchedules.js
|
||||
```
|
||||
|
||||
## 3. Route → module map (exact)
|
||||
|
||||
| Module | Endpoints |
|
||||
| --- | --- |
|
||||
| `health.js` | GET `/health` |
|
||||
| `daily.js` | GET `/schedule/daily`, GET `/driver/schedules/daily` |
|
||||
| `routes.js` | GET `/routes/:routeId`, PATCH `/routes/:routeId/status` |
|
||||
| `stops.js` | GET/POST `/admin/stops`, PATCH/DELETE `/admin/stops/:stopId` |
|
||||
| `lines.js` | GET `/bus-lines/:busLineId/stops`, GET/POST `/admin/bus-lines`, PATCH/DELETE `/admin/bus-lines/:busLineId`, GET/POST `/admin/bus-lines/:busLineId/stops`, POST `/admin/bus-lines/:busLineId/stops/reorder`, DELETE `/admin/bus-lines/:busLineId/stops/:routeStopId` |
|
||||
| `fleet.js` | GET/POST `/admin/fleet/buses`, PATCH/DELETE `/admin/fleet/buses/:busId` |
|
||||
| `drivers.js` | GET `/admin/drivers`, GET `/admin/shift-drivers` |
|
||||
| `navigation.js` | POST `/navigation/route` |
|
||||
| `gps.js` | POST `/gps/track`, GET `/gps/route/:routeId` |
|
||||
| `schedules.js` | GET `/schedules`, `/schedules/overview`, `/schedules/recurring`, `/schedules/:scheduleId`, `/schedules/:scheduleId/stops`, PATCH `/driver/schedules/:scheduleId/status`, POST `/driver/schedules/:scheduleId/stops/:stopOrder/passed`, GET `/public/schedules/:scheduleId/progress` |
|
||||
| `weekly.js` | PATCH `/driver/weekly-schedules/:scheduleId/status`, GET/POST/PUT/DELETE `/admin/weekly-schedules*` incl. `/bulk` and `/:scheduleId/shift` |
|
||||
| `adminSchedules.js` | GET/POST/PUT/DELETE `/admin/schedules*` incl. `/bulk`, POST `/admin/check-driver-conflicts`, POST `/admin/assign-schedules` |
|
||||
|
||||
## 4. Stage A — extract `lib/` helpers
|
||||
|
||||
Create the files listed in section 2 by moving code verbatim from index.js:
|
||||
|
||||
- `env.js`: `DEV_MOCK = process.env.DEV_MOCK === "1"`, `ERROR_LOG_API_URL = process.env.ERROR_LOG_API_URL || "https://error.xtransit.testinstance.nl/error_log"`.
|
||||
- `log.js`: the `log(level, message, meta)` JSON logger (index.js:8–11).
|
||||
- `errors.js`: `friendlyErrorMessage(statusCode)` (index.js:29–35).
|
||||
- `report-error.js`: `reportError({partofservice, error})` (index.js:37–52); requires `env`.
|
||||
- `validate.js`: `isValidDateKey(value)` (index.js:21–27).
|
||||
- `schedule.js`: move these pure functions verbatim — `parseWeekdaysValue`,
|
||||
`toNumberOrNull`, `parseCommaSeparatedValues`, `toDateKey`,
|
||||
`expandRecurringScheduleRows`, `mapAdminScheduleRow`, `scheduleMatchesFilters`,
|
||||
`parseScheduleFilters`, `formatWeekdayLabel`, `buildDepartureTimes`,
|
||||
`calculateDepartureTimes`, `isRecurringSchedule`, `scheduleOccursOnDate`,
|
||||
`materializeScheduleOverviewRow`, `mapRecurringOccurrenceRows`,
|
||||
`materializeScheduleRowsInRange`, `doSchedulesDateOverlap`,
|
||||
`checkDriverConflict`, `checkBusConflict`. `schedule.js` may `require('./schedule')`
|
||||
internally; no `db`, `env`, or HTTP dependencies.
|
||||
- `schema.js`: `ensureScheduleStopProgressTable()` (index.js:874–966); skips when
|
||||
`DEV_MOCK` (reads `env`), uses `db`.
|
||||
|
||||
Verify: `node --check` all files; `node -e "require('./lib/schedule')"` to confirm
|
||||
inline requires resolve.
|
||||
|
||||
## 5. Stage B — isolate mock state
|
||||
|
||||
Create `mock/store.js` from the `_mock` object and seed block (index.js:85–280,
|
||||
everything between `const DEV_MOCK` and the `//=== HELPER FUNCTIONS` header):
|
||||
|
||||
- Move `_mock = { stops, routeStops, busLines, fleetBuses, schedules,
|
||||
scheduleStopProgress, drivers, nextStopId, … }` and the mock driver seed +
|
||||
schedule seed verbatim into the module, freezing only the *shape* —
|
||||
keep it a mutable singleton.
|
||||
- Export `module.exports = { store }` (do NOT mutate any field names casing),
|
||||
plus anything the seed block references.
|
||||
- index.js should no longer define `_mock`; its handlers later read it via the
|
||||
store. Until Stage C moves the handlers, index.js `require`s the store and
|
||||
aliases `_mock` to `store` so the existing handler code compiles unchanged:
|
||||
`const { store: _mock } = require('./mock/store');`
|
||||
|
||||
Verify: boot mock mode; a stop/line/schedule read+write round-trips still work.
|
||||
|
||||
## 6. Stage C — split route modules
|
||||
|
||||
For each module in section 3, in dependency-light→heavy order
|
||||
(`health` → `stops` → `lines` → `fleet` → `drivers` → `navigation` → `gps` →
|
||||
`routes` → `daily` → `weekly` → `adminSchedules` → `schedules`):
|
||||
|
||||
1. Create `routes/<name>.js`:
|
||||
```js
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
const { authMiddleware, adminMiddleware } = require("../middleware");
|
||||
// + its lib/mock/repo requires
|
||||
// <moved handlers, unchanged — register on `router`>
|
||||
module.exports = router;
|
||||
```
|
||||
2. Move the handler block verbatim from index.js (the `app.` method calls become
|
||||
`router.` calls). **Do not rewrite handler logic yet** — `DEV_MOCK` branches
|
||||
stay inside the handlers for now; they consume `_mock` via the `mock/store`
|
||||
singleton and DB via `db`.
|
||||
3. Move `adminMiddleware` into `middleware.js` (verbatim from index.js:282–288),
|
||||
add it to `module.exports`. The `DEV_MOCK`/`db`/helper requires each module
|
||||
needs are added to its header.
|
||||
4. Remove the moved block from index.js.
|
||||
|
||||
Keep index.js: global middlewares, `health` mounting, `busRouter`
|
||||
declaration, the final double-mount, the startup `ensureScheduleStopProgressTable`
|
||||
call, `app.listen`, and `module.exports = app`. At the end of Stage C index.js
|
||||
must read:
|
||||
|
||||
```js
|
||||
const busRouter = express.Router();
|
||||
busRouter.use(require("./routes/stops"));
|
||||
busRouter.use(require("./routes/lines"));
|
||||
/* ... every module except health ... */
|
||||
|
||||
app.use("/health", require("./routes/health"));
|
||||
app.use("/bus", busRouter);
|
||||
app.use("/", (req, res, next) => {
|
||||
res.set("Deprecation", 'true; date="2030-01-01"');
|
||||
next();
|
||||
}, busRouter);
|
||||
```
|
||||
|
||||
Verify: after EACH module — `node --check`, mock boot, curl at least a GET and a
|
||||
mutating endpoint of that module under `/bus/...`, plus one legacy root alias.
|
||||
|
||||
## 7. Stage D — repository layer (kills the 40 `DEV_MOCK` branches)
|
||||
|
||||
Create `repo/index.js`:
|
||||
|
||||
```js
|
||||
const { DEV_MOCK } = require("../lib/env");
|
||||
const pg = { ...require("./stops"), ...require("./lines"), /* … */ };
|
||||
const mock = { ...require("./stops.mock"), /* only modules with mock branches */ };
|
||||
module.exports = DEV_MOCK ? { ...pg, ...mock } : pg;
|
||||
```
|
||||
|
||||
**Implemented design (deviation from the original plan, documented):** each
|
||||
domain lives in `repo/<domain>.js` (PostgreSQL branch) and, where the original
|
||||
handler had an `if (DEV_MOCK)` branch, a `repo/<domain>.mock.js` (mock branch).
|
||||
`repo/index.js` splices the mock methods over the pg ones when `DEV_MOCK=1`.
|
||||
Endpoints with no mock branch are pg-only and are therefore used in *both* modes
|
||||
(routes, daily, weekly, gps, navigation, health, `listShiftDrivers`).
|
||||
|
||||
Instead of returning native rows, every repo method **takes the Express `req`**
|
||||
and **returns `{ statusCode, body }`** — the exact payload the original handler
|
||||
sent via `res.status(n).json(body)`. Route modules are one-line wrappers:
|
||||
|
||||
```js
|
||||
router.get("/schedules", async (req, res) => {
|
||||
const out = await repo.listSchedules(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
});
|
||||
```
|
||||
|
||||
This is required for byte-parity: the mock and DB branches of a single original
|
||||
handler build **different JSON shapes** (the "unify via
|
||||
`row.busLineId ?? row.bus_line_id`" idea in the first plan would have changed
|
||||
responses). Repo branches are verbatim copies of the moved handler bodies.
|
||||
|
||||
Implemented methods (per repo files):
|
||||
|
||||
- `repo/stops.js` + `.mock.js`: `listStops`, `createStop`, `updateStop`, `deleteStop`
|
||||
- `repo/lines.js` + `.mock.js`: `listLineStops`, `listLines`, `createLine`,
|
||||
`updateLine`, `listLineStopsDetailed`, `addLineStop`, `reorderLineStops`,
|
||||
`deleteLine`, `deleteLineStop`
|
||||
- `repo/fleet.js` + `.mock.js`: `listFleetBuses`, `createFleetBus`,
|
||||
`updateFleetBus`, `deleteFleetBus`
|
||||
- `repo/drivers.js` + `.mock.js`: `listAdminDrivers`, `listShiftDrivers`
|
||||
(shift-drivers is pg-only, shared)
|
||||
- `repo/route.js` (pg-only): `getRoute`, `updateRouteStatus`
|
||||
- `repo/daily.js` (pg-only, uses `DEV_MOCK` guard for its fallbacks):
|
||||
`listDailySchedules`, `listDriverDailySchedules`
|
||||
- `repo/weekly.js` (pg-only): `updateWeeklyScheduleStatus`, `listWeeklySchedules`,
|
||||
`createWeeklySchedule`, `bulkCreateWeeklySchedules`, `updateWeeklySchedule`,
|
||||
`updateWeeklyScheduleShift`, `deleteWeeklySchedule`
|
||||
- `repo/navigation.js` (pg-only): `saveNavigationRoute`
|
||||
- `repo/gps.js` (pg-only): `saveGpsTrack`, `listRouteGpsPoints`
|
||||
- `repo/adminSchedules.js` + `.mock.js`: `listAdminSchedules`,
|
||||
`createAdminSchedule`, `updateAdminSchedule`, `bulkCreateAdminSchedules`,
|
||||
`deleteAdminSchedule`, `checkDriverConflicts` (shared, has its own tiny
|
||||
`DEV_MOCK ? mock.schedules : db` ternary), `assignSchedules`
|
||||
- `repo/schedules.js` + `.mock.js`: `updateDriverScheduleStatus`, `listSchedules`,
|
||||
`getSchedulesOverview`, `listRecurringSchedules`, `getScheduleById`,
|
||||
`getScheduleStops`, `markStopPassed`, `getPublicScheduleProgress`
|
||||
|
||||
`grep -n DEV_MOCK transit_api/index.js transit_api/routes/*.js` is clean; `DEV_MOCK`
|
||||
appears only in `lib/env.js`, `lib/schema.js`, `repo/index.js`, `repo/daily.js`
|
||||
(fallback guard), `repo/adminSchedules.js` (shared conflict ternary), and
|
||||
`mock/store.js` (seed guard).
|
||||
|
||||
Verify after each module (section 8).
|
||||
|
||||
## 8. Verification (gate after every stage/module)
|
||||
|
||||
```bash
|
||||
cd transit_api
|
||||
node --check index.js && node --check lib/*.js routes/*.js repo/index.js mock/store.js
|
||||
ADMIN_USER_CODES=42 DEV_MOCK=1 JWT_SECRET=devsecret npm start # mock boot
|
||||
```
|
||||
|
||||
Curl checks (against `http://localhost:5001`):
|
||||
- `GET /bus/health` → 404; `GET /health` → 200 `{status:"ok"}`.
|
||||
- `GET /bus/admin/stops` → 401 `missing_token`.
|
||||
- For each touched module: `GET /bus/<endpoint>` → 200 (mock shape), one mutating
|
||||
call → 201/200, and one legacy alias `GET /<endpoint>` → 200 + `Deprecation` header.
|
||||
|
||||
## 9. Stage E — final pass / definition of done
|
||||
|
||||
1. `grep -n "DEV_MOCK" transit_api/index.js routes/*.js` → only `lib/env.js`,
|
||||
`repo/index.js`, and `lib/schema.js` may reference it.
|
||||
2. index.js is ≤ ~150 lines.
|
||||
3. `npm start` boots in mock mode, and (where a DB is reachable) prod mode with
|
||||
the `"transit.bus"` search_path; `GET /health` shows `database:"ok"`.
|
||||
4. Full curl sweep of all 47 endpoint patterns under `/bus/` at least once.
|
||||
5. Report per-module verification results at the end.
|
||||
|
||||
## 10. Implementation status (Stages B–E)
|
||||
|
||||
Completed and verified. Counts at time of writing:
|
||||
|
||||
```
|
||||
transit_api/index.js 86 lines (bootstrap; target ≤ ~150)
|
||||
transit_api/routes/*.js 11 modules (12 incl. health) — thin wrappers
|
||||
transit_api/repo/*.js 17 files (8 pg + 2 with .mock twins etc.)
|
||||
transit_api/mock/store.js 183 lines
|
||||
transit_api/lib/*.js Stage A modules (verified byte-identical)
|
||||
```
|
||||
|
||||
**Verification method.** Two layers:
|
||||
1. A differential harness (`/tmp/opencode/harness2.js`) that stubs `db.js` in
|
||||
`require.cache` (`DB_QUERY_BLOCKED` / `DB_POOL_BLOCKED`), boots the original
|
||||
monolith (`/tmp/opencode/index.c-original.js`, = post-A/pre-B/C snapshot) and
|
||||
the refactored `index.js` side by side, fires identical request sequences,
|
||||
ISO-date normalizes, and byte-compares JSON. This covers both modes and the
|
||||
DB error path.
|
||||
2. Live boots against the real PostgreSQL (once `PG_HOST` in `.env` was
|
||||
corrected from `192.168.2.66` to reachable `PG_HOST`), comparing the original
|
||||
and refactored apps response-for-response on real data (above).
|
||||
|
||||
**Results:**
|
||||
- 57/57 compared responses **byte-identical** (deviation-free) across the full
|
||||
48-endpoint inventory + `/health` + legacy aliases + no-auth case.
|
||||
- **Live DB verification (added after PG access restored):** with PostgreSQL
|
||||
reachable at `PG_HOST` (schema `transit.bus`, verified via
|
||||
`SELECT current_schema() = 'transit.bus'`), the *original monolith* and the
|
||||
refactored app were booted side by side in prod mode and fired with identical
|
||||
read-only requests. **24/24 responses byte-identical** (status, body, and the
|
||||
legacy `Deprecation` header) against real data
|
||||
(`/tmp/opencode/live-parity.js`, baseline was copied back in from
|
||||
`/tmp/opencode/index.c-original.js` and removed afterwards).
|
||||
This closes the one gap the stub harness had: PG success paths on real rows.
|
||||
Note: `GET /bus/schedules` 500s on the live DB (`column s.bus_id does not
|
||||
exist`) — this exists in the original too (identical 500, byte-identical), so
|
||||
it is a pre-existing DB/data-model mismatch with this environment, **not** a
|
||||
refactor regression, and is out of scope here.
|
||||
- **Live mock boot:** `DEV_MOCK=1 npm start` boots (PG now reachable), `/health`
|
||||
reports `database:"ok"`, mock stops/schedules served, legacy aliases set
|
||||
`Deprecation: true; date="2030-01-01"`.
|
||||
- 3 **documented, intentional improvements** (mock-only; the original required a
|
||||
live DB even in `DEV_MOCK=1` because it called `db.pool.connect()` *before* the
|
||||
mock branch):
|
||||
- `POST /admin/bus-lines/:busLineId/stops/reorder`
|
||||
- `DELETE /admin/bus-lines/:busLineId`
|
||||
- `DELETE /admin/bus-lines/:busLineId/stops/:routeStopId`
|
||||
Old (no DB): 500 empty. New (mock): proper 200/400 responses from
|
||||
`repo/lines.mock.js`. In PG mode both behave identically.
|
||||
|
||||
**Quirks preserved verbatim (do not "fix"):**
|
||||
- `POST /driver/schedules/:scheduleId/stops/:stopOrder/passed` mock branch reads
|
||||
an undeclared `delayMinutes` → `ReferenceError` → 500 in **both** modes
|
||||
(`repo/schedules.js` + `repo/schedules.mock.js`).
|
||||
- `bulkCreateWeeklySchedules` and admin bulk normalization `throw` outside `try`/
|
||||
before it; pg `db.pool.connect()` calls stay **before** `try` (hang when PG is
|
||||
unreachable) — control flow preserved exactly.
|
||||
- `repo/adminSchedules.js` `listAdminSchedules`/`createAdminSchedule` use the
|
||||
literal `table_schema='transit'` while `repo/schedules.js` overview/recurring
|
||||
use `'transit.bus'` — matching the original's inconsistent literals.
|
||||
- The `GET /schedules/overview` handler's `else if (DEV_MOCK && …)` enrichment is
|
||||
**dead code** in the original (the mock branch returns earlier); it is NOT
|
||||
copied into `repo/schedules.mock.js`.
|
||||
|
||||
**End-of-Stage checks:**
|
||||
- `grep -rn DEV_MOCK index.js routes/` → clean.
|
||||
- `node --check` → passes for `index.js`, all `routes/*.js`, `repo/*.js`,
|
||||
`mock/store.js`, `middleware.js`, `db.js`.
|
||||
- `git status`/`git diff` → the only file changes in `transit_api/` are the Stage
|
||||
splits (no stubs or temp files left behind; the parity baseline
|
||||
`index.c-original.js` is only ever copied in from `/tmp/opencode` during runs
|
||||
and removed afterwards).
|
||||
@@ -72,52 +72,54 @@ All endpoints require an `Authorization: Bearer {token}` header.
|
||||
|
||||
### Schedule
|
||||
|
||||
Schedules are weekly templates. Each row in `transit.schedules` represents one
|
||||
Schedules are weekly templates. Each row in `"transit.bus".schedules` represents one
|
||||
departure time for one bus line and one `day_of_week`:
|
||||
|
||||
- `1` to `5`: weekdays
|
||||
- `6`: Saturday
|
||||
- `0`: Sunday
|
||||
|
||||
Driver assignments are stored separately in `transit.shifts` with
|
||||
Driver assignments are stored separately in `"transit.bus".shifts` with
|
||||
`(schedule_id, driver_id)`, where `driver_id` references `workers.users(id)`.
|
||||
The current `shifts` shape represents the active assignment. To retain different
|
||||
assignments for different calendar weeks, add a `week_start` (or assignment date
|
||||
range) column and include it in the unique key.
|
||||
|
||||
**GET** `/driver/schedules/daily?date=2026-09-18`
|
||||
**GET** `/bus/driver/schedules/daily?date=2026-09-18`
|
||||
|
||||
- Returns only weekly schedule templates assigned to the authenticated driver through `transit.shifts`.
|
||||
- Returns only weekly schedule templates assigned to the authenticated driver through `"transit.bus".shifts`.
|
||||
|
||||
**PATCH** `/driver/weekly-schedules/:scheduleId/status`
|
||||
**PATCH** `/bus/driver/weekly-schedules/:scheduleId/status`
|
||||
|
||||
- Updates the status of an assigned weekly schedule.
|
||||
|
||||
**GET** `/admin/weekly-schedules`
|
||||
**GET** `/bus/admin/weekly-schedules`
|
||||
|
||||
- Lists weekly templates and their current driver assignments.
|
||||
|
||||
**GET** `/admin/shift-drivers`
|
||||
**GET** `/bus/admin/shift-drivers`
|
||||
|
||||
- Lists drivers from `workers.users` for shift assignment.
|
||||
|
||||
**POST** `/admin/weekly-schedules`
|
||||
**POST** `/bus/admin/weekly-schedules`
|
||||
|
||||
- Creates one template. Body: `{ "busLineId": 1, "dayOfWeek": 1, "startTime": "08:00", "endTime": "09:00", "driverId": 12 }`.
|
||||
|
||||
**POST** `/admin/weekly-schedules/bulk`
|
||||
**POST** `/bus/admin/weekly-schedules/bulk`
|
||||
|
||||
- Creates multiple departures in one transaction. The API derives each end time
|
||||
from the selected bus line duration. Body: `{ "busLineId": 1, "dayOfWeeks": [1,2,3,4,5], "startTimes": ["06:00", "06:30", "07:00"], "driverId": 12 }`.
|
||||
|
||||
**PUT** `/admin/weekly-schedules/:scheduleId/shift`
|
||||
**PUT** `/bus/admin/weekly-schedules/:scheduleId/shift`
|
||||
|
||||
- Replaces the driver assignment. Pass `{ "driverId": 12 }` or `{ "driverId": null }`.
|
||||
|
||||
The legacy `/admin/schedules` and `/schedule/daily` endpoints remain for older
|
||||
clients, but new clients must use the weekly schedule endpoints above.
|
||||
The legacy `/admin/schedules` and `/schedule/daily` aliases remain for older
|
||||
clients, but new clients must use the `/bus/`-prefixed canonical endpoints
|
||||
(`/bus/admin/schedules`, `/bus/schedule/daily`) and the weekly schedule
|
||||
endpoints above.
|
||||
|
||||
**GET** `/schedule/daily?date=2024-05-05`
|
||||
**GET** `/bus/schedule/daily?date=2024-05-05`
|
||||
|
||||
- Retrieve daily schedule for driver
|
||||
- Response:
|
||||
@@ -145,14 +147,14 @@ clients, but new clients must use the weekly schedule endpoints above.
|
||||
}
|
||||
```
|
||||
|
||||
**GET** `/routes/:routeId`
|
||||
**GET** `/bus/routes/:routeId`
|
||||
|
||||
- Details of a specific route
|
||||
- Response: `{ "route": {...} }`
|
||||
|
||||
### Public Schedule Overview
|
||||
|
||||
**GET** `/schedules/overview?date=2026-05-06&lineId=1`
|
||||
**GET** `/bus/schedules/overview?date=2026-05-06&lineId=1`
|
||||
|
||||
- Fetches a date-based overview of scheduled services
|
||||
- Weekly templates are selected by `day_of_week` and driver assignments by `shifts`
|
||||
@@ -160,17 +162,18 @@ clients, but new clients must use the weekly schedule endpoints above.
|
||||
- Filters: `driverId`/`chauffeurId`, `stopId`/`busStopId`, `lineId`, `status`
|
||||
- Response: `{ "date": "2026-05-06", "summary": {...}, "schedules": [...] }`
|
||||
|
||||
The legacy endpoint **GET** `/schedules` remains available for existing clients.
|
||||
The legacy endpoint **GET** `/schedules` remains available as a deprecated alias
|
||||
for existing clients; the canonical path is **GET** `/bus/schedules`.
|
||||
|
||||
Example:
|
||||
|
||||
```bash
|
||||
curl "https://transit.xtransit.testinstance.nl/schedules/overview?date=2026-05-06&chauffeurId=12&busStopId=3"
|
||||
curl "https://transit.xtransit.testinstance.nl/bus/schedules/overview?date=2026-05-06&chauffeurId=12&busStopId=3"
|
||||
```
|
||||
|
||||
### Navigation
|
||||
|
||||
**POST** `/navigation/route`
|
||||
**POST** `/bus/navigation/route`
|
||||
|
||||
- Save calculated route from OSRM
|
||||
- Body:
|
||||
@@ -191,7 +194,7 @@ curl "https://transit.xtransit.testinstance.nl/schedules/overview?date=2026-05-0
|
||||
}
|
||||
```
|
||||
|
||||
**GET** `/bus-lines/:busLineId/stops`
|
||||
**GET** `/bus/bus-lines/:busLineId/stops`
|
||||
|
||||
- All stops of a bus line (for navigation waypoints)
|
||||
- Response:
|
||||
@@ -212,7 +215,7 @@ curl "https://transit.xtransit.testinstance.nl/schedules/overview?date=2026-05-0
|
||||
|
||||
### GPS Tracking
|
||||
|
||||
**POST** `/gps/track`
|
||||
**POST** `/bus/gps/track`
|
||||
|
||||
- Save real-time position
|
||||
- Body:
|
||||
@@ -228,7 +231,7 @@ curl "https://transit.xtransit.testinstance.nl/schedules/overview?date=2026-05-0
|
||||
}
|
||||
```
|
||||
|
||||
**GET** `/gps/route/:routeId`
|
||||
**GET** `/bus/gps/route/:routeId`
|
||||
|
||||
- All recorded position points of a route
|
||||
- Response:
|
||||
@@ -250,7 +253,7 @@ curl "https://transit.xtransit.testinstance.nl/schedules/overview?date=2026-05-0
|
||||
|
||||
### Route Status
|
||||
|
||||
**PATCH** `/routes/:routeId/status`
|
||||
**PATCH** `/bus/routes/:routeId/status`
|
||||
|
||||
- Update route status
|
||||
- Body: `{ "status": "in_progress" }`
|
||||
@@ -260,7 +263,7 @@ curl "https://transit.xtransit.testinstance.nl/schedules/overview?date=2026-05-0
|
||||
|
||||
All admin endpoints require, in addition to a valid JWT token, that your `userCode` is present in `ADMIN_USER_CODES`.
|
||||
|
||||
**POST** `/admin/stops`
|
||||
**POST** `/bus/admin/stops`
|
||||
|
||||
- Create a reusable stop
|
||||
- Body:
|
||||
@@ -273,7 +276,7 @@ All admin endpoints require, in addition to a valid JWT token, that your `userCo
|
||||
}
|
||||
```
|
||||
|
||||
**POST** `/admin/bus-lines/:busLineId/stops`
|
||||
**POST** `/bus/admin/bus-lines/:busLineId/stops`
|
||||
|
||||
- Link an existing stop to a bus line
|
||||
- Body:
|
||||
|
||||
+22
-3
@@ -6,8 +6,8 @@ const pool = new Pool({
|
||||
user: process.env.PG_USER,
|
||||
password: process.env.PG_PASS,
|
||||
database: process.env.PG_DB,
|
||||
// Ensure unqualified table names resolve to transit schema first.
|
||||
options: '-c search_path=transit,public',
|
||||
// Ensure unqualified table names resolve to the "transit.bus" schema first.
|
||||
options: '-c search_path="transit.bus",public',
|
||||
max: 10,
|
||||
idleTimeoutMillis: 30000
|
||||
});
|
||||
@@ -17,7 +17,26 @@ pool.query('SELECT 1')
|
||||
.catch(err => {
|
||||
console.error('PostgreSQL connection FAILED');
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
const url = process.env.ERROR_LOG_API_URL || 'https://error.xtransit.testinstance.nl/error_log';
|
||||
const controller = new AbortController();
|
||||
const timer = setTimeout(() => controller.abort(), 5000);
|
||||
fetch(url, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-internal-api-key': process.env.INTERNAL_API_KEY || '',
|
||||
},
|
||||
body: JSON.stringify({
|
||||
service: 'transit_api',
|
||||
partofservice: 'startup: db connection',
|
||||
error: err.stack || err.message || String(err),
|
||||
}),
|
||||
signal: controller.signal,
|
||||
}).catch(() => {})
|
||||
.finally(() => {
|
||||
clearTimeout(timer);
|
||||
process.exit(1);
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
|
||||
+40
-5315
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,8 @@
|
||||
// Environment/config shared by transit_api modules.
|
||||
const DEV_MOCK = process.env.DEV_MOCK === "1";
|
||||
const ERROR_LOG_API_URL =
|
||||
process.env.ERROR_LOG_API_URL ||
|
||||
"https://error.xtransit.testinstance.nl/error_log";
|
||||
|
||||
module.exports = { DEV_MOCK, ERROR_LOG_API_URL };
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
|
||||
function friendlyErrorMessage(statusCode) {
|
||||
if (statusCode === 400) return "Controleer je invoer en probeer opnieuw.";
|
||||
if (statusCode === 401) return "Je sessie is verlopen. Log opnieuw in.";
|
||||
if (statusCode === 403) return "Je hebt hier geen toegang toe.";
|
||||
if (statusCode === 404) return "Niet gevonden.";
|
||||
return "Er ging iets mis. Probeer het later opnieuw.";
|
||||
}
|
||||
|
||||
module.exports = { friendlyErrorMessage };
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
|
||||
function log(level, message, meta = {}) {
|
||||
const timestamp = new Date().toISOString();
|
||||
console.log(JSON.stringify({ timestamp, level, service: "transit_api", message, ...meta }));
|
||||
}
|
||||
|
||||
module.exports = { log };
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
const { ERROR_LOG_API_URL } = require("./env");
|
||||
|
||||
|
||||
async function reportError({ partofservice, error }) {
|
||||
try {
|
||||
await fetch(ERROR_LOG_API_URL, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"x-internal-api-key": process.env.INTERNAL_API_KEY || "",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
service: "transit_api",
|
||||
partofservice,
|
||||
error,
|
||||
}),
|
||||
});
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
|
||||
function reportUnhandled(context, err) {
|
||||
const message = err?.message || String(err);
|
||||
const stack = err?.stack || message;
|
||||
log("error", context, { error: message, stack });
|
||||
void reportError({ partofservice: context, error: stack });
|
||||
}
|
||||
|
||||
module.exports = { reportError, reportUnhandled };
|
||||
@@ -0,0 +1,606 @@
|
||||
// Pure schedule/conflict helpers (moved from index.js — do not add db or http deps).
|
||||
|
||||
|
||||
// ============================================
|
||||
// HELPER FUNCTIONS
|
||||
// ============================================
|
||||
|
||||
/**
|
||||
* Calculate departure times for each stop in a route
|
||||
* Returns array of { order, stopName, departureTime }
|
||||
*/
|
||||
function calculateDepartureTimes(startTime, busLineId, mock) {
|
||||
const startDate = new Date(startTime);
|
||||
|
||||
// Get route stops for this bus line
|
||||
const routeStops = mock.routeStops
|
||||
.filter((rs) => rs.busLineId === busLineId)
|
||||
.sort((a, b) => a.stopOrder - b.stopOrder);
|
||||
|
||||
return routeStops.map((rs) => {
|
||||
const departureDate = new Date(startDate);
|
||||
// Add estimated arrival minutes to get departure time
|
||||
departureDate.setMinutes(
|
||||
departureDate.getMinutes() + (rs.estimatedArrivalMinutes || 0),
|
||||
);
|
||||
|
||||
const stop = mock.stops.find((s) => s.id === rs.stopId);
|
||||
return {
|
||||
order: rs.stopOrder,
|
||||
stopId: rs.stopId,
|
||||
stopName: stop?.name || "Unknown Stop",
|
||||
estimatedArrivalMinutes: rs.estimatedArrivalMinutes || 0,
|
||||
departureTime: departureDate.toISOString(),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function parseWeekdaysValue(value) {
|
||||
if (!value) return [];
|
||||
if (Array.isArray(value))
|
||||
return value.map((v) => Number(v)).filter((v) => Number.isFinite(v));
|
||||
|
||||
if (typeof value === "string") {
|
||||
const trimmed = value.trim();
|
||||
if (!trimmed) return [];
|
||||
|
||||
try {
|
||||
const parsed = JSON.parse(trimmed);
|
||||
if (Array.isArray(parsed)) {
|
||||
return parsed.map((v) => Number(v)).filter((v) => Number.isFinite(v));
|
||||
}
|
||||
} catch (_) {
|
||||
// Fall through to CSV/PG-array parsing.
|
||||
}
|
||||
|
||||
// Handles values like "1,2,3" or "{1,2,3}".
|
||||
const normalized = trimmed.replace("{", "").replace("}", "");
|
||||
return normalized
|
||||
.split(",")
|
||||
.map((v) => Number(v.trim()))
|
||||
.filter((v) => Number.isFinite(v));
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
|
||||
function toNumberOrNull(value) {
|
||||
if (value === undefined || value === null || value === "") return null;
|
||||
const numberValue = Number(value);
|
||||
return Number.isFinite(numberValue) ? numberValue : null;
|
||||
}
|
||||
|
||||
|
||||
function parseCommaSeparatedValues(value) {
|
||||
if (!value && value !== 0) return [];
|
||||
if (Array.isArray(value)) {
|
||||
return value.map((item) => String(item).trim()).filter(Boolean);
|
||||
}
|
||||
|
||||
return String(value)
|
||||
.split(",")
|
||||
.map((item) => item.trim())
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
|
||||
function toDateKey(value) {
|
||||
if (!value) return null;
|
||||
const date = new Date(value);
|
||||
if (Number.isNaN(date.getTime())) return null;
|
||||
return date.toISOString().slice(0, 10);
|
||||
}
|
||||
|
||||
|
||||
function expandRecurringScheduleRows(rows) {
|
||||
const expandedRows = [];
|
||||
|
||||
for (const row of rows) {
|
||||
const weekdays = parseWeekdaysValue(row.weekdays);
|
||||
if (!weekdays || weekdays.length === 0) {
|
||||
expandedRows.push(row);
|
||||
continue;
|
||||
}
|
||||
|
||||
const templateDate = new Date(row.start_time);
|
||||
if (Number.isNaN(templateDate.getTime())) {
|
||||
expandedRows.push(row);
|
||||
continue;
|
||||
}
|
||||
|
||||
const templateDayOfWeek = templateDate.getDay();
|
||||
const sortedWeekdays = [...new Set(weekdays)].sort((a, b) => a - b);
|
||||
const durationMinutes = Number(
|
||||
row.estimated_duration_minutes ?? row.estimatedDurationMinutes ?? 60,
|
||||
);
|
||||
|
||||
for (let week = 0; week < 52; week++) {
|
||||
for (const targetWeekday of sortedWeekdays) {
|
||||
const daysOffset = (targetWeekday - templateDayOfWeek + 7) % 7;
|
||||
const totalDays = week * 7 + daysOffset;
|
||||
|
||||
const scheduleDate = new Date(templateDate);
|
||||
scheduleDate.setDate(scheduleDate.getDate() + totalDays);
|
||||
|
||||
const endDate = new Date(scheduleDate);
|
||||
endDate.setMinutes(endDate.getMinutes() + durationMinutes);
|
||||
|
||||
expandedRows.push({
|
||||
...row,
|
||||
start_time: scheduleDate.toISOString(),
|
||||
end_time: endDate.toISOString(),
|
||||
is_recurring_instance: true,
|
||||
template_id: row.id,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return expandedRows;
|
||||
}
|
||||
|
||||
|
||||
function mapAdminScheduleRow(row, routeStopsByLine = new Map()) {
|
||||
const weekdays = parseWeekdaysValue(row.weekdays);
|
||||
const busLineId = Number(row.busLineId ?? row.bus_line_id);
|
||||
const startTime = row.startTime ?? row.start_time;
|
||||
const routeStops = routeStopsByLine.get(busLineId) || [];
|
||||
const departureTimes = buildDepartureTimes(startTime, routeStops);
|
||||
|
||||
return {
|
||||
id: Number(row.id),
|
||||
busLineId,
|
||||
busId: Number(row.busId ?? row.bus_id),
|
||||
driverId:
|
||||
row.driverId !== null && row.driverId !== undefined
|
||||
? Number(row.driverId)
|
||||
: null,
|
||||
driverName: row.driverName ?? row.driver_name ?? null,
|
||||
startTime,
|
||||
endTime: row.endTime ?? row.end_time,
|
||||
weekdays,
|
||||
departureTimes,
|
||||
status: row.status,
|
||||
lineNumber: Number(row.lineNumber ?? row.line_number) || null,
|
||||
busName: row.busName ?? row.bus_name ?? "Unknown Bus",
|
||||
licensePlate: row.licensePlate ?? row.license_plate ?? null,
|
||||
createdAt: row.createdAt ?? row.created_at ?? null,
|
||||
isRecurringInstance: row.is_recurring_instance || false,
|
||||
templateId: row.template_id || row.templateId || null,
|
||||
recurrenceLabel: formatWeekdayLabel(weekdays),
|
||||
isRecurring: weekdays.length > 0,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
function scheduleMatchesFilters(
|
||||
schedule,
|
||||
filters,
|
||||
routeStopsByLine = new Map(),
|
||||
) {
|
||||
if (!schedule || !filters) return true;
|
||||
|
||||
const scheduleBusLineId = Number(schedule.bus_line_id ?? schedule.busLineId);
|
||||
const scheduleBusId = Number(schedule.bus_id ?? schedule.busId);
|
||||
const scheduleDriverId = Number(schedule.driver_id ?? schedule.driverId);
|
||||
const scheduleStatus = String(schedule.status || "");
|
||||
const scheduleDateKey = toDateKey(schedule.start_time ?? schedule.startTime);
|
||||
|
||||
if (filters.busLineId !== null && scheduleBusLineId !== filters.busLineId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (filters.busId !== null && scheduleBusId !== filters.busId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (filters.driverId !== null && scheduleDriverId !== filters.driverId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (
|
||||
filters.statuses.length > 0 &&
|
||||
!filters.statuses.includes(scheduleStatus)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (filters.date && scheduleDateKey !== filters.date) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (
|
||||
filters.fromDate &&
|
||||
scheduleDateKey &&
|
||||
scheduleDateKey < filters.fromDate
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (filters.toDate && scheduleDateKey && scheduleDateKey > filters.toDate) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (filters.stopId !== null) {
|
||||
const routeStops = routeStopsByLine.get(scheduleBusLineId) || [];
|
||||
const matchesStop = routeStops.some(
|
||||
(routeStop) =>
|
||||
Number(routeStop.stop_id ?? routeStop.stopId) === filters.stopId,
|
||||
);
|
||||
if (!matchesStop) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
function parseScheduleFilters(query, options = {}) {
|
||||
const statuses = parseCommaSeparatedValues(
|
||||
query.status || query.statuses || options.statuses || [],
|
||||
);
|
||||
|
||||
return {
|
||||
date: query.date ? String(query.date).trim() : null,
|
||||
fromDate: query.fromDate ? String(query.fromDate).trim() : null,
|
||||
toDate: query.toDate ? String(query.toDate).trim() : null,
|
||||
driverId: toNumberOrNull(
|
||||
query.driverId ??
|
||||
query.driver_id ??
|
||||
query.chauffeurId ??
|
||||
query.chauffeur_id,
|
||||
),
|
||||
busId: toNumberOrNull(query.busId ?? query.bus_id),
|
||||
busLineId: toNumberOrNull(
|
||||
query.busLineId ?? query.lineId ?? query.bus_line_id,
|
||||
),
|
||||
stopId: toNumberOrNull(
|
||||
query.stopId ?? query.stop_id ?? query.busStopId ?? query.bus_stop_id,
|
||||
),
|
||||
statuses,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
function formatWeekdayLabel(weekdays) {
|
||||
const labels = ["Zo", "Ma", "Di", "Wo", "Do", "Vr", "Za"];
|
||||
|
||||
if (!Array.isArray(weekdays) || weekdays.length === 0) {
|
||||
return "Eenmalig";
|
||||
}
|
||||
|
||||
if (weekdays.length === 7) {
|
||||
return "Dagelijks";
|
||||
}
|
||||
|
||||
return [...new Set(weekdays)]
|
||||
.sort((a, b) => a - b)
|
||||
.filter((day) => day >= 0 && day < labels.length)
|
||||
.map((day) => labels[day])
|
||||
.join(", ");
|
||||
}
|
||||
|
||||
|
||||
function buildDepartureTimes(startTime, routeStops = []) {
|
||||
const startDate = new Date(startTime);
|
||||
|
||||
return routeStops.map((routeStop) => {
|
||||
const departureDate = new Date(startDate);
|
||||
const etaMinutes =
|
||||
Number(
|
||||
routeStop.estimated_arrival_minutes ??
|
||||
routeStop.estimatedArrivalMinutes,
|
||||
) || 0;
|
||||
departureDate.setMinutes(departureDate.getMinutes() + etaMinutes);
|
||||
|
||||
return {
|
||||
order: Number(routeStop.stop_order ?? routeStop.stopOrder),
|
||||
stopId: Number(routeStop.stop_id ?? routeStop.stopId),
|
||||
stopName: routeStop.stop_name || routeStop.name || "Unknown Stop",
|
||||
estimatedArrivalMinutes: etaMinutes,
|
||||
departureTime: departureDate.toISOString(),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function isRecurringSchedule(schedule) {
|
||||
return parseWeekdaysValue(schedule.weekdays).length > 0;
|
||||
}
|
||||
|
||||
|
||||
function scheduleOccursOnDate(schedule, dateKey) {
|
||||
if (!dateKey) return true;
|
||||
|
||||
const weekdays = parseWeekdaysValue(schedule.weekdays);
|
||||
const scheduleDateKey = toDateKey(schedule.start_time ?? schedule.startTime);
|
||||
|
||||
if (!scheduleDateKey) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (weekdays.length === 0) {
|
||||
return scheduleDateKey === dateKey;
|
||||
}
|
||||
|
||||
if (scheduleDateKey > dateKey) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const targetDate = new Date(`${dateKey}T00:00:00`);
|
||||
return weekdays.includes(targetDate.getDay());
|
||||
}
|
||||
|
||||
|
||||
function materializeScheduleOverviewRow(
|
||||
row,
|
||||
dateKey,
|
||||
routeStopsByLine = new Map(),
|
||||
) {
|
||||
const weekdays = parseWeekdaysValue(row.weekdays);
|
||||
const recurring = weekdays.length > 0;
|
||||
const templateStart = new Date(row.startTime ?? row.start_time);
|
||||
const templateEnd = new Date(row.endTime ?? row.end_time);
|
||||
let startTime = row.startTime ?? row.start_time;
|
||||
let endTime = row.endTime ?? row.end_time;
|
||||
|
||||
if (recurring && dateKey) {
|
||||
const occurrenceStart = new Date(`${dateKey}T00:00:00`);
|
||||
occurrenceStart.setHours(
|
||||
templateStart.getHours(),
|
||||
templateStart.getMinutes(),
|
||||
templateStart.getSeconds(),
|
||||
templateStart.getMilliseconds(),
|
||||
);
|
||||
|
||||
startTime = occurrenceStart.toISOString();
|
||||
|
||||
if (
|
||||
Number.isFinite(templateEnd.getTime()) &&
|
||||
Number.isFinite(templateStart.getTime())
|
||||
) {
|
||||
const durationMillis = templateEnd.getTime() - templateStart.getTime();
|
||||
const occurrenceEnd = new Date(occurrenceStart);
|
||||
occurrenceEnd.setMilliseconds(
|
||||
occurrenceEnd.getMilliseconds() + durationMillis,
|
||||
);
|
||||
endTime = occurrenceEnd.toISOString();
|
||||
}
|
||||
}
|
||||
|
||||
const busLineId = Number(row.busLineId ?? row.bus_line_id);
|
||||
const routeStops = routeStopsByLine.get(busLineId) || [];
|
||||
const departureTimes = buildDepartureTimes(startTime, routeStops);
|
||||
|
||||
return {
|
||||
id: Number(row.id),
|
||||
busLineId,
|
||||
busId: Number(row.busId ?? row.bus_id),
|
||||
// Hide driver information in public schedule materialization for privacy
|
||||
driverId: null,
|
||||
driverName: null,
|
||||
startTime,
|
||||
endTime,
|
||||
weekdays,
|
||||
status: row.status,
|
||||
lineNumber: Number(row.lineNumber ?? row.line_number) || null,
|
||||
busName: row.busName ?? row.bus_name ?? "Unknown Bus",
|
||||
licensePlate: row.licensePlate ?? row.license_plate ?? null,
|
||||
createdAt: row.createdAt ?? row.created_at ?? null,
|
||||
scheduleType: recurring ? "recurring" : "one_time",
|
||||
recurrenceLabel: formatWeekdayLabel(weekdays),
|
||||
isRecurringInstance: recurring && Boolean(dateKey),
|
||||
templateId: row.templateId ?? row.template_id ?? null,
|
||||
departureTimes,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
function mapRecurringOccurrenceRows(rows, dateKey, routeStopsByLine) {
|
||||
return rows
|
||||
.filter((row) => scheduleOccursOnDate(row, dateKey))
|
||||
.map((row) =>
|
||||
materializeScheduleOverviewRow(row, dateKey, routeStopsByLine),
|
||||
)
|
||||
.sort((a, b) => new Date(a.startTime) - new Date(b.startTime));
|
||||
}
|
||||
|
||||
|
||||
function materializeScheduleRowsInRange(
|
||||
rows,
|
||||
fromDateKey,
|
||||
toDateKey,
|
||||
routeStopsByLine,
|
||||
) {
|
||||
const occurrences = [];
|
||||
if (!fromDateKey || !toDateKey) return occurrences;
|
||||
|
||||
const fromDate = new Date(`${fromDateKey}T00:00:00`);
|
||||
const toDate = new Date(`${toDateKey}T23:59:59`);
|
||||
|
||||
// Safety cap: don't generate more than 366 days
|
||||
const maxDays = 366;
|
||||
const spanDays = Math.ceil((toDate - fromDate) / (24 * 60 * 60 * 1000));
|
||||
if (spanDays > maxDays) return occurrences;
|
||||
|
||||
for (const row of rows) {
|
||||
const weekdays = parseWeekdaysValue(row.weekdays);
|
||||
|
||||
if (!weekdays || weekdays.length === 0) {
|
||||
const rowDateKey = toDateKey(row.start_time ?? row.startTime);
|
||||
if (rowDateKey && rowDateKey >= fromDateKey && rowDateKey <= toDateKey) {
|
||||
occurrences.push(
|
||||
materializeScheduleOverviewRow(row, rowDateKey, routeStopsByLine),
|
||||
);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// recurring template: generate occurrences per weekday within range
|
||||
const templateStart = new Date(row.start_time ?? row.startTime);
|
||||
for (const wd of [...new Set(weekdays)]) {
|
||||
// find first date >= fromDate that matches weekday wd and is >= templateStart
|
||||
let cursor = new Date(fromDate);
|
||||
const fromWeekday = cursor.getDay();
|
||||
const daysOffset = (wd - fromWeekday + 7) % 7;
|
||||
cursor.setDate(cursor.getDate() + daysOffset);
|
||||
|
||||
// ensure cursor >= templateStart (so recurrence doesn't start before template)
|
||||
if (cursor < templateStart) {
|
||||
// advance by weeks until >= templateStart
|
||||
const diffDays = Math.ceil(
|
||||
(templateStart - cursor) / (24 * 60 * 60 * 1000),
|
||||
);
|
||||
const weeksToAdvance = Math.ceil(diffDays / 7);
|
||||
cursor.setDate(cursor.getDate() + weeksToAdvance * 7);
|
||||
}
|
||||
|
||||
while (cursor <= toDate) {
|
||||
const occurrenceKey = cursor.toISOString().slice(0, 10);
|
||||
occurrences.push(
|
||||
materializeScheduleOverviewRow(row, occurrenceKey, routeStopsByLine),
|
||||
);
|
||||
cursor.setDate(cursor.getDate() + 7);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// sort and return
|
||||
return occurrences.sort(
|
||||
(a, b) => new Date(a.startTime) - new Date(b.startTime),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if two schedules have date/weekday overlap
|
||||
*/
|
||||
function doSchedulesDateOverlap(
|
||||
schedule1Start,
|
||||
schedule1End,
|
||||
schedule1Weekdays,
|
||||
schedule2Start,
|
||||
schedule2End,
|
||||
schedule2Weekdays,
|
||||
) {
|
||||
const s1Start = new Date(schedule1Start);
|
||||
const s1End = new Date(schedule1End);
|
||||
const s2Start = new Date(schedule2Start);
|
||||
const s2End = new Date(schedule2End);
|
||||
|
||||
// Check time overlap
|
||||
if (s1End <= s2Start || s2End <= s1Start) {
|
||||
return false; // No time overlap
|
||||
}
|
||||
|
||||
// If both have no weekdays (one-time), check if same day
|
||||
if (
|
||||
(!schedule1Weekdays || schedule1Weekdays.length === 0) &&
|
||||
(!schedule2Weekdays || schedule2Weekdays.length === 0)
|
||||
) {
|
||||
return s1Start.toDateString() === s2Start.toDateString();
|
||||
}
|
||||
|
||||
// If one has weekdays and other doesn't, check if one-time falls on weekday
|
||||
if (
|
||||
schedule1Weekdays &&
|
||||
schedule1Weekdays.length > 0 &&
|
||||
(!schedule2Weekdays || schedule2Weekdays.length === 0)
|
||||
) {
|
||||
const s2Day = s2Start.getDay(); // 0=Sun, 1=Mon, etc.
|
||||
const s2Adjusted = s2Day === 0 ? 0 : s2Day; // Normalize to 0-6
|
||||
return schedule1Weekdays.includes(s2Adjusted);
|
||||
}
|
||||
|
||||
if (
|
||||
(!schedule1Weekdays || schedule1Weekdays.length === 0) &&
|
||||
schedule2Weekdays &&
|
||||
schedule2Weekdays.length > 0
|
||||
) {
|
||||
const s1Day = s1Start.getDay();
|
||||
const s1Adjusted = s1Day === 0 ? 0 : s1Day;
|
||||
return schedule2Weekdays.includes(s1Adjusted);
|
||||
}
|
||||
|
||||
// Both have weekdays: check if any day overlaps
|
||||
if (schedule1Weekdays && schedule2Weekdays) {
|
||||
return schedule1Weekdays.some((d) => schedule2Weekdays.includes(d));
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if a driver has conflicts with existing schedules
|
||||
*/
|
||||
function checkDriverConflict(
|
||||
driverId,
|
||||
startTime,
|
||||
endTime,
|
||||
weekdays,
|
||||
allSchedules,
|
||||
excludeScheduleId = null,
|
||||
) {
|
||||
if (!driverId) return false; // No conflict if no driver specified
|
||||
|
||||
return allSchedules.some((s) => {
|
||||
if (excludeScheduleId && s.id === excludeScheduleId) return false;
|
||||
const sDriverId = s.driverId ?? s.driver_id;
|
||||
if (Number(sDriverId) !== Number(driverId)) return false;
|
||||
|
||||
const sWeekdays = parseWeekdaysValue(s.weekdays);
|
||||
const wkdays = weekdays || [];
|
||||
const sStart = s.startTime ?? s.start_time;
|
||||
const sEnd = s.endTime ?? s.end_time;
|
||||
|
||||
return doSchedulesDateOverlap(
|
||||
startTime,
|
||||
endTime,
|
||||
wkdays,
|
||||
sStart,
|
||||
sEnd,
|
||||
sWeekdays,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check if a bus has conflicts with existing schedules
|
||||
*/
|
||||
function checkBusConflict(
|
||||
busId,
|
||||
startTime,
|
||||
endTime,
|
||||
weekdays,
|
||||
allSchedules,
|
||||
excludeScheduleId = null,
|
||||
) {
|
||||
if (!busId) return false;
|
||||
|
||||
return allSchedules.some((s) => {
|
||||
if (excludeScheduleId && s.id === excludeScheduleId) return false;
|
||||
const sBusId = s.busId ?? s.bus_id;
|
||||
if (Number(sBusId) !== Number(busId)) return false;
|
||||
|
||||
const sWeekdays = parseWeekdaysValue(s.weekdays);
|
||||
const wkdays = weekdays || [];
|
||||
const sStart = s.startTime ?? s.start_time;
|
||||
const sEnd = s.endTime ?? s.end_time;
|
||||
|
||||
return doSchedulesDateOverlap(
|
||||
startTime,
|
||||
endTime,
|
||||
wkdays,
|
||||
sStart,
|
||||
sEnd,
|
||||
sWeekdays,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { calculateDepartureTimes, parseWeekdaysValue, toNumberOrNull, parseCommaSeparatedValues, toDateKey, expandRecurringScheduleRows, mapAdminScheduleRow, scheduleMatchesFilters, parseScheduleFilters, formatWeekdayLabel, buildDepartureTimes, isRecurringSchedule, scheduleOccursOnDate, materializeScheduleOverviewRow, mapRecurringOccurrenceRows, materializeScheduleRowsInRange, doSchedulesDateOverlap, checkDriverConflict, checkBusConflict };
|
||||
@@ -0,0 +1,79 @@
|
||||
const db = require("../db");
|
||||
const { DEV_MOCK } = require("./env");
|
||||
|
||||
|
||||
async function ensureScheduleStopProgressTable() {
|
||||
if (DEV_MOCK) return;
|
||||
|
||||
await db.query(`
|
||||
CREATE TABLE IF NOT EXISTS schedule_stop_progress (
|
||||
id BIGSERIAL PRIMARY KEY,
|
||||
schedule_id INTEGER NOT NULL,
|
||||
bus_line_id INTEGER NOT NULL,
|
||||
stop_id INTEGER NOT NULL,
|
||||
stop_order INTEGER NOT NULL,
|
||||
stop_name TEXT,
|
||||
estimated_arrival_minutes INTEGER NOT NULL DEFAULT 0,
|
||||
scheduled_passed_at TIMESTAMPTZ NOT NULL,
|
||||
scheduled_passed_date DATE NOT NULL,
|
||||
actual_passed_at TIMESTAMPTZ NOT NULL,
|
||||
delay_minutes INTEGER NOT NULL DEFAULT 0,
|
||||
marked_by_user_id INTEGER,
|
||||
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
|
||||
UNIQUE (schedule_id, stop_order, scheduled_passed_date)
|
||||
)
|
||||
`);
|
||||
|
||||
await db.query(`
|
||||
ALTER TABLE schedule_stop_progress
|
||||
ADD COLUMN IF NOT EXISTS scheduled_passed_date DATE
|
||||
`);
|
||||
|
||||
await db.query(`
|
||||
UPDATE schedule_stop_progress
|
||||
SET scheduled_passed_date = scheduled_passed_at::date
|
||||
WHERE scheduled_passed_date IS NULL
|
||||
`);
|
||||
|
||||
await db.query(`
|
||||
ALTER TABLE schedule_stop_progress
|
||||
ALTER COLUMN scheduled_passed_date SET NOT NULL
|
||||
`);
|
||||
|
||||
await db.query(`
|
||||
DO $$
|
||||
DECLARE
|
||||
constraint_name text;
|
||||
BEGIN
|
||||
SELECT tc.constraint_name
|
||||
INTO constraint_name
|
||||
FROM information_schema.table_constraints tc
|
||||
JOIN information_schema.key_column_usage kcu
|
||||
ON tc.constraint_name = kcu.constraint_name
|
||||
AND tc.table_schema = kcu.table_schema
|
||||
AND tc.table_name = kcu.table_name
|
||||
WHERE tc.table_schema = 'transit.bus'
|
||||
AND tc.table_name = 'schedule_stop_progress'
|
||||
AND tc.constraint_type = 'UNIQUE'
|
||||
GROUP BY tc.constraint_name
|
||||
HAVING array_agg(kcu.column_name ORDER BY kcu.ordinal_position)::text[] = ARRAY['schedule_id', 'stop_order']::text[]
|
||||
LIMIT 1;
|
||||
|
||||
IF constraint_name IS NOT NULL THEN
|
||||
EXECUTE format('ALTER TABLE schedule_stop_progress DROP CONSTRAINT %I', constraint_name);
|
||||
END IF;
|
||||
END $$;
|
||||
`);
|
||||
|
||||
await db.query(`
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_schedule_stop_progress_schedule_order_date
|
||||
ON schedule_stop_progress (schedule_id, stop_order, scheduled_passed_date)
|
||||
`);
|
||||
|
||||
await db.query(`
|
||||
CREATE INDEX IF NOT EXISTS idx_schedule_stop_progress_schedule_order
|
||||
ON schedule_stop_progress (schedule_id, stop_order, scheduled_passed_date)
|
||||
`);
|
||||
}
|
||||
|
||||
module.exports = { ensureScheduleStopProgressTable };
|
||||
@@ -0,0 +1,11 @@
|
||||
|
||||
function isValidDateKey(value) {
|
||||
if (!/^\d{4}-\d{2}-\d{2}$/.test(String(value || ""))) return false;
|
||||
const date = new Date(`${value}T00:00:00Z`);
|
||||
return (
|
||||
!Number.isNaN(date.getTime()) && date.toISOString().slice(0, 10) === value
|
||||
);
|
||||
}
|
||||
|
||||
module.exports = { isValidDateKey };
|
||||
|
||||
@@ -3,10 +3,37 @@ require('dotenv').config();
|
||||
|
||||
const JWT_SECRET = process.env.JWT_SECRET;
|
||||
|
||||
const ERROR_LOG_API_URL =
|
||||
process.env.ERROR_LOG_API_URL ||
|
||||
"https://error.xtransit.testinstance.nl/error_log";
|
||||
|
||||
function log(level, message, meta = {}) {
|
||||
const timestamp = new Date().toISOString();
|
||||
console.log(JSON.stringify({ timestamp, level, service: "transit_api", message, ...meta }));
|
||||
}
|
||||
|
||||
async function reportError({ partofservice, error }) {
|
||||
try {
|
||||
await fetch(ERROR_LOG_API_URL, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"x-internal-api-key": process.env.INTERNAL_API_KEY || "",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
service: "transit_api",
|
||||
partofservice,
|
||||
error,
|
||||
}),
|
||||
});
|
||||
} catch (_) {}
|
||||
}
|
||||
|
||||
// Middleware to verify JWT token
|
||||
const authMiddleware = (req, res, next) => {
|
||||
const auth = req.headers.authorization;
|
||||
if (!auth || !auth.startsWith('Bearer ')) {
|
||||
log("warn", "Missing or invalid Authorization header", { path: req.originalUrl });
|
||||
return res.status(401).json({ error: 'missing_token' });
|
||||
}
|
||||
|
||||
@@ -16,8 +43,23 @@ const authMiddleware = (req, res, next) => {
|
||||
req.user = payload;
|
||||
next();
|
||||
} catch (err) {
|
||||
log("error", "JWT verification failed", { error: err.message, path: req.originalUrl });
|
||||
void reportError({
|
||||
partofservice: `authMiddleware ${req.method} ${req.originalUrl}`,
|
||||
error: err.stack || err.message,
|
||||
});
|
||||
return res.status(401).json({ error: 'invalid_token' });
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = { authMiddleware };
|
||||
|
||||
// Middleware to enforce admin role
|
||||
const adminMiddleware = (req, res, next) => {
|
||||
if (!req.user || !req.user.isAdmin) {
|
||||
return res.status(403).json({ error: "forbidden" });
|
||||
}
|
||||
|
||||
next();
|
||||
};
|
||||
|
||||
module.exports = { authMiddleware, adminMiddleware };
|
||||
|
||||
@@ -0,0 +1,183 @@
|
||||
// Development mock storage when DEV_MOCK=1
|
||||
const { DEV_MOCK } = require("../lib/env");
|
||||
|
||||
const store = {
|
||||
stops: [],
|
||||
routeStops: [],
|
||||
busLines: [],
|
||||
fleetBuses: [],
|
||||
schedules: [],
|
||||
scheduleStopProgress: [],
|
||||
drivers: [],
|
||||
nextStopId: 1,
|
||||
nextRouteStopId: 1,
|
||||
nextBusLineId: 1,
|
||||
nextFleetBusId: 1,
|
||||
nextScheduleId: 1,
|
||||
nextScheduleStopProgressId: 1,
|
||||
nextDriverId: 1,
|
||||
};
|
||||
|
||||
// Initialize mock drivers
|
||||
if (DEV_MOCK) {
|
||||
store.drivers = [
|
||||
{
|
||||
id: 1,
|
||||
name: "Jan Peterse",
|
||||
email: "jan@transit.local",
|
||||
phone: "06-12345678",
|
||||
status: "active",
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: "Maria Kuijpers",
|
||||
email: "maria@transit.local",
|
||||
phone: "06-87654321",
|
||||
status: "active",
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
name: "Robert Smits",
|
||||
email: "robert@transit.local",
|
||||
phone: "06-55555555",
|
||||
status: "active",
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
name: "Anja Voorn",
|
||||
email: "anja@transit.local",
|
||||
phone: "06-44444444",
|
||||
status: "inactive",
|
||||
},
|
||||
];
|
||||
store.nextDriverId = 5;
|
||||
|
||||
// Initialize test bus lines
|
||||
store.busLines = [
|
||||
{
|
||||
id: 1,
|
||||
lineNumber: 1,
|
||||
startStop: "Central Station",
|
||||
endStop: "Airport",
|
||||
estimatedDurationMinutes: 45,
|
||||
description: "Main route",
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
lineNumber: 2,
|
||||
startStop: "Central Station",
|
||||
endStop: "Harbor",
|
||||
estimatedDurationMinutes: 30,
|
||||
description: "Harbor route",
|
||||
},
|
||||
];
|
||||
store.nextBusLineId = 3;
|
||||
|
||||
// Initialize test stops
|
||||
store.stops = [
|
||||
{
|
||||
id: 1,
|
||||
name: "Central Station",
|
||||
latitude: 52.0116,
|
||||
longitude: 4.7055,
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: "City Center",
|
||||
latitude: 52.0125,
|
||||
longitude: 4.71,
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
name: "Airport",
|
||||
latitude: 52.03,
|
||||
longitude: 4.75,
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
},
|
||||
];
|
||||
store.nextStopId = 4;
|
||||
|
||||
// Initialize test route stops
|
||||
store.routeStops = [
|
||||
{
|
||||
id: 1,
|
||||
busLineId: 1,
|
||||
stopId: 1,
|
||||
stopOrder: 1,
|
||||
estimatedArrivalMinutes: 0,
|
||||
name: "Central Station",
|
||||
latitude: 52.0116,
|
||||
longitude: 4.7055,
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
busLineId: 1,
|
||||
stopId: 2,
|
||||
stopOrder: 2,
|
||||
estimatedArrivalMinutes: 15,
|
||||
name: "City Center",
|
||||
latitude: 52.0125,
|
||||
longitude: 4.71,
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
busLineId: 1,
|
||||
stopId: 3,
|
||||
stopOrder: 3,
|
||||
estimatedArrivalMinutes: 45,
|
||||
name: "Airport",
|
||||
latitude: 52.03,
|
||||
longitude: 4.75,
|
||||
},
|
||||
];
|
||||
store.nextRouteStopId = 4;
|
||||
|
||||
// Initialize test fleet buses
|
||||
store.fleetBuses = [
|
||||
{
|
||||
id: 1,
|
||||
name: "Bus 101",
|
||||
seatCapacity: 45,
|
||||
licensePlate: "AB-01-CD",
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
name: "Bus 102",
|
||||
seatCapacity: 50,
|
||||
licensePlate: "AB-02-CD",
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
},
|
||||
];
|
||||
store.nextFleetBusId = 3;
|
||||
|
||||
// Initialize test schedule
|
||||
const now = new Date();
|
||||
const tomorrow = new Date(now.getTime() + 24 * 60 * 60 * 1000);
|
||||
tomorrow.setHours(10, 0, 0, 0);
|
||||
const endTime = new Date(tomorrow.getTime() + 45 * 60 * 1000);
|
||||
|
||||
store.schedules = [
|
||||
{
|
||||
id: 1,
|
||||
busLineId: 1,
|
||||
busId: 1,
|
||||
driverId: null,
|
||||
startTime: tomorrow.toISOString(),
|
||||
endTime: endTime.toISOString(),
|
||||
weekdays: [],
|
||||
status: "planned",
|
||||
createdAt: new Date().toISOString(),
|
||||
},
|
||||
];
|
||||
store.nextScheduleId = 2;
|
||||
}
|
||||
|
||||
module.exports = { store };
|
||||
@@ -0,0 +1,663 @@
|
||||
const db = require("../db");
|
||||
const { DEV_MOCK } = require("../lib/env");
|
||||
const { store: _mock } = require("../mock/store");
|
||||
const { calculateDepartureTimes, parseWeekdaysValue, expandRecurringScheduleRows, mapAdminScheduleRow, scheduleMatchesFilters, parseScheduleFilters, checkDriverConflict } = require("../lib/schedule");
|
||||
const { reportError } = require("../lib/report-error");
|
||||
|
||||
// Admin schedules repository methods (PostgreSQL mode). Each returns
|
||||
// { statusCode, body } mirroring the original handler's DB branch.
|
||||
|
||||
async function listAdminSchedules(req) {
|
||||
try {
|
||||
const templatesOnly =
|
||||
String(
|
||||
req.query.templates || req.query.includeTemplates || "",
|
||||
).toLowerCase() === "true" ||
|
||||
String(req.query.templates || req.query.includeTemplates || "") === "1";
|
||||
|
||||
const hasWeekdaysColumn = await db.query(`
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema='transit' AND table_name='schedules' AND column_name='weekdays'
|
||||
`);
|
||||
|
||||
const baseSelect = `
|
||||
SELECT
|
||||
s.id,
|
||||
s.bus_line_id,
|
||||
s.bus_id,
|
||||
s.driver_id,
|
||||
s.start_time,
|
||||
s.end_time,
|
||||
s.status,
|
||||
s.created_at,
|
||||
bl.line_number,
|
||||
bl.estimated_duration_minutes,
|
||||
b.name as bus_name,
|
||||
b.license_plate,
|
||||
TRIM(COALESCE(e.firstname, '') || ' ' || COALESCE(e.lastname, '')) as driver_name`;
|
||||
|
||||
const result =
|
||||
hasWeekdaysColumn.rows.length > 0
|
||||
? await db.query(`${baseSelect}, s.weekdays as weekdays
|
||||
FROM schedules s
|
||||
LEFT JOIN bus_lines bl ON s.bus_line_id = bl.id
|
||||
LEFT JOIN fleet.bus b ON s.bus_id = b.id
|
||||
LEFT JOIN workers.employees e ON s.driver_id = e.id
|
||||
ORDER BY s.start_time DESC`)
|
||||
: await db.query(`${baseSelect}, NULL as weekdays
|
||||
FROM schedules s
|
||||
LEFT JOIN bus_lines bl ON s.bus_line_id = bl.id
|
||||
LEFT JOIN fleet.bus b ON s.bus_id = b.id
|
||||
LEFT JOIN workers.employees e ON s.driver_id = e.id
|
||||
ORDER BY s.start_time DESC`);
|
||||
|
||||
const lineIds = Array.from(
|
||||
new Set(
|
||||
result.rows
|
||||
.map((row) => Number(row.bus_line_id))
|
||||
.filter(Number.isFinite),
|
||||
),
|
||||
);
|
||||
|
||||
const routeStopsByLine = new Map();
|
||||
if (lineIds.length > 0) {
|
||||
const routeStopsResult = await db.query(
|
||||
`
|
||||
SELECT
|
||||
rs.bus_line_id,
|
||||
rs.stop_id,
|
||||
rs.stop_order,
|
||||
rs.estimated_arrival_minutes,
|
||||
s.name AS stop_name
|
||||
FROM route_stops rs
|
||||
JOIN stops s ON s.id = rs.stop_id
|
||||
WHERE rs.bus_line_id = ANY($1::int[])
|
||||
ORDER BY rs.bus_line_id ASC, rs.stop_order ASC
|
||||
`,
|
||||
[lineIds],
|
||||
);
|
||||
|
||||
for (const row of routeStopsResult.rows) {
|
||||
const busLineId = Number(row.bus_line_id);
|
||||
if (!routeStopsByLine.has(busLineId)) {
|
||||
routeStopsByLine.set(busLineId, []);
|
||||
}
|
||||
routeStopsByLine.get(busLineId).push(row);
|
||||
}
|
||||
}
|
||||
|
||||
const filters = parseScheduleFilters(req.query);
|
||||
const rowSource = templatesOnly
|
||||
? result.rows
|
||||
: expandRecurringScheduleRows(result.rows);
|
||||
const expandedSchedules = rowSource.filter((row) =>
|
||||
scheduleMatchesFilters(row, filters, routeStopsByLine),
|
||||
);
|
||||
|
||||
const schedules = expandedSchedules
|
||||
.map((row) => mapAdminScheduleRow(row, routeStopsByLine))
|
||||
.sort((a, b) => new Date(a.startTime) - new Date(b.startTime));
|
||||
|
||||
return { statusCode: 200, body: { schedules } };
|
||||
} catch (err) {
|
||||
console.error("GET /admin/schedules error:", err);
|
||||
reportError({ partofservice: "GET /admin/schedules", error: err.stack || err.message });
|
||||
return { statusCode: 500, body: { error: "internal_error", details: err.message } };
|
||||
}
|
||||
}
|
||||
|
||||
async function createAdminSchedule(req) {
|
||||
try {
|
||||
const { busLineId, busId, driverId, startTime, weekdays } = req.body;
|
||||
|
||||
if (!busLineId || !busId || !startTime) {
|
||||
return {
|
||||
statusCode: 400,
|
||||
body: {
|
||||
error:
|
||||
"missing_required_fields: busLineId, busId, startTime required",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// Validate weekdays - should be array of day numbers (0-6) or empty for single schedule
|
||||
const scheduleWeekdays =
|
||||
Array.isArray(weekdays) && weekdays.length > 0
|
||||
? weekdays.map((w) => Number(w)).filter((w) => w >= 0 && w <= 6)
|
||||
: [];
|
||||
|
||||
// Database mode (production)
|
||||
const busLine = await db.query(
|
||||
"SELECT estimated_duration_minutes FROM bus_lines WHERE id = $1",
|
||||
[busLineId],
|
||||
);
|
||||
if (!busLine.rows[0]) {
|
||||
return { statusCode: 404, body: { error: "bus_line_not_found" } };
|
||||
}
|
||||
|
||||
const startDate = new Date(startTime);
|
||||
const endDate = new Date(startDate);
|
||||
endDate.setMinutes(
|
||||
endDate.getMinutes() +
|
||||
(busLine.rows[0].estimated_duration_minutes || 60),
|
||||
);
|
||||
|
||||
// Check if weekdays column exists before including it in INSERT
|
||||
const hasWeekdaysColumn = await db.query(`
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema='transit' AND table_name='schedules' AND column_name='weekdays'
|
||||
`);
|
||||
|
||||
let result;
|
||||
if (hasWeekdaysColumn.rows.length > 0) {
|
||||
result = await db.query(
|
||||
`
|
||||
WITH next_id AS (
|
||||
SELECT COALESCE(MAX(id), 0) + 1 AS id FROM schedules
|
||||
)
|
||||
INSERT INTO schedules (id, bus_line_id, bus_id, driver_id, start_time, end_time, status, weekdays)
|
||||
SELECT next_id.id, $1, $2, $3, $4, $5, 'planned', $6
|
||||
FROM next_id
|
||||
RETURNING id, bus_line_id, bus_id, driver_id, start_time, end_time, status, weekdays, created_at
|
||||
`,
|
||||
[
|
||||
busLineId,
|
||||
busId,
|
||||
driverId || null,
|
||||
startTime,
|
||||
endDate.toISOString(),
|
||||
JSON.stringify(scheduleWeekdays),
|
||||
],
|
||||
);
|
||||
} else {
|
||||
result = await db.query(
|
||||
`
|
||||
WITH next_id AS (
|
||||
SELECT COALESCE(MAX(id), 0) + 1 AS id FROM schedules
|
||||
)
|
||||
INSERT INTO schedules (id, bus_line_id, bus_id, driver_id, start_time, end_time, status)
|
||||
SELECT next_id.id, $1, $2, $3, $4, $5, 'planned'
|
||||
FROM next_id
|
||||
RETURNING id, bus_line_id, bus_id, driver_id, start_time, end_time, status, created_at
|
||||
`,
|
||||
[
|
||||
busLineId,
|
||||
busId,
|
||||
driverId || null,
|
||||
startTime,
|
||||
endDate.toISOString(),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
const schedule = result.rows[0];
|
||||
const departureTimes = calculateDepartureTimes(
|
||||
startTime,
|
||||
busLineId,
|
||||
_mock,
|
||||
);
|
||||
|
||||
const weekdaysResult = parseWeekdaysValue(schedule.weekdays);
|
||||
|
||||
return {
|
||||
statusCode: 201,
|
||||
body: {
|
||||
schedule: {
|
||||
id: Number(schedule.id),
|
||||
busLineId: Number(schedule.bus_line_id),
|
||||
busId: Number(schedule.bus_id),
|
||||
driverId:
|
||||
schedule.driver_id !== null && schedule.driver_id !== undefined
|
||||
? Number(schedule.driver_id)
|
||||
: null,
|
||||
startTime: schedule.start_time,
|
||||
endTime: schedule.end_time,
|
||||
weekdays: weekdaysResult,
|
||||
departureTimes,
|
||||
status: schedule.status,
|
||||
createdAt: schedule.created_at,
|
||||
},
|
||||
},
|
||||
};
|
||||
} 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 updateAdminSchedule(req) {
|
||||
try {
|
||||
const { scheduleId } = req.params;
|
||||
const { busLineId, busId, driverId, startTime, weekdays } = req.body;
|
||||
|
||||
if (!scheduleId || !Number.isFinite(Number(scheduleId))) {
|
||||
return { statusCode: 400, body: { error: "invalid_schedule_id" } };
|
||||
}
|
||||
|
||||
if (!busLineId || !busId || !startTime) {
|
||||
return {
|
||||
statusCode: 400,
|
||||
body: {
|
||||
error:
|
||||
"missing_required_fields: busLineId, busId, startTime required",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const scheduleWeekdays =
|
||||
Array.isArray(weekdays) && weekdays.length > 0
|
||||
? weekdays.map((w) => Number(w)).filter((w) => w >= 0 && w <= 6)
|
||||
: [];
|
||||
|
||||
const existingResult = await db.query(
|
||||
`
|
||||
SELECT id, status
|
||||
FROM schedules
|
||||
WHERE id = $1
|
||||
`,
|
||||
[scheduleId],
|
||||
);
|
||||
|
||||
if (existingResult.rows.length === 0) {
|
||||
return { statusCode: 404, body: { error: "schedule_not_found" } };
|
||||
}
|
||||
|
||||
const busLine = await db.query(
|
||||
"SELECT estimated_duration_minutes FROM bus_lines WHERE id = $1",
|
||||
[busLineId],
|
||||
);
|
||||
if (!busLine.rows[0]) {
|
||||
return { statusCode: 404, body: { error: "bus_line_not_found" } };
|
||||
}
|
||||
|
||||
const startDate = new Date(startTime);
|
||||
const endDate = new Date(startDate);
|
||||
endDate.setMinutes(
|
||||
endDate.getMinutes() +
|
||||
(busLine.rows[0].estimated_duration_minutes || 60),
|
||||
);
|
||||
|
||||
const hasWeekdaysColumn = await db.query(`
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema='transit' AND table_name='schedules' AND column_name='weekdays'
|
||||
`);
|
||||
|
||||
let result;
|
||||
if (hasWeekdaysColumn.rows.length > 0) {
|
||||
result = await db.query(
|
||||
`
|
||||
UPDATE schedules
|
||||
SET
|
||||
bus_line_id = $2,
|
||||
bus_id = $3,
|
||||
driver_id = $4,
|
||||
start_time = $5,
|
||||
end_time = $6,
|
||||
weekdays = $7,
|
||||
updated_at = NOW()
|
||||
WHERE id = $1
|
||||
RETURNING id, bus_line_id, bus_id, driver_id, start_time, end_time, status, weekdays, created_at
|
||||
`,
|
||||
[
|
||||
scheduleId,
|
||||
busLineId,
|
||||
busId,
|
||||
driverId || null,
|
||||
startTime,
|
||||
endDate.toISOString(),
|
||||
JSON.stringify(scheduleWeekdays),
|
||||
],
|
||||
);
|
||||
} else {
|
||||
result = await db.query(
|
||||
`
|
||||
UPDATE schedules
|
||||
SET
|
||||
bus_line_id = $2,
|
||||
bus_id = $3,
|
||||
driver_id = $4,
|
||||
start_time = $5,
|
||||
end_time = $6,
|
||||
updated_at = NOW()
|
||||
WHERE id = $1
|
||||
RETURNING id, bus_line_id, bus_id, driver_id, start_time, end_time, status, created_at
|
||||
`,
|
||||
[
|
||||
scheduleId,
|
||||
busLineId,
|
||||
busId,
|
||||
driverId || null,
|
||||
startTime,
|
||||
endDate.toISOString(),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
const schedule = result.rows[0];
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
schedule: {
|
||||
id: Number(schedule.id),
|
||||
busLineId: Number(schedule.bus_line_id),
|
||||
busId: Number(schedule.bus_id),
|
||||
driverId:
|
||||
schedule.driver_id !== null && schedule.driver_id !== undefined
|
||||
? Number(schedule.driver_id)
|
||||
: null,
|
||||
startTime: schedule.start_time,
|
||||
endTime: schedule.end_time,
|
||||
weekdays: parseWeekdaysValue(schedule.weekdays),
|
||||
status: schedule.status,
|
||||
createdAt: schedule.created_at,
|
||||
},
|
||||
},
|
||||
};
|
||||
} 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 bulkCreateAdminSchedules(req) {
|
||||
const requestedSchedules = Array.isArray(req.body?.schedules)
|
||||
? req.body.schedules
|
||||
: [];
|
||||
|
||||
if (requestedSchedules.length === 0) {
|
||||
return {
|
||||
statusCode: 400,
|
||||
body: { error: "missing_required_fields: schedules array required" },
|
||||
};
|
||||
}
|
||||
|
||||
const normalizedSchedules = requestedSchedules.map((schedule, index) => {
|
||||
const busLineId = Number(schedule?.busLineId);
|
||||
const busId = Number(schedule?.busId);
|
||||
const driverId =
|
||||
schedule?.driverId !== null &&
|
||||
schedule?.driverId !== undefined &&
|
||||
schedule?.driverId !== ""
|
||||
? Number(schedule.driverId)
|
||||
: null;
|
||||
const startTime = schedule?.startTime ? String(schedule.startTime) : "";
|
||||
const weekdays =
|
||||
Array.isArray(schedule?.weekdays) && schedule.weekdays.length > 0
|
||||
? schedule.weekdays
|
||||
.map((day) => Number(day))
|
||||
.filter((day) => day >= 0 && day <= 6)
|
||||
: [];
|
||||
|
||||
if (
|
||||
!Number.isFinite(busLineId) ||
|
||||
!Number.isFinite(busId) ||
|
||||
!startTime
|
||||
) {
|
||||
throw new Error(`invalid_schedule_entry:${index}`);
|
||||
}
|
||||
|
||||
return { busLineId, busId, driverId, startTime, weekdays };
|
||||
});
|
||||
|
||||
try {
|
||||
const dbClient = await db.pool.connect();
|
||||
try {
|
||||
await dbClient.query("BEGIN");
|
||||
|
||||
const hasWeekdaysColumn = await dbClient.query(`
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema='transit' AND table_name='schedules' AND column_name='weekdays'
|
||||
`);
|
||||
|
||||
const createdSchedules = [];
|
||||
|
||||
for (const entry of normalizedSchedules) {
|
||||
const busLine = await dbClient.query(
|
||||
"SELECT estimated_duration_minutes FROM bus_lines WHERE id = $1",
|
||||
[entry.busLineId],
|
||||
);
|
||||
if (!busLine.rows[0]) {
|
||||
throw new Error("bus_line_not_found");
|
||||
}
|
||||
|
||||
const startDate = new Date(entry.startTime);
|
||||
const endDate = new Date(startDate);
|
||||
endDate.setMinutes(
|
||||
endDate.getMinutes() +
|
||||
(busLine.rows[0].estimated_duration_minutes || 60),
|
||||
);
|
||||
|
||||
let result;
|
||||
if (hasWeekdaysColumn.rows.length > 0) {
|
||||
result = await dbClient.query(
|
||||
`
|
||||
WITH next_id AS (
|
||||
SELECT COALESCE(MAX(id), 0) + 1 AS id FROM schedules
|
||||
)
|
||||
INSERT INTO schedules (id, bus_line_id, bus_id, driver_id, start_time, end_time, status, weekdays)
|
||||
SELECT next_id.id, $1, $2, $3, $4, $5, 'planned', $6
|
||||
FROM next_id
|
||||
RETURNING id, bus_line_id, bus_id, driver_id, start_time, end_time, status, weekdays, created_at
|
||||
`,
|
||||
[
|
||||
entry.busLineId,
|
||||
entry.busId,
|
||||
entry.driverId,
|
||||
entry.startTime,
|
||||
endDate.toISOString(),
|
||||
JSON.stringify(entry.weekdays),
|
||||
],
|
||||
);
|
||||
} else {
|
||||
result = await dbClient.query(
|
||||
`
|
||||
WITH next_id AS (
|
||||
SELECT COALESCE(MAX(id), 0) + 1 AS id FROM schedules
|
||||
)
|
||||
INSERT INTO schedules (id, bus_line_id, bus_id, driver_id, start_time, end_time, status)
|
||||
SELECT next_id.id, $1, $2, $3, $4, $5, 'planned'
|
||||
FROM next_id
|
||||
RETURNING id, bus_line_id, bus_id, driver_id, start_time, end_time, status, created_at
|
||||
`,
|
||||
[
|
||||
entry.busLineId,
|
||||
entry.busId,
|
||||
entry.driverId,
|
||||
entry.startTime,
|
||||
endDate.toISOString(),
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
const schedule = result.rows[0];
|
||||
createdSchedules.push({
|
||||
id: Number(schedule.id),
|
||||
busLineId: Number(schedule.bus_line_id),
|
||||
busId: Number(schedule.bus_id),
|
||||
driverId:
|
||||
schedule.driver_id !== null && schedule.driver_id !== undefined
|
||||
? Number(schedule.driver_id)
|
||||
: null,
|
||||
startTime: schedule.start_time,
|
||||
endTime: schedule.end_time,
|
||||
weekdays: parseWeekdaysValue(schedule.weekdays),
|
||||
status: schedule.status,
|
||||
createdAt: schedule.created_at,
|
||||
});
|
||||
}
|
||||
|
||||
await dbClient.query("COMMIT");
|
||||
return {
|
||||
statusCode: 201,
|
||||
body: {
|
||||
createdCount: createdSchedules.length,
|
||||
schedules: createdSchedules,
|
||||
},
|
||||
};
|
||||
} catch (err) {
|
||||
await dbClient.query("ROLLBACK");
|
||||
throw err;
|
||||
} finally {
|
||||
dbClient.release();
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
reportError({ partofservice: "POST /admin/schedules/bulk", error: err.stack || err.message });
|
||||
const errorMessage =
|
||||
err.message === "bus_line_not_found"
|
||||
? "bus_line_not_found"
|
||||
: err.message.startsWith("invalid_schedule_entry:")
|
||||
? err.message
|
||||
: "internal_error";
|
||||
const statusCode =
|
||||
errorMessage === "bus_line_not_found"
|
||||
? 404
|
||||
: errorMessage.startsWith("invalid_schedule_entry:")
|
||||
? 400
|
||||
: 500;
|
||||
return { statusCode, body: { error: errorMessage, details: err.message } };
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteAdminSchedule(req) {
|
||||
try {
|
||||
const { scheduleId } = req.params;
|
||||
|
||||
if (!scheduleId || !Number.isFinite(Number(scheduleId))) {
|
||||
return { statusCode: 400, body: { error: "invalid_schedule_id" } };
|
||||
}
|
||||
|
||||
const result = await db.query(
|
||||
`DELETE FROM schedules WHERE id = $1 RETURNING id`,
|
||||
[scheduleId],
|
||||
);
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
return { statusCode: 404, body: { error: "schedule_not_found" } };
|
||||
}
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: { success: true, schedule: { id: Number(result.rows[0].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 checkDriverConflicts(req) {
|
||||
try {
|
||||
const {
|
||||
generatedTimes,
|
||||
drivers,
|
||||
busLineId,
|
||||
busId,
|
||||
weekdays = [],
|
||||
} = req.body;
|
||||
|
||||
if (!Array.isArray(generatedTimes) || !Array.isArray(drivers)) {
|
||||
return { statusCode: 400, body: { error: "missing_required_fields" } };
|
||||
}
|
||||
|
||||
// Get all existing schedules
|
||||
const schedResult = DEV_MOCK
|
||||
? { rows: _mock.schedules || [] }
|
||||
: await db.query(
|
||||
`SELECT * FROM schedules WHERE status IN ('planned', 'active')`,
|
||||
);
|
||||
|
||||
const existingSchedules = schedResult.rows.map((row) => ({
|
||||
...row,
|
||||
weekdays: parseWeekdaysValue(row.weekdays || row.weekday_list || "[]"),
|
||||
}));
|
||||
|
||||
// For each generated time, check conflicts with each driver
|
||||
const assignments = generatedTimes.map((timeStr) => {
|
||||
const driverAssignments = drivers.map((driver) => {
|
||||
const hasConflict = checkDriverConflict(
|
||||
driver.id,
|
||||
timeStr,
|
||||
timeStr, // Will be checked with start/end times of conflicts
|
||||
weekdays,
|
||||
existingSchedules,
|
||||
);
|
||||
|
||||
return {
|
||||
driverId: driver.id,
|
||||
driverName: driver.name,
|
||||
hasConflict,
|
||||
assigned: false,
|
||||
};
|
||||
});
|
||||
|
||||
return {
|
||||
time: timeStr,
|
||||
driverAssignments,
|
||||
assignedDriverId: null,
|
||||
};
|
||||
});
|
||||
|
||||
return { statusCode: 200, body: { assignments } };
|
||||
} catch (err) {
|
||||
console.error("/admin/check-driver-conflicts error:", err);
|
||||
reportError({ partofservice: "/admin/check-driver-conflicts", error: err.stack || err.message });
|
||||
return { statusCode: 500, body: { error: "internal_error", details: err.message } };
|
||||
}
|
||||
}
|
||||
|
||||
async function assignSchedules(req) {
|
||||
try {
|
||||
const { assignments, busLineId, busId, weekdays = [] } = req.body;
|
||||
|
||||
if (!Array.isArray(assignments)) {
|
||||
return { statusCode: 400, body: { error: "missing_assignments" } };
|
||||
}
|
||||
|
||||
// Group assignments by driver (round-robin or custom)
|
||||
const schedulesToCreate = [];
|
||||
let driverIndex = 0;
|
||||
|
||||
for (const assignment of assignments) {
|
||||
if (!assignment.time || !assignment.assignedDriverId) continue;
|
||||
|
||||
const startTime = assignment.time;
|
||||
const endTime = assignment.endTime;
|
||||
|
||||
schedulesToCreate.push({
|
||||
busLineId,
|
||||
busId,
|
||||
driverId: assignment.assignedDriverId,
|
||||
startTime,
|
||||
endTime,
|
||||
weekdays,
|
||||
});
|
||||
}
|
||||
|
||||
// For DB: use bulk insert
|
||||
return {
|
||||
statusCode: 201,
|
||||
body: {
|
||||
createdCount: schedulesToCreate.length,
|
||||
message: "would_insert_schedules",
|
||||
},
|
||||
};
|
||||
} catch (err) {
|
||||
console.error("/admin/assign-schedules error:", err);
|
||||
reportError({ partofservice: "/admin/assign-schedules", error: err.stack || err.message });
|
||||
return { statusCode: 500, body: { error: "internal_error", details: err.message } };
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
listAdminSchedules,
|
||||
createAdminSchedule,
|
||||
updateAdminSchedule,
|
||||
bulkCreateAdminSchedules,
|
||||
deleteAdminSchedule,
|
||||
checkDriverConflicts,
|
||||
assignSchedules,
|
||||
};
|
||||
@@ -0,0 +1,390 @@
|
||||
const { store: _mock } = require("../mock/store");
|
||||
const { calculateDepartureTimes, expandRecurringScheduleRows, scheduleMatchesFilters, parseScheduleFilters } = require("../lib/schedule");
|
||||
const { reportError } = require("../lib/report-error");
|
||||
|
||||
// Admin schedules repository methods (DEV_MOCK mode). These are exact copies of
|
||||
// the original handlers’ mock branches, returning { statusCode, body }.
|
||||
|
||||
async function listAdminSchedules(req) {
|
||||
try {
|
||||
const templatesOnly =
|
||||
String(
|
||||
req.query.templates || req.query.includeTemplates || "",
|
||||
).toLowerCase() === "true" ||
|
||||
String(req.query.templates || req.query.includeTemplates || "") === "1";
|
||||
|
||||
const routeStopsByLine = new Map();
|
||||
for (const routeStop of _mock.routeStops || []) {
|
||||
const busLineId = Number(routeStop.busLineId);
|
||||
if (!routeStopsByLine.has(busLineId)) {
|
||||
routeStopsByLine.set(busLineId, []);
|
||||
}
|
||||
routeStopsByLine.get(busLineId).push({
|
||||
stop_id: routeStop.stopId,
|
||||
stop_order: routeStop.stopOrder,
|
||||
});
|
||||
}
|
||||
|
||||
const rawSchedules = (_mock.schedules || []).map((s) => {
|
||||
const busLine = _mock.busLines.find((bl) => bl.id === s.busLineId);
|
||||
const bus = _mock.fleetBuses.find((b) => b.id === s.busId);
|
||||
return {
|
||||
...s,
|
||||
busLineId: s.busLineId,
|
||||
busId: s.busId,
|
||||
startTime: s.startTime,
|
||||
endTime: s.endTime,
|
||||
weekdays: s.weekdays || [],
|
||||
status: s.status,
|
||||
lineNumber: busLine?.lineNumber || 0,
|
||||
busName: bus?.name || "Unknown Bus",
|
||||
driverId: s.driverId || null,
|
||||
estimatedDurationMinutes: busLine?.estimatedDurationMinutes || 60,
|
||||
};
|
||||
});
|
||||
|
||||
const schedules = (
|
||||
templatesOnly
|
||||
? rawSchedules
|
||||
: expandRecurringScheduleRows(rawSchedules)
|
||||
)
|
||||
.filter((row) =>
|
||||
scheduleMatchesFilters(
|
||||
row,
|
||||
parseScheduleFilters(req.query),
|
||||
routeStopsByLine,
|
||||
),
|
||||
)
|
||||
.map((s) => ({
|
||||
...s,
|
||||
departureTimes: calculateDepartureTimes(
|
||||
s.startTime,
|
||||
s.busLineId,
|
||||
_mock,
|
||||
),
|
||||
}))
|
||||
.sort((a, b) => new Date(a.startTime) - new Date(b.startTime));
|
||||
|
||||
return { statusCode: 200, body: { schedules } };
|
||||
} catch (err) {
|
||||
console.error("GET /admin/schedules error:", err);
|
||||
reportError({ partofservice: "GET /admin/schedules", error: err.stack || err.message });
|
||||
return { statusCode: 500, body: { error: "internal_error", details: err.message } };
|
||||
}
|
||||
}
|
||||
|
||||
async function createAdminSchedule(req) {
|
||||
try {
|
||||
const { busLineId, busId, driverId, startTime, weekdays } = req.body;
|
||||
|
||||
if (!busLineId || !busId || !startTime) {
|
||||
return {
|
||||
statusCode: 400,
|
||||
body: {
|
||||
error:
|
||||
"missing_required_fields: busLineId, busId, startTime required",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const scheduleWeekdays =
|
||||
Array.isArray(weekdays) && weekdays.length > 0
|
||||
? weekdays.map((w) => Number(w)).filter((w) => w >= 0 && w <= 6)
|
||||
: [];
|
||||
|
||||
if (!_mock.schedules) _mock.schedules = [];
|
||||
|
||||
const busLine = _mock.busLines.find((bl) => bl.id == busLineId);
|
||||
if (!busLine) {
|
||||
return { statusCode: 404, body: { error: "bus_line_not_found" } };
|
||||
}
|
||||
|
||||
const startDate = new Date(startTime);
|
||||
const endDate = new Date(startDate);
|
||||
endDate.setMinutes(
|
||||
endDate.getMinutes() + (busLine.estimatedDurationMinutes || 60),
|
||||
);
|
||||
|
||||
const departureTimes = calculateDepartureTimes(
|
||||
startTime,
|
||||
Number(busLineId),
|
||||
_mock,
|
||||
);
|
||||
|
||||
const schedule = {
|
||||
id: _mock.nextScheduleId || 1,
|
||||
busLineId,
|
||||
busId,
|
||||
driverId: driverId ? Number(driverId) : null,
|
||||
startTime,
|
||||
endTime: endDate.toISOString(),
|
||||
weekdays: scheduleWeekdays,
|
||||
departureTimes,
|
||||
status: "planned",
|
||||
createdAt: new Date().toISOString(),
|
||||
};
|
||||
_mock.nextScheduleId = (_mock.nextScheduleId || 1) + 1;
|
||||
_mock.schedules.push(schedule);
|
||||
return { statusCode: 201, body: { schedule } };
|
||||
} 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 updateAdminSchedule(req) {
|
||||
try {
|
||||
const { scheduleId } = req.params;
|
||||
const { busLineId, busId, driverId, startTime, weekdays } = req.body;
|
||||
|
||||
if (!scheduleId || !Number.isFinite(Number(scheduleId))) {
|
||||
return { statusCode: 400, body: { error: "invalid_schedule_id" } };
|
||||
}
|
||||
|
||||
if (!busLineId || !busId || !startTime) {
|
||||
return {
|
||||
statusCode: 400,
|
||||
body: {
|
||||
error:
|
||||
"missing_required_fields: busLineId, busId, startTime required",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const scheduleWeekdays =
|
||||
Array.isArray(weekdays) && weekdays.length > 0
|
||||
? weekdays.map((w) => Number(w)).filter((w) => w >= 0 && w <= 6)
|
||||
: [];
|
||||
|
||||
const index = (_mock.schedules || []).findIndex(
|
||||
(s) => Number(s.id) === Number(scheduleId),
|
||||
);
|
||||
if (index === -1) {
|
||||
return { statusCode: 404, body: { error: "schedule_not_found" } };
|
||||
}
|
||||
|
||||
const busLine = _mock.busLines.find((bl) => bl.id == busLineId);
|
||||
if (!busLine) {
|
||||
return { statusCode: 404, body: { error: "bus_line_not_found" } };
|
||||
}
|
||||
|
||||
const startDate = new Date(startTime);
|
||||
const endDate = new Date(startDate);
|
||||
endDate.setMinutes(
|
||||
endDate.getMinutes() + (busLine.estimatedDurationMinutes || 60),
|
||||
);
|
||||
|
||||
_mock.schedules[index] = {
|
||||
..._mock.schedules[index],
|
||||
busLineId: Number(busLineId),
|
||||
busId: Number(busId),
|
||||
driverId: driverId ? Number(driverId) : null,
|
||||
startTime,
|
||||
endTime: endDate.toISOString(),
|
||||
weekdays: scheduleWeekdays,
|
||||
};
|
||||
|
||||
return { statusCode: 200, body: { schedule: _mock.schedules[index] } };
|
||||
} 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 bulkCreateAdminSchedules(req) {
|
||||
const requestedSchedules = Array.isArray(req.body?.schedules)
|
||||
? req.body.schedules
|
||||
: [];
|
||||
|
||||
if (requestedSchedules.length === 0) {
|
||||
return {
|
||||
statusCode: 400,
|
||||
body: { error: "missing_required_fields: schedules array required" },
|
||||
};
|
||||
}
|
||||
|
||||
const normalizedSchedules = requestedSchedules.map((schedule, index) => {
|
||||
const busLineId = Number(schedule?.busLineId);
|
||||
const busId = Number(schedule?.busId);
|
||||
const driverId =
|
||||
schedule?.driverId !== null &&
|
||||
schedule?.driverId !== undefined &&
|
||||
schedule?.driverId !== ""
|
||||
? Number(schedule.driverId)
|
||||
: null;
|
||||
const startTime = schedule?.startTime ? String(schedule.startTime) : "";
|
||||
const weekdays =
|
||||
Array.isArray(schedule?.weekdays) && schedule.weekdays.length > 0
|
||||
? schedule.weekdays
|
||||
.map((day) => Number(day))
|
||||
.filter((day) => day >= 0 && day <= 6)
|
||||
: [];
|
||||
|
||||
if (
|
||||
!Number.isFinite(busLineId) ||
|
||||
!Number.isFinite(busId) ||
|
||||
!startTime
|
||||
) {
|
||||
throw new Error(`invalid_schedule_entry:${index}`);
|
||||
}
|
||||
|
||||
return { busLineId, busId, driverId, startTime, weekdays };
|
||||
});
|
||||
|
||||
try {
|
||||
if (!_mock.schedules) _mock.schedules = [];
|
||||
|
||||
const createdSchedules = [];
|
||||
|
||||
for (const entry of normalizedSchedules) {
|
||||
const busLine = _mock.busLines.find((bl) => bl.id == entry.busLineId);
|
||||
if (!busLine) {
|
||||
return { statusCode: 404, body: { error: "bus_line_not_found" } };
|
||||
}
|
||||
|
||||
const startDate = new Date(entry.startTime);
|
||||
const endDate = new Date(startDate);
|
||||
endDate.setMinutes(
|
||||
endDate.getMinutes() + (busLine.estimatedDurationMinutes || 60),
|
||||
);
|
||||
|
||||
const departureTimes = calculateDepartureTimes(
|
||||
entry.startTime,
|
||||
Number(entry.busLineId),
|
||||
_mock,
|
||||
);
|
||||
|
||||
const schedule = {
|
||||
id: _mock.nextScheduleId || 1,
|
||||
busLineId: entry.busLineId,
|
||||
busId: entry.busId,
|
||||
driverId: entry.driverId,
|
||||
startTime: entry.startTime,
|
||||
endTime: endDate.toISOString(),
|
||||
weekdays: entry.weekdays,
|
||||
departureTimes,
|
||||
status: "planned",
|
||||
createdAt: new Date().toISOString(),
|
||||
};
|
||||
|
||||
_mock.nextScheduleId = (_mock.nextScheduleId || 1) + 1;
|
||||
_mock.schedules.push(schedule);
|
||||
createdSchedules.push(schedule);
|
||||
}
|
||||
|
||||
return {
|
||||
statusCode: 201,
|
||||
body: {
|
||||
createdCount: createdSchedules.length,
|
||||
schedules: createdSchedules,
|
||||
},
|
||||
};
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
reportError({ partofservice: "POST /admin/schedules/bulk", error: err.stack || err.message });
|
||||
const errorMessage =
|
||||
err.message === "bus_line_not_found"
|
||||
? "bus_line_not_found"
|
||||
: err.message.startsWith("invalid_schedule_entry:")
|
||||
? err.message
|
||||
: "internal_error";
|
||||
const statusCode =
|
||||
errorMessage === "bus_line_not_found"
|
||||
? 404
|
||||
: errorMessage.startsWith("invalid_schedule_entry:")
|
||||
? 400
|
||||
: 500;
|
||||
return { statusCode, body: { error: errorMessage, details: err.message } };
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteAdminSchedule(req) {
|
||||
try {
|
||||
const { scheduleId } = req.params;
|
||||
|
||||
if (!scheduleId || !Number.isFinite(Number(scheduleId))) {
|
||||
return { statusCode: 400, body: { error: "invalid_schedule_id" } };
|
||||
}
|
||||
|
||||
if (!_mock.schedules) _mock.schedules = [];
|
||||
const index = _mock.schedules.findIndex(
|
||||
(s) => Number(s.id) === Number(scheduleId),
|
||||
);
|
||||
if (index === -1) {
|
||||
return { statusCode: 404, body: { error: "schedule_not_found" } };
|
||||
}
|
||||
const deleted = _mock.schedules.splice(index, 1)[0];
|
||||
return { statusCode: 200, body: { success: true, schedule: 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 } };
|
||||
}
|
||||
}
|
||||
|
||||
async function assignSchedules(req) {
|
||||
try {
|
||||
const { assignments, busLineId, busId, weekdays = [] } = req.body;
|
||||
|
||||
if (!Array.isArray(assignments)) {
|
||||
return { statusCode: 400, body: { error: "missing_assignments" } };
|
||||
}
|
||||
|
||||
const schedulesToCreate = [];
|
||||
let driverIndex = 0;
|
||||
|
||||
for (const assignment of assignments) {
|
||||
if (!assignment.time || !assignment.assignedDriverId) continue;
|
||||
|
||||
const startTime = assignment.time;
|
||||
const endTime = assignment.endTime;
|
||||
|
||||
schedulesToCreate.push({
|
||||
busLineId,
|
||||
busId,
|
||||
driverId: assignment.assignedDriverId,
|
||||
startTime,
|
||||
endTime,
|
||||
weekdays,
|
||||
});
|
||||
}
|
||||
|
||||
if (!_mock.schedules) _mock.schedules = [];
|
||||
const createdSchedules = [];
|
||||
|
||||
for (const entry of schedulesToCreate) {
|
||||
const schedule = {
|
||||
id: _mock.nextScheduleId || 1,
|
||||
...entry,
|
||||
status: "planned",
|
||||
createdAt: new Date().toISOString(),
|
||||
};
|
||||
_mock.nextScheduleId = (_mock.nextScheduleId || 1) + 1;
|
||||
_mock.schedules.push(schedule);
|
||||
createdSchedules.push(schedule);
|
||||
}
|
||||
|
||||
return {
|
||||
statusCode: 201,
|
||||
body: {
|
||||
createdCount: createdSchedules.length,
|
||||
schedules: createdSchedules,
|
||||
},
|
||||
};
|
||||
} catch (err) {
|
||||
console.error("/admin/assign-schedules error:", err);
|
||||
reportError({ partofservice: "/admin/assign-schedules", error: err.stack || err.message });
|
||||
return { statusCode: 500, body: { error: "internal_error", details: err.message } };
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
listAdminSchedules,
|
||||
createAdminSchedule,
|
||||
updateAdminSchedule,
|
||||
bulkCreateAdminSchedules,
|
||||
deleteAdminSchedule,
|
||||
assignSchedules,
|
||||
};
|
||||
@@ -0,0 +1,238 @@
|
||||
const db = require("../db");
|
||||
const { DEV_MOCK } = require("../lib/env");
|
||||
const { isValidDateKey } = require("../lib/validate");
|
||||
const { reportError } = require("../lib/report-error");
|
||||
|
||||
// Daily schedule repository methods (PostgreSQL-mode behavior; the original
|
||||
// handlers always queried the DB and only used `DEV_MOCK` to suppress the
|
||||
// fallbacks, so this module is used in both modes).
|
||||
|
||||
async function listDailySchedules(req) {
|
||||
try {
|
||||
const { date } = req.query;
|
||||
const userId = req.user.userId;
|
||||
|
||||
if (!date) {
|
||||
return { statusCode: 400, body: { error: "date query parameter required" } };
|
||||
}
|
||||
|
||||
const result = await db.query(
|
||||
`
|
||||
SELECT
|
||||
r.id,
|
||||
r.start_time,
|
||||
r.end_time,
|
||||
r.status,
|
||||
bl.id as line_id,
|
||||
bl.line_number,
|
||||
bl.start_stop,
|
||||
bl.end_stop,
|
||||
bl.estimated_duration_minutes,
|
||||
bl.description,
|
||||
bt.id as bus_type_id,
|
||||
bt.name as bus_type,
|
||||
bt.seat_capacity,
|
||||
bt.license_plate
|
||||
FROM routes r
|
||||
JOIN bus_lines bl ON r.bus_line_id = bl.id
|
||||
JOIN fleet.bus bt ON r.bus_type_id = bt.id
|
||||
WHERE r.user_id = $1 AND DATE(r.start_time) = $2
|
||||
ORDER BY r.start_time ASC
|
||||
`,
|
||||
[userId, date],
|
||||
);
|
||||
|
||||
let routes = result.rows.map((row) => ({
|
||||
id: row.id,
|
||||
startTime: row.start_time,
|
||||
endTime: row.end_time,
|
||||
status: row.status,
|
||||
busLine: {
|
||||
id: row.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 no explicit "routes" rows exist for this user/date, fall back to
|
||||
// checking scheduled runs in `schedules` (filtered by driver_id).
|
||||
// This works around cases where DB permissions prevent inserting into
|
||||
// `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 schedules s
|
||||
JOIN 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 schedules s
|
||||
JOIN 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,
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return { statusCode: 200, body: { date, routes } };
|
||||
} 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 listDriverDailySchedules(req) {
|
||||
try {
|
||||
const { date } = req.query;
|
||||
if (!isValidDateKey(date)) {
|
||||
return { statusCode: 400, body: { error: "date must use YYYY-MM-DD format" } };
|
||||
}
|
||||
|
||||
const result = await db.query(
|
||||
`
|
||||
SELECT s.id, s.bus_line_id, s.start_time, s.end_time, s.status, s.day_of_week,
|
||||
bl.line_number, bl.start_stop, bl.end_stop, bl.estimated_duration_minutes,
|
||||
bl.description
|
||||
FROM schedules s
|
||||
JOIN bus_lines bl ON bl.id = s.bus_line_id
|
||||
WHERE EXISTS (
|
||||
SELECT 1
|
||||
FROM shifts sh
|
||||
WHERE sh.schedule_id = s.id AND sh.driver_id = $1
|
||||
)
|
||||
AND s.day_of_week = EXTRACT(DOW FROM $2::date)::int
|
||||
AND s.status <> 'cancelled'
|
||||
ORDER BY s.start_time ASC
|
||||
`,
|
||||
[req.user.userId, date],
|
||||
);
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
date,
|
||||
routes: result.rows.map((row) => ({
|
||||
id: Number(row.id),
|
||||
startTime: row.start_time,
|
||||
endTime: row.end_time,
|
||||
status: row.status,
|
||||
dayOfWeek: Number(row.day_of_week),
|
||||
busLine: {
|
||||
id: Number(row.bus_line_id),
|
||||
lineNumber: row.line_number,
|
||||
startStop: row.start_stop,
|
||||
endStop: row.end_stop,
|
||||
estimatedDuration: row.estimated_duration_minutes,
|
||||
description: row.description,
|
||||
},
|
||||
})),
|
||||
},
|
||||
};
|
||||
} catch (err) {
|
||||
console.error("GET /driver/schedules/daily error:", err);
|
||||
reportError({ partofservice: "GET /driver/schedules/daily", error: err.stack || err.message });
|
||||
return { statusCode: 500, body: { error: "internal_error" } };
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { listDailySchedules, listDriverDailySchedules };
|
||||
@@ -0,0 +1,88 @@
|
||||
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 };
|
||||
@@ -0,0 +1,34 @@
|
||||
const { store } = require("../mock/store");
|
||||
const { checkDriverConflict } = require("../lib/schedule");
|
||||
const { reportError } = require("../lib/report-error");
|
||||
|
||||
// Driver repository methods (DEV_MOCK mode).
|
||||
|
||||
async function listDrivers(req) {
|
||||
try {
|
||||
const drivers = store.drivers
|
||||
.slice()
|
||||
.filter((d) => d.status === "active");
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
drivers: drivers.map((d) => ({
|
||||
...d,
|
||||
available: !checkDriverConflict(
|
||||
d.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 } };
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { listDrivers };
|
||||
@@ -0,0 +1,159 @@
|
||||
const db = require("../db");
|
||||
const { parseWeekdaysValue, checkBusConflict } = require("../lib/schedule");
|
||||
const { reportError } = require("../lib/report-error");
|
||||
|
||||
// Fleet repository methods (PostgreSQL mode).
|
||||
|
||||
async function listFleetBuses(req) {
|
||||
try {
|
||||
const result = await db.query(`
|
||||
SELECT id, name, seat_capacity, license_plate
|
||||
FROM fleet.bus
|
||||
ORDER BY id 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: {
|
||||
buses: result.rows.map((row) => {
|
||||
const busId = row.id;
|
||||
const now = new Date();
|
||||
const tomorrow = new Date(now);
|
||||
tomorrow.setDate(tomorrow.getDate() + 1);
|
||||
const isAvailable = !checkBusConflict(
|
||||
busId,
|
||||
now.toISOString(),
|
||||
tomorrow.toISOString(),
|
||||
[],
|
||||
schedules,
|
||||
);
|
||||
|
||||
return {
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
seatCapacity: row.seat_capacity,
|
||||
licensePlate: row.license_plate,
|
||||
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 createFleetBus(req) {
|
||||
try {
|
||||
const { name, seatCapacity, licensePlate } = req.body;
|
||||
|
||||
if (!name || seatCapacity === undefined || !licensePlate) {
|
||||
return { statusCode: 400, body: { error: "missing_required_fields" } };
|
||||
}
|
||||
|
||||
const result = await db.query(
|
||||
`
|
||||
WITH next_id AS (
|
||||
SELECT COALESCE(MAX(id), 0) + 1 AS id FROM fleet.bus
|
||||
)
|
||||
INSERT INTO fleet.bus (id, name, seat_capacity, license_plate)
|
||||
SELECT next_id.id, $1, $2, $3
|
||||
FROM next_id
|
||||
RETURNING id, name, seat_capacity, license_plate
|
||||
`,
|
||||
[name, seatCapacity, licensePlate],
|
||||
);
|
||||
|
||||
const bus = result.rows[0];
|
||||
return {
|
||||
statusCode: 201,
|
||||
body: {
|
||||
bus: {
|
||||
id: bus.id,
|
||||
name: bus.name,
|
||||
seatCapacity: bus.seat_capacity,
|
||||
licensePlate: bus.license_plate,
|
||||
},
|
||||
},
|
||||
};
|
||||
} 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 result = await db.query(
|
||||
`
|
||||
UPDATE fleet.bus
|
||||
SET name = COALESCE($1, name), seat_capacity = COALESCE($2, seat_capacity), license_plate = COALESCE($3, license_plate), updated_at = NOW()
|
||||
WHERE id = $4
|
||||
RETURNING id, name, seat_capacity, license_plate
|
||||
`,
|
||||
[name ?? null, seatCapacity ?? null, licensePlate ?? null, busId],
|
||||
);
|
||||
|
||||
if (!result.rows[0])
|
||||
return { statusCode: 404, body: { error: "bus_not_found" } };
|
||||
|
||||
const bus = result.rows[0];
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
bus: {
|
||||
id: bus.id,
|
||||
name: bus.name,
|
||||
seatCapacity: bus.seat_capacity,
|
||||
licensePlate: bus.license_plate,
|
||||
},
|
||||
},
|
||||
};
|
||||
} 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 result = await db.query(
|
||||
`DELETE FROM fleet.bus WHERE id = $1 RETURNING id, name`,
|
||||
[busId],
|
||||
);
|
||||
if (result.rows.length === 0)
|
||||
return { statusCode: 404, body: { error: "bus_not_found" } };
|
||||
return { statusCode: 200, body: { success: true, bus: result.rows[0] } };
|
||||
} 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 };
|
||||
@@ -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 };
|
||||
@@ -0,0 +1,88 @@
|
||||
const db = require("../db");
|
||||
const { reportError } = require("../lib/report-error");
|
||||
|
||||
// GPS repository methods (PostgreSQL mode; no DEV_MOCK branch).
|
||||
|
||||
async function saveGpsTrack(req) {
|
||||
try {
|
||||
const { routeId, latitude, longitude, speed, heading, accuracy } = req.body;
|
||||
|
||||
if (!routeId || !latitude || !longitude) {
|
||||
return { statusCode: 400, body: { error: "missing_required_fields" } };
|
||||
}
|
||||
|
||||
await db.query(
|
||||
`
|
||||
INSERT INTO gps_tracking
|
||||
(route_id, latitude, longitude, speed_kmh, heading, accuracy, recorded_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, NOW())
|
||||
`,
|
||||
[
|
||||
routeId,
|
||||
latitude,
|
||||
longitude,
|
||||
speed || null,
|
||||
heading || null,
|
||||
accuracy || null,
|
||||
],
|
||||
);
|
||||
|
||||
return { statusCode: 200, body: { ok: true } };
|
||||
} 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 listRouteGpsPoints(req) {
|
||||
try {
|
||||
const { routeId } = req.params;
|
||||
const userId = req.user.userId;
|
||||
|
||||
// Verify user owns this route
|
||||
const routeCheck = await db.query(
|
||||
"SELECT id FROM routes WHERE id = $1 AND user_id = $2",
|
||||
[routeId, userId],
|
||||
);
|
||||
|
||||
if (routeCheck.rows.length === 0) {
|
||||
return { statusCode: 403, body: { error: "unauthorized" } };
|
||||
}
|
||||
|
||||
const result = await db.query(
|
||||
`
|
||||
SELECT
|
||||
id,
|
||||
latitude,
|
||||
longitude,
|
||||
speed_kmh,
|
||||
heading,
|
||||
accuracy,
|
||||
recorded_at
|
||||
FROM gps_tracking
|
||||
WHERE route_id = $1
|
||||
ORDER BY recorded_at ASC
|
||||
`,
|
||||
[routeId],
|
||||
);
|
||||
|
||||
const positions = result.rows.map((row) => ({
|
||||
id: row.id,
|
||||
latitude: parseFloat(row.latitude),
|
||||
longitude: parseFloat(row.longitude),
|
||||
speed: row.speed_kmh,
|
||||
heading: row.heading,
|
||||
accuracy: row.accuracy,
|
||||
recordedAt: row.recorded_at,
|
||||
}));
|
||||
|
||||
return { statusCode: 200, body: { positions } };
|
||||
} 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 = { saveGpsTrack, listRouteGpsPoints };
|
||||
@@ -0,0 +1,36 @@
|
||||
const { DEV_MOCK } = require("../lib/env");
|
||||
|
||||
// The repository layer merges every domain's PostgreSQL implementation with the
|
||||
// mock implementations. In DEV_MOCK mode the mock methods (which read the
|
||||
// in-memory `mock/store`) shadow the DB-backed ones; endpoints that have no
|
||||
// mock branch always use the PostgreSQL implementation, exactly as the original
|
||||
// handlers did.
|
||||
//
|
||||
// Every method takes the Express `req` (the original handlers read params,
|
||||
// query, body and user from it) and returns `{ statusCode, body }`. Route
|
||||
// handlers only transform that into `res.status(...).json(...)`.
|
||||
|
||||
const pg = {
|
||||
...require("./stops"),
|
||||
...require("./lines"),
|
||||
...require("./fleet"),
|
||||
...require("./drivers"),
|
||||
...require("./daily"),
|
||||
...require("./weekly"),
|
||||
...require("./adminSchedules"),
|
||||
...require("./schedules"),
|
||||
...require("./route"),
|
||||
...require("./navigation"),
|
||||
...require("./gps"),
|
||||
};
|
||||
|
||||
const mock = {
|
||||
...require("./stops.mock"),
|
||||
...require("./lines.mock"),
|
||||
...require("./fleet.mock"),
|
||||
...require("./drivers.mock"),
|
||||
...require("./adminSchedules.mock"),
|
||||
...require("./schedules.mock"),
|
||||
};
|
||||
|
||||
module.exports = DEV_MOCK ? { ...pg, ...mock } : pg;
|
||||
@@ -0,0 +1,478 @@
|
||||
const db = require("../db");
|
||||
const { reportError } = require("../lib/report-error");
|
||||
|
||||
// Bus-line repository methods (PostgreSQL mode).
|
||||
// The client-based methods keep their original `db.pool.connect()` call before
|
||||
// the try block, preserving the exact original control flow (including the
|
||||
// connection/rollback/release dance) for byte-identical behavior.
|
||||
|
||||
async function listLineStops(req) {
|
||||
try {
|
||||
const { busLineId } = req.params;
|
||||
|
||||
const result = await db.query(
|
||||
`
|
||||
SELECT
|
||||
rs.id,
|
||||
rs.stop_order,
|
||||
s.name as stop_name,
|
||||
s.latitude,
|
||||
s.longitude,
|
||||
rs.estimated_arrival_minutes
|
||||
FROM route_stops rs
|
||||
JOIN stops s ON s.id = rs.stop_id
|
||||
WHERE rs.bus_line_id = $1
|
||||
ORDER BY rs.stop_order ASC
|
||||
`,
|
||||
[busLineId],
|
||||
);
|
||||
|
||||
const stops = result.rows.map((row) => ({
|
||||
id: row.id,
|
||||
order: row.stop_order,
|
||||
name: row.stop_name,
|
||||
latitude: parseFloat(row.latitude),
|
||||
longitude: parseFloat(row.longitude),
|
||||
estimatedArrivalMinutes: row.estimated_arrival_minutes,
|
||||
}));
|
||||
|
||||
return { statusCode: 200, body: { stops } };
|
||||
} 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 listLines(req) {
|
||||
try {
|
||||
const result = await db.query(`
|
||||
SELECT id, line_number, start_stop, end_stop, estimated_duration_minutes, description
|
||||
FROM bus_lines
|
||||
ORDER BY line_number ASC, id ASC
|
||||
`);
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
busLines: result.rows.map((row) => ({
|
||||
id: row.id,
|
||||
lineNumber: row.line_number,
|
||||
startStop: row.start_stop,
|
||||
endStop: row.end_stop,
|
||||
estimatedDurationMinutes: row.estimated_duration_minutes,
|
||||
description: row.description,
|
||||
})),
|
||||
},
|
||||
};
|
||||
} 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 createLine(req) {
|
||||
try {
|
||||
const {
|
||||
lineNumber,
|
||||
startStop,
|
||||
endStop,
|
||||
estimatedDurationMinutes,
|
||||
description,
|
||||
} = req.body;
|
||||
|
||||
if (
|
||||
lineNumber === undefined ||
|
||||
!startStop ||
|
||||
!endStop ||
|
||||
estimatedDurationMinutes === undefined
|
||||
) {
|
||||
return { statusCode: 400, body: { error: "missing_required_fields" } };
|
||||
}
|
||||
|
||||
const result = await db.query(
|
||||
`
|
||||
WITH next_id AS (
|
||||
SELECT COALESCE(MAX(id), 0) + 1 AS id FROM bus_lines
|
||||
)
|
||||
INSERT INTO bus_lines (id, line_number, start_stop, end_stop, estimated_duration_minutes, description)
|
||||
SELECT next_id.id, $1, $2, $3, $4, $5
|
||||
FROM next_id
|
||||
RETURNING id, line_number, start_stop, end_stop, estimated_duration_minutes, description
|
||||
`,
|
||||
[
|
||||
lineNumber,
|
||||
startStop,
|
||||
endStop,
|
||||
estimatedDurationMinutes,
|
||||
description || null,
|
||||
],
|
||||
);
|
||||
|
||||
const busLine = result.rows[0];
|
||||
return {
|
||||
statusCode: 201,
|
||||
body: {
|
||||
busLine: {
|
||||
id: busLine.id,
|
||||
lineNumber: busLine.line_number,
|
||||
startStop: busLine.start_stop,
|
||||
endStop: busLine.end_stop,
|
||||
estimatedDurationMinutes: busLine.estimated_duration_minutes,
|
||||
description: busLine.description,
|
||||
},
|
||||
},
|
||||
};
|
||||
} 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 updateLine(req) {
|
||||
try {
|
||||
const { busLineId } = req.params;
|
||||
const {
|
||||
lineNumber,
|
||||
startStop,
|
||||
endStop,
|
||||
estimatedDurationMinutes,
|
||||
description,
|
||||
} = req.body;
|
||||
|
||||
const result = await db.query(
|
||||
`
|
||||
UPDATE bus_lines
|
||||
SET
|
||||
line_number = COALESCE($1, line_number),
|
||||
start_stop = COALESCE($2, start_stop),
|
||||
end_stop = COALESCE($3, end_stop),
|
||||
estimated_duration_minutes = COALESCE($4, estimated_duration_minutes),
|
||||
description = COALESCE($5, description)
|
||||
WHERE id = $6
|
||||
RETURNING id, line_number, start_stop, end_stop, estimated_duration_minutes, description
|
||||
`,
|
||||
[
|
||||
lineNumber ?? null,
|
||||
startStop ?? null,
|
||||
endStop ?? null,
|
||||
estimatedDurationMinutes ?? null,
|
||||
description ?? null,
|
||||
busLineId,
|
||||
],
|
||||
);
|
||||
|
||||
if (!result.rows[0])
|
||||
return { statusCode: 404, body: { error: "bus_line_not_found" } };
|
||||
|
||||
const busLine = result.rows[0];
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
busLine: {
|
||||
id: busLine.id,
|
||||
lineNumber: busLine.line_number,
|
||||
startStop: busLine.start_stop,
|
||||
endStop: busLine.end_stop,
|
||||
estimatedDurationMinutes: busLine.estimated_duration_minutes,
|
||||
description: busLine.description,
|
||||
},
|
||||
},
|
||||
};
|
||||
} 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 listLineStopsDetailed(req) {
|
||||
try {
|
||||
const { busLineId } = req.params;
|
||||
if (!busLineId || !Number.isFinite(Number(busLineId))) {
|
||||
return { statusCode: 400, body: { error: "invalid_bus_line_id" } };
|
||||
}
|
||||
|
||||
const result = await db.query(
|
||||
`
|
||||
SELECT
|
||||
rs.id,
|
||||
rs.bus_line_id,
|
||||
rs.stop_id,
|
||||
rs.stop_order,
|
||||
rs.estimated_arrival_minutes,
|
||||
s.name as stop_name,
|
||||
s.latitude,
|
||||
s.longitude
|
||||
FROM route_stops rs
|
||||
JOIN stops s ON s.id = rs.stop_id
|
||||
WHERE rs.bus_line_id = $1
|
||||
ORDER BY rs.stop_order ASC
|
||||
`,
|
||||
[busLineId],
|
||||
);
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
routeStops: result.rows.map((row) => ({
|
||||
id: row.id,
|
||||
busLineId: row.bus_line_id,
|
||||
stopId: row.stop_id,
|
||||
stopOrder: row.stop_order,
|
||||
estimatedArrivalMinutes: row.estimated_arrival_minutes,
|
||||
name: row.stop_name,
|
||||
latitude: parseFloat(row.latitude),
|
||||
longitude: parseFloat(row.longitude),
|
||||
})),
|
||||
},
|
||||
};
|
||||
} 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 addLineStop(req) {
|
||||
try {
|
||||
const { busLineId } = req.params;
|
||||
const { stopId, stopOrder, estimatedArrivalMinutes } = req.body;
|
||||
|
||||
if (!stopId || stopOrder === undefined) {
|
||||
return { statusCode: 400, body: { error: "missing_required_fields" } };
|
||||
}
|
||||
|
||||
const result = await db.query(
|
||||
`
|
||||
WITH next_id AS (
|
||||
SELECT COALESCE(MAX(id), 0) + 1 AS id FROM route_stops
|
||||
)
|
||||
INSERT INTO route_stops (id, bus_line_id, stop_id, stop_order, estimated_arrival_minutes)
|
||||
SELECT next_id.id, $1, $2, $3, $4
|
||||
FROM next_id
|
||||
ON CONFLICT (bus_line_id, stop_order) DO UPDATE SET
|
||||
stop_id = EXCLUDED.stop_id,
|
||||
estimated_arrival_minutes = EXCLUDED.estimated_arrival_minutes,
|
||||
updated_at = NOW()
|
||||
RETURNING id, bus_line_id, stop_id, stop_order, estimated_arrival_minutes, created_at, updated_at
|
||||
`,
|
||||
[busLineId, stopId, stopOrder, estimatedArrivalMinutes ?? null],
|
||||
);
|
||||
|
||||
const link = result.rows[0];
|
||||
return {
|
||||
statusCode: 201,
|
||||
body: {
|
||||
routeStop: {
|
||||
id: link.id,
|
||||
busLineId: link.bus_line_id,
|
||||
stopId: link.stop_id,
|
||||
stopOrder: link.stop_order,
|
||||
estimatedArrivalMinutes: link.estimated_arrival_minutes,
|
||||
createdAt: link.created_at,
|
||||
updatedAt: link.updated_at,
|
||||
},
|
||||
},
|
||||
};
|
||||
} 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 reorderLineStops(req) {
|
||||
const client = await db.pool.connect();
|
||||
try {
|
||||
const { busLineId } = req.params;
|
||||
const { routeStopIds } = req.body || {};
|
||||
|
||||
if (!Array.isArray(routeStopIds) || routeStopIds.length === 0) {
|
||||
return { statusCode: 400, body: { error: "routeStopIds array required" } };
|
||||
}
|
||||
|
||||
const normalizedIds = routeStopIds
|
||||
.map((id) => Number(id))
|
||||
.filter((id) => Number.isFinite(id));
|
||||
if (normalizedIds.length !== routeStopIds.length) {
|
||||
return { statusCode: 400, body: { error: "routeStopIds must contain only numeric ids" } };
|
||||
}
|
||||
|
||||
const current = await client.query(
|
||||
`SELECT id FROM route_stops WHERE bus_line_id = $1 ORDER BY stop_order ASC`,
|
||||
[busLineId],
|
||||
);
|
||||
|
||||
const currentIds = current.rows.map((row) => Number(row.id));
|
||||
const sameCount = currentIds.length === normalizedIds.length;
|
||||
const sameIds =
|
||||
sameCount && normalizedIds.every((id) => currentIds.includes(id));
|
||||
|
||||
if (!sameIds) {
|
||||
return { statusCode: 400, body: { error: "routeStopIds must match current bus line stops" } };
|
||||
}
|
||||
|
||||
await client.query("BEGIN");
|
||||
|
||||
for (let index = 0; index < normalizedIds.length; index += 1) {
|
||||
await client.query(
|
||||
`UPDATE route_stops
|
||||
SET stop_order = $1, updated_at = NOW()
|
||||
WHERE id = $2 AND bus_line_id = $3`,
|
||||
[-(index + 1), normalizedIds[index], busLineId],
|
||||
);
|
||||
}
|
||||
|
||||
for (let index = 0; index < normalizedIds.length; index += 1) {
|
||||
await client.query(
|
||||
`UPDATE route_stops
|
||||
SET stop_order = $1, updated_at = NOW()
|
||||
WHERE id = $2 AND bus_line_id = $3`,
|
||||
[index + 1, normalizedIds[index], busLineId],
|
||||
);
|
||||
}
|
||||
|
||||
const result = await client.query(
|
||||
`SELECT
|
||||
rs.id,
|
||||
rs.stop_order,
|
||||
s.name as stop_name,
|
||||
s.latitude,
|
||||
s.longitude,
|
||||
rs.estimated_arrival_minutes
|
||||
FROM route_stops rs
|
||||
JOIN stops s ON s.id = rs.stop_id
|
||||
WHERE rs.bus_line_id = $1
|
||||
ORDER BY rs.stop_order ASC`,
|
||||
[busLineId],
|
||||
);
|
||||
|
||||
await client.query("COMMIT");
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
routeStops: result.rows.map((row) => ({
|
||||
id: row.id,
|
||||
order: row.stop_order,
|
||||
name: row.stop_name,
|
||||
latitude: parseFloat(row.latitude),
|
||||
longitude: parseFloat(row.longitude),
|
||||
estimatedArrivalMinutes: row.estimated_arrival_minutes,
|
||||
})),
|
||||
},
|
||||
};
|
||||
} catch (err) {
|
||||
await client.query("ROLLBACK").catch(() => {});
|
||||
console.error(err);
|
||||
reportError({ partofservice: `${req.method} ${req.originalUrl}`, error: err.stack || err.message });
|
||||
return { statusCode: 500, body: { error: "internal_error", details: err.message } };
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteLine(req) {
|
||||
const client = await db.pool.connect();
|
||||
try {
|
||||
const { busLineId } = req.params;
|
||||
|
||||
if (!busLineId || !Number.isFinite(Number(busLineId))) {
|
||||
return { statusCode: 400, body: { error: "invalid_bus_line_id" } };
|
||||
}
|
||||
|
||||
await client.query("BEGIN");
|
||||
|
||||
// Delete associated route_stops first
|
||||
await client.query(
|
||||
`DELETE FROM route_stops WHERE bus_line_id = $1`,
|
||||
[busLineId],
|
||||
);
|
||||
|
||||
// Delete the bus line
|
||||
const result = await client.query(
|
||||
`DELETE FROM bus_lines WHERE id = $1 RETURNING id, line_number`,
|
||||
[busLineId],
|
||||
);
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
await client.query("ROLLBACK");
|
||||
return { statusCode: 404, body: { error: "bus_line_not_found" } };
|
||||
}
|
||||
|
||||
await client.query("COMMIT");
|
||||
return { statusCode: 200, body: { success: true, busLine: result.rows[0] } };
|
||||
} catch (err) {
|
||||
await client.query("ROLLBACK").catch(() => {});
|
||||
console.error(err);
|
||||
reportError({ partofservice: `${req.method} ${req.originalUrl}`, error: err.stack || err.message });
|
||||
return { statusCode: 500, body: { error: "internal_error", details: err.message } };
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteLineStop(req) {
|
||||
const client = await db.pool.connect();
|
||||
try {
|
||||
const { busLineId, routeStopId } = req.params;
|
||||
|
||||
if (!busLineId || !Number.isFinite(Number(busLineId))) {
|
||||
return { statusCode: 400, body: { error: "invalid_bus_line_id" } };
|
||||
}
|
||||
|
||||
if (!routeStopId || !Number.isFinite(Number(routeStopId))) {
|
||||
return { statusCode: 400, body: { error: "invalid_route_stop_id" } };
|
||||
}
|
||||
|
||||
await client.query("BEGIN");
|
||||
|
||||
// Find and delete the route_stop
|
||||
const result = await client.query(
|
||||
`DELETE FROM route_stops
|
||||
WHERE id = $1 AND bus_line_id = $2
|
||||
RETURNING id, stop_order`,
|
||||
[routeStopId, busLineId],
|
||||
);
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
await client.query("ROLLBACK");
|
||||
return { statusCode: 404, body: { error: "route_stop_not_found" } };
|
||||
}
|
||||
|
||||
const deletedOrder = result.rows[0].stop_order;
|
||||
|
||||
// Re-order remaining stops
|
||||
await client.query(
|
||||
`UPDATE route_stops
|
||||
SET stop_order = stop_order - 1, updated_at = NOW()
|
||||
WHERE bus_line_id = $1 AND stop_order > $2`,
|
||||
[busLineId, deletedOrder],
|
||||
);
|
||||
|
||||
await client.query("COMMIT");
|
||||
return { statusCode: 200, body: { success: true, routeStop: result.rows[0] } };
|
||||
} catch (err) {
|
||||
await client.query("ROLLBACK").catch(() => {});
|
||||
console.error(err);
|
||||
reportError({ partofservice: `${req.method} ${req.originalUrl}`, error: err.stack || err.message });
|
||||
return { statusCode: 500, body: { error: "internal_error", details: err.message } };
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
listLineStops,
|
||||
listLines,
|
||||
createLine,
|
||||
updateLine,
|
||||
listLineStopsDetailed,
|
||||
addLineStop,
|
||||
reorderLineStops,
|
||||
deleteLine,
|
||||
deleteLineStop,
|
||||
};
|
||||
@@ -0,0 +1,308 @@
|
||||
const { store } = require("../mock/store");
|
||||
const { reportError } = require("../lib/report-error");
|
||||
|
||||
// Bus-line repository methods (DEV_MOCK mode). Each mirrors the original
|
||||
// handler's `if (DEV_MOCK) { ... }` branch verbatim and returns
|
||||
// { statusCode, body }. The original reorder/delete handlers acquired a
|
||||
// db.pool.connect() before the mock branch; the mock implementation below
|
||||
// intentionally skips that so mock-only deletion/reorder work without a
|
||||
// database (documented improvement).
|
||||
|
||||
async function listLineStops(req) {
|
||||
try {
|
||||
const { busLineId } = req.params;
|
||||
const stops = store.routeStops
|
||||
.filter((rs) => String(rs.busLineId) === String(busLineId))
|
||||
.sort((a, b) => a.stopOrder - b.stopOrder)
|
||||
.map((rs) => ({
|
||||
id: rs.id,
|
||||
order: rs.stopOrder,
|
||||
name: rs.name,
|
||||
latitude: rs.latitude,
|
||||
longitude: rs.longitude,
|
||||
estimatedArrivalMinutes: rs.estimatedArrivalMinutes,
|
||||
}));
|
||||
return { statusCode: 200, body: { stops } };
|
||||
} 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 listLines(req) {
|
||||
try {
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
busLines: store.busLines
|
||||
.slice()
|
||||
.sort((a, b) => a.lineNumber - b.lineNumber),
|
||||
},
|
||||
};
|
||||
} 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 createLine(req) {
|
||||
try {
|
||||
const {
|
||||
lineNumber,
|
||||
startStop,
|
||||
endStop,
|
||||
estimatedDurationMinutes,
|
||||
description,
|
||||
} = req.body;
|
||||
|
||||
if (
|
||||
lineNumber === undefined ||
|
||||
!startStop ||
|
||||
!endStop ||
|
||||
estimatedDurationMinutes === undefined
|
||||
) {
|
||||
return { statusCode: 400, body: { error: "missing_required_fields" } };
|
||||
}
|
||||
|
||||
const busLine = {
|
||||
id: store.nextBusLineId++,
|
||||
lineNumber: Number(lineNumber),
|
||||
startStop,
|
||||
endStop,
|
||||
estimatedDurationMinutes: Number(estimatedDurationMinutes),
|
||||
description: description || "",
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
};
|
||||
store.busLines.push(busLine);
|
||||
return { statusCode: 201, body: { busLine } };
|
||||
} 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 updateLine(req) {
|
||||
try {
|
||||
const { busLineId } = req.params;
|
||||
const {
|
||||
lineNumber,
|
||||
startStop,
|
||||
endStop,
|
||||
estimatedDurationMinutes,
|
||||
description,
|
||||
} = req.body;
|
||||
|
||||
const existing = store.busLines.find(
|
||||
(line) => String(line.id) === String(busLineId),
|
||||
);
|
||||
if (!existing)
|
||||
return { statusCode: 404, body: { error: "bus_line_not_found" } };
|
||||
if (lineNumber !== undefined) existing.lineNumber = Number(lineNumber);
|
||||
if (startStop !== undefined) existing.startStop = startStop;
|
||||
if (endStop !== undefined) existing.endStop = endStop;
|
||||
if (estimatedDurationMinutes !== undefined)
|
||||
existing.estimatedDurationMinutes = Number(estimatedDurationMinutes);
|
||||
if (description !== undefined) existing.description = description;
|
||||
existing.updatedAt = new Date().toISOString();
|
||||
return { statusCode: 200, body: { busLine: existing } };
|
||||
} 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 listLineStopsDetailed(req) {
|
||||
try {
|
||||
const { busLineId } = req.params;
|
||||
if (!busLineId || !Number.isFinite(Number(busLineId))) {
|
||||
return { statusCode: 400, body: { error: "invalid_bus_line_id" } };
|
||||
}
|
||||
|
||||
const stops = store.routeStops
|
||||
.filter((rs) => String(rs.busLineId) === String(busLineId))
|
||||
.sort((a, b) => a.stopOrder - b.stopOrder)
|
||||
.map((rs) => ({
|
||||
id: rs.id,
|
||||
busLineId: rs.busLineId,
|
||||
stopId: rs.stopId,
|
||||
stopOrder: rs.stopOrder,
|
||||
estimatedArrivalMinutes: rs.estimatedArrivalMinutes,
|
||||
name: rs.name,
|
||||
latitude: rs.latitude,
|
||||
longitude: rs.longitude,
|
||||
}));
|
||||
return { statusCode: 200, body: { routeStops: stops } };
|
||||
} 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 addLineStop(req) {
|
||||
try {
|
||||
const { busLineId } = req.params;
|
||||
const { stopId, stopOrder, estimatedArrivalMinutes } = req.body;
|
||||
|
||||
if (!stopId || stopOrder === undefined) {
|
||||
return { statusCode: 400, body: { error: "missing_required_fields" } };
|
||||
}
|
||||
|
||||
const stop = store.stops.find((s) => String(s.id) === String(stopId));
|
||||
const routeStop = {
|
||||
id: store.nextRouteStopId++,
|
||||
busLineId: Number(busLineId),
|
||||
stopId: Number(stopId),
|
||||
stopOrder: Number(stopOrder),
|
||||
estimatedArrivalMinutes: estimatedArrivalMinutes ?? null,
|
||||
createdAt: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
name: stop ? stop.name : `stop-${stopId}`,
|
||||
latitude: stop ? stop.latitude : 0,
|
||||
longitude: stop ? stop.longitude : 0,
|
||||
};
|
||||
// remove existing with same busLineId & stopOrder
|
||||
store.routeStops = store.routeStops.filter(
|
||||
(rs) =>
|
||||
!(
|
||||
rs.busLineId === routeStop.busLineId &&
|
||||
rs.stopOrder === routeStop.stopOrder
|
||||
),
|
||||
);
|
||||
store.routeStops.push(routeStop);
|
||||
return { statusCode: 201, body: { routeStop } };
|
||||
} 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 reorderLineStops(req) {
|
||||
try {
|
||||
const { busLineId } = req.params;
|
||||
const { routeStopIds } = req.body || {};
|
||||
|
||||
if (!Array.isArray(routeStopIds) || routeStopIds.length === 0) {
|
||||
return { statusCode: 400, body: { error: "routeStopIds array required" } };
|
||||
}
|
||||
|
||||
const normalizedIds = routeStopIds
|
||||
.map((id) => Number(id))
|
||||
.filter((id) => Number.isFinite(id));
|
||||
if (normalizedIds.length !== routeStopIds.length) {
|
||||
return { statusCode: 400, body: { error: "routeStopIds must contain only numeric ids" } };
|
||||
}
|
||||
|
||||
const busLineStops = store.routeStops
|
||||
.filter((rs) => String(rs.busLineId) === String(busLineId))
|
||||
.sort((a, b) => a.stopOrder - b.stopOrder);
|
||||
|
||||
const idSet = new Set(busLineStops.map((rs) => Number(rs.id)));
|
||||
const sameCount = busLineStops.length === normalizedIds.length;
|
||||
const sameIds = sameCount && normalizedIds.every((id) => idSet.has(id));
|
||||
if (!sameIds) {
|
||||
return { statusCode: 400, body: { error: "routeStopIds must match current bus line stops" } };
|
||||
}
|
||||
|
||||
const updated = normalizedIds.map((routeStopId, index) => {
|
||||
const stop = store.routeStops.find(
|
||||
(rs) =>
|
||||
Number(rs.id) === routeStopId &&
|
||||
String(rs.busLineId) === String(busLineId),
|
||||
);
|
||||
if (!stop) return null;
|
||||
stop.stopOrder = index + 1;
|
||||
stop.updatedAt = new Date().toISOString();
|
||||
return stop;
|
||||
});
|
||||
|
||||
if (updated.some((item) => !item)) {
|
||||
return { statusCode: 400, body: { error: "routeStopIds must match current bus line stops" } };
|
||||
}
|
||||
|
||||
store.routeStops = store.routeStops.map((routeStop) => {
|
||||
const reordered = updated.find((item) => item.id === routeStop.id);
|
||||
return reordered || routeStop;
|
||||
});
|
||||
|
||||
return { statusCode: 200, body: { routeStops: updated } };
|
||||
} 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 deleteLine(req) {
|
||||
try {
|
||||
const { busLineId } = req.params;
|
||||
|
||||
if (!busLineId || !Number.isFinite(Number(busLineId))) {
|
||||
return { statusCode: 400, body: { error: "invalid_bus_line_id" } };
|
||||
}
|
||||
|
||||
const lineIndex = store.busLines.findIndex(
|
||||
(bl) => Number(bl.id) === Number(busLineId),
|
||||
);
|
||||
if (lineIndex === -1) {
|
||||
return { statusCode: 404, body: { error: "bus_line_not_found" } };
|
||||
}
|
||||
const deletedLine = store.busLines.splice(lineIndex, 1)[0];
|
||||
// Also delete route_stops for this line
|
||||
store.routeStops = store.routeStops.filter(
|
||||
(rs) => String(rs.busLineId) !== String(busLineId),
|
||||
);
|
||||
return { statusCode: 200, body: { success: true, busLine: deletedLine } };
|
||||
} 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 deleteLineStop(req) {
|
||||
try {
|
||||
const { busLineId, routeStopId } = req.params;
|
||||
|
||||
if (!busLineId || !Number.isFinite(Number(busLineId))) {
|
||||
return { statusCode: 400, body: { error: "invalid_bus_line_id" } };
|
||||
}
|
||||
|
||||
if (!routeStopId || !Number.isFinite(Number(routeStopId))) {
|
||||
return { statusCode: 400, body: { error: "invalid_route_stop_id" } };
|
||||
}
|
||||
|
||||
const index = store.routeStops.findIndex(
|
||||
(rs) =>
|
||||
Number(rs.id) === Number(routeStopId) &&
|
||||
String(rs.busLineId) === String(busLineId),
|
||||
);
|
||||
if (index === -1) {
|
||||
return { statusCode: 404, body: { error: "route_stop_not_found" } };
|
||||
}
|
||||
const deletedRouteStop = store.routeStops.splice(index, 1)[0];
|
||||
return { statusCode: 200, body: { success: true, routeStop: deletedRouteStop } };
|
||||
} 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 = {
|
||||
listLineStops,
|
||||
listLines,
|
||||
createLine,
|
||||
updateLine,
|
||||
listLineStopsDetailed,
|
||||
addLineStop,
|
||||
reorderLineStops,
|
||||
deleteLine,
|
||||
deleteLineStop,
|
||||
};
|
||||
@@ -0,0 +1,67 @@
|
||||
const db = require("../db");
|
||||
const { reportError } = require("../lib/report-error");
|
||||
|
||||
// Navigation repository methods (PostgreSQL mode; no DEV_MOCK branch).
|
||||
|
||||
async function saveNavigationRoute(req) {
|
||||
try {
|
||||
const {
|
||||
routeId,
|
||||
startLat,
|
||||
startLon,
|
||||
endLat,
|
||||
endLon,
|
||||
totalDistance,
|
||||
totalDuration,
|
||||
geojson,
|
||||
} = req.body;
|
||||
|
||||
if (
|
||||
!routeId ||
|
||||
!startLat ||
|
||||
!startLon ||
|
||||
!endLat ||
|
||||
!endLon ||
|
||||
!totalDistance ||
|
||||
!totalDuration
|
||||
) {
|
||||
return { statusCode: 400, body: { error: "missing_required_fields" } };
|
||||
}
|
||||
|
||||
const result = await db.query(
|
||||
`
|
||||
INSERT INTO navigation_routes
|
||||
(route_id, start_lat, start_lon, end_lat, end_lon,
|
||||
total_distance_meters, total_duration_seconds, polyline_geojson)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
|
||||
ON CONFLICT (route_id) DO UPDATE SET
|
||||
start_lat = $2,
|
||||
start_lon = $3,
|
||||
end_lat = $4,
|
||||
end_lon = $5,
|
||||
total_distance_meters = $6,
|
||||
total_duration_seconds = $7,
|
||||
polyline_geojson = $8
|
||||
RETURNING *
|
||||
`,
|
||||
[
|
||||
routeId,
|
||||
startLat,
|
||||
startLon,
|
||||
endLat,
|
||||
endLon,
|
||||
totalDistance,
|
||||
totalDuration,
|
||||
JSON.stringify(geojson),
|
||||
],
|
||||
);
|
||||
|
||||
return { statusCode: 200, body: { route: result.rows[0] } };
|
||||
} 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 = { saveNavigationRoute };
|
||||
@@ -0,0 +1,109 @@
|
||||
const db = require("../db");
|
||||
const { reportError } = require("../lib/report-error");
|
||||
|
||||
// Per-user driving route repository methods (PostgreSQL mode; no DEV_MOCK
|
||||
// branch in the original handlers, so this is used in both modes).
|
||||
|
||||
async function getRoute(req) {
|
||||
try {
|
||||
const { routeId } = req.params;
|
||||
const userId = req.user.userId;
|
||||
|
||||
const result = await db.query(
|
||||
`
|
||||
SELECT
|
||||
r.id,
|
||||
r.start_time,
|
||||
r.end_time,
|
||||
r.status,
|
||||
bl.id as line_id,
|
||||
bl.line_number,
|
||||
bl.start_stop,
|
||||
bl.end_stop,
|
||||
bl.estimated_duration_minutes,
|
||||
bl.description,
|
||||
bt.id as bus_type_id,
|
||||
bt.name as bus_type,
|
||||
bt.seat_capacity,
|
||||
bt.license_plate
|
||||
FROM routes r
|
||||
JOIN bus_lines bl ON r.bus_line_id = bl.id
|
||||
JOIN bus_types bt ON r.bus_type_id = bt.id
|
||||
WHERE r.id = $1 AND r.user_id = $2
|
||||
`,
|
||||
[routeId, userId],
|
||||
);
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
return { statusCode: 404, body: { error: "route_not_found" } };
|
||||
}
|
||||
|
||||
const row = result.rows[0];
|
||||
const route = {
|
||||
id: row.id,
|
||||
startTime: row.start_time,
|
||||
endTime: row.end_time,
|
||||
status: row.status,
|
||||
busLine: {
|
||||
id: row.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,
|
||||
},
|
||||
};
|
||||
|
||||
return { statusCode: 200, body: { route } };
|
||||
} 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 updateRouteStatus(req) {
|
||||
try {
|
||||
const { routeId } = req.params;
|
||||
const { status } = req.body;
|
||||
const userId = req.user.userId;
|
||||
|
||||
const validStatuses = [
|
||||
"scheduled",
|
||||
"in_progress",
|
||||
"completed",
|
||||
"cancelled",
|
||||
];
|
||||
if (!validStatuses.includes(status)) {
|
||||
return { statusCode: 400, body: { error: "invalid_status" } };
|
||||
}
|
||||
|
||||
const result = await db.query(
|
||||
`
|
||||
UPDATE routes
|
||||
SET status = $1, updated_at = NOW()
|
||||
WHERE id = $2 AND user_id = $3
|
||||
RETURNING *
|
||||
`,
|
||||
[status, routeId, userId],
|
||||
);
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
return { statusCode: 404, body: { error: "route_not_found" } };
|
||||
}
|
||||
|
||||
return { statusCode: 200, body: { route: result.rows[0] } };
|
||||
} 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 = { getRoute, updateRouteStatus };
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,545 @@
|
||||
const { store: _mock } = require("../mock/store");
|
||||
const { toDateKey, expandRecurringScheduleRows, scheduleMatchesFilters, parseScheduleFilters, mapRecurringOccurrenceRows, materializeScheduleOverviewRow } = require("../lib/schedule");
|
||||
const { reportError } = require("../lib/report-error");
|
||||
|
||||
// Schedules repository methods (DEV_MOCK mode). Exact copies of the original
|
||||
// handlers’ mock branches, returning { statusCode, body }. Note: the markStopPassed
|
||||
// mock branch reproduces the original ReferenceError (it reads an undeclared
|
||||
// `delayMinutes`), so it always returns 500 in mock mode, exactly as before.
|
||||
|
||||
async function listSchedules(req) {
|
||||
try {
|
||||
const filters = parseScheduleFilters(req.query, {
|
||||
statuses: ["planned", "active"],
|
||||
});
|
||||
|
||||
const routeStopsByLine = new Map();
|
||||
for (const routeStop of _mock.routeStops || []) {
|
||||
const busLineId = Number(routeStop.busLineId);
|
||||
if (!routeStopsByLine.has(busLineId)) {
|
||||
routeStopsByLine.set(busLineId, []);
|
||||
}
|
||||
routeStopsByLine.get(busLineId).push({
|
||||
stop_id: routeStop.stopId,
|
||||
stop_order: routeStop.stopOrder,
|
||||
});
|
||||
}
|
||||
|
||||
const schedules = expandRecurringScheduleRows(
|
||||
(_mock.schedules || []).map((s) => {
|
||||
const busLine = _mock.busLines.find((bl) => bl.id === s.busLineId);
|
||||
const bus = _mock.fleetBuses.find((b) => b.id === s.busId);
|
||||
return {
|
||||
id: s.id,
|
||||
busLineId: s.busLineId,
|
||||
busId: s.busId,
|
||||
startTime: s.startTime,
|
||||
endTime: s.endTime,
|
||||
weekdays: s.weekdays || [],
|
||||
status: s.status,
|
||||
lineNumber: busLine?.lineNumber || 0,
|
||||
busName: bus?.name || "Unknown Bus",
|
||||
driverId: null,
|
||||
estimatedDurationMinutes: busLine?.estimatedDurationMinutes || 60,
|
||||
};
|
||||
}),
|
||||
).filter((row) => scheduleMatchesFilters(row, filters, routeStopsByLine));
|
||||
|
||||
return { statusCode: 200, body: { 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 getSchedulesOverview(req) {
|
||||
try {
|
||||
const filters = parseScheduleFilters(req.query, {
|
||||
statuses: ["planned", "active"],
|
||||
});
|
||||
const overviewDateKey = filters.date || null;
|
||||
|
||||
const routeStopsByLine = new Map();
|
||||
|
||||
for (const routeStop of _mock.routeStops || []) {
|
||||
const busLineId = Number(routeStop.busLineId);
|
||||
if (!routeStopsByLine.has(busLineId)) {
|
||||
routeStopsByLine.set(busLineId, []);
|
||||
}
|
||||
|
||||
routeStopsByLine.get(busLineId).push({
|
||||
bus_line_id: busLineId,
|
||||
stop_id: routeStop.stopId,
|
||||
stop_order: routeStop.stopOrder,
|
||||
estimated_arrival_minutes: routeStop.estimatedArrivalMinutes,
|
||||
stop_name: routeStop.name || "",
|
||||
});
|
||||
}
|
||||
|
||||
const baseRows = (_mock.schedules || []).map((schedule) => {
|
||||
const busLine = _mock.busLines.find(
|
||||
(line) => Number(line.id) === Number(schedule.busLineId),
|
||||
);
|
||||
const bus = _mock.fleetBuses.find(
|
||||
(vehicle) => Number(vehicle.id) === Number(schedule.busId),
|
||||
);
|
||||
const driver = _mock.drivers.find(
|
||||
(person) => Number(person.id) === Number(schedule.driverId),
|
||||
);
|
||||
|
||||
return {
|
||||
id: schedule.id,
|
||||
busLineId: schedule.busLineId,
|
||||
busId: schedule.busId,
|
||||
driverId: schedule.driverId ?? null,
|
||||
driverName: driver ? driver.name : null,
|
||||
startTime: schedule.startTime,
|
||||
endTime: schedule.endTime,
|
||||
weekdays: schedule.weekdays || [],
|
||||
status: schedule.status,
|
||||
lineNumber: busLine?.lineNumber || 0,
|
||||
busName: bus?.name || "Unknown Bus",
|
||||
licensePlate: bus?.licensePlate || "",
|
||||
createdAt: schedule.createdAt,
|
||||
};
|
||||
});
|
||||
|
||||
const schedules = mapRecurringOccurrenceRows(
|
||||
baseRows,
|
||||
overviewDateKey,
|
||||
routeStopsByLine,
|
||||
).filter((row) => scheduleMatchesFilters(row, filters, routeStopsByLine));
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
date: overviewDateKey,
|
||||
summary: {
|
||||
total: schedules.length,
|
||||
recurring: schedules.filter(
|
||||
(schedule) => schedule.scheduleType === "recurring",
|
||||
).length,
|
||||
oneTime: schedules.filter(
|
||||
(schedule) => schedule.scheduleType === "one_time",
|
||||
).length,
|
||||
},
|
||||
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 listRecurringSchedules(req) {
|
||||
try {
|
||||
const filters = parseScheduleFilters(req.query);
|
||||
|
||||
const routeStopsByLine = new Map();
|
||||
|
||||
for (const routeStop of _mock.routeStops || []) {
|
||||
const busLineId = Number(routeStop.busLineId);
|
||||
if (!routeStopsByLine.has(busLineId)) {
|
||||
routeStopsByLine.set(busLineId, []);
|
||||
}
|
||||
|
||||
routeStopsByLine.get(busLineId).push({
|
||||
bus_line_id: busLineId,
|
||||
stop_id: routeStop.stopId,
|
||||
stop_order: routeStop.stopOrder,
|
||||
estimated_arrival_minutes: routeStop.estimatedArrivalMinutes,
|
||||
stop_name: routeStop.name || "",
|
||||
});
|
||||
}
|
||||
|
||||
const baseRows = (_mock.schedules || [])
|
||||
.filter(
|
||||
(schedule) =>
|
||||
Array.isArray(schedule.weekdays) && schedule.weekdays.length > 0,
|
||||
)
|
||||
.map((schedule) => {
|
||||
const busLine = _mock.busLines.find(
|
||||
(line) => Number(line.id) === Number(schedule.busLineId),
|
||||
);
|
||||
const bus = _mock.fleetBuses.find(
|
||||
(vehicle) => Number(vehicle.id) === Number(schedule.busId),
|
||||
);
|
||||
const driver = _mock.drivers.find(
|
||||
(person) => Number(person.id) === Number(schedule.driverId),
|
||||
);
|
||||
|
||||
return {
|
||||
id: schedule.id,
|
||||
busLineId: schedule.busLineId,
|
||||
busId: schedule.busId,
|
||||
driverId: schedule.driverId ?? null,
|
||||
driverName: driver ? driver.name : null,
|
||||
startTime: schedule.startTime,
|
||||
endTime: schedule.endTime,
|
||||
weekdays: schedule.weekdays || [],
|
||||
lineNumber: busLine?.lineNumber || 0,
|
||||
busName: bus?.name || "Unknown Bus",
|
||||
licensePlate: bus?.licensePlate || "",
|
||||
createdAt: schedule.createdAt,
|
||||
};
|
||||
});
|
||||
|
||||
const schedules = baseRows
|
||||
.filter((row) => scheduleMatchesFilters(row, filters, routeStopsByLine))
|
||||
.map((row) => {
|
||||
const materialized = materializeScheduleOverviewRow(
|
||||
row,
|
||||
null,
|
||||
routeStopsByLine,
|
||||
);
|
||||
return {
|
||||
...materialized,
|
||||
status: undefined,
|
||||
};
|
||||
});
|
||||
|
||||
return { statusCode: 200, body: { 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 getScheduleById(req) {
|
||||
try {
|
||||
const { scheduleId } = req.params;
|
||||
|
||||
const schedule = (_mock.schedules || []).find(
|
||||
(s) => Number(s.id) === Number(scheduleId),
|
||||
);
|
||||
if (!schedule) {
|
||||
return { statusCode: 404, body: { error: "schedule_not_found" } };
|
||||
}
|
||||
|
||||
const busLine = _mock.busLines.find((bl) => bl.id === schedule.busLineId);
|
||||
const bus = _mock.fleetBuses.find((b) => b.id === schedule.busId);
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
schedule: {
|
||||
id: schedule.id,
|
||||
busLineId: schedule.busLineId,
|
||||
busId: schedule.busId,
|
||||
startTime: schedule.startTime,
|
||||
endTime: schedule.endTime,
|
||||
weekdays: schedule.weekdays || [],
|
||||
status: schedule.status,
|
||||
lineNumber: busLine?.lineNumber || 0,
|
||||
busName: bus?.name || "Unknown 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 getScheduleStops(req) {
|
||||
try {
|
||||
const { scheduleId } = req.params;
|
||||
|
||||
const schedule = (_mock.schedules || []).find(
|
||||
(s) => Number(s.id) === Number(scheduleId),
|
||||
);
|
||||
if (!schedule) {
|
||||
return { statusCode: 404, body: { error: "schedule_not_found" } };
|
||||
}
|
||||
const routeStops = (_mock.routeStops || [])
|
||||
.filter((rs) => Number(rs.busLineId) === Number(schedule.busLineId))
|
||||
.sort((a, b) => a.stopOrder - b.stopOrder);
|
||||
|
||||
const formattedStops = routeStops.map((rs) => {
|
||||
const stop = (_mock.stops || []).find(
|
||||
(s) => Number(s.id) === Number(rs.stopId),
|
||||
);
|
||||
return {
|
||||
id: rs.id,
|
||||
order: rs.stopOrder,
|
||||
name: stop?.name || rs.name || "Unknown Stop",
|
||||
latitude: stop?.latitude ?? rs.latitude ?? 0,
|
||||
longitude: stop?.longitude ?? rs.longitude ?? 0,
|
||||
estimatedArrivalMinutes: rs.estimatedArrivalMinutes || 0,
|
||||
};
|
||||
});
|
||||
return { statusCode: 200, body: { stops: formattedStops } };
|
||||
} 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 markStopPassed(req) {
|
||||
try {
|
||||
const scheduleId = Number(req.params.scheduleId);
|
||||
const stopOrder = Number(req.params.stopOrder);
|
||||
|
||||
if (
|
||||
!Number.isFinite(scheduleId) ||
|
||||
!Number.isFinite(stopOrder) ||
|
||||
stopOrder < 1
|
||||
) {
|
||||
return {
|
||||
statusCode: 400,
|
||||
body: { error: "invalid_schedule_or_stop_order" },
|
||||
};
|
||||
}
|
||||
|
||||
const actualPassedAt = req.body?.actualPassedAt
|
||||
? new Date(req.body.actualPassedAt)
|
||||
: new Date();
|
||||
if (Number.isNaN(actualPassedAt.getTime())) {
|
||||
return { statusCode: 400, body: { error: "invalid_actual_passed_at" } };
|
||||
}
|
||||
|
||||
const schedule = (_mock.schedules || []).find(
|
||||
(s) => Number(s.id) === scheduleId,
|
||||
);
|
||||
if (!schedule) {
|
||||
return { statusCode: 404, body: { error: "schedule_not_found" } };
|
||||
}
|
||||
|
||||
const routeStop = (_mock.routeStops || []).find(
|
||||
(rs) =>
|
||||
Number(rs.busLineId) === Number(schedule.busLineId) &&
|
||||
Number(rs.stopOrder) === stopOrder,
|
||||
);
|
||||
|
||||
if (!routeStop) {
|
||||
return { statusCode: 404, body: { error: "stop_not_found_for_schedule" } };
|
||||
}
|
||||
|
||||
const stop = (_mock.stops || []).find(
|
||||
(s) => Number(s.id) === Number(routeStop.stopId),
|
||||
);
|
||||
const occurrenceDateKey =
|
||||
req.query.date || req.body.date || toDateKey(schedule.startTime);
|
||||
const templateStart = new Date(schedule.startTime);
|
||||
const scheduledPassedAt = new Date(`${occurrenceDateKey}T00:00:00`);
|
||||
scheduledPassedAt.setHours(
|
||||
templateStart.getHours(),
|
||||
templateStart.getMinutes(),
|
||||
templateStart.getSeconds(),
|
||||
templateStart.getMilliseconds(),
|
||||
);
|
||||
scheduledPassedAt.setMinutes(
|
||||
scheduledPassedAt.getMinutes() +
|
||||
(Number(routeStop.estimatedArrivalMinutes) || 0),
|
||||
);
|
||||
|
||||
const existingIndex = (_mock.scheduleStopProgress || []).findIndex(
|
||||
(p) =>
|
||||
Number(p.scheduleId) === scheduleId &&
|
||||
Number(p.stopOrder) === stopOrder,
|
||||
);
|
||||
|
||||
const progressRow = {
|
||||
id:
|
||||
existingIndex >= 0
|
||||
? _mock.scheduleStopProgress[existingIndex].id
|
||||
: _mock.nextScheduleStopProgressId++,
|
||||
scheduleId,
|
||||
busLineId: Number(schedule.busLineId),
|
||||
stopId: Number(routeStop.stopId),
|
||||
stopOrder,
|
||||
stopName: stop?.name || routeStop.name || "Unknown Stop",
|
||||
estimatedArrivalMinutes:
|
||||
Number(routeStop.estimatedArrivalMinutes) || 0,
|
||||
scheduledPassedAt: scheduledPassedAt.toISOString(),
|
||||
scheduledPassedDate: occurrenceDateKey,
|
||||
actualPassedAt: actualPassedAt.toISOString(),
|
||||
delayMinutes,
|
||||
markedByUserId: Number(req.user?.userId) || null,
|
||||
createdAt: new Date().toISOString(),
|
||||
};
|
||||
|
||||
if (existingIndex >= 0) {
|
||||
_mock.scheduleStopProgress[existingIndex] = progressRow;
|
||||
} else {
|
||||
_mock.scheduleStopProgress.push(progressRow);
|
||||
}
|
||||
|
||||
const totalStops = (_mock.routeStops || []).filter(
|
||||
(rs) => Number(rs.busLineId) === Number(schedule.busLineId),
|
||||
).length;
|
||||
const passedCount = (_mock.scheduleStopProgress || []).filter(
|
||||
(p) =>
|
||||
Number(p.scheduleId) === scheduleId &&
|
||||
p.scheduledPassedDate === occurrenceDateKey,
|
||||
).length;
|
||||
|
||||
if (passedCount >= totalStops) {
|
||||
schedule.status = "completed";
|
||||
schedule.endTime = actualPassedAt.toISOString();
|
||||
} else if (schedule.status === "planned") {
|
||||
schedule.status = "in_progress";
|
||||
}
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
scheduleId,
|
||||
stopOrder,
|
||||
status: schedule.status,
|
||||
delayMinutes,
|
||||
passedStops: passedCount,
|
||||
totalStops,
|
||||
occurrenceDate: occurrenceDateKey,
|
||||
},
|
||||
};
|
||||
} catch (err) {
|
||||
console.error(
|
||||
"POST /driver/schedules/:scheduleId/stops/:stopOrder/passed error:",
|
||||
err,
|
||||
);
|
||||
reportError({ partofservice: "POST /driver/schedules/:scheduleId/stops/:stopOrder/passed", error: err.stack || err.message });
|
||||
return { statusCode: 500, body: { error: "internal_error", details: err.message } };
|
||||
}
|
||||
}
|
||||
|
||||
async function getPublicScheduleProgress(req) {
|
||||
try {
|
||||
const scheduleId = Number(req.params.scheduleId);
|
||||
if (!Number.isFinite(scheduleId)) {
|
||||
return { statusCode: 400, body: { error: "invalid_schedule_id" } };
|
||||
}
|
||||
|
||||
const schedule = (_mock.schedules || []).find(
|
||||
(s) => Number(s.id) === scheduleId,
|
||||
);
|
||||
if (!schedule) {
|
||||
return { statusCode: 404, body: { error: "schedule_not_found" } };
|
||||
}
|
||||
|
||||
const busLine = (_mock.busLines || []).find(
|
||||
(bl) => Number(bl.id) === Number(schedule.busLineId),
|
||||
);
|
||||
const routeStops = (_mock.routeStops || [])
|
||||
.filter((rs) => Number(rs.busLineId) === Number(schedule.busLineId))
|
||||
.sort((a, b) => Number(a.stopOrder) - Number(b.stopOrder));
|
||||
|
||||
const mockProgressDate =
|
||||
req.query.date || req.query.occurrenceDate || null;
|
||||
let progressEvents = (_mock.scheduleStopProgress || []).filter(
|
||||
(p) => Number(p.scheduleId) === scheduleId,
|
||||
);
|
||||
if (mockProgressDate) {
|
||||
progressEvents = progressEvents.filter(
|
||||
(p) => p.scheduledPassedDate === mockProgressDate,
|
||||
);
|
||||
}
|
||||
progressEvents = progressEvents.sort(
|
||||
(a, b) => Number(a.stopOrder) - Number(b.stopOrder),
|
||||
);
|
||||
|
||||
const progressByOrder = new Map(
|
||||
progressEvents.map((p) => [Number(p.stopOrder), p]),
|
||||
);
|
||||
const latestProgress =
|
||||
progressEvents.length > 0
|
||||
? progressEvents[progressEvents.length - 1]
|
||||
: null;
|
||||
const currentDelayMinutes = latestProgress
|
||||
? Number(latestProgress.delayMinutes) || 0
|
||||
: 0;
|
||||
|
||||
const occurrenceDateKey =
|
||||
req.query.date ||
|
||||
req.query.occurrenceDate ||
|
||||
toDateKey(schedule.startTime);
|
||||
const stops = routeStops.map((rs) => {
|
||||
const stop = (_mock.stops || []).find(
|
||||
(s) => Number(s.id) === Number(rs.stopId),
|
||||
);
|
||||
const templateStart = new Date(schedule.startTime);
|
||||
const scheduledTime = new Date(`${occurrenceDateKey}T00:00:00`);
|
||||
scheduledTime.setHours(
|
||||
templateStart.getHours(),
|
||||
templateStart.getMinutes(),
|
||||
templateStart.getSeconds(),
|
||||
templateStart.getMilliseconds(),
|
||||
);
|
||||
scheduledTime.setMinutes(
|
||||
scheduledTime.getMinutes() +
|
||||
(Number(rs.estimatedArrivalMinutes) || 0),
|
||||
);
|
||||
const progress = progressByOrder.get(Number(rs.stopOrder));
|
||||
|
||||
return {
|
||||
stopId: Number(rs.stopId),
|
||||
stopOrder: Number(rs.stopOrder),
|
||||
stopName: stop?.name || rs.name || "Unknown Stop",
|
||||
estimatedArrivalMinutes: Number(rs.estimatedArrivalMinutes) || 0,
|
||||
scheduledPassedAt: scheduledTime.toISOString(),
|
||||
actualPassedAt: progress?.actualPassedAt || null,
|
||||
delayMinutes: progress ? Number(progress.delayMinutes) || 0 : null,
|
||||
status: progress ? "passed" : "pending",
|
||||
};
|
||||
});
|
||||
|
||||
const passedStops = stops.filter((s) => s.status === "passed");
|
||||
const nextStop = stops.find((s) => s.status === "pending") || null;
|
||||
const driverState =
|
||||
passedStops.length === 0
|
||||
? "not_started"
|
||||
: passedStops.length >= stops.length
|
||||
? "completed"
|
||||
: "en_route";
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
schedule: {
|
||||
id: scheduleId,
|
||||
lineNumber: Number(busLine?.lineNumber || 0),
|
||||
startStop: busLine?.startStop || null,
|
||||
endStop: busLine?.endStop || null,
|
||||
startTime: schedule.startTime,
|
||||
endTime: schedule.endTime,
|
||||
status: schedule.status,
|
||||
},
|
||||
driverState,
|
||||
currentDelayMinutes,
|
||||
passedStopCount: passedStops.length,
|
||||
totalStopCount: stops.length,
|
||||
nextStop: nextStop
|
||||
? {
|
||||
...nextStop,
|
||||
predictedPassedAt: new Date(
|
||||
new Date(nextStop.scheduledPassedAt).getTime() +
|
||||
currentDelayMinutes * 60000,
|
||||
).toISOString(),
|
||||
}
|
||||
: null,
|
||||
stops,
|
||||
updatedAt: latestProgress?.actualPassedAt || null,
|
||||
},
|
||||
};
|
||||
} catch (err) {
|
||||
console.error("GET /public/schedules/:scheduleId/progress error:", err);
|
||||
reportError({ partofservice: "GET /public/schedules/:scheduleId/progress", error: err.stack || err.message });
|
||||
return { statusCode: 500, body: { error: "internal_error", details: err.message } };
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
listSchedules,
|
||||
getSchedulesOverview,
|
||||
listRecurringSchedules,
|
||||
getScheduleById,
|
||||
getScheduleStops,
|
||||
markStopPassed,
|
||||
getPublicScheduleProgress,
|
||||
};
|
||||
@@ -0,0 +1,149 @@
|
||||
const db = require("../db");
|
||||
const { reportError } = require("../lib/report-error");
|
||||
|
||||
// Stop repository methods (PostgreSQL mode). Each returns { statusCode, body }
|
||||
// mirroring the original handler's DB branch.
|
||||
|
||||
async function listStops(req) {
|
||||
try {
|
||||
const result = await db.query(`
|
||||
SELECT id, name, latitude, longitude, created_at, updated_at
|
||||
FROM stops
|
||||
ORDER BY id ASC
|
||||
`);
|
||||
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
stops: result.rows.map((row) => ({
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
latitude: parseFloat(row.latitude),
|
||||
longitude: parseFloat(row.longitude),
|
||||
createdAt: row.created_at,
|
||||
updatedAt: row.updated_at,
|
||||
})),
|
||||
},
|
||||
};
|
||||
} 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 result = await db.query(
|
||||
`
|
||||
WITH next_id AS (
|
||||
SELECT COALESCE(MAX(id), 0) + 1 AS id FROM stops
|
||||
)
|
||||
INSERT INTO stops (id, name, latitude, longitude)
|
||||
SELECT next_id.id, $1, $2, $3
|
||||
FROM next_id
|
||||
RETURNING id, name, latitude, longitude, created_at, updated_at
|
||||
`,
|
||||
[name, latitude, longitude],
|
||||
);
|
||||
|
||||
const stop = result.rows[0];
|
||||
return {
|
||||
statusCode: 201,
|
||||
body: {
|
||||
stop: {
|
||||
id: stop.id,
|
||||
name: stop.name,
|
||||
latitude: parseFloat(stop.latitude),
|
||||
longitude: parseFloat(stop.longitude),
|
||||
createdAt: stop.created_at,
|
||||
updatedAt: stop.updated_at,
|
||||
},
|
||||
},
|
||||
};
|
||||
} 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 result = await db.query(
|
||||
`DELETE FROM stops WHERE id = $1 RETURNING id, name`,
|
||||
[stopId],
|
||||
);
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
return { statusCode: 404, body: { error: "stop_not_found" } };
|
||||
}
|
||||
|
||||
return { statusCode: 200, body: { success: true, stop: result.rows[0] } };
|
||||
} 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 result = await db.query(
|
||||
`UPDATE stops
|
||||
SET name = COALESCE($1, name),
|
||||
latitude = COALESCE($2, latitude),
|
||||
longitude = COALESCE($3, longitude),
|
||||
updated_at = NOW()
|
||||
WHERE id = $4
|
||||
RETURNING id, name, latitude, longitude, created_at, updated_at`,
|
||||
[name ?? null, latitude ?? null, longitude ?? null, stopId],
|
||||
);
|
||||
|
||||
if (!result.rows[0])
|
||||
return { statusCode: 404, body: { error: "stop_not_found" } };
|
||||
|
||||
const stop = result.rows[0];
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
stop: {
|
||||
id: stop.id,
|
||||
name: stop.name,
|
||||
latitude: parseFloat(stop.latitude),
|
||||
longitude: parseFloat(stop.longitude),
|
||||
createdAt: stop.created_at,
|
||||
updatedAt: stop.updated_at,
|
||||
},
|
||||
},
|
||||
};
|
||||
} 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 };
|
||||
@@ -0,0 +1,96 @@
|
||||
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 };
|
||||
@@ -0,0 +1,330 @@
|
||||
const db = require("../db");
|
||||
const { reportError } = require("../lib/report-error");
|
||||
|
||||
// Weekly schedule repository methods (PostgreSQL mode; no DEV_MOCK branch in
|
||||
// the original handlers, so this module is used in both modes).
|
||||
|
||||
async function updateWeeklyScheduleStatus(req) {
|
||||
try {
|
||||
const scheduleId = Number(req.params.scheduleId);
|
||||
const { status } = req.body;
|
||||
const validStatuses = [
|
||||
"planned",
|
||||
"in_progress",
|
||||
"completed",
|
||||
"cancelled",
|
||||
"active",
|
||||
];
|
||||
if (!Number.isInteger(scheduleId) || !validStatuses.includes(status)) {
|
||||
return { statusCode: 400, body: { error: "invalid_status" } };
|
||||
}
|
||||
|
||||
const normalizedStatus = status === "active" ? "in_progress" : status;
|
||||
const result = await db.query(
|
||||
`
|
||||
UPDATE schedules s
|
||||
SET status = $1, updated_at = NOW()
|
||||
WHERE s.id = $2
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM shifts sh
|
||||
WHERE sh.schedule_id = s.id AND sh.driver_id = $3
|
||||
)
|
||||
RETURNING s.id, s.status
|
||||
`,
|
||||
[normalizedStatus, scheduleId, req.user.userId],
|
||||
);
|
||||
|
||||
if (result.rows.length === 0)
|
||||
return { statusCode: 404, body: { error: "schedule_not_found" } };
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
schedule: {
|
||||
id: Number(result.rows[0].id),
|
||||
status: result.rows[0].status,
|
||||
},
|
||||
},
|
||||
};
|
||||
} catch (err) {
|
||||
console.error("PATCH /driver/weekly-schedules/:id/status error:", err);
|
||||
reportError({ partofservice: "PATCH /driver/weekly-schedules/:id/status", error: err.stack || err.message });
|
||||
return { statusCode: 500, body: { error: "internal_error" } };
|
||||
}
|
||||
}
|
||||
|
||||
async function listWeeklySchedules(req) {
|
||||
try {
|
||||
const result = await db.query(`
|
||||
SELECT s.id, s.bus_line_id, s.start_time, s.end_time, s.status, s.day_of_week,
|
||||
bl.line_number, bl.start_stop, bl.end_stop,
|
||||
sh.id AS shift_id, sh.driver_id, u.usercode AS driver_name
|
||||
FROM schedules s
|
||||
JOIN bus_lines bl ON bl.id = s.bus_line_id
|
||||
LEFT JOIN shifts sh ON sh.schedule_id = s.id
|
||||
LEFT JOIN workers.users u ON u.id = sh.driver_id
|
||||
ORDER BY s.day_of_week ASC, s.start_time ASC, s.id ASC
|
||||
`);
|
||||
return {
|
||||
statusCode: 200,
|
||||
body: {
|
||||
schedules: result.rows.map((row) => ({
|
||||
id: Number(row.id),
|
||||
busLineId: Number(row.bus_line_id),
|
||||
lineNumber: row.line_number,
|
||||
startStop: row.start_stop,
|
||||
endStop: row.end_stop,
|
||||
startTime: row.start_time,
|
||||
endTime: row.end_time,
|
||||
status: row.status,
|
||||
dayOfWeek: Number(row.day_of_week),
|
||||
shiftId: row.shift_id ? Number(row.shift_id) : null,
|
||||
driverId: row.driver_id ? Number(row.driver_id) : null,
|
||||
driverName: row.driver_name || null,
|
||||
})),
|
||||
},
|
||||
};
|
||||
} catch (err) {
|
||||
console.error("GET /admin/weekly-schedules error:", err);
|
||||
reportError({ partofservice: "GET /admin/weekly-schedules", error: err.stack || err.message });
|
||||
return { statusCode: 500, body: { error: "internal_error" } };
|
||||
}
|
||||
}
|
||||
|
||||
async function createWeeklySchedule(req) {
|
||||
try {
|
||||
const busLineId = Number(req.body?.busLineId);
|
||||
const dayOfWeek = Number(req.body?.dayOfWeek);
|
||||
const startTime = String(req.body?.startTime || "");
|
||||
const endTime = String(req.body?.endTime || "");
|
||||
const driverId = req.body?.driverId ? Number(req.body.driverId) : null;
|
||||
|
||||
if (
|
||||
!Number.isInteger(busLineId) ||
|
||||
!Number.isInteger(dayOfWeek) ||
|
||||
dayOfWeek < 0 ||
|
||||
dayOfWeek > 6 ||
|
||||
!startTime ||
|
||||
!endTime
|
||||
) {
|
||||
return {
|
||||
statusCode: 400,
|
||||
body: {
|
||||
error: "busLineId, dayOfWeek, startTime and endTime are required",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const created = await db.query(
|
||||
`
|
||||
INSERT INTO schedules (bus_line_id, start_time, end_time, status, day_of_week)
|
||||
VALUES ($1, $2::time, $3::time, 'planned', $4)
|
||||
RETURNING id, bus_line_id, start_time, end_time, status, day_of_week
|
||||
`,
|
||||
[busLineId, startTime, endTime, dayOfWeek],
|
||||
);
|
||||
|
||||
const schedule = created.rows[0];
|
||||
if (driverId) {
|
||||
await db.query(
|
||||
`
|
||||
INSERT INTO shifts (schedule_id, driver_id)
|
||||
VALUES ($1, $2)
|
||||
`,
|
||||
[schedule.id, driverId],
|
||||
);
|
||||
}
|
||||
|
||||
return {
|
||||
statusCode: 201,
|
||||
body: {
|
||||
schedule: {
|
||||
id: Number(schedule.id),
|
||||
busLineId: Number(schedule.bus_line_id),
|
||||
startTime: schedule.start_time,
|
||||
endTime: schedule.end_time,
|
||||
status: schedule.status,
|
||||
dayOfWeek: Number(schedule.day_of_week),
|
||||
driverId,
|
||||
},
|
||||
},
|
||||
};
|
||||
} catch (err) {
|
||||
console.error("POST /admin/weekly-schedules error:", err);
|
||||
reportError({ partofservice: "POST /admin/weekly-schedules", error: err.stack || err.message });
|
||||
return { statusCode: 500, body: { error: "internal_error" } };
|
||||
}
|
||||
}
|
||||
|
||||
async function bulkCreateWeeklySchedules(req) {
|
||||
const busLineId = Number(req.body?.busLineId);
|
||||
const dayOfWeeks = Array.isArray(req.body?.dayOfWeeks)
|
||||
? req.body.dayOfWeeks
|
||||
.map(Number)
|
||||
.filter((day) => Number.isInteger(day) && day >= 0 && day <= 6)
|
||||
: [];
|
||||
const startTimes = Array.isArray(req.body?.startTimes)
|
||||
? [...new Set(req.body.startTimes.map(String).filter(Boolean))]
|
||||
: [];
|
||||
const driverId = req.body?.driverId ? Number(req.body.driverId) : null;
|
||||
|
||||
if (
|
||||
!Number.isInteger(busLineId) ||
|
||||
dayOfWeeks.length === 0 ||
|
||||
startTimes.length === 0
|
||||
) {
|
||||
return {
|
||||
statusCode: 400,
|
||||
body: { error: "busLineId, dayOfWeeks and startTimes are required" },
|
||||
};
|
||||
}
|
||||
|
||||
const client = await db.pool.connect();
|
||||
try {
|
||||
await client.query("BEGIN");
|
||||
const line = await client.query(
|
||||
"SELECT estimated_duration_minutes FROM bus_lines WHERE id = $1",
|
||||
[busLineId],
|
||||
);
|
||||
if (line.rows.length === 0) {
|
||||
await client.query("ROLLBACK");
|
||||
return { statusCode: 404, body: { error: "bus_line_not_found" } };
|
||||
}
|
||||
|
||||
const duration = Number(line.rows[0].estimated_duration_minutes) || 60;
|
||||
const created = [];
|
||||
for (const dayOfWeek of dayOfWeeks) {
|
||||
for (const startTime of startTimes) {
|
||||
const result = await client.query(
|
||||
`INSERT INTO schedules
|
||||
(bus_line_id, start_time, end_time, status, day_of_week)
|
||||
VALUES ($1, $2::time, ($2::time + ($3::text || ' minutes')::interval)::time, 'planned', $4)
|
||||
RETURNING id, bus_line_id, start_time, end_time, status, day_of_week`,
|
||||
[busLineId, startTime, duration, dayOfWeek],
|
||||
);
|
||||
const schedule = result.rows[0];
|
||||
if (driverId) {
|
||||
await client.query(
|
||||
"INSERT INTO shifts (schedule_id, driver_id) VALUES ($1, $2)",
|
||||
[schedule.id, driverId],
|
||||
);
|
||||
}
|
||||
created.push(schedule);
|
||||
}
|
||||
}
|
||||
|
||||
await client.query("COMMIT");
|
||||
return {
|
||||
statusCode: 201,
|
||||
body: {
|
||||
createdCount: created.length,
|
||||
schedules: created.map((schedule) => ({
|
||||
id: Number(schedule.id),
|
||||
busLineId: Number(schedule.bus_line_id),
|
||||
startTime: schedule.start_time,
|
||||
endTime: schedule.end_time,
|
||||
status: schedule.status,
|
||||
dayOfWeek: Number(schedule.day_of_week),
|
||||
driverId,
|
||||
})),
|
||||
},
|
||||
};
|
||||
} catch (err) {
|
||||
await client.query("ROLLBACK");
|
||||
console.error("POST /admin/weekly-schedules/bulk error:", err);
|
||||
reportError({ partofservice: "POST /admin/weekly-schedules/bulk", error: err.stack || err.message });
|
||||
return { statusCode: 500, body: { error: "internal_error" } };
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
}
|
||||
|
||||
async function updateWeeklySchedule(req) {
|
||||
try {
|
||||
const scheduleId = Number(req.params.scheduleId);
|
||||
const busLineId = Number(req.body?.busLineId);
|
||||
const dayOfWeek = Number(req.body?.dayOfWeek);
|
||||
const startTime = String(req.body?.startTime || "");
|
||||
const endTime = String(req.body?.endTime || "");
|
||||
if (
|
||||
!Number.isInteger(scheduleId) ||
|
||||
!Number.isInteger(busLineId) ||
|
||||
!Number.isInteger(dayOfWeek) ||
|
||||
dayOfWeek < 0 ||
|
||||
dayOfWeek > 6 ||
|
||||
!startTime ||
|
||||
!endTime
|
||||
) {
|
||||
return { statusCode: 400, body: { error: "invalid_schedule" } };
|
||||
}
|
||||
|
||||
const result = await db.query(
|
||||
`
|
||||
UPDATE schedules
|
||||
SET bus_line_id = $2, start_time = $3::time, end_time = $4::time,
|
||||
day_of_week = $5, updated_at = NOW()
|
||||
WHERE id = $1
|
||||
RETURNING id, bus_line_id, start_time, end_time, status, day_of_week
|
||||
`,
|
||||
[scheduleId, busLineId, startTime, endTime, dayOfWeek],
|
||||
);
|
||||
if (result.rows.length === 0)
|
||||
return { statusCode: 404, body: { error: "schedule_not_found" } };
|
||||
return { statusCode: 200, body: { schedule: result.rows[0] } };
|
||||
} catch (err) {
|
||||
console.error("PUT /admin/weekly-schedules error:", err);
|
||||
reportError({ partofservice: "PUT /admin/weekly-schedules", error: err.stack || err.message });
|
||||
return { statusCode: 500, body: { error: "internal_error" } };
|
||||
}
|
||||
}
|
||||
|
||||
async function updateWeeklyScheduleShift(req) {
|
||||
try {
|
||||
const scheduleId = Number(req.params.scheduleId);
|
||||
const driverId = req.body?.driverId ? Number(req.body.driverId) : null;
|
||||
if (!Number.isInteger(scheduleId))
|
||||
return { statusCode: 400, body: { error: "invalid_schedule_id" } };
|
||||
await db.query("DELETE FROM shifts WHERE schedule_id = $1", [
|
||||
scheduleId,
|
||||
]);
|
||||
if (driverId) {
|
||||
await db.query(
|
||||
"INSERT INTO shifts (schedule_id, driver_id) VALUES ($1, $2)",
|
||||
[scheduleId, driverId],
|
||||
);
|
||||
}
|
||||
return { statusCode: 200, body: { scheduleId, driverId } };
|
||||
} catch (err) {
|
||||
console.error("PUT /admin/weekly-schedules/:id/shift error:", err);
|
||||
reportError({ partofservice: "PUT /admin/weekly-schedules/:id/shift", error: err.stack || err.message });
|
||||
return { statusCode: 500, body: { error: "internal_error" } };
|
||||
}
|
||||
}
|
||||
|
||||
async function deleteWeeklySchedule(req) {
|
||||
try {
|
||||
const scheduleId = Number(req.params.scheduleId);
|
||||
if (!Number.isInteger(scheduleId))
|
||||
return { statusCode: 400, body: { error: "invalid_schedule_id" } };
|
||||
const result = await db.query(
|
||||
"DELETE FROM schedules WHERE id = $1 RETURNING id",
|
||||
[scheduleId],
|
||||
);
|
||||
if (result.rows.length === 0)
|
||||
return { statusCode: 404, body: { error: "schedule_not_found" } };
|
||||
return { statusCode: 200, body: { success: true, scheduleId } };
|
||||
} catch (err) {
|
||||
console.error("DELETE /admin/weekly-schedules error:", err);
|
||||
reportError({ partofservice: "DELETE /admin/weekly-schedules", error: err.stack || err.message });
|
||||
return { statusCode: 500, body: { error: "internal_error" } };
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
updateWeeklyScheduleStatus,
|
||||
listWeeklySchedules,
|
||||
createWeeklySchedule,
|
||||
bulkCreateWeeklySchedules,
|
||||
updateWeeklySchedule,
|
||||
updateWeeklyScheduleShift,
|
||||
deleteWeeklySchedule,
|
||||
};
|
||||
@@ -0,0 +1,76 @@
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
const { authMiddleware, adminMiddleware } = require("../middleware");
|
||||
const repo = require("../repo");
|
||||
|
||||
router.get(
|
||||
"/admin/schedules",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.listAdminSchedules(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.post(
|
||||
"/admin/schedules",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.createAdminSchedule(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.put(
|
||||
"/admin/schedules/:scheduleId",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.updateAdminSchedule(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.post(
|
||||
"/admin/schedules/bulk",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.bulkCreateAdminSchedules(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.delete(
|
||||
"/admin/schedules/:scheduleId",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.deleteAdminSchedule(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.post(
|
||||
"/admin/check-driver-conflicts",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.checkDriverConflicts(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.post(
|
||||
"/admin/assign-schedules",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.assignSchedules(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,16 @@
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
const { authMiddleware } = require("../middleware");
|
||||
const repo = require("../repo");
|
||||
|
||||
router.get("/schedule/daily", authMiddleware, async (req, res) => {
|
||||
const out = await repo.listDailySchedules(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
});
|
||||
|
||||
router.get("/driver/schedules/daily", authMiddleware, async (req, res) => {
|
||||
const out = await repo.listDriverDailySchedules(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,21 @@
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
const { authMiddleware, adminMiddleware } = require("../middleware");
|
||||
const repo = require("../repo");
|
||||
|
||||
router.get("/admin/drivers", authMiddleware, adminMiddleware, async (req, res) => {
|
||||
const out = await repo.listDrivers(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
});
|
||||
|
||||
router.get(
|
||||
"/admin/shift-drivers",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.listShiftDrivers(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,46 @@
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
const { authMiddleware, adminMiddleware } = require("../middleware");
|
||||
const repo = require("../repo");
|
||||
|
||||
router.get(
|
||||
"/admin/fleet/buses",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.listFleetBuses(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.post(
|
||||
"/admin/fleet/buses",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.createFleetBus(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.patch(
|
||||
"/admin/fleet/buses/:busId",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.updateFleetBus(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.delete(
|
||||
"/admin/fleet/buses/:busId",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.deleteFleetBus(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,16 @@
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
const { authMiddleware } = require("../middleware");
|
||||
const repo = require("../repo");
|
||||
|
||||
router.post("/gps/track", authMiddleware, async (req, res) => {
|
||||
const out = await repo.saveGpsTrack(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
});
|
||||
|
||||
router.get("/gps/route/:routeId", authMiddleware, async (req, res) => {
|
||||
const out = await repo.listRouteGpsPoints(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,18 @@
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
const db = require("../db");
|
||||
const { log } = require("../lib/log");
|
||||
const { reportError } = require("../lib/report-error");
|
||||
|
||||
router.get("/", async (_req, res) => {
|
||||
try {
|
||||
await db.query("SELECT 1");
|
||||
res.json({ status: "ok", service: "transit_api", database: "ok" });
|
||||
} catch (err) {
|
||||
log("error", "GET /health error", { error: err.message, stack: err.stack });
|
||||
reportError({ partofservice: "GET /health", error: err.stack || err.message });
|
||||
res.status(503).json({ status: "unavailable", service: "transit_api" });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,91 @@
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
const { authMiddleware, adminMiddleware } = require("../middleware");
|
||||
const repo = require("../repo");
|
||||
|
||||
router.get("/bus-lines/:busLineId/stops", authMiddleware, async (req, res) => {
|
||||
const out = await repo.listLineStops(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
});
|
||||
|
||||
router.get(
|
||||
"/admin/bus-lines",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.listLines(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.post(
|
||||
"/admin/bus-lines",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.createLine(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.patch(
|
||||
"/admin/bus-lines/:busLineId",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.updateLine(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.get(
|
||||
"/admin/bus-lines/:busLineId/stops",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.listLineStopsDetailed(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.post(
|
||||
"/admin/bus-lines/:busLineId/stops",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.addLineStop(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.post(
|
||||
"/admin/bus-lines/:busLineId/stops/reorder",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.reorderLineStops(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.delete(
|
||||
"/admin/bus-lines/:busLineId",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.deleteLine(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.delete(
|
||||
"/admin/bus-lines/:busLineId/stops/:routeStopId",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.deleteLineStop(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,11 @@
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
const { authMiddleware } = require("../middleware");
|
||||
const repo = require("../repo");
|
||||
|
||||
router.post("/navigation/route", authMiddleware, async (req, res) => {
|
||||
const out = await repo.saveNavigationRoute(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,16 @@
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
const { authMiddleware } = require("../middleware");
|
||||
const repo = require("../repo");
|
||||
|
||||
router.get("/routes/:routeId", authMiddleware, async (req, res) => {
|
||||
const out = await repo.getRoute(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
});
|
||||
|
||||
router.patch("/routes/:routeId/status", authMiddleware, async (req, res) => {
|
||||
const out = await repo.updateRouteStatus(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,54 @@
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
const { authMiddleware } = require("../middleware");
|
||||
const repo = require("../repo");
|
||||
|
||||
router.patch(
|
||||
"/driver/schedules/:scheduleId/status",
|
||||
authMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.updateDriverScheduleStatus(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.get("/schedules", async (req, res) => {
|
||||
const out = await repo.listSchedules(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
});
|
||||
|
||||
router.get("/schedules/overview", async (req, res) => {
|
||||
const out = await repo.getSchedulesOverview(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
});
|
||||
|
||||
router.get("/schedules/recurring", async (req, res) => {
|
||||
const out = await repo.listRecurringSchedules(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
});
|
||||
|
||||
router.get("/schedules/:scheduleId", async (req, res) => {
|
||||
const out = await repo.getScheduleById(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
});
|
||||
|
||||
router.get("/schedules/:scheduleId/stops", async (req, res) => {
|
||||
const out = await repo.getScheduleStops(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
});
|
||||
|
||||
router.post(
|
||||
"/driver/schedules/:scheduleId/stops/:stopOrder/passed",
|
||||
authMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.markStopPassed(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.get("/public/schedules/:scheduleId/progress", async (req, res) => {
|
||||
const out = await repo.getPublicScheduleProgress(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,36 @@
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
const { authMiddleware, adminMiddleware } = require("../middleware");
|
||||
const repo = require("../repo");
|
||||
|
||||
router.get("/admin/stops", authMiddleware, adminMiddleware, async (req, res) => {
|
||||
const out = await repo.listStops(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
});
|
||||
|
||||
router.post("/admin/stops", authMiddleware, adminMiddleware, async (req, res) => {
|
||||
const out = await repo.createStop(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
});
|
||||
|
||||
router.delete(
|
||||
"/admin/stops/:stopId",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.deleteStop(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.patch(
|
||||
"/admin/stops/:stopId",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.updateStop(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
module.exports = router;
|
||||
@@ -0,0 +1,75 @@
|
||||
const express = require("express");
|
||||
const router = express.Router();
|
||||
const { authMiddleware, adminMiddleware } = require("../middleware");
|
||||
const repo = require("../repo");
|
||||
|
||||
router.patch(
|
||||
"/driver/weekly-schedules/:scheduleId/status",
|
||||
authMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.updateWeeklyScheduleStatus(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.get(
|
||||
"/admin/weekly-schedules",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.listWeeklySchedules(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.post(
|
||||
"/admin/weekly-schedules",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.createWeeklySchedule(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.post(
|
||||
"/admin/weekly-schedules/bulk",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.bulkCreateWeeklySchedules(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.put(
|
||||
"/admin/weekly-schedules/:scheduleId",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.updateWeeklySchedule(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.put(
|
||||
"/admin/weekly-schedules/:scheduleId/shift",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.updateWeeklyScheduleShift(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
router.delete(
|
||||
"/admin/weekly-schedules/:scheduleId",
|
||||
authMiddleware,
|
||||
adminMiddleware,
|
||||
async (req, res) => {
|
||||
const out = await repo.deleteWeeklySchedule(req);
|
||||
return res.status(out.statusCode).json(out.body);
|
||||
},
|
||||
);
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user