- Uusi catalog-moduuli: palveluyksikkö → (palvelu) → työtehtävä ilman hintoja. - Pohjatiedot-sivu /catalog/ pääkäyttäjälle ja johdolle (oikeus catalog.manage): lisäys ja muokkaus dialogissa, Siirrä ylös / Siirrä alas, käytöstä poisto ja käyttöön otto. Fokus ja aria-live-ilmoitus jokaisen muutoksen jälkeen. - Rivejä ei poisteta. Käytöstä poistetun yläkohdan alla olevat rivit säilyttävät oman tilansa, mutta eivät ole valittavissa. Nimi on yksilöllinen samalla tasolla kirjainkoosta riippumatta. - Työtehtävän voi siirtää saman yksikön toiseen palveluun tai suoraan yksikön alle. - Luvun 11.1 lista data-migraationa (0002_seed), jos taulut ovat tyhjiä. - CatalogQueries: work_type_options, active_service_units ja get_work_type tehtäviä ja tuntikirjausta varten. - Import-linter-sopimukset, suomennokset, yksikkö-, integraatio- ja selaintestit. - Päivitetty CLAUDE.md ja määrittelyn kohta 16.3. Co-Authored-By: Claude Opus 5.5 <[email protected]>
195 lines
6.6 KiB
Python
195 lines
6.6 KiB
Python
from apps.catalog.application.dto import ServiceData, ServiceUnitData, WorkTypeData
|
|
from apps.catalog.domain.entities import Service, ServiceUnit, WorkType
|
|
from apps.catalog.domain.exceptions import (
|
|
ServiceNameTaken,
|
|
ServiceNotFound,
|
|
ServiceUnitNameTaken,
|
|
ServiceUnitNotFound,
|
|
WorkTypeNameTaken,
|
|
WorkTypeNotFound,
|
|
)
|
|
from apps.catalog.domain.ordering import Direction, move
|
|
from apps.catalog.domain.repositories import (
|
|
CatalogItem,
|
|
OrderedRepository,
|
|
ServiceRepository,
|
|
ServiceUnitRepository,
|
|
WorkTypeRepository,
|
|
)
|
|
from apps.catalog.domain.value_objects import CatalogName
|
|
from apps.shared.application.ports import UnitOfWork
|
|
from apps.shared.domain.exceptions import DomainError
|
|
|
|
|
|
class AddServiceUnit:
|
|
def __init__(self, units: ServiceUnitRepository, uow: UnitOfWork) -> None:
|
|
self.units = units
|
|
self.uow = uow
|
|
|
|
def execute(self, data: ServiceUnitData) -> int:
|
|
name = CatalogName(data.name)
|
|
with self.uow:
|
|
if self.units.is_name_taken(name):
|
|
raise ServiceUnitNameTaken()
|
|
unit = ServiceUnit.register(name=name, sort_order=self.units.next_sort_order())
|
|
saved = self.units.save(unit)
|
|
assert saved.id is not None
|
|
return saved.id
|
|
|
|
|
|
class RenameServiceUnit:
|
|
def __init__(self, units: ServiceUnitRepository, uow: UnitOfWork) -> None:
|
|
self.units = units
|
|
self.uow = uow
|
|
|
|
def execute(self, unit_id: int, data: ServiceUnitData) -> None:
|
|
name = CatalogName(data.name)
|
|
with self.uow:
|
|
unit = self.units.get(unit_id)
|
|
if unit is None:
|
|
raise ServiceUnitNotFound()
|
|
if self.units.is_name_taken(name, exclude_id=unit_id):
|
|
raise ServiceUnitNameTaken()
|
|
self.units.save(unit.rename(name))
|
|
|
|
|
|
class AddService:
|
|
def __init__(
|
|
self, units: ServiceUnitRepository, services: ServiceRepository, uow: UnitOfWork
|
|
) -> None:
|
|
self.units = units
|
|
self.services = services
|
|
self.uow = uow
|
|
|
|
def execute(self, unit_id: int, data: ServiceData) -> int:
|
|
name = CatalogName(data.name)
|
|
with self.uow:
|
|
if self.units.get(unit_id) is None:
|
|
raise ServiceUnitNotFound()
|
|
if self.services.is_name_taken(unit_id, name):
|
|
raise ServiceNameTaken()
|
|
service = Service.register(
|
|
service_unit_id=unit_id,
|
|
name=name,
|
|
sort_order=self.services.next_sort_order(unit_id),
|
|
)
|
|
saved = self.services.save(service)
|
|
assert saved.id is not None
|
|
return saved.id
|
|
|
|
|
|
class RenameService:
|
|
def __init__(self, services: ServiceRepository, uow: UnitOfWork) -> None:
|
|
self.services = services
|
|
self.uow = uow
|
|
|
|
def execute(self, service_id: int, data: ServiceData) -> None:
|
|
name = CatalogName(data.name)
|
|
with self.uow:
|
|
service = self.services.get(service_id)
|
|
if service is None:
|
|
raise ServiceNotFound()
|
|
if self.services.is_name_taken(service.service_unit_id, name, exclude_id=service_id):
|
|
raise ServiceNameTaken()
|
|
self.services.save(service.rename(name))
|
|
|
|
|
|
class _WorkTypeUseCase:
|
|
def __init__(
|
|
self,
|
|
units: ServiceUnitRepository,
|
|
services: ServiceRepository,
|
|
work_types: WorkTypeRepository,
|
|
uow: UnitOfWork,
|
|
) -> None:
|
|
self.units = units
|
|
self.services = services
|
|
self.work_types = work_types
|
|
self.uow = uow
|
|
|
|
def _service(self, service_id: int | None) -> Service | None:
|
|
if service_id is None:
|
|
return None
|
|
service = self.services.get(service_id)
|
|
if service is None:
|
|
raise ServiceNotFound()
|
|
return service
|
|
|
|
|
|
class AddWorkType(_WorkTypeUseCase):
|
|
def execute(self, unit_id: int, data: WorkTypeData) -> int:
|
|
name = CatalogName(data.name)
|
|
with self.uow:
|
|
if self.units.get(unit_id) is None:
|
|
raise ServiceUnitNotFound()
|
|
work_type = WorkType.register(
|
|
service_unit_id=unit_id,
|
|
service=self._service(data.service_id),
|
|
name=name,
|
|
sort_order=self.work_types.next_sort_order(unit_id, data.service_id),
|
|
)
|
|
if self.work_types.is_name_taken(unit_id, data.service_id, name):
|
|
raise WorkTypeNameTaken()
|
|
saved = self.work_types.save(work_type)
|
|
assert saved.id is not None
|
|
return saved.id
|
|
|
|
|
|
class UpdateWorkType(_WorkTypeUseCase):
|
|
"""Rename, and move under another service of the same unit or directly under the unit
|
|
(a moved work type goes last on its new level)."""
|
|
|
|
def execute(self, work_type_id: int, data: WorkTypeData) -> None:
|
|
name = CatalogName(data.name)
|
|
with self.uow:
|
|
work_type = self.work_types.get(work_type_id)
|
|
if work_type is None:
|
|
raise WorkTypeNotFound()
|
|
unit_id = work_type.service_unit_id
|
|
if data.service_id != work_type.service_id:
|
|
work_type = work_type.move_to(
|
|
self._service(data.service_id),
|
|
sort_order=self.work_types.next_sort_order(unit_id, data.service_id),
|
|
)
|
|
if self.work_types.is_name_taken(
|
|
unit_id, work_type.service_id, name, exclude_id=work_type_id
|
|
):
|
|
raise WorkTypeNameTaken()
|
|
self.work_types.save(work_type.rename(name))
|
|
|
|
|
|
class SetActive[T: CatalogItem]:
|
|
"""Deactivate or reactivate a service unit, service or work type (never deleted)."""
|
|
|
|
def __init__(
|
|
self, repo: OrderedRepository[T], uow: UnitOfWork, not_found: type[DomainError]
|
|
) -> None:
|
|
self.repo = repo
|
|
self.uow = uow
|
|
self.not_found = not_found
|
|
|
|
def execute(self, item_id: int, active: bool) -> None:
|
|
with self.uow:
|
|
item = self.repo.get(item_id)
|
|
if item is None:
|
|
raise self.not_found()
|
|
self.repo.save(item.with_active(active))
|
|
|
|
|
|
class MoveItem[T: CatalogItem]:
|
|
"""Swap a row with its previous or next sibling."""
|
|
|
|
def __init__(
|
|
self, repo: OrderedRepository[T], uow: UnitOfWork, not_found: type[DomainError]
|
|
) -> None:
|
|
self.repo = repo
|
|
self.uow = uow
|
|
self.not_found = not_found
|
|
|
|
def execute(self, item_id: int, direction: Direction) -> None:
|
|
with self.uow:
|
|
item = self.repo.get(item_id)
|
|
if item is None:
|
|
raise self.not_found()
|
|
self.repo.set_order(move(self.repo.sibling_ids(item), item_id, direction))
|