From a3fb09bbf6352fa13e0f4f546cdae7c18f686299 Mon Sep 17 00:00:00 2001 From: Petri Saarinen Date: Sun, 22 Nov 2026 10:23:33 +0200 Subject: [PATCH] Add modular layered architecture with Customers module Introduce a modular monolith under app/Modules with Domain, Application, Infrastructure and Presentation layers. - Shared module: DomainException base, TransactionManager and an EventDispatcher that publishes events after commit. - Customers module: Customer entity, CustomerName and BusinessId (Y-tunnus checksum) value objects, RegisterCustomer/UpdateCustomer use cases, CustomerQueries read contract, Eloquent repository, and Livewire list and form pages with Finnish translations. - Pest arch tests enforce layer and module dependency rules for every module; Larastan (level 6) added as a dev dependency. - Raise Flux error text contrast to meet WCAG 2.2 AA. - Document the architecture in docs/architecture.md and CLAUDE.md. Co-Authored-By: Claude Opus 5.5 --- AGENTS.md | 10 + CLAUDE.md | 28 ++- .../Application/Contracts/CustomerQueries.php | 31 +++ .../Application/DTOs/CustomerDetailsData.php | 33 +++ .../Application/DTOs/CustomerSummary.php | 20 ++ .../Application/UseCases/RegisterCustomer.php | 47 ++++ .../Application/UseCases/UpdateCustomer.php | 43 ++++ .../Domain/Contracts/CustomerRepository.php | 21 ++ .../Customers/Domain/Entities/Customer.php | 79 +++++++ .../Customers/Domain/Enums/CustomerType.php | 11 + .../Domain/Events/CustomerRegistered.php | 10 + .../Exceptions/BusinessIdAlreadyInUse.php | 16 ++ .../ConsumerCannotHaveBusinessId.php | 15 ++ .../Domain/Exceptions/CustomerNotFound.php | 15 ++ .../Domain/Exceptions/InvalidBusinessId.php | 15 ++ .../Domain/Exceptions/InvalidCustomerName.php | 15 ++ .../Domain/ValueObjects/BusinessId.php | 62 ++++++ .../Domain/ValueObjects/CustomerName.php | 30 +++ .../Persistence/Eloquent/CustomerModel.php | 40 ++++ .../Repositories/EloquentCustomerQueries.php | 59 +++++ .../EloquentCustomerRepository.php | 52 +++++ .../Providers/CustomersServiceProvider.php | 36 +++ .../Livewire/Forms/CustomerForm.php | 90 ++++++++ .../Presentation/Livewire/⚡form.blade.php | 102 +++++++++ .../Presentation/Livewire/⚡index.blade.php | 98 +++++++++ .../Customers/Presentation/Routes/web.php | 12 + .../Application/Contracts/EventDispatcher.php | 16 ++ .../Contracts/TransactionManager.php | 23 ++ .../Domain/Exceptions/DomainException.php | 15 ++ .../Events/LaravelEventDispatcher.php | 25 +++ .../Persistence/LaravelTransactionManager.php | 16 ++ .../Providers/SharedServiceProvider.php | 20 ++ bootstrap/providers.php | 4 + composer.json | 4 + composer.lock | 199 ++++++++++++++++- database/factories/CustomerFactory.php | 44 ++++ ...26_11_22_081108_create_customers_table.php | 30 +++ docs/architecture.md | 208 ++++++++++++++++++ lang/fi/customers.php | 40 ++++ phpstan.neon | 10 + resources/css/app.css | 13 ++ tests/Browser/CustomersAccessibilityTest.php | 55 +++++ tests/Fakes/FakeEventDispatcher.php | 18 ++ tests/Fakes/FakeTransactionManager.php | 17 ++ tests/Fakes/InMemoryCustomerRepository.php | 43 ++++ .../Modules/Customers/CustomerPagesTest.php | 103 +++++++++ .../EloquentCustomerRepositoryTest.php | 57 +++++ .../Shared/TransactionsAndEventsTest.php | 55 +++++ tests/Unit/ArchitectureTest.php | 70 ++++++ .../Application/CustomerDetailsDataTest.php | 11 + .../Application/RegisterCustomerTest.php | 47 ++++ .../Application/UpdateCustomerTest.php | 47 ++++ .../Customers/Domain/BusinessIdTest.php | 29 +++ .../Customers/Domain/CustomerNameTest.php | 20 ++ .../Modules/Customers/Domain/CustomerTest.php | 44 ++++ 55 files changed, 2270 insertions(+), 3 deletions(-) create mode 100644 app/Modules/Customers/Application/Contracts/CustomerQueries.php create mode 100644 app/Modules/Customers/Application/DTOs/CustomerDetailsData.php create mode 100644 app/Modules/Customers/Application/DTOs/CustomerSummary.php create mode 100644 app/Modules/Customers/Application/UseCases/RegisterCustomer.php create mode 100644 app/Modules/Customers/Application/UseCases/UpdateCustomer.php create mode 100644 app/Modules/Customers/Domain/Contracts/CustomerRepository.php create mode 100644 app/Modules/Customers/Domain/Entities/Customer.php create mode 100644 app/Modules/Customers/Domain/Enums/CustomerType.php create mode 100644 app/Modules/Customers/Domain/Events/CustomerRegistered.php create mode 100644 app/Modules/Customers/Domain/Exceptions/BusinessIdAlreadyInUse.php create mode 100644 app/Modules/Customers/Domain/Exceptions/ConsumerCannotHaveBusinessId.php create mode 100644 app/Modules/Customers/Domain/Exceptions/CustomerNotFound.php create mode 100644 app/Modules/Customers/Domain/Exceptions/InvalidBusinessId.php create mode 100644 app/Modules/Customers/Domain/Exceptions/InvalidCustomerName.php create mode 100644 app/Modules/Customers/Domain/ValueObjects/BusinessId.php create mode 100644 app/Modules/Customers/Domain/ValueObjects/CustomerName.php create mode 100644 app/Modules/Customers/Infrastructure/Persistence/Eloquent/CustomerModel.php create mode 100644 app/Modules/Customers/Infrastructure/Persistence/Repositories/EloquentCustomerQueries.php create mode 100644 app/Modules/Customers/Infrastructure/Persistence/Repositories/EloquentCustomerRepository.php create mode 100644 app/Modules/Customers/Infrastructure/Providers/CustomersServiceProvider.php create mode 100644 app/Modules/Customers/Presentation/Livewire/Forms/CustomerForm.php create mode 100644 app/Modules/Customers/Presentation/Livewire/⚡form.blade.php create mode 100644 app/Modules/Customers/Presentation/Livewire/⚡index.blade.php create mode 100644 app/Modules/Customers/Presentation/Routes/web.php create mode 100644 app/Modules/Shared/Application/Contracts/EventDispatcher.php create mode 100644 app/Modules/Shared/Application/Contracts/TransactionManager.php create mode 100644 app/Modules/Shared/Domain/Exceptions/DomainException.php create mode 100644 app/Modules/Shared/Infrastructure/Events/LaravelEventDispatcher.php create mode 100644 app/Modules/Shared/Infrastructure/Persistence/LaravelTransactionManager.php create mode 100644 app/Modules/Shared/Infrastructure/Providers/SharedServiceProvider.php create mode 100644 database/factories/CustomerFactory.php create mode 100644 database/migrations/2026_11_22_081108_create_customers_table.php create mode 100644 docs/architecture.md create mode 100644 lang/fi/customers.php create mode 100644 phpstan.neon create mode 100644 tests/Browser/CustomersAccessibilityTest.php create mode 100644 tests/Fakes/FakeEventDispatcher.php create mode 100644 tests/Fakes/FakeTransactionManager.php create mode 100644 tests/Fakes/InMemoryCustomerRepository.php create mode 100644 tests/Feature/Modules/Customers/CustomerPagesTest.php create mode 100644 tests/Feature/Modules/Customers/EloquentCustomerRepositoryTest.php create mode 100644 tests/Feature/Modules/Shared/TransactionsAndEventsTest.php create mode 100644 tests/Unit/ArchitectureTest.php create mode 100644 tests/Unit/Modules/Customers/Application/CustomerDetailsDataTest.php create mode 100644 tests/Unit/Modules/Customers/Application/RegisterCustomerTest.php create mode 100644 tests/Unit/Modules/Customers/Application/UpdateCustomerTest.php create mode 100644 tests/Unit/Modules/Customers/Domain/BusinessIdTest.php create mode 100644 tests/Unit/Modules/Customers/Domain/CustomerNameTest.php create mode 100644 tests/Unit/Modules/Customers/Domain/CustomerTest.php diff --git a/AGENTS.md b/AGENTS.md index 5723e44..4acaa95 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -98,6 +98,16 @@ This project has domain-specific skills available in `**/skills/**`. You MUST ac - Laravel can be deployed using [Laravel Cloud](https://cloud.laravel.com/), which is the fastest way to deploy and scale production Laravel applications. - Activate the `deploying-to-cloud` skill whenever deploying to Laravel Cloud, configuring Cloud environments or resources, using the Cloud CLI, or troubleshooting Cloud deployments. +=== tests rules === + +# Test Enforcement + +- Add or update tests for behavior and logic changes when a test provides meaningful regression coverage. +- Pure copy, styling, and layout-only changes do not require new or updated tests. +- When test coverage applies, run the affected tests and ensure they pass. +- Test the changed behavior and its important failure modes, but do not add tests beyond them. +- Read the `testing-best-practices` skill before writing tests. + === laravel/core rules === # Do Things the Laravel Way diff --git a/CLAUDE.md b/CLAUDE.md index 440dc81..7294db9 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -4,7 +4,7 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co ## Project -An ERP / work-management system (*toiminnanohjausjärjestelmä*) for a small (5–10 person) Finnish media/marketing company. Stack: Laravel 13, Livewire 4 (single-file components), Flux UI 2 free edition, Tailwind 4, Vite 8, Pest. No domain code exists yet; `/` is a temporary Livewire demo page (`resources/views/pages/⚡home.blade.php`, layout `resources/views/layouts/app.blade.php`). +An ERP / work-management system (*toiminnanohjausjärjestelmä*) for a small (5–10 person) Finnish media/marketing company. Stack: Laravel 13, Livewire 4 (single-file components), Flux UI 2 free edition, Tailwind 4, Vite 8, Pest, Larastan. Domain code lives in modules under `app/Modules` (so far `Shared` and `Customers`); `/` is still a temporary Livewire demo page (`resources/views/pages/⚡home.blade.php`, layout `resources/views/layouts/app.blade.php`). There is no login yet, so module pages sit behind `auth` and are reachable only in tests (`actingAs`). - **The specification is `määrittely.txt`** (Finnish). It defines users/roles, customer jobs (*asiakastyö*) and their fields and enums, time tracking, expenses, Kanban tasks, the service unit → service → work task hierarchy, and billing-material export. It ends with Claude's open questions and a proposed phased plan. Read it before designing any domain feature. - **Language rule:** code (identifiers, tables, columns, enum keys) is in English; the specification and the UI are in Finnish. Keep UI strings in translation files from the start. @@ -12,6 +12,19 @@ An ERP / work-management system (*toiminnanohjausjärjestelmä*) for a small (5 - **Access control is central.** Roles: admin (*pääkäyttäjä*), management (*johto*), employee (*työntekijä*), intern (*harjoittelija*), subcontractor (*alihankkija*). Employees and interns see only the jobs assigned to them and never see money amounts. Interns have an end date after which they can't log in, plus a supervisor. Subcontractors see only their assigned jobs. - **UI targets:** logged-in users only (no public pages besides login). It must work on phones (new-job intake, time entry) and on FullHD desktops. +## Architecture (modular monolith) + +Read `docs/architecture.md` (Finnish) before adding or changing domain code. `app/Modules/Customers` is the reference module to copy. + +- Each business area is a module `app/Modules//` with four layers: `Domain`, `Application`, `Infrastructure`, `Presentation`. Create only the folders and modules a feature needs. +- **Domain** is plain PHP: entities, value objects, enums, exceptions extending `Shared\Domain\Exceptions\DomainException`, and repository interfaces in `Domain/Contracts`. No `Illuminate`, `Livewire`, Eloquent or facades. +- **Application** holds one use case per class (`execute()`), `final readonly` DTOs, and query contracts that return DTOs. It never touches Eloquent, `Illuminate\Database`, facades or HTTP. Wrap writes in `Shared\Application\Contracts\TransactionManager` and publish events through `EventDispatcher` (dispatched after commit). +- **Infrastructure** has Eloquent models named `…Model`, repository and query implementations, and the module's service provider (bindings, routes, `Livewire::addNamespace`), which is registered in `bootstrap/providers.php`. Migrations and factories stay in `database/`. +- **Presentation** is Livewire SFC pages in `Presentation/Livewire` (`::page`), Livewire Form objects for input validation, and `Presentation/Routes/web.php`. Pages call use cases and query contracts, and turn domain exceptions into field errors with translated messages. +- Modules talk only through another module's Application contracts or events, never its Infrastructure or Eloquent models. +- `tests/Unit/ArchitectureTest.php` enforces these rules for every module. Domain and Application files declare `strict_types=1`. Test domain rules and use cases in `tests/Unit` without Laravel (fakes in `tests/Fakes`), and repositories and pages in `tests/Feature`. +- When writing PHP through a bash heredoc, `\\` can collapse to `\`, and `"App\Modules\{$x}"` then stops interpolating. Write PHP files with the Write tool. + ## Accessibility (WCAG 2.2 AA, EN 301 549) Accessibility is a contractual acceptance criterion. It covers the whole UI: navigation, forms, tables, reports, notifications, dialogs, login, and every business process. Every new or changed view must meet these rules: @@ -23,7 +36,7 @@ Accessibility is a contractual acceptance criterion. It covers the whole UI: nav - The layout (`resources/views/layouts/app.blade.php`) provides a skip link and a `
` landmark. Keep them, and add `