Add customer contacts, billing units and consumer personal identity codes

Customer page and contacts/billing units (phase 1, item 2):
- New customer page (customers.show) with details, contacts and billing
  units; add and edit on their own pages, delete after confirmation.
  Creating or editing a customer now leads to this page.
- Contacts: name, title, phone, email, note. Billing units: name,
  billing address, e-invoice address (12-35 letters/digits), operator
  (required with an e-invoice address) and reference.
- Use cases check that the row belongs to the customer, so ids in the
  URL can't reach another customer's rows.
- EmailAddress moves from Users to Shared so both modules can use it.
- <x-confirm-dialog> (native <dialog>) replaces <x-modal>, whose title,
  close button and focus handling are not accessible; solid red buttons
  use red-700 via published NormalButtonColors for 4.5:1 contrast.

Personal identity code (henkilötunnus) for consumers who are invoiced:
- Validated (date, century sign, check character), stored encrypted
  with an HMAC blind index for duplicate checks; never in
  CustomerSummary, search, logs or Livewire state.
- Admins and management see it in full and may remove it; others enter
  it and see only the masked form. The form never shows the stored code.

Spec, architecture doc and CLAUDE.md updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-23 18:11:06 +02:00
co-authored by Claude Opus 5.5
parent 8dfc02c4e5
commit e0b8f4970f
93 changed files with 3777 additions and 58 deletions
@@ -0,0 +1,104 @@
<?php
use App\Modules\Customers\Application\DTOs\BillingUnitData;
use App\Modules\Customers\Application\DTOs\ContactData;
use App\Modules\Customers\Application\UseCases\AddBillingUnit;
use App\Modules\Customers\Application\UseCases\AddContact;
use App\Modules\Customers\Application\UseCases\RemoveBillingUnit;
use App\Modules\Customers\Application\UseCases\RemoveContact;
use App\Modules\Customers\Application\UseCases\UpdateBillingUnit;
use App\Modules\Customers\Application\UseCases\UpdateContact;
use App\Modules\Customers\Domain\Entities\Customer;
use App\Modules\Customers\Domain\Enums\CustomerType;
use App\Modules\Customers\Domain\Exceptions\BillingUnitNotFound;
use App\Modules\Customers\Domain\Exceptions\ContactNotFound;
use App\Modules\Customers\Domain\Exceptions\CustomerNotFound;
use App\Modules\Customers\Domain\Exceptions\EInvoiceOperatorRequired;
use App\Modules\Customers\Domain\ValueObjects\CustomerName;
use App\Modules\Shared\Domain\Exceptions\InvalidEmailAddress;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\InMemoryBillingUnitRepository;
use Tests\Fakes\InMemoryCustomerContactRepository;
use Tests\Fakes\InMemoryCustomerRepository;
beforeEach(function () {
$this->customers = new InMemoryCustomerRepository;
$this->contacts = new InMemoryCustomerContactRepository;
$this->billingUnits = new InMemoryBillingUnitRepository;
$transactions = new FakeTransactionManager;
$this->addContact = new AddContact($this->customers, $this->contacts, $transactions);
$this->updateContact = new UpdateContact($this->contacts, $transactions);
$this->removeContact = new RemoveContact($this->contacts, $transactions);
$this->addBillingUnit = new AddBillingUnit($this->customers, $this->billingUnits, $transactions);
$this->updateBillingUnit = new UpdateBillingUnit($this->billingUnits, $transactions);
$this->removeBillingUnit = new RemoveBillingUnit($this->billingUnits, $transactions);
$this->customerId = $this->customers->save(Customer::register(new CustomerName('Mainos Oy'), CustomerType::Company))->id();
$this->otherCustomerId = $this->customers->save(Customer::register(new CustomerName('Toinen Oy'), CustomerType::Company))->id();
});
it('adds, changes and removes a contact', function () {
$contactId = $this->addContact->execute($this->customerId, new ContactData('Maija', 'Markkinointi', '040 123 4567', '[email protected]'));
expect($this->contacts->findForCustomer($this->customerId, $contactId)?->email()?->value)->toBe('[email protected]');
$this->updateContact->execute($this->customerId, $contactId, new ContactData('Maija Meikäläinen'));
$contact = $this->contacts->findForCustomer($this->customerId, $contactId);
expect($contact?->name()->value)->toBe('Maija Meikäläinen')
->and($contact?->email())->toBeNull();
$this->removeContact->execute($this->customerId, $contactId);
expect($this->contacts->contacts)->toBeEmpty();
});
it('rejects an invalid contact email', function () {
$this->addContact->execute($this->customerId, new ContactData('Maija', email: 'maija@'));
})->throws(InvalidEmailAddress::class);
it('adds a contact or a billing unit only to a stored customer', function (string $useCase, object $data) {
$this->{$useCase}->execute(999, $data);
})->throws(CustomerNotFound::class)->with([
'contact' => ['addContact', new ContactData('Maija')],
'billing unit' => ['addBillingUnit', new BillingUnitData('Mainos Oy')],
]);
it('does not touch a contact of another customer', function (string $action) {
$contactId = $this->addContact->execute($this->otherCustomerId, new ContactData('Toisen kontakti'));
match ($action) {
'update' => $this->updateContact->execute($this->customerId, $contactId, new ContactData('Muutettu')),
'remove' => $this->removeContact->execute($this->customerId, $contactId),
};
})->throws(ContactNotFound::class)->with(['update', 'remove']);
it('adds, changes and removes a billing unit', function () {
$billingUnitId = $this->addBillingUnit->execute($this->customerId, new BillingUnitData('Mainos Oy', "Katu 1\n00100 Helsinki"));
$this->updateBillingUnit->execute($this->customerId, $billingUnitId, new BillingUnitData('Mainos Oy, markkinointi', null, '0037 1234 5678', '003721291126', 'PO-42'));
$billingUnit = $this->billingUnits->findForCustomer($this->customerId, $billingUnitId);
expect($billingUnit?->name()->value)->toBe('Mainos Oy, markkinointi')
->and($billingUnit?->billingAddress())->toBeNull()
->and($billingUnit?->eInvoiceAddress()?->value)->toBe('003712345678')
->and($billingUnit?->reference())->toBe('PO-42');
$this->removeBillingUnit->execute($this->customerId, $billingUnitId);
expect($this->billingUnits->billingUnits)->toBeEmpty();
});
it('rejects an e-invoice address without an operator', function () {
$this->addBillingUnit->execute($this->customerId, new BillingUnitData('Mainos Oy', eInvoiceAddress: '003712345678'));
})->throws(EInvoiceOperatorRequired::class);
it('does not touch a billing unit of another customer', function (string $action) {
$billingUnitId = $this->addBillingUnit->execute($this->otherCustomerId, new BillingUnitData('Toinen Oy'));
match ($action) {
'update' => $this->updateBillingUnit->execute($this->customerId, $billingUnitId, new BillingUnitData('Muutettu')),
'remove' => $this->removeBillingUnit->execute($this->customerId, $billingUnitId),
};
})->throws(BillingUnitNotFound::class)->with(['update', 'remove']);
@@ -0,0 +1,50 @@
<?php
use App\Modules\Customers\Application\DTOs\CustomerDetailsData;
use App\Modules\Customers\Application\UseCases\RegisterCustomer;
use App\Modules\Customers\Application\UseCases\UpdateCustomer;
use App\Modules\Customers\Domain\Enums\CustomerType;
use App\Modules\Customers\Domain\Exceptions\PersonalIdentityCodeAlreadyInUse;
use Tests\Fakes\FakeEventDispatcher;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\InMemoryCustomerRepository;
beforeEach(function () {
$this->customers = new InMemoryCustomerRepository;
$transactions = new FakeTransactionManager;
$this->registerCustomer = new RegisterCustomer($this->customers, $transactions, new FakeEventDispatcher);
$this->updateCustomer = new UpdateCustomer($this->customers, $transactions);
$this->customerId = $this->registerCustomer->execute(
new CustomerDetailsData('Maija Meikäläinen', CustomerType::Consumer, personalIdentityCode: '131052-308t'),
);
});
it('stores the normalised code of a consumer', function () {
expect($this->customers->findById($this->customerId)?->personalIdentityCode()?->value)->toBe('131052-308T');
});
it('rejects a code that belongs to another customer', function (string $useCase) {
match ($useCase) {
'register' => $this->registerCustomer->execute(new CustomerDetailsData('Toinen', CustomerType::Consumer, personalIdentityCode: '131052-308T')),
'update' => $this->updateCustomer->execute(
$this->registerCustomer->execute(new CustomerDetailsData('Toinen', CustomerType::Consumer)),
new CustomerDetailsData('Toinen', CustomerType::Consumer, personalIdentityCode: '131052-308T'),
),
};
})->throws(PersonalIdentityCodeAlreadyInUse::class)->with(['register', 'update']);
it('keeps the stored code when the form leaves it empty, and lets the customer keep its own code', function () {
$this->updateCustomer->execute($this->customerId, new CustomerDetailsData('Maija M.', CustomerType::Consumer));
$this->updateCustomer->execute($this->customerId, new CustomerDetailsData('Maija M.', CustomerType::Consumer, personalIdentityCode: '131052-308T'));
expect($this->customers->findById($this->customerId)?->personalIdentityCode()?->value)->toBe('131052-308T');
});
it('removes the code when asked, unless a new code is given', function () {
$this->updateCustomer->execute($this->customerId, new CustomerDetailsData('Maija', CustomerType::Consumer, personalIdentityCode: '010594Y9032', removePersonalIdentityCode: true));
expect($this->customers->findById($this->customerId)?->personalIdentityCode()?->value)->toBe('010594Y9032');
$this->updateCustomer->execute($this->customerId, new CustomerDetailsData('Maija', CustomerType::Consumer, removePersonalIdentityCode: true));
expect($this->customers->findById($this->customerId)?->personalIdentityCode())->toBeNull();
});
@@ -0,0 +1,58 @@
<?php
use App\Modules\Customers\Domain\Entities\BillingUnit;
use App\Modules\Customers\Domain\Entities\CustomerContact;
use App\Modules\Customers\Domain\Exceptions\EInvoiceOperatorRequired;
use App\Modules\Customers\Domain\Exceptions\InvalidBillingUnitName;
use App\Modules\Customers\Domain\Exceptions\InvalidContactName;
use App\Modules\Customers\Domain\Exceptions\InvalidEInvoiceAddress;
use App\Modules\Customers\Domain\ValueObjects\BillingUnitName;
use App\Modules\Customers\Domain\ValueObjects\ContactName;
use App\Modules\Customers\Domain\ValueObjects\EInvoiceAddress;
it('normalises an e-invoice address', function (string $input, string $expected) {
expect((new EInvoiceAddress($input))->value)->toBe($expected);
})->with([
'OVT with spaces' => ['0037 1234 5678', '003712345678'],
'IBAN in lower case' => ['fi49 5000 9420 0287 30', 'FI4950009420028730'],
]);
it('rejects an e-invoice address that is too short or has other characters', function (string $input) {
new EInvoiceAddress($input);
})->throws(InvalidEInvoiceAddress::class)->with(['00371234', '0037-1234-5678', '', str_repeat('1', 36)]);
it('rejects an empty or too long contact or billing unit name', function (string $class, string $exception, string $name) {
expect(fn () => new $class($name))->toThrow($exception);
})->with([
'contact, empty' => [ContactName::class, InvalidContactName::class, ' '],
'contact, too long' => [ContactName::class, InvalidContactName::class, str_repeat('a', 256)],
'billing unit, empty' => [BillingUnitName::class, InvalidBillingUnitName::class, ''],
'billing unit, too long' => [BillingUnitName::class, InvalidBillingUnitName::class, str_repeat('a', 256)],
]);
it('stores empty optional contact fields as none', function () {
$contact = CustomerContact::create(1, new ContactName('Maija Meikäläinen'), ' ', '', null, " Soita aamulla \n");
expect($contact->title())->toBeNull()
->and($contact->phone())->toBeNull()
->and($contact->note())->toBe('Soita aamulla');
});
it('requires an operator for an e-invoice address', function () {
BillingUnit::create(1, new BillingUnitName('Mainos Oy'), null, new EInvoiceAddress('003712345678'), ' ');
})->throws(EInvoiceOperatorRequired::class);
it('requires an operator when an e-invoice address is added later', function () {
$billingUnit = BillingUnit::create(1, new BillingUnitName('Mainos Oy'), "Katu 1\n00100 Helsinki");
expect(fn () => $billingUnit->changeDetails(new BillingUnitName('Mainos Oy'), null, new EInvoiceAddress('003712345678'), null, null))
->toThrow(EInvoiceOperatorRequired::class)
->and($billingUnit->eInvoiceAddress())->toBeNull()
->and($billingUnit->billingAddress())->toBe("Katu 1\n00100 Helsinki");
});
it('normalises the operator code', function () {
$billingUnit = BillingUnit::create(1, new BillingUnitName('Mainos Oy'), null, new EInvoiceAddress('003712345678'), ' ndea fihh ');
expect($billingUnit->eInvoiceOperator())->toBe('NDEAFIHH');
});
@@ -0,0 +1,59 @@
<?php
use App\Modules\Customers\Domain\Entities\Customer;
use App\Modules\Customers\Domain\Enums\CustomerType;
use App\Modules\Customers\Domain\Exceptions\CompanyCannotHavePersonalIdentityCode;
use App\Modules\Customers\Domain\Exceptions\InvalidPersonalIdentityCode;
use App\Modules\Customers\Domain\ValueObjects\CustomerName;
use App\Modules\Customers\Domain\ValueObjects\PersonalIdentityCode;
it('accepts valid personal identity codes of every century', function (string $code) {
expect((new PersonalIdentityCode($code))->value)->toBe($code);
})->with([
'1900s, hyphen' => '131052-308T',
'1900s, new sign Y' => '010594Y9032',
'1800s' => '010185+123B',
'2000s, leap day' => '290200A4561',
'2000s, new sign E' => '020502E902X',
]);
it('normalises spaces and lower case', function () {
expect((new PersonalIdentityCode(' 020502e902x '))->value)->toBe('020502E902X');
});
it('rejects a wrong check character, an impossible date or a wrong format', function (string $code) {
new PersonalIdentityCode($code);
})->throws(InvalidPersonalIdentityCode::class)->with([
'wrong check character' => '131052-308U',
'no such day (29.2.2001)' => '290201A4569',
'no such day (31.4.1952)' => '310452-308K',
'unknown century sign' => '131052G308T',
'too short' => '131052-30T',
'empty' => '',
]);
it('keeps the code out of the exception message', function () {
try {
new PersonalIdentityCode('131052-308U');
} catch (InvalidPersonalIdentityCode $exception) {
expect($exception->getMessage())->not->toContain('131052');
}
});
it('masks the date of birth', function () {
expect((new PersonalIdentityCode('131052-308T'))->masked())->toBe('••••••-308T');
});
it('gives a personal identity code only to a consumer', function () {
Customer::register(new CustomerName('Mainos Oy'), CustomerType::Company, null, new PersonalIdentityCode('131052-308T'));
})->throws(CompanyCannotHavePersonalIdentityCode::class);
it('keeps the stored code when no new one is given, and drops it when the customer becomes a company', function () {
$customer = Customer::register(new CustomerName('Maija'), CustomerType::Consumer, null, new PersonalIdentityCode('131052-308T'));
$customer->changeDetails(new CustomerName('Maija Meikäläinen'), CustomerType::Consumer, null);
expect($customer->personalIdentityCode()?->value)->toBe('131052-308T');
$customer->changeDetails(new CustomerName('Maijan Kuvaamo Oy'), CustomerType::Company, null);
expect($customer->personalIdentityCode())->toBeNull();
});