Vaihe 1/2: asiakkaat, yhteyshenkilöt ja laskutusyksiköt

- customers-moduuli Clean Architecture -kerroksin: asiakas (yritys/kuluttaja),
  Y-tunnuksen tarkiste ja yksilöllisyys, yhteyshenkilöt, laskutusyksiköt
  (verkkolaskuosoite vaatii välittäjätunnuksen)
- henkilötunnus salattuna (Fernet) ja HMAC-tiiviste päällekkäisyyksiin;
  pääkäyttäjä ja johto näkevät ja poistavat, muut näkevät peitettynä
- sisäinen asiakas data-migraatiolla, ei poistettavissa
- yleinen poistonesto käänteisten relaatioiden perusteella (tulevat
  PROTECT-viiteavaimet estävät poiston automaattisesti)
- asiakkaan sivu: lisäys ja muokkaus dialogissa HTMX:llä, fokuksen hallinta
  ja ruudunlukijailmoitukset; HTMX näyttää nyt 422-validointivirheet
- testit: domain, application, integraatio ja selaintestit (axe);
  yhteiset fixturet apps/conftest.py:hin, FakeUnitOfWork shared-moduuliin
- import-linter: moduulien riippumattomuussäännöt
- dokumentit päivitetty

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-29 13:47:09 +02:00
co-authored by Claude Opus 5.5
parent e7b2745916
commit a81c073d6a
68 changed files with 3470 additions and 144 deletions
+15 -2
View File
@@ -9,7 +9,11 @@ Toiminnanohjaus is an ERP for a small (5–10 people) Finnish media and marketin
- Stack: Django 5.2 LTS, Python 3.14, MariaDB 10.11 (XAMPP in development), HTMX, Alpine.js, Tailwind CSS 4 and daisyUI 5.
- Language: code (identifiers, tables, columns, enum keys) is in English. Specs, UI texts, docs and commit messages are in Finnish.
**Current state:** phase 1 item 1 is done: the `shared` module and the `users` module (custom user model, login, password links, user management). It is the reference implementation of the architecture; copy its patterns for new modules. Other modules described in `docs/architecture.md` are not built yet.
**Current state:** phase 1 items 1–2 are done.
- `shared` and `users`: custom user model, login, password links, user management.
- `customers`: customers, contacts, billing units, the internal customer, and the encrypted personal identity code.
`users` and `customers` are the reference implementations; copy their patterns for new modules. The other modules in `docs/architecture.md` are not built yet. Next in the spec is phase 1 item 3: service units, services and work types as base data.
## Source of truth
@@ -43,6 +47,10 @@ Translations: source strings are English; Finnish is in `locale/fi/LC_MESSAGES/d
- `AUTH_USER_MODEL = "users.User"` (`apps/users/infrastructure/orm.py`): email login, a role field, no `PermissionsMixin` and no Django admin. Permissions are role-based: `apps/users/domain/permissions.py` (`role_has`), `PermissionRequiredMixin`, and the template filter `{% if request.user|can:"users.manage" %}` (`{% load users_tags %}`).
- `LoginRequiredMiddleware` is on, so mark public views with `login_not_required`. `EnsureAccountIsUsable` logs out deactivated users and interns past their end date on every request.
- The cache is `DatabaseCache` (login throttling), so run `createcachetable` on new databases.
- **Personal identity code (henkilötunnus):**
- It is encrypted with Fernet (`FIELD_ENCRYPTION_KEYS`, comma-separated; the first key encrypts and all keys decrypt) and has an HMAC fingerprint (`PERSONAL_ID_HASH_KEY`) for duplicate checks. A system check fails if either key is missing.
- Never change these keys in `.env` without rotating them; otherwise stored codes cannot be decrypted.
- Only `customers.infrastructure.vault` touches plain codes. The domain `PersonalIdentityCode` hides the code in `repr`, and display uses `masked`.
- **Templates and tags:**
- Each module's templates live in `apps/<module>/presentation/templates` and are added to `TEMPLATES['DIRS']` by a glob in settings.
- Template tag libraries live in `presentation/templatetags` and must be registered in `TEMPLATES['OPTIONS']['libraries']`.
@@ -58,7 +66,8 @@ The project is a modular monolith. Each business area is a Django app under `app
- `infrastructure/` holds the Django models (named `…Model`, in `orm.py` and re-exported from the app's `models.py`; set `Meta.db_table` to the table names in spec section 14), the repository implementations and the query implementations, which return DTOs straight from the ORM.
- `presentation/` holds thin views, Django Forms and templates. Forms validate input shape and build DTOs. Domain errors are turned into field errors with `form.add_error()`. HTMX requests get partial templates.
- `container.py` is the composition root, with factory functions that wire the use cases. There is no DI framework. Presentation reaches infrastructure only through the container (import-linter enforces this; add every new module to the contracts in `pyproject.toml`).
- Tests live in `apps/<module>/tests/{domain,application,integration}`. Application tests use in-memory fakes (`apps/users/tests/fakes.py` shows the pattern) and need no DB.
- Tests live in `apps/<module>/tests/{domain,application,integration}`. Application tests use in-memory fakes (`apps/<module>/tests/fakes.py`; `FakeUnitOfWork` is in `apps/shared/tests/fakes.py`) and need no DB. The shared pytest fixtures (`make_user`, `admin`, `admin_client`) are in `apps/conftest.py`, and the browser test helpers (`themed_page`, `ALL`, `assert_accessible`, `login`) are in `tests/browser/conftest.py`.
- **Deletion guards are generic.** `apps/customers/infrastructure/references.py` (`is_referenced`) checks every reverse relation. When a new module adds a `ForeignKey(..., on_delete=PROTECT)` to customers, contacts or billing units, the delete buttons and use cases block deletion automatically, so don't add cross-module checks. Copy the pattern for other modules' entities.
Cross-cutting rules:
@@ -79,5 +88,9 @@ WCAG 2.2 AA / EN 301 549 is the contract's acceptance criterion for the whole UI
- **Layout:** no horizontal page scrolling from 320 px up to FullHD. The machine console needs touch targets of at least 44 × 44 px.
- **Implementation:** use daisyUI classes and the shared partials in `templates/components/`. Alpine.js is for local UI state only, never for business logic.
- **Colours:** daisyUI's default text colours fail WCAG contrast in places. `static/src/app.css` overrides soft alerts and badges, `.menu-title`, and the light theme's error and success colours. Use `link` rather than `link-primary` for text links. The browser tests (axe) catch regressions.
- **Dialog forms (customers module pattern):**
- The form is loaded with `hx-get` into the shared `<dialog>`.
- When the form is invalid, the server returns the form with status 422. `static/src/app.js` makes HTMX swap 422 responses and focuses the first `aria-invalid` field.
- When the form is valid, the server re-renders the section with `HX-Retarget`/`HX-Reswap`. It sets `HX-Trigger-After-Settle: {"itemSaved": {"message", "focus"}}` (the handler in app.js closes the dialog, moves focus and announces the message) and adds any changed header actions as an out-of-band swap.
- **Assets:** no CDN in production; build the assets with npm.
- **Texts:** all UI text goes through `gettext` / `{% translate %}`.