Files
heala-backend/docs/admin-panel-integrity.md

90 lines
5.2 KiB
Markdown

# AdminJS data integrity
مستند دقیق فارسی و توضیح فایل‌به‌فایل: [admin-panel-integrity.fa.md](admin-panel-integrity.fa.md).
## Write path
AdminJS uses an explicit resource list in `admin-panel.resources.ts`. Every resource
uses `AdminPanelResource`; its create/update/delete methods never call the raw
TypeORM adapter. Catalog writes are normalized, validated with the existing create
DTO (merged with the current record on edit), then delegated to the existing
TestsService, QuestionsService, ChoicesService, QuestionRangeRuleService,
QuestionTransitionService or ResultRangeService. Their command/query/validation
providers remain responsible for business rules and persistence.
The panel converts numeric references and booleans explicitly (`"false"` remains
false), parses JSON fields, rejects unexpected fields, checks integer bounds and
retains the TypeORM-generated `test.id` reference for transitions. Read-only values
sent back unchanged by an edit form are accepted; attempts to change them fail.
## Permissions and sensitive records
- ADMIN and SUPERUSER can manage draft catalog records.
- User accounts are visible/editable only to SUPERUSER. The panel can change
first/last name and an explicitly confirmed new password through UsersService
and UserProfileProvider. Password policy is reused and bcrypt input is capped at
72 UTF-8 bytes. Phone and role cannot be changed here. Account creation/deletion
requires a separate workflow and is disabled in this panel.
- Submissions, answers, results, conversations and messages are read-only and
visible only to SUPERUSER. Bulk deletion is disabled for every resource.
- OTP is not registered. New entity classes are not automatically exposed.
- The actor's role is reloaded from the database before every resource action;
a role stored in an old admin session does not retain revoked privileges.
- Password hashes are removed from adapter reads and response payloads, including
populated references. New-password/confirmation values are scrubbed from form
responses. i18n validation messages are rendered without returning their raw
JSON payload, which otherwise contains the submitted value.
## Published structure and history
Question/choice/range/transition writes and catalog deletions are refused once the
test is published, permanently structure-locked, or has any persisted submission.
Publishing sets `Test.structureLocked`; unpublishing does not clear it. This also
protects guest sessions that do not have a persisted Submission row. To change
structure, create a new test. Normal test metadata/status/activation changes remain
available, with publication validation reused; only published tests may be active.
The migration marks currently published tests and tests with submissions as locked.
Previously published tests that are already drafts, have no persisted submissions,
and have no stored publication history cannot be identified retrospectively. If
such tests exist and must be preserved, lock them before allowing panel edits.
Moving child records between parents is blocked. Changing question type while it
has choices, range rules or transitions is blocked. Deleting a choice referenced
by a transition condition is blocked. This avoids dangling references inside JSON.
Panel writes across instances use one PostgreSQL session advisory lock. Acquisition
is nonblocking: a simultaneous panel write is rejected with a retry message instead
of filling the connection pool with lock waiters. Providers keep their existing
transactions. This is serialization, not a new transaction around all providers;
it does not serialize direct SQL or other API write paths. If the currently disabled
AdminModule is enabled later, apply the same structure/locking policy there too.
## Shared provider fixes
- Range-overlap updates exclude the edited ID explicitly instead of allowing one
overlap. Scoring overlap SQL is grouped under the test filter.
- Relationship updates persist proper relation objects and FK columns. Question
movement between tests is rejected so counts/transitions cannot silently drift.
- Question creation also runs metadata validation in its command provider.
- Single-choice transition validation rejects missing conditions cleanly;
multi-choice IDs are sorted on update as well as creation.
- Test updates preserve an explicit null price and validate the resulting published
state. The existing publication graph validator is reused.
No existing history is deleted or rewritten. Password changes follow the existing
profile behavior; this change does not add global token/session revocation.
## Deployment and verification
Build the new application and run `npm run migration:run:prod` before serving it
(development: `npm run migration:run:dev`). The migration is
`1794182400000-LockPublishedTestStructure.ts`. It has not been run automatically.
Tests build real TypeORM metadata and real AdminJS resource configuration without
database connections. Provider calls and database operations are mocked. They cover
DTO conversion/validation, permission refresh, immutable records, secret redaction,
generated references, lock contention and range-update regressions. Browser UI,
live PostgreSQL advisory-lock behavior and migration execution still require a
deployment smoke test.