Add customer jobs with numbering, members, tags, links and history

Phase 1, item 4. New Jobs module:
- Customer jobs with the spec's fields except money (phase 2): yearly
  running numbers (2026-0001) from a locked counter row, service units,
  schedule, kind, status, priority, payment basis, estimated hours,
  PO reference, instructions, members (responsible, contact person,
  customer contact, biller, billing unit, other members), tags shared
  across jobs, and links.
- Every change is recorded field by field and shown on the job page with
  names and Finnish dates.
- References to other modules are checked through their query
  contracts: contact and billing unit must be the customer's own;
  service units and users must be in use unless already on the job.
- Job list with search, filters and "my jobs" on by default; job form
  with an accessible customer search and quick add; job page.
- Access: subcontractors see only jobs they are members of, read only;
  billing statuses for management and the job's biller; archiving for
  management.

Related changes:
- Customers: one internal customer (created by a migration) for internal
  work; contacts and billing units used by a job cannot be removed.
- Users and Catalog: batch name lookups and active user options with
  roles for other modules.
- <x-checkbox-group> component; spec and CLAUDE.md updated.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-11-23 18:57:18 +02:00
co-authored by Claude Opus 5.5
parent 51b2d4a252
commit 70e107f6e2
95 changed files with 4631 additions and 26 deletions
@@ -0,0 +1,106 @@
<?php
use App\Modules\Jobs\Application\DTOs\CustomerJobData;
use App\Modules\Jobs\Application\Services\JobDetailsFactory;
use App\Modules\Jobs\Application\UseCases\ChangeJobStatus;
use App\Modules\Jobs\Application\UseCases\CreateCustomerJob;
use App\Modules\Jobs\Application\UseCases\UpdateCustomerJob;
use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\Jobs\Domain\Exceptions\InvalidJobReference;
use App\Modules\Users\Domain\Enums\Role;
use Tests\Fakes\FakeCatalogQueries;
use Tests\Fakes\FakeClock;
use Tests\Fakes\FakeCustomerQueries;
use Tests\Fakes\FakeTransactionManager;
use Tests\Fakes\FakeUserAccountQueries;
use Tests\Fakes\InMemoryCustomerJobRepository;
beforeEach(function () {
$this->jobs = new InMemoryCustomerJobRepository;
$this->customers = new FakeCustomerQueries;
$this->catalog = new FakeCatalogQueries;
$this->users = new FakeUserAccountQueries;
$this->clock = new FakeClock(new DateTimeImmutable('2026-12-31'));
$transactions = new FakeTransactionManager;
$this->customers->customers = [1 => 'Mainos Oy', 2 => 'Toinen Oy'];
$this->customers->contacts = [[1, 5, 'Maija'], [2, 6, 'Toisen kontakti']];
$this->catalog->addServiceUnit(1, 'Mediatuotanto');
$this->catalog->addServiceUnit(2, 'Markkinointi', active: false);
$this->catalog->addServiceUnit(9, 'Kaikille yhteiset', shared: true);
$this->users->addUser(10, 'Eero');
$this->users->addUser(11, 'Aino', Role::Subcontractor);
$this->users->addUser(12, 'Pois', active: false);
$factory = new JobDetailsFactory($this->customers, $this->catalog, $this->users);
$this->create = new CreateCustomerJob($this->jobs, $this->jobs, $factory, $transactions, $this->clock);
$this->update = new UpdateCustomerJob($this->jobs, $factory, $transactions);
$this->changeStatus = new ChangeJobStatus($this->jobs, $transactions);
});
/**
* @param array<string, mixed> $overrides
*/
function jobData(array $overrides = []): CustomerJobData
{
return new CustomerJobData(...array_merge([
'name' => 'Messuvideo',
'customerId' => 1,
'serviceUnitIds' => [1],
'responsibleUserId' => 10,
'kind' => 'one_off',
'status' => 'tentative',
'priority' => 'normal',
], $overrides));
}
it('numbers jobs per year and starts the history', function () {
$first = $this->create->execute(jobData(), 10);
$second = $this->create->execute(jobData(), 10);
$this->clock->today = new DateTimeImmutable('2027-01-01');
$nextYear = $this->create->execute(jobData(), 10);
expect($this->jobs->findById($first)?->number()->format())->toBe('2026-0001')
->and($this->jobs->findById($second)?->number()->format())->toBe('2026-0002')
->and($this->jobs->findById($nextYear)?->number()->format())->toBe('2027-0001')
->and($this->jobs->changedFields($first))->toBe(['created']);
});
it('rejects references that do not fit the job', function (array $overrides, string $field) {
try {
$this->create->execute(jobData($overrides), 10);
$this->fail('No exception');
} catch (InvalidJobReference $exception) {
expect($exception->field)->toBe($field);
}
})->with([
'unknown customer' => [['customerId' => 99], 'customer'],
'contact of another customer' => [['customerContactId' => 6], 'customer_contact'],
'inactive service unit' => [['serviceUnitIds' => [2]], 'service_units'],
'shared service unit' => [['serviceUnitIds' => [9]], 'service_units'],
'subcontractor as responsible' => [['responsibleUserId' => 11], 'responsible_user'],
'inactive member' => [['memberUserIds' => [12]], 'members'],
]);
it('keeps an inactive unit or user that is already on the job', function () {
$this->catalog->addServiceUnit(2, 'Markkinointi');
$this->users->addUser(12, 'Pois');
$jobId = $this->create->execute(jobData(['serviceUnitIds' => [1, 2], 'memberUserIds' => [12]]), 10);
$this->catalog->addServiceUnit(2, 'Markkinointi', active: false);
$this->users->addUser(12, 'Pois', active: false);
$this->update->execute($jobId, jobData(['name' => 'Messuvideo 2', 'serviceUnitIds' => [1, 2], 'memberUserIds' => [12]]), 10);
expect($this->jobs->findById($jobId)?->details()->name->value)->toBe('Messuvideo 2');
});
it('records each changed field, and nothing when nothing changed', function () {
$jobId = $this->create->execute(jobData(), 10);
$this->update->execute($jobId, jobData(['priority' => 'urgent', 'customerContactId' => 5, 'tags' => ['messut']]), 10);
$this->update->execute($jobId, jobData(['priority' => 'urgent', 'customerContactId' => 5, 'tags' => ['Messut']]), 10);
$this->changeStatus->execute($jobId, JobStatus::Order, 10);
expect($this->jobs->changedFields($jobId))->toBe(['created', 'priority', 'customer_contact', 'tags', 'status']);
});
@@ -0,0 +1,99 @@
<?php
use App\Modules\Jobs\Domain\Entities\CustomerJob;
use App\Modules\Jobs\Domain\Enums\JobKind;
use App\Modules\Jobs\Domain\Enums\JobStatus;
use App\Modules\Jobs\Domain\Enums\Priority;
use App\Modules\Jobs\Domain\Exceptions\DueBeforeStart;
use App\Modules\Jobs\Domain\Exceptions\InvalidEstimatedHours;
use App\Modules\Jobs\Domain\Exceptions\InvalidJobLink;
use App\Modules\Jobs\Domain\Exceptions\InvalidTag;
use App\Modules\Jobs\Domain\Exceptions\PlanEndsBeforeStart;
use App\Modules\Jobs\Domain\Exceptions\ServiceUnitRequired;
use App\Modules\Jobs\Domain\ValueObjects\EstimatedHours;
use App\Modules\Jobs\Domain\ValueObjects\JobDetails;
use App\Modules\Jobs\Domain\ValueObjects\JobLink;
use App\Modules\Jobs\Domain\ValueObjects\JobName;
use App\Modules\Jobs\Domain\ValueObjects\JobNumber;
use App\Modules\Jobs\Domain\ValueObjects\Tag;
/**
* @param array<string, mixed> $overrides
*/
function jobDetails(array $overrides = []): JobDetails
{
return new JobDetails(...array_merge([
'name' => new JobName('Messuvideo'),
'description' => null,
'customerId' => 1,
'serviceUnitIds' => [1],
'startsOn' => null,
'dueOn' => null,
'plannedFrom' => null,
'plannedTo' => null,
'kind' => JobKind::OneOff,
'status' => JobStatus::Tentative,
'priority' => Priority::Normal,
'paymentBasis' => null,
'estimatedHours' => null,
'customerReference' => null,
'instructions' => null,
'responsibleUserId' => 10,
'contactUserId' => null,
'customerContactId' => null,
'billerUserId' => null,
'billingUnitId' => null,
'memberUserIds' => [],
'tags' => [],
'links' => [],
], $overrides));
}
it('formats and parses job numbers', function () {
expect((new JobNumber(2026, 42))->format())->toBe('2026-0042')
->and(JobNumber::fromString('2027-0001')->sequence)->toBe(1);
});
it('accepts estimated hours in quarters only', function () {
expect(EstimatedHours::fromString('7,25')->format())->toBe('7.25')
->and(EstimatedHours::fromString('8')->format())->toBe('8')
->and(fn () => EstimatedHours::fromString('1.1'))->toThrow(InvalidEstimatedHours::class)
->and(fn () => new EstimatedHours(-1))->toThrow(InvalidEstimatedHours::class);
});
it('accepts only http and https links with a title', function () {
expect((new JobLink(' Briiffi ', 'https://drive.example.test/x'))->title)->toBe('Briiffi')
->and(fn () => new JobLink('Tiedosto', 'javascript:alert(1)'))->toThrow(InvalidJobLink::class)
->and(fn () => new JobLink('', 'https://example.test'))->toThrow(InvalidJobLink::class)
->and(fn () => new Tag(str_repeat('a', 51)))->toThrow(InvalidTag::class);
});
it('checks the dates and service units of a job', function (array $overrides, string $exception) {
expect(fn () => jobDetails($overrides))->toThrow($exception);
})->with([
'due before start' => [['startsOn' => new DateTimeImmutable('2026-05-10'), 'dueOn' => new DateTimeImmutable('2026-05-09')], DueBeforeStart::class],
'plan ends before start' => [['plannedFrom' => new DateTimeImmutable('2026-05-10'), 'plannedTo' => new DateTimeImmutable('2026-05-01')], PlanEndsBeforeStart::class],
'no service unit' => [['serviceUnitIds' => []], ServiceUnitRequired::class],
]);
it('removes duplicate tags ignoring case, and sorts ids', function () {
$details = jobDetails(['tags' => [new Tag('Messut'), new Tag('messut'), new Tag('video')], 'memberUserIds' => [3, 1, 3]]);
expect(array_map(fn (Tag $tag) => $tag->value, $details->tags))->toBe(['Messut', 'video'])
->and($details->memberUserIds)->toBe([1, 3])
->and($details->involves(3))->toBeTrue()
->and($details->involves(99))->toBeFalse();
});
it('returns only the fields that changed', function () {
$job = CustomerJob::open(new JobNumber(2026, 1), jobDetails(), 10);
$changes = $job->changeDetails(jobDetails(['priority' => Priority::Urgent, 'tags' => [new Tag('messut')]]));
expect(array_map(fn ($change) => [$change->field, $change->oldValue, $change->newValue], $changes))->toBe([
['priority', 'normal', 'urgent'],
['tags', null, 'messut'],
])
->and($job->changeStatus(JobStatus::Tentative))->toBe([])
->and(JobStatus::kanbanStatuses())->not->toContain(JobStatus::Archived);
});