40227-vm/backend/docs/timetable_periods.md
2026-06-10 18:27:19 +02:00

95 lines
4.4 KiB
Markdown

# Timetable Periods Backend
## Purpose
`timetable_periods` is a single scheduled slot within a timetable — a weekday, start/end time,
room, and the class subject taught. It is a generic-CRUD slice assembled from the shared
factories and belongs to one `timetables` row.
## Slice Files (by layer)
- Route: `src/routes/timetable_periods.ts``createCrudRouter(controller, { permission: 'timetable_periods' })`.
- Controller: `src/api/controllers/timetable_periods.controller.ts``createCrudController(service, { csvFields })`.
- Service (BLL): `src/services/timetable_periods.ts``createCrudService(DbApi, { notFoundCode: 'timetable_periodsNotFound' })`.
- Repository (DAL): `src/db/api/timetable_periods.ts` (`Timetable_periodsDBApi`) — entity-specific
`create`/`bulkImport`/`update`/`findBy`/`findAll`; `remove`/`deleteByIds`/`findAllAutocomplete`
delegate to `db/api/shared/repository.ts`.
- Model: `src/db/models/timetable_periods.ts`.
- Shared used: CRUD factories (`services/shared/crud-service.ts`,
`api/controllers/shared/crud-controller.ts`, `api/http/crud-router.ts`), repository helpers
(`db/api/shared/repository.ts`), `shared/constants/pagination.ts` (`resolvePagination`),
`db/utils.ts` (`Utils.uuid`/`Utils.ilike`), `shared/constants/database.ts`
(`BULK_IMPORT_TIMESTAMP_STEP_MS`).
## API
The standard generic-CRUD surface (all under `/api/timetable_periods`, JWT +
`${METHOD}_TIMETABLE_PERIODS` permission, all `200`) — see `backend-architecture.md` for the
shared 9-endpoint contract:
- `POST /` — body `{ data }`, returns `true`.
- `POST /bulk-import` — multipart CSV file, returns `true`.
- `PUT /:id` — body `{ data, id }` (the service reads the id from the **body**), returns `true`.
- `DELETE /:id` — returns `true`.
- `POST /deleteByIds` — body `{ data: string[] }`, returns `true`.
- `GET /` — query filters, returns `{ rows, count }`; `?filetype=csv` streams a CSV of `csvFields`.
- `GET /count` — returns `{ rows: [], count }`.
- `GET /autocomplete``?query&limit&offset`, returns `[{ id, label }]` where `label` is
`room`.
- `GET /:id` — returns the record with eager associations (see Data Contract).
`csvFields`: `id`, `room`, `starts_at`, `ends_at`.
## Access Rules
- JWT required; the whole router is guarded by `checkCrudPermissions('timetable_periods')`,
deriving `READ_TIMETABLE_PERIODS` / `CREATE_TIMETABLE_PERIODS` / `UPDATE_TIMETABLE_PERIODS` /
`DELETE_TIMETABLE_PERIODS` per HTTP method.
- Access is granted by role permission or per-user `custom_permissions` (see `permissions.md`).
## Tenant Scope
- `findAll` scopes `where.organizationId` to `currentUser.organizationId`; a `globalAccess`
role clears the org filter (sees all tenants).
- `create` assigns the organization from `currentUser.organizationId`; `update` only reassigns
organization for `globalAccess` users (otherwise it stays the caller's org).
## Data Contract
Model columns (`paranoid`, soft-delete via `deletedAt`):
- `id` (UUID PK).
- `day_of_week` — ENUM `monday` | `tuesday` | `wednesday` | `thursday` | `friday` | `saturday`
| `sunday`.
- `starts_at`, `ends_at` — DATE.
- `room` — TEXT.
- `importHash` (STRING(255), unique), `class_subjectId`, `organizationId`, `timetableId`,
`createdById`, `updatedById`, timestamps (all UUID FKs nullable).
Associations: `belongsTo` organization, timetable (`timetables`),
class_subject (`class_subjects`), createdBy/updatedBy (users). `findBy`/`GET /:id` eager-load
organization, timetable, and class_subject in a single `Promise.all`.
List filters (`TimetablePeriodsFilter`): `id`, `room` (iLike), `starts_atRange`, `ends_atRange`,
`day_of_week`, `timetable` (id or name, `|`-separated), `class_subject` (id or status),
`organization`, `createdAtRange`, plus `field`/`sort` ordering and `limit`/`page` pagination.
## Behavior / Notes
- `create`/`bulkImport`/`update` set associations via the Sequelize `set*` mixins (no file
relations on this entity).
- `bulkImport` offsets `createdAt` per row by `BULK_IMPORT_TIMESTAMP_STEP_MS` to preserve order.
- List pagination uses the shared `resolvePagination` defaults (page size 10, capped at 100).
- Note: `TimetablePeriodsFilter` accepts an `active` flag the model has no column for; it is
applied to `where.active` but, with no such column, is currently inert (kept for source
accuracy).
## Tests
None yet.
## Related
- Generic-CRUD contract: `backend-architecture.md`; related slices: `timetables`,
`class_subjects`, `permissions.md`.