Add catalog module for service units, services and work types
Phase 1, item 3: the hierarchy customer jobs, tasks and time entries will refer to. - New Catalog module: service unit > optional service > work type. Names are unique per level (case-insensitive); items are never deleted, only deactivated, and foreign keys restrict deletes. - Pohjatiedot page (/catalog, admins and management): add, rename, reorder with move up/down buttons (keyboard-friendly, focus kept and announced) and an "in use" checkbox per row. Work types can move to another service of the same unit. - A migration inserts the starting catalog from the specification when the tables are empty, so every installation has it. - CatalogQueries gives other modules the active service units and the work types of chosen units plus the shared ones. - Spec and CLAUDE.md updated. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
use App\Models\User;
|
||||
use App\Modules\Catalog\Application\Contracts\CatalogQueries;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceModel;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ServiceUnitModel;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Livewire\Features\SupportLockedProperties\CannotUpdateLockedPropertyException;
|
||||
use Livewire\Livewire;
|
||||
|
||||
uses(RefreshDatabase::class);
|
||||
|
||||
beforeEach(function () {
|
||||
$this->actingAs(User::factory()->management()->create());
|
||||
$this->unit = fn (string $name) => ServiceUnitModel::query()->where('name', $name)->sole();
|
||||
});
|
||||
|
||||
it('has the starting catalog from the specification', function () {
|
||||
$tree = app(CatalogQueries::class)->tree();
|
||||
$media = $tree[0];
|
||||
|
||||
expect(array_map(fn ($node) => $node->serviceUnit->name, $tree))->toBe([
|
||||
'Mediatuotanto', 'Markkinointi', 'Painotuotanto', 'Ylläpitopalvelut', 'Verkkosivut', 'Ohjelmistot', 'Kuluttajapalvelut', 'Kaikille yhteiset',
|
||||
])
|
||||
->and(array_map(fn ($node) => $node->service->name, $media->services))->toBe(['Videotuotanto', 'Valokuvaus', 'Äänituotanto'])
|
||||
->and(array_map(fn ($workType) => $workType->name, $media->services[0]->workTypes))->toBe(['videokuvaus', 'kuvausassistentti', 'editointi', 'animointi', '3D-mallinnus'])
|
||||
->and(end($tree)->serviceUnit->isShared)->toBeTrue()
|
||||
->and(WorkTypeModel::query()->count())->toBe(29);
|
||||
});
|
||||
|
||||
it('offers the work types of the chosen units and the shared ones, but not inactive ones', function () {
|
||||
$marketing = ($this->unit)('Markkinointi');
|
||||
WorkTypeModel::query()->where('name', 'kampanjatyö')->update(['is_active' => false]);
|
||||
|
||||
$nodes = app(CatalogQueries::class)->activeWorkTypesFor([$marketing->id]);
|
||||
|
||||
expect(array_map(fn ($node) => $node->serviceUnit->name, $nodes))->toBe(['Markkinointi', 'Kaikille yhteiset'])
|
||||
->and(array_map(fn ($workType) => $workType->name, $nodes[0]->workTypes))->toBe(['graafinen työ', 'julkaisutyö'])
|
||||
->and(array_map(fn ($unit) => $unit->name, app(CatalogQueries::class)->activeServiceUnits()))->not->toContain('Kaikille yhteiset');
|
||||
});
|
||||
|
||||
it('shows the catalog page to admins and management only', function (string $role, int $status) {
|
||||
$this->actingAs(User::factory()->{$role}()->create())->get(route('catalog.index'))->assertStatus($status);
|
||||
})->with([
|
||||
'admin' => ['admin', 200],
|
||||
'management' => ['management', 200],
|
||||
'employee' => ['employee', 403],
|
||||
'intern' => ['intern', 403],
|
||||
'subcontractor' => ['subcontractor', 403],
|
||||
]);
|
||||
|
||||
it('shows the menu link only to those who may manage the catalog', function () {
|
||||
$this->get(route('customers.index'))->assertSee('Pohjatiedot');
|
||||
$this->actingAs(User::factory()->create())->get(route('customers.index'))->assertDontSee('Pohjatiedot');
|
||||
});
|
||||
|
||||
it('adds a service unit, a service and a work type', function () {
|
||||
Livewire::test('catalog::service-unit-form')
|
||||
->set('form.name', 'Tapahtumat')
|
||||
->call('save')
|
||||
->assertHasNoErrors()
|
||||
->assertRedirect(route('catalog.index'));
|
||||
|
||||
$unit = ($this->unit)('Tapahtumat');
|
||||
|
||||
Livewire::test('catalog::service-form', ['serviceUnit' => $unit->id])
|
||||
->set('form.name', 'Messut')
|
||||
->call('save')
|
||||
->assertHasNoErrors();
|
||||
|
||||
$service = ServiceModel::query()->where('name', 'Messut')->sole();
|
||||
|
||||
Livewire::test('catalog::work-type-form', ['serviceUnit' => $unit->id])
|
||||
->set('form.name', 'osastosuunnittelu')
|
||||
->set('form.service_id', (string) $service->id)
|
||||
->call('save')
|
||||
->assertHasNoErrors();
|
||||
|
||||
expect(WorkTypeModel::query()->where('name', 'osastosuunnittelu')->sole()->service_id)->toBe($service->id);
|
||||
});
|
||||
|
||||
it('shows a taken name as a Finnish field error', function () {
|
||||
Livewire::test('catalog::service-unit-form')
|
||||
->set('form.name', 'markkinointi')
|
||||
->call('save')
|
||||
->assertHasErrors(['form.name'])
|
||||
->assertSee('Samalla tasolla on jo tämänniminen rivi.');
|
||||
});
|
||||
|
||||
it('moves and deactivates rows on the catalog page', function () {
|
||||
$marketing = ($this->unit)('Markkinointi');
|
||||
|
||||
Livewire::test('catalog::index')
|
||||
->call('move', 'service_unit', $marketing->id, 'up')
|
||||
->assertSee('Markkinointi siirretty ylöspäin.')
|
||||
->call('move', 'service_unit', $marketing->id, 'up')
|
||||
->assertSee('Markkinointi on jo ensimmäisenä.')
|
||||
->call('toggleActive', 'service_unit', $marketing->id)
|
||||
->assertSee('Markkinointi poistettu käytöstä.')
|
||||
->set('showInactive', false)
|
||||
->assertDontSee('graafinen työ');
|
||||
|
||||
expect($marketing->fresh()->sort_order)->toBe(1)
|
||||
->and($marketing->fresh()->is_active)->toBeFalse();
|
||||
});
|
||||
|
||||
it('returns 404 for unknown items and does not add services to the shared unit', function () {
|
||||
$shared = ($this->unit)('Kaikille yhteiset');
|
||||
|
||||
$this->get(route('catalog.service-units.edit', 999))->assertNotFound();
|
||||
$this->get(route('catalog.work-types.edit', 999))->assertNotFound();
|
||||
$this->get(route('catalog.services.create', $shared->id))->assertNotFound();
|
||||
});
|
||||
|
||||
it('does not let the edited item be changed in the browser', function () {
|
||||
Livewire::test('catalog::work-type-form', ['serviceUnit' => ($this->unit)('Markkinointi')->id])
|
||||
->set('workTypeId', 1);
|
||||
})->throws(CannotUpdateLockedPropertyException::class);
|
||||
Reference in New Issue
Block a user