from __future__ import annotations from pathlib import Path from govoplan_core.core.access import CAPABILITY_AUTH_PERMISSION_EVALUATOR, CAPABILITY_AUTH_PRINCIPAL_RESOLVER from govoplan_core.core.module_guards import drop_table_retirement_provider, persistent_table_uninstall_guard from govoplan_core.core.modules import ( DocumentationTopic, FrontendModule, MigrationSpec, ModuleContext, ModuleInterfaceProvider, ModuleInterfaceRequirement, ModuleManifest, NavItem, PermissionDefinition, RoleTemplate, ) from govoplan_core.db.base import Base from govoplan_scheduling.backend.db import models as scheduling_models # noqa: F401 - populate Scheduling ORM metadata MODULE_ID = "scheduling" MODULE_NAME = "Scheduling" MODULE_VERSION = "0.1.8" READ_SCOPE = "scheduling:schedule:read" WRITE_SCOPE = "scheduling:schedule:write" ADMIN_SCOPE = "scheduling:schedule:admin" RESPOND_SCOPE = "scheduling:availability:write" def _permission(scope: str, label: str, description: str) -> PermissionDefinition: module_id, resource, action = scope.split(":", 2) return PermissionDefinition( scope=scope, label=label, description=description, category="Scheduling", level="tenant", module_id=module_id, resource=resource, action=action, ) PERMISSIONS = ( _permission(READ_SCOPE, "View scheduling", "Read scheduling polls, proposals, participant state, and selected outcomes."), _permission(WRITE_SCOPE, "Manage scheduling", "Create and update scheduling polls, candidate slots, reminders, and decision handoff."), _permission(ADMIN_SCOPE, "Administer scheduling", "Configure tenant-level scheduling policies, external participation, and retention defaults."), _permission(RESPOND_SCOPE, "Respond to scheduling polls", "Submit and update own scheduling availability responses."), ) ROLE_TEMPLATES = ( RoleTemplate( slug="scheduling_manager", name="Scheduling manager", description="Create scheduling polls, manage candidate slots, and decide outcomes.", permissions=(READ_SCOPE, WRITE_SCOPE, RESPOND_SCOPE), ), RoleTemplate( slug="scheduling_participant", name="Scheduling participant", description="Read assigned scheduling polls and submit own availability.", permissions=(READ_SCOPE, RESPOND_SCOPE), ), ) DOCUMENTATION = ( DocumentationTopic( id="scheduling.module-boundary", title="Scheduling module boundary", summary="Meeting scheduling and Terminfindung built on reusable Poll availability primitives.", body=( "Scheduling owns meeting scheduling workflows, candidate slots, participant availability " "collection, conflict explanation, reminders, and decision handoff. It depends on Poll " "for reusable availability matrices and poll-backed workflow context, and can optionally " "trigger Evaluation for post-event feedback. " "Access is optional: when installed, Scheduling can use principal resolution, permission " "evaluation, groups, and role templates; without it, reduced signed-link or local organizer " "flows remain possible." ), layer="available", documentation_types=("admin",), audience=("operator", "module_admin", "product_owner"), related_modules=("poll", "evaluation", "calendar", "appointments", "mail", "notifications", "portal"), metadata={"seed": True}, ), ) def _tenant_summary(session, tenant_id: str) -> dict[str, int]: from govoplan_scheduling.backend.db.models import SchedulingCandidateSlot, SchedulingNotification, SchedulingParticipant, SchedulingRequest return { "scheduling_requests": ( session.query(SchedulingRequest) .filter(SchedulingRequest.tenant_id == tenant_id, SchedulingRequest.deleted_at.is_(None)) .count() ), "scheduling_candidate_slots": ( session.query(SchedulingCandidateSlot) .filter(SchedulingCandidateSlot.tenant_id == tenant_id, SchedulingCandidateSlot.deleted_at.is_(None)) .count() ), "scheduling_participants": ( session.query(SchedulingParticipant) .filter(SchedulingParticipant.tenant_id == tenant_id, SchedulingParticipant.deleted_at.is_(None)) .count() ), "scheduling_notifications": ( session.query(SchedulingNotification) .filter(SchedulingNotification.tenant_id == tenant_id, SchedulingNotification.status == "pending") .count() ), } def _scheduling_router(_context: ModuleContext): from govoplan_scheduling.backend.router import router return router manifest = ModuleManifest( id=MODULE_ID, name=MODULE_NAME, version=MODULE_VERSION, dependencies=("poll",), optional_dependencies=("access", "calendar", "appointments", "evaluation", "mail", "notifications", "portal", "workflow", "tasks", "idm", "organizations"), optional_capabilities=(CAPABILITY_AUTH_PRINCIPAL_RESOLVER, CAPABILITY_AUTH_PERMISSION_EVALUATOR), provides_interfaces=( ModuleInterfaceProvider(name="scheduling.candidate_slots", version="0.1.8"), ModuleInterfaceProvider(name="scheduling.decision_handoff", version="0.1.8"), ), requires_interfaces=( ModuleInterfaceRequirement(name="poll.availability_matrix", version_min="0.1.8", version_max_exclusive="0.2.0"), ModuleInterfaceRequirement(name="poll.workflow_context", version_min="0.1.8", version_max_exclusive="0.2.0"), ModuleInterfaceRequirement(name="poll.signed_participation", version_min="0.1.8", version_max_exclusive="0.2.0", optional=True), ModuleInterfaceRequirement(name="evaluation.feedback", version_min="0.1.8", version_max_exclusive="0.2.0", optional=True), ), permissions=PERMISSIONS, role_templates=ROLE_TEMPLATES, nav_items=(NavItem(path="/scheduling", label="Scheduling", icon="calendar", required_any=(READ_SCOPE,), order=56),), frontend=FrontendModule( module_id=MODULE_ID, package_name="@govoplan/scheduling-webui", nav_items=(NavItem(path="/scheduling", label="Scheduling", icon="calendar", required_any=(READ_SCOPE,), order=56),), ), route_factory=_scheduling_router, tenant_summary_providers=(_tenant_summary,), migration_spec=MigrationSpec( module_id=MODULE_ID, metadata=Base.metadata, script_location=str(Path(__file__).with_name("migrations") / "versions"), retirement_supported=True, retirement_provider=drop_table_retirement_provider( scheduling_models.SchedulingRequest, scheduling_models.SchedulingCandidateSlot, scheduling_models.SchedulingParticipant, scheduling_models.SchedulingNotification, label="Scheduling", ), retirement_notes="Destructive retirement drops scheduling-owned database tables after the installer captures a database snapshot.", ), uninstall_guard_providers=( persistent_table_uninstall_guard( scheduling_models.SchedulingRequest, scheduling_models.SchedulingCandidateSlot, scheduling_models.SchedulingParticipant, scheduling_models.SchedulingNotification, label="Scheduling", ), ), documentation=DOCUMENTATION, ) def get_manifest() -> ModuleManifest: return manifest