Add task category master for service task templates

This commit is contained in:
A R R R Associates
2026-08-30 21:42:32 +05:30
parent 31b576b363
commit ad0815c807
9 changed files with 328 additions and 11 deletions
+58
View File
@@ -11,6 +11,7 @@ from app.modules.services.models import (
ServiceCatalogue,
ServiceCategory,
ServiceDefaultTaskTemplate,
ServiceTaskCategory,
ServiceDueDateRule,
)
@@ -267,3 +268,60 @@ def get_default_task_template(
ServiceDefaultTaskTemplate.service_catalogue_id == catalogue_id,
)
).scalar_one_or_none()
def list_task_categories(db: Session, *, catalogue_id: int, tenant_id: int | None, include_inactive: bool = False):
query = select(ServiceTaskCategory).where(
ServiceTaskCategory.service_catalogue_id == catalogue_id,
ServiceTaskCategory.tenant_id.is_(None) if tenant_id is None else ServiceTaskCategory.tenant_id == tenant_id,
)
if not include_inactive:
query = query.where(ServiceTaskCategory.is_active.is_(True))
return db.execute(query.order_by(ServiceTaskCategory.sort_order.asc(), ServiceTaskCategory.name.asc(), ServiceTaskCategory.id.asc())).scalars().all()
def get_task_category(db: Session, *, category_id: int, catalogue_id: int, tenant_id: int | None) -> ServiceTaskCategory | None:
query = select(ServiceTaskCategory).where(
ServiceTaskCategory.id == category_id,
ServiceTaskCategory.service_catalogue_id == catalogue_id,
ServiceTaskCategory.tenant_id.is_(None) if tenant_id is None else ServiceTaskCategory.tenant_id == tenant_id,
)
return db.execute(query).scalar_one_or_none()
def ensure_task_category(db: Session, *, catalogue_id: int, tenant_id: int | None, name: str | None, user_id: int | None = None) -> ServiceTaskCategory | None:
clean_name = (name or "").strip()
if not clean_name:
return None
scope_filter = ServiceTaskCategory.tenant_id.is_(None) if tenant_id is None else ServiceTaskCategory.tenant_id == tenant_id
existing = db.execute(select(ServiceTaskCategory).where(
ServiceTaskCategory.service_catalogue_id == catalogue_id,
scope_filter,
func.lower(ServiceTaskCategory.name) == clean_name.lower(),
)).scalar_one_or_none()
if existing:
return existing
base_code = normalize_code(clean_name)[:50] or "CATEGORY"
code = base_code
suffix = 2
while db.execute(select(ServiceTaskCategory.id).where(
ServiceTaskCategory.service_catalogue_id == catalogue_id,
scope_filter,
ServiceTaskCategory.code == code,
)).scalar_one_or_none() is not None:
tail = f"-{suffix}"
code = f"{base_code[:50-len(tail)]}{tail}"
suffix += 1
row = ServiceTaskCategory(
tenant_id=tenant_id,
service_catalogue_id=catalogue_id,
code=code,
name=clean_name,
sort_order=100,
is_active=True,
created_by_user_id=user_id,
updated_by_user_id=user_id,
)
db.add(row)
db.flush()
return row