Files
Toiminnanohjaus/app/Modules/Users/Presentation/Livewire/Forms/UserForm.php
T
petriandClaude Opus 5.5 e0b8f4970f 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]>
2026-11-23 18:11:06 +02:00

121 lines
4.5 KiB
PHP

<?php
namespace App\Modules\Users\Presentation\Livewire\Forms;
use App\Modules\Shared\Domain\Exceptions\DomainException;
use App\Modules\Shared\Domain\Exceptions\InvalidEmailAddress;
use App\Modules\Users\Application\DTOs\UserAccountData;
use App\Modules\Users\Application\DTOs\UserSummary;
use App\Modules\Users\Domain\Enums\Role;
use App\Modules\Users\Domain\Exceptions\EmailAlreadyInUse;
use App\Modules\Users\Domain\Exceptions\InternRequiresEndDateAndSupervisor;
use App\Modules\Users\Domain\Exceptions\InvalidPersonName;
use App\Modules\Users\Domain\Exceptions\InvalidSupervisor;
use App\Modules\Users\Domain\Exceptions\LastAdminCannotBeRemoved;
use App\Modules\Users\Domain\Exceptions\SupervisorHasInterns;
use Illuminate\Validation\Rule;
use Livewire\Form;
/**
* User details form shared by the create and edit pages. Validates the
* shape of the input; business rules come from the Domain and are shown as
* field errors by showDomainError().
*/
class UserForm extends Form
{
public string $name = '';
public string $email = '';
public string $role = Role::Employee->value;
public bool $active = true;
public string $employment_ends_on = '';
public string $supervisor_id = '';
public function fillFromSummary(UserSummary $user): void
{
$this->name = $user->name;
$this->email = $user->email;
$this->role = $user->role->value;
$this->active = $user->active;
$this->employment_ends_on = $user->employmentEndsOn ?? '';
$this->supervisor_id = $user->supervisorId === null ? '' : (string) $user->supervisorId;
}
public function isIntern(): bool
{
return $this->role === Role::Intern->value;
}
public function toData(): UserAccountData
{
$this->validate();
return UserAccountData::fromArray([
'name' => $this->name,
'email' => $this->email,
'role' => $this->role,
'active' => $this->active,
'employment_ends_on' => $this->isIntern() ? $this->employment_ends_on : null,
'supervisor_id' => $this->isIntern() ? $this->supervisor_id : null,
]);
}
/**
* Shows a broken business rule as an error on the field it concerns.
*/
public function showDomainError(DomainException $exception): void
{
[$field, $key] = match ($exception::class) {
InvalidPersonName::class => ['name', 'users.errors.invalid_name'],
InvalidEmailAddress::class => ['email', 'users.errors.invalid_email'],
EmailAlreadyInUse::class => ['email', 'users.errors.email_taken'],
InternRequiresEndDateAndSupervisor::class => ['supervisor_id', 'users.errors.supervisor_required'],
InvalidSupervisor::class => ['supervisor_id', 'users.errors.invalid_supervisor'],
LastAdminCannotBeRemoved::class => ['role', 'users.errors.last_admin'],
SupervisorHasInterns::class => ['role', 'users.errors.supervisor_has_interns'],
default => throw $exception,
};
$this->addError($field, __($key));
}
/**
* @return array<string, array<int, mixed>>
*/
protected function rules(): array
{
return [
'name' => ['required', 'string', 'max:255'],
'email' => ['required', 'string', 'email:filter', 'max:255'],
'role' => ['required', Rule::enum(Role::class)],
'active' => ['boolean'],
'employment_ends_on' => [Rule::requiredIf($this->isIntern()), 'nullable', 'date_format:Y-m-d'],
'supervisor_id' => [Rule::requiredIf($this->isIntern()), 'nullable', 'integer'],
];
}
/**
* @return array<string, string>
*/
protected function messages(): array
{
return [
'name.required' => __('users.errors.name_required'),
'name.max' => __('users.errors.invalid_name'),
'email.required' => __('users.errors.email_required'),
'email.email' => __('users.errors.invalid_email'),
'email.max' => __('users.errors.invalid_email'),
'role.required' => __('users.errors.role_required'),
'role.enum' => __('users.errors.role_required'),
'employment_ends_on.required' => __('users.errors.end_date_required'),
'employment_ends_on.date_format' => __('users.errors.end_date_invalid'),
'supervisor_id.required' => __('users.errors.supervisor_required'),
'supervisor_id.integer' => __('users.errors.invalid_supervisor'),
];
}
}