Vaihe 1/3: pohjatiedot (palveluyksiköt, palvelut ja työtehtävät)
- 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]>
This commit is contained in:
@@ -0,0 +1,64 @@
|
||||
from django.db import models
|
||||
|
||||
from apps.catalog.domain.value_objects import NAME_MAX_LENGTH
|
||||
from apps.shared.infrastructure.models import TimestampedModel
|
||||
|
||||
|
||||
class ServiceUnitModel(TimestampedModel):
|
||||
name = models.CharField(max_length=NAME_MAX_LENGTH)
|
||||
is_shared = models.BooleanField(default=False)
|
||||
is_active = models.BooleanField(default=True)
|
||||
sort_order = models.PositiveIntegerField(default=0)
|
||||
|
||||
class Meta:
|
||||
db_table = "service_units"
|
||||
ordering = ["sort_order", "id"]
|
||||
# The utf8mb4 collation is case-insensitive, matching the rule in the domain.
|
||||
constraints = [models.UniqueConstraint(fields=["name"], name="service_units_name_uniq")]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
|
||||
|
||||
class ServiceModel(TimestampedModel):
|
||||
service_unit = models.ForeignKey(
|
||||
ServiceUnitModel, on_delete=models.PROTECT, related_name="services"
|
||||
)
|
||||
name = models.CharField(max_length=NAME_MAX_LENGTH)
|
||||
is_active = models.BooleanField(default=True)
|
||||
sort_order = models.PositiveIntegerField(default=0)
|
||||
|
||||
class Meta:
|
||||
db_table = "services"
|
||||
ordering = ["sort_order", "id"]
|
||||
constraints = [
|
||||
models.UniqueConstraint(fields=["service_unit", "name"], name="services_unit_name_uniq")
|
||||
]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
|
||||
|
||||
class WorkTypeModel(TimestampedModel):
|
||||
# Unique name per level is checked in the use case: a NULL service would slip past
|
||||
# a database unique constraint.
|
||||
service_unit = models.ForeignKey(
|
||||
ServiceUnitModel, on_delete=models.PROTECT, related_name="work_types"
|
||||
)
|
||||
service = models.ForeignKey(
|
||||
ServiceModel,
|
||||
on_delete=models.PROTECT,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name="work_types",
|
||||
)
|
||||
name = models.CharField(max_length=NAME_MAX_LENGTH)
|
||||
is_active = models.BooleanField(default=True)
|
||||
sort_order = models.PositiveIntegerField(default=0)
|
||||
|
||||
class Meta:
|
||||
db_table = "work_types"
|
||||
ordering = ["sort_order", "id"]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return self.name
|
||||
@@ -0,0 +1,111 @@
|
||||
from collections.abc import Iterable, Sequence
|
||||
|
||||
from django.db.models import Q
|
||||
|
||||
from apps.catalog.application.dto import (
|
||||
ServiceNode,
|
||||
ServiceUnitNode,
|
||||
ServiceUnitSummary,
|
||||
WorkTypeNode,
|
||||
WorkTypeOption,
|
||||
)
|
||||
from apps.catalog.infrastructure.orm import ServiceUnitModel, WorkTypeModel
|
||||
|
||||
|
||||
def _is_selectable(model: WorkTypeModel) -> bool:
|
||||
return (
|
||||
model.is_active
|
||||
and model.service_unit.is_active
|
||||
and (model.service is None or model.service.is_active)
|
||||
)
|
||||
|
||||
|
||||
def _option(model: WorkTypeModel) -> WorkTypeOption:
|
||||
return WorkTypeOption(
|
||||
id=model.pk,
|
||||
name=model.name,
|
||||
service_unit_id=model.service_unit_id,
|
||||
service_unit_name=model.service_unit.name,
|
||||
service_name=model.service.name if model.service else None,
|
||||
is_selectable=_is_selectable(model),
|
||||
)
|
||||
|
||||
|
||||
# Selectable = the row and every parent above it are active (specification 16.3).
|
||||
_SELECTABLE = Q(is_active=True, service_unit__is_active=True) & (
|
||||
Q(service__isnull=True) | Q(service__is_active=True)
|
||||
)
|
||||
|
||||
|
||||
class DjangoCatalogQueries:
|
||||
def tree(self) -> Sequence[ServiceUnitNode]:
|
||||
units = ServiceUnitModel.objects.prefetch_related("services", "work_types")
|
||||
result = []
|
||||
for unit in units:
|
||||
by_service: dict[int | None, list[WorkTypeNode]] = {}
|
||||
services = {s.pk: s for s in unit.services.all()}
|
||||
for wt in unit.work_types.all():
|
||||
service = services.get(wt.service_id) if wt.service_id else None
|
||||
by_service.setdefault(wt.service_id, []).append(
|
||||
WorkTypeNode(
|
||||
id=wt.pk,
|
||||
service_unit_id=unit.pk,
|
||||
service_id=wt.service_id,
|
||||
name=wt.name,
|
||||
is_active=wt.is_active,
|
||||
is_selectable=wt.is_active
|
||||
and unit.is_active
|
||||
and (service is None or service.is_active),
|
||||
)
|
||||
)
|
||||
result.append(
|
||||
ServiceUnitNode(
|
||||
id=unit.pk,
|
||||
name=unit.name,
|
||||
is_shared=unit.is_shared,
|
||||
is_active=unit.is_active,
|
||||
services=tuple(
|
||||
ServiceNode(
|
||||
id=s.pk,
|
||||
service_unit_id=unit.pk,
|
||||
name=s.name,
|
||||
is_active=s.is_active,
|
||||
work_types=tuple(by_service.get(s.pk, [])),
|
||||
)
|
||||
for s in services.values()
|
||||
),
|
||||
work_types=tuple(by_service.get(None, [])),
|
||||
)
|
||||
)
|
||||
return result
|
||||
|
||||
def active_service_units(self) -> Sequence[ServiceUnitSummary]:
|
||||
return [
|
||||
ServiceUnitSummary(id=u.pk, name=u.name, is_shared=u.is_shared)
|
||||
for u in ServiceUnitModel.objects.filter(is_active=True)
|
||||
]
|
||||
|
||||
def work_type_options(self, service_unit_ids: Iterable[int]) -> Sequence[WorkTypeOption]:
|
||||
qs = (
|
||||
WorkTypeModel.objects.filter(_SELECTABLE)
|
||||
.filter(Q(service_unit_id__in=list(service_unit_ids)) | Q(service_unit__is_shared=True))
|
||||
.select_related("service_unit", "service")
|
||||
.order_by(
|
||||
"service_unit__is_shared",
|
||||
"service_unit__sort_order",
|
||||
"service_unit_id",
|
||||
"service__sort_order",
|
||||
"service_id",
|
||||
"sort_order",
|
||||
"id",
|
||||
)
|
||||
)
|
||||
return [_option(m) for m in qs]
|
||||
|
||||
def get_work_type(self, work_type_id: int) -> WorkTypeOption | None:
|
||||
model = (
|
||||
WorkTypeModel.objects.select_related("service_unit", "service")
|
||||
.filter(pk=work_type_id)
|
||||
.first()
|
||||
)
|
||||
return _option(model) if model else None
|
||||
@@ -0,0 +1,156 @@
|
||||
from collections.abc import Sequence
|
||||
from typing import Any
|
||||
|
||||
from django.db.models import Max, Model, QuerySet
|
||||
|
||||
from apps.catalog.domain.entities import Service, ServiceUnit, WorkType
|
||||
from apps.catalog.domain.value_objects import CatalogName
|
||||
from apps.catalog.infrastructure.orm import ServiceModel, ServiceUnitModel, WorkTypeModel
|
||||
|
||||
|
||||
def _next_sort_order(qs: QuerySet[Any]) -> int:
|
||||
return int(qs.aggregate(m=Max("sort_order"))["m"] or 0) + 1
|
||||
|
||||
|
||||
def _ids(qs: QuerySet[Any]) -> list[int]:
|
||||
return list(qs.order_by("sort_order", "id").values_list("id", flat=True))
|
||||
|
||||
|
||||
def _set_order(model: type[Model], ids: Sequence[int]) -> None:
|
||||
for position, pk in enumerate(ids, start=1):
|
||||
model._default_manager.filter(pk=pk).update(sort_order=position)
|
||||
|
||||
|
||||
def service_unit_entity(model: ServiceUnitModel) -> ServiceUnit:
|
||||
return ServiceUnit.reconstitute(
|
||||
id=model.pk,
|
||||
name=CatalogName(model.name),
|
||||
is_shared=model.is_shared,
|
||||
is_active=model.is_active,
|
||||
sort_order=model.sort_order,
|
||||
)
|
||||
|
||||
|
||||
class DjangoServiceUnitRepository:
|
||||
def get(self, item_id: int) -> ServiceUnit | None:
|
||||
model = ServiceUnitModel.objects.filter(pk=item_id).first()
|
||||
return service_unit_entity(model) if model else None
|
||||
|
||||
def save(self, item: ServiceUnit) -> ServiceUnit:
|
||||
model = ServiceUnitModel() if item.id is None else ServiceUnitModel.objects.get(pk=item.id)
|
||||
model.name = item.name.value
|
||||
model.is_shared = item.is_shared
|
||||
model.is_active = item.is_active
|
||||
model.sort_order = item.sort_order
|
||||
model.save()
|
||||
return item.with_id(model.pk)
|
||||
|
||||
def is_name_taken(self, name: CatalogName, *, exclude_id: int | None = None) -> bool:
|
||||
qs = ServiceUnitModel.objects.filter(name__iexact=name.value)
|
||||
if exclude_id is not None:
|
||||
qs = qs.exclude(pk=exclude_id)
|
||||
return qs.exists()
|
||||
|
||||
def next_sort_order(self) -> int:
|
||||
return _next_sort_order(ServiceUnitModel.objects.all())
|
||||
|
||||
def sibling_ids(self, item: ServiceUnit) -> list[int]:
|
||||
return _ids(ServiceUnitModel.objects.all())
|
||||
|
||||
def set_order(self, ids: Sequence[int]) -> None:
|
||||
_set_order(ServiceUnitModel, ids)
|
||||
|
||||
|
||||
def service_entity(model: ServiceModel) -> Service:
|
||||
return Service.reconstitute(
|
||||
id=model.pk,
|
||||
service_unit_id=model.service_unit_id,
|
||||
name=CatalogName(model.name),
|
||||
is_active=model.is_active,
|
||||
sort_order=model.sort_order,
|
||||
)
|
||||
|
||||
|
||||
class DjangoServiceRepository:
|
||||
def get(self, item_id: int) -> Service | None:
|
||||
model = ServiceModel.objects.filter(pk=item_id).first()
|
||||
return service_entity(model) if model else None
|
||||
|
||||
def save(self, item: Service) -> Service:
|
||||
model = ServiceModel() if item.id is None else ServiceModel.objects.get(pk=item.id)
|
||||
model.service_unit_id = item.service_unit_id
|
||||
model.name = item.name.value
|
||||
model.is_active = item.is_active
|
||||
model.sort_order = item.sort_order
|
||||
model.save()
|
||||
return item.with_id(model.pk)
|
||||
|
||||
def is_name_taken(
|
||||
self, service_unit_id: int, name: CatalogName, *, exclude_id: int | None = None
|
||||
) -> bool:
|
||||
qs = ServiceModel.objects.filter(service_unit_id=service_unit_id, name__iexact=name.value)
|
||||
if exclude_id is not None:
|
||||
qs = qs.exclude(pk=exclude_id)
|
||||
return qs.exists()
|
||||
|
||||
def next_sort_order(self, service_unit_id: int) -> int:
|
||||
return _next_sort_order(ServiceModel.objects.filter(service_unit_id=service_unit_id))
|
||||
|
||||
def sibling_ids(self, item: Service) -> list[int]:
|
||||
return _ids(ServiceModel.objects.filter(service_unit_id=item.service_unit_id))
|
||||
|
||||
def set_order(self, ids: Sequence[int]) -> None:
|
||||
_set_order(ServiceModel, ids)
|
||||
|
||||
|
||||
def work_type_entity(model: WorkTypeModel) -> WorkType:
|
||||
return WorkType.reconstitute(
|
||||
id=model.pk,
|
||||
service_unit_id=model.service_unit_id,
|
||||
service_id=model.service_id,
|
||||
name=CatalogName(model.name),
|
||||
is_active=model.is_active,
|
||||
sort_order=model.sort_order,
|
||||
)
|
||||
|
||||
|
||||
def _level(service_unit_id: int, service_id: int | None) -> QuerySet[WorkTypeModel]:
|
||||
return WorkTypeModel.objects.filter(service_unit_id=service_unit_id, service_id=service_id)
|
||||
|
||||
|
||||
class DjangoWorkTypeRepository:
|
||||
def get(self, item_id: int) -> WorkType | None:
|
||||
model = WorkTypeModel.objects.filter(pk=item_id).first()
|
||||
return work_type_entity(model) if model else None
|
||||
|
||||
def save(self, item: WorkType) -> WorkType:
|
||||
model = WorkTypeModel() if item.id is None else WorkTypeModel.objects.get(pk=item.id)
|
||||
model.service_unit_id = item.service_unit_id
|
||||
model.service_id = item.service_id
|
||||
model.name = item.name.value
|
||||
model.is_active = item.is_active
|
||||
model.sort_order = item.sort_order
|
||||
model.save()
|
||||
return item.with_id(model.pk)
|
||||
|
||||
def is_name_taken(
|
||||
self,
|
||||
service_unit_id: int,
|
||||
service_id: int | None,
|
||||
name: CatalogName,
|
||||
*,
|
||||
exclude_id: int | None = None,
|
||||
) -> bool:
|
||||
qs = _level(service_unit_id, service_id).filter(name__iexact=name.value)
|
||||
if exclude_id is not None:
|
||||
qs = qs.exclude(pk=exclude_id)
|
||||
return qs.exists()
|
||||
|
||||
def next_sort_order(self, service_unit_id: int, service_id: int | None) -> int:
|
||||
return _next_sort_order(_level(service_unit_id, service_id))
|
||||
|
||||
def sibling_ids(self, item: WorkType) -> list[int]:
|
||||
return _ids(_level(item.service_unit_id, item.service_id))
|
||||
|
||||
def set_order(self, ids: Sequence[int]) -> None:
|
||||
_set_order(WorkTypeModel, ids)
|
||||
Reference in New Issue
Block a user