2026-04-06 12:41:49 +01:00
|
|
|
"""
|
2026-05-28 14:23:36 +01:00
|
|
|
Dependencies related to the organisation module
|
2026-04-06 12:41:49 +01:00
|
|
|
|
2026-05-28 14:23:36 +01:00
|
|
|
Exports:
|
|
|
|
|
- org_model_query_dependency: org_model: Gets org model from db, if it exists. Uses org_id from query param. Also verifies if the org has been approved.
|
|
|
|
|
- org_model_body_dependency: org_model: Gets org model from db, if it exists. Uses org_id from request body. Also verifies if the org has been approved.
|
2026-05-25 09:05:17 +01:00
|
|
|
"""
|
2026-06-08 15:31:37 +01:00
|
|
|
|
2026-05-28 10:55:39 +01:00
|
|
|
from typing import Annotated, Optional
|
2026-05-29 09:24:51 +01:00
|
|
|
from sqlalchemy.orm import Session
|
2026-05-25 09:05:17 +01:00
|
|
|
|
2026-05-28 10:55:39 +01:00
|
|
|
from fastapi import Depends, Query, Request
|
2026-05-25 09:05:17 +01:00
|
|
|
|
|
|
|
|
from src.database import db_dependency
|
|
|
|
|
|
2026-05-27 12:21:03 +01:00
|
|
|
from src.organisation.schemas import OrgIDMixin
|
2026-05-25 09:05:17 +01:00
|
|
|
from src.organisation.models import Organisation as Org
|
2026-05-28 10:55:39 +01:00
|
|
|
from src.organisation.exceptions import OrgNotFoundException, AwaitingApprovalException
|
|
|
|
|
from src.organisation.constants import Status as OrgStatus
|
2026-05-25 09:05:17 +01:00
|
|
|
|
|
|
|
|
|
2026-05-29 09:24:51 +01:00
|
|
|
def get_org_model(db: Session, request: Request, org_id: int):
|
2026-05-27 12:21:03 +01:00
|
|
|
org_model = db.get(Org, org_id)
|
2026-05-25 09:05:17 +01:00
|
|
|
if org_model is None:
|
2026-05-27 12:21:03 +01:00
|
|
|
raise OrgNotFoundException(org_id)
|
2026-05-25 09:05:17 +01:00
|
|
|
|
2026-05-29 09:24:51 +01:00
|
|
|
root = "/api/v1"
|
|
|
|
|
|
2026-06-08 15:31:37 +01:00
|
|
|
pre_approval_endpoints = [
|
|
|
|
|
f"PATCH{root}/org/status",
|
|
|
|
|
f"PATCH{root}/org/questionnaire",
|
|
|
|
|
f"GET{root}/org",
|
|
|
|
|
f"GET{root}/org/contact",
|
|
|
|
|
f"PATCH{root}/org/contact",
|
2026-06-10 10:15:27 +01:00
|
|
|
f"DELETE{root}/org/self",
|
2026-06-08 15:31:37 +01:00
|
|
|
]
|
2026-05-28 10:55:39 +01:00
|
|
|
current_request = f"{request.method}{request.url.path}"
|
2026-06-08 15:31:37 +01:00
|
|
|
if (
|
|
|
|
|
current_request not in pre_approval_endpoints
|
|
|
|
|
and org_model.status != OrgStatus.APPROVED
|
|
|
|
|
):
|
2026-05-28 10:55:39 +01:00
|
|
|
raise AwaitingApprovalException(org_id)
|
|
|
|
|
|
2026-05-25 09:05:17 +01:00
|
|
|
return org_model
|
|
|
|
|
|
2026-05-28 10:55:39 +01:00
|
|
|
|
2026-06-08 15:31:37 +01:00
|
|
|
def get_org_model_query(
|
|
|
|
|
db: db_dependency, request: Request, org_id: Annotated[int, Query(gt=0)]
|
|
|
|
|
) -> type[Org]:
|
2026-05-28 10:55:39 +01:00
|
|
|
return get_org_model(db, request, org_id)
|
|
|
|
|
|
2026-06-08 15:31:37 +01:00
|
|
|
|
2026-05-27 12:21:03 +01:00
|
|
|
org_model_query_dependency = Annotated[type[Org], Depends(get_org_model_query)]
|
|
|
|
|
|
|
|
|
|
|
2026-06-08 15:31:37 +01:00
|
|
|
def get_org_model_body(
|
|
|
|
|
db: db_dependency, request: Request, request_model: OrgIDMixin
|
|
|
|
|
) -> type[Org]:
|
2026-05-28 10:55:39 +01:00
|
|
|
org_id: Optional[int] = getattr(request_model, "organisation_id", None)
|
2026-05-27 12:21:03 +01:00
|
|
|
if org_id is None:
|
2026-06-11 16:02:51 +01:00
|
|
|
raise OrgNotFoundException()
|
2026-05-27 12:21:03 +01:00
|
|
|
|
2026-05-28 10:55:39 +01:00
|
|
|
return get_org_model(db, request, org_id)
|
2026-05-27 12:21:03 +01:00
|
|
|
|
2026-06-08 15:31:37 +01:00
|
|
|
|
2026-05-27 12:21:03 +01:00
|
|
|
org_model_body_dependency = Annotated[type[Org], Depends(get_org_model_body)]
|