"""Who may set which status (specification 2.4, 5.3 and 16.4).""" from apps.jobs.domain.enums import JobStatus from apps.users.domain.enums import Role _EDITORS = frozenset({Role.ADMIN, Role.MANAGEMENT, Role.EMPLOYEE, Role.INTERN}) _LEADERS = frozenset({Role.ADMIN, Role.MANAGEMENT}) _INVOICE_STATUSES = frozenset({JobStatus.INVOICED, JobStatus.BILLED}) def can_set_status( role: Role, target: JobStatus, *, current: JobStatus | None = None, is_biller: bool = False ) -> bool: """Up to "done" every editor; "invoice created"/"invoiced" management or the job's biller (an employee); "archived" management only. Keeping the current status is always allowed.""" if role not in _EDITORS: return False if target == current: return True if target in _INVOICE_STATUSES: return role in _LEADERS or (is_biller and role is Role.EMPLOYEE) if target is JobStatus.ARCHIVED: return role in _LEADERS return True def allowed_statuses( role: Role, *, current: JobStatus | None = None, is_biller: bool = False ) -> list[JobStatus]: return [s for s in JobStatus if can_set_status(role, s, current=current, is_biller=is_biller)]