feat: questionnaire shape update
This commit is contained in:
parent
c268097306
commit
0a7f9092c7
7 changed files with 85 additions and 24 deletions
|
|
@ -16,6 +16,7 @@ Endpoints:
|
|||
- [PATCH](/org/contact): [root user]: Updates the (contact_type) contact for an org(id). Any number of details can be changed.
|
||||
"""
|
||||
|
||||
from datetime import datetime, timezone
|
||||
from typing import Annotated
|
||||
|
||||
from fastapi import APIRouter, status
|
||||
|
|
@ -29,6 +30,7 @@ from src.contact.models import Contact
|
|||
from src.contact.schemas import ContactAddress
|
||||
from src.contact.exceptions import ContactNotFoundException
|
||||
from src.database import db_dependency
|
||||
from src.organisation.schemas_questionnaires import QuestionnaireQuestionsVersion0
|
||||
from src.user.dependencies import (
|
||||
user_model_body_dependency,
|
||||
user_model_claims_dependency,
|
||||
|
|
@ -64,6 +66,7 @@ from src.organisation.schemas import (
|
|||
OrgPatchRootResponse,
|
||||
Questionnaire,
|
||||
OrgPatchContactResponse,
|
||||
QuestionnaireMetadata,
|
||||
)
|
||||
|
||||
router = APIRouter(
|
||||
|
|
@ -88,7 +91,9 @@ router = APIRouter(
|
|||
},
|
||||
},
|
||||
)
|
||||
async def get_org_by_id(org_model: org_model_root_claim_query_dependency):
|
||||
async def get_org_by_id(
|
||||
db: db_dependency, org_model: org_model_root_claim_query_dependency
|
||||
):
|
||||
"""
|
||||
Returns organisation details including key member email addresses
|
||||
"""
|
||||
|
|
@ -143,10 +148,21 @@ async def create_org(
|
|||
ALl organisations are given the "partial" status on creation. See update_questionnaire() for more details.
|
||||
"""
|
||||
if request_model.intake_questionnaire:
|
||||
intake_questionnaire = request_model.intake_questionnaire.model_dump()
|
||||
questionnaire_questions = request_model.intake_questionnaire.model_dump()
|
||||
else:
|
||||
intake_questionnaire = None
|
||||
org_model = Org(name=request_model.name, intake_questionnaire=intake_questionnaire)
|
||||
questionnaire_questions = QuestionnaireQuestionsVersion0().model_dump()
|
||||
|
||||
questionnaire_metadata = QuestionnaireMetadata(version=0, submission_date=None)
|
||||
|
||||
intake_questionnaire = Questionnaire(
|
||||
metadata=questionnaire_metadata,
|
||||
questions=questionnaire_questions,
|
||||
)
|
||||
|
||||
org_model = Org(
|
||||
name=request_model.name,
|
||||
intake_questionnaire=intake_questionnaire.model_dump(mode="json"),
|
||||
)
|
||||
|
||||
org_model.status = "partial"
|
||||
|
||||
|
|
@ -204,18 +220,27 @@ async def update_questionnaire(
|
|||
final "are you sure" check before setting the org to be in "submitted" status, awaiting admin approval.
|
||||
"""
|
||||
update_data = request_model.intake_questionnaire.model_dump(exclude_none=True)
|
||||
questionnaire_model = Questionnaire(**org_model.intake_questionnaire)
|
||||
questionnaire = org_model.intake_questionnaire
|
||||
questions_model = QuestionnaireQuestionsVersion0(**questionnaire["questions"])
|
||||
for key, value in update_data.items():
|
||||
if hasattr(questionnaire_model, key):
|
||||
setattr(questionnaire_model, key, value)
|
||||
if hasattr(questions_model, key):
|
||||
setattr(questions_model, key, value)
|
||||
else:
|
||||
raise UnprocessableContentException("Invalid keys in update request")
|
||||
|
||||
metadata = QuestionnaireMetadata(version=questionnaire["metadata"]["version"])
|
||||
|
||||
# Allows for partially completed questionnaires to be saved without being submitted for review
|
||||
if not request_model.partial:
|
||||
org_model.status = "submitted"
|
||||
metadata.submission_date = datetime.now(timezone.utc)
|
||||
|
||||
org_model.intake_questionnaire = questionnaire_model.model_dump()
|
||||
questionnaire_model = Questionnaire(
|
||||
metadata=metadata,
|
||||
questions=questions_model,
|
||||
)
|
||||
|
||||
org_model.intake_questionnaire = questionnaire_model.model_dump(mode="json")
|
||||
db.flush()
|
||||
response = OrgPatchQuestionnaireResponse(**org_model.__dict__)
|
||||
db.commit()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue