83 lines
3.8 KiB
Markdown
83 lines
3.8 KiB
Markdown
# Grades Backend
|
|
|
|
## Purpose
|
|
|
|
`grades` is the per-organization catalog of grade levels (e.g. year/grade tiers) used to
|
|
classify classes and fee plans. It is a generic-CRUD slice assembled from the shared factories;
|
|
the backend is the source of truth for these records.
|
|
|
|
## Slice Files (by layer)
|
|
|
|
- Route: `src/routes/grades.ts` — `createCrudRouter(controller, { permission: 'grades' })`.
|
|
- Controller: `src/api/controllers/grades.controller.ts` — `createCrudController(service, { csvFields })`.
|
|
- Service (BLL): `src/services/grades.ts` — `createCrudService(DbApi, { notFoundCode: 'gradesNotFound' })`.
|
|
- Repository (DAL): `src/db/api/grades.ts` (`GradesDBApi`) — entity-specific
|
|
`create`/`bulkImport`/`update`/`findBy`/`findAll`; `remove`/`deleteByIds`/`findAllAutocomplete`
|
|
delegate to `db/api/shared/repository.ts`.
|
|
- Model: `src/db/models/grades.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`).
|
|
|
|
## API
|
|
|
|
The standard generic-CRUD surface (all under `/api/grades`, JWT + `${METHOD}_GRADES`
|
|
permission, all `200`) — see `backend-architecture.md` for the shared 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 `name`.
|
|
- `GET /:id` — returns the record with eager associations (see Data Contract).
|
|
|
|
`csvFields`: `id`, `name`, `code`, `description`, `sort_order`.
|
|
|
|
## Access Rules
|
|
|
|
- JWT required; the whole router is guarded by `checkCrudPermissions('grades')`, deriving
|
|
`READ_GRADES` / `CREATE_GRADES` / `UPDATE_GRADES` / `DELETE_GRADES` 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), `name`, `code`, `description` (TEXT, nullable).
|
|
- `sort_order` — INTEGER (nullable).
|
|
- `importHash` (unique), `organizationId`, `createdById`, `updatedById`, timestamps.
|
|
|
|
Associations: `belongsTo` organization, createdBy/updatedBy (users); `hasMany`
|
|
`classes_grade` (classes), `fee_plans_grade` (fee_plans). `findBy`/`GET /:id` eager-load
|
|
`classes_grade`, `fee_plans_grade`, and `organization` in a single `Promise.all`.
|
|
|
|
List filters (`GradesFilter`): `id`, `name`, `code`, `description`, `sort_orderRange`, `active`,
|
|
`organization` (`|`-separated ids), `createdAtRange`, plus `field`/`sort` ordering and
|
|
`limit`/`page` pagination.
|
|
|
|
## Behavior / Notes
|
|
|
|
- `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: `GradesFilter` accepts an `active` flag the model has no column for; it is currently
|
|
inert (kept for source accuracy).
|
|
|
|
## Tests
|
|
|
|
None yet.
|
|
|
|
## Related
|
|
|
|
- Generic-CRUD contract: `backend-architecture.md`; related slices: `classes`, `fee_plans`,
|
|
`organizations`, `permissions.md`.
|