From 783338635005e9b9ece8ccc2f1a15dbfcebb5f66 Mon Sep 17 00:00:00 2001 From: luxferre Date: Wed, 3 Jun 2026 09:29:06 +0100 Subject: [PATCH] feat: patch questionnaire doesn't overwrite with none --- src/organisation/router.py | 13 +++++++++++-- test/test_organisation.py | 2 +- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/organisation/router.py b/src/organisation/router.py index 6204e1e..6c9ab13 100644 --- a/src/organisation/router.py +++ b/src/organisation/router.py @@ -39,7 +39,7 @@ from src.organisation.schemas import OrgPostOrgRequest, OrgPatchQuestionnaireReq OrgPatchContactRequest, \ OrgPostUserRequest, OrgGetUserResponse, OrgGetContactResponse, OrgGetOrgResponse, OrgPatchRootRequest, \ OrgGetGroupResponse, OrgDeleteUserRequest, OrgDeleteOrgRequest, OrgPostOrgResponse, OrgPatchQuestionnaireResponse, \ - OrgPatchStatusResponse, OrgPostUserResponse, OrgPatchRootResponse + OrgPatchStatusResponse, OrgPostUserResponse, OrgPatchRootResponse, Questionnaire router = APIRouter( prefix="/org", @@ -131,12 +131,21 @@ async def update_questionnaire(db: db_dependency, org_model: org_model_root_clai The partial bool allows for submission of partially completed questionnaire and/or final "are you sure" check before setting the org to be in "submitted" status, awaiting admin approval. """ - org_model.intake_questionnaire = request_model.intake_questionnaire.model_dump() + update_data = request_model.intake_questionnaire.model_dump(exclude_none=True) + questionnaire_model = Questionnaire(**org_model.intake_questionnaire) + for key, value in update_data.items(): + if hasattr(questionnaire_model, key): + setattr(questionnaire_model, key, value) + else: + if key == "partial" or key == "organisation_id": + continue + raise UnprocessableContentException("Invalid keys in update request") # Allows for partially completed questionnaires to be saved without being submitted for review if not request_model.partial: org_model.status = "submitted" + org_model.intake_questionnaire = questionnaire_model.model_dump() db.flush() response = OrgPatchQuestionnaireResponse(**org_model.__dict__) db.commit() diff --git a/test/test_organisation.py b/test/test_organisation.py index 906563d..6b4648c 100644 --- a/test/test_organisation.py +++ b/test/test_organisation.py @@ -76,7 +76,7 @@ async def test_patch_org_questionnaire_partial_success(client: AsyncClient, db_s assert data["name"] == "Test Org" assert data["intake_questionnaire"]["question_one"] == "new answer one" assert data["status"] == "partial" - # assert type(data["intake_questionnaire"]["question_two"]) == str + assert data["intake_questionnaire"]["question_two"] == "answer two" assert data["intake_questionnaire"]["question_three"] is None