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

4.4 KiB

Message Recipients Backend

Purpose

message_recipients is the per-organization store of per-recipient delivery records for messages (one row per recipient, with delivery and read status). It is a generic-CRUD slice assembled from the shared factories; the backend is the source of truth for these records. This is the generic-CRUD slice for the message_recipients table; the higher-level parent-messaging workflow is documented separately in communications.md.

Slice Files (by layer)

  • Route: src/routes/message_recipients.tscreateCrudRouter(controller, { permission: 'message_recipients' }).
  • Controller: src/api/controllers/message_recipients.controller.tscreateCrudController(service, { csvFields }).
  • Service (BLL): src/services/message_recipients.tscreateCrudService(DbApi, { notFoundCode: 'message_recipientsNotFound' }).
  • Repository (DAL): src/db/api/message_recipients.ts (Message_recipientsDBApi) — entity-specific create/bulkImport/update/findBy/findAll; remove/deleteByIds/findAllAutocomplete delegate to db/api/shared/repository.ts.
  • Model: src/db/models/message_recipients.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/message_recipients, JWT + ${METHOD}_MESSAGE_RECIPIENTS 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, not the path param), 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 recipient_label.
  • GET /:id — returns the record with eager associations (see Data Contract).

csvFields: id, recipient_label, destination, delivered_at, read_at.

Access Rules

  • JWT required; the whole router is guarded by checkCrudPermissions('message_recipients'), deriving READ_MESSAGE_RECIPIENTS / CREATE_MESSAGE_RECIPIENTS / UPDATE_MESSAGE_RECIPIENTS / DELETE_MESSAGE_RECIPIENTS 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), recipient_label, destination (TEXT, nullable).
  • recipient_type — ENUM user | student | guardian.
  • delivery_status — ENUM pending | sent | delivered | failed | read.
  • delivered_at, read_at — DATE.
  • importHash (unique), organizationId, messageId, createdById, updatedById, timestamps.

Associations: belongsTo organization, message, createdBy/updatedBy (users). findBy/GET /:id eager-load organization and message in a single Promise.all.

List filters (MessageRecipientsFilter): id, recipient_label, destination, delivered_atRange, read_atRange, recipient_type, delivery_status, message (id or subject, |-separated), organization, createdAtRange, plus field/sort ordering and limit/page pagination.

Behavior / Notes

  • This slice has no file relations.
  • 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: MessageRecipientsFilter accepts an active flag the model has no column for; it is currently inert (kept for source accuracy).

Tests

None yet.

  • Generic-CRUD contract: backend-architecture.md; parent-messaging workflow: communications.md; related slices: messages, permissions.md.