From b3ab62767647d82b1cdf687fb3f5ef05c1ecb0e1 Mon Sep 17 00:00:00 2001 From: Steven Date: Mon, 25 Nov 2024 17:01:48 +0000 Subject: [PATCH] WIP: moves patching logic down to the service layer Moving the logic for updating applications into the code fetching the application and forms lets us update a copy of each directly and then run one single db commit transaction -- the code should read a bit more simply and we should get the "all or nothing" guarantee of a transaction. Also expose various properties for the API the frontend relies on. --- api/routes/application/routes.py | 38 +++---------------------------- db/queries/application/queries.py | 27 ++++++++++++++++++++++ db/queries/statuses/queries.py | 8 ++++--- db/schemas/form.py | 2 ++ 4 files changed, 37 insertions(+), 38 deletions(-) diff --git a/api/routes/application/routes.py b/api/routes/application/routes.py index a1094eda..400fc511 100644 --- a/api/routes/application/routes.py +++ b/api/routes/application/routes.py @@ -22,11 +22,10 @@ from db.queries import submit_application from db.queries import upsert_feedback from db.queries.application import create_qa_base64file -from db.queries.application.queries import patch_application +from db.queries.application.queries import patch from db.queries.feedback import retrieve_all_feedbacks_and_surveys from db.queries.feedback import retrieve_end_of_application_survey_data from db.queries.feedback import upsert_end_of_application_survey_data -from db.queries.form.queries import patch_form from db.queries.reporting.queries import export_application_statuses_to_csv from db.queries.reporting.queries import map_application_key_fields from db.queries.research import retrieve_research_survey_data @@ -422,7 +421,7 @@ def get_research_survey_data(self, application_id): def post_request_changes(self, application_id: str): try: - application = get_application( + get_application( application_id, as_json=True, include_forms=True, @@ -437,35 +436,4 @@ def post_request_changes(self, application_id: str): field_ids = args["field_ids"] feedback_message = args["feedback_message"] - application_requires_changes = False - for application_form in application["forms"]: - from_requires_changes = False - forms_json = [] - - for question in application_form["questions"]: - forms_json_queston = question - for field in question["fields"]: - if field["key"] in field_ids: - from_requires_changes = True - application_requires_changes = True - forms_json_queston["status"] = "CHANGES_REQUESTED" - - forms_json_queston["fields"] = [field] - forms_json.append(forms_json_queston) - - if from_requires_changes: - form_patch_fields = { - "json": forms_json, - "status": "CHANGES_REQUESTED", - "has_completed": False, - "feedback_message": feedback_message, - } - - patch_form(application_id, application_form["name"], form_patch_fields) - - if application_requires_changes: - application_patch_fields = { - "status": "CHANGES_REQUESTED", - } - - patch_application(application_id, application_patch_fields) + patch(application_id=application_id, field_ids=field_ids, message=feedback_message) diff --git a/db/queries/application/queries.py b/db/queries/application/queries.py index 2ac2ce6c..37bcaf0f 100644 --- a/db/queries/application/queries.py +++ b/db/queries/application/queries.py @@ -12,6 +12,7 @@ from db.exceptions import ApplicationError from db.models import Applications from db.models.application.enums import Status as ApplicationStatus +from db.models.forms.enums import Status from db.schemas import ApplicationSchema from external_services import get_fund from external_services import get_round @@ -246,6 +247,9 @@ def submit_application(application_id) -> Applications: all_application_files = list_files_by_prefix(application_id) application = process_files(application, all_application_files) + for form in application.forms: + form.feedback_message = None + application.status = "SUBMITTED" db.session.commit() return application @@ -311,3 +315,26 @@ def patch_application(application_id: str, patch_fields: dict) -> Applications: db.session.commit() return application + + +def patch(application_id: str, field_ids: list, message: str): + application = get_application(application_id, include_forms=True) + + application_should_update = False + for form in application.forms: + form_should_update = False + for category in form.json: + for field in category["fields"]: + if field["key"] in field_ids: + form.status = Status.CHANGES_REQUESTED + form.has_completed = False + form.feedback_message = message + form_should_update = True + application_should_update = True + + if field["key"] == "markAsComplete" and form_should_update: + field["answer"] = False + + if application_should_update: + application.status = ApplicationStatus.CHANGES_REQUESTED + db.session.commit() diff --git a/db/queries/statuses/queries.py b/db/queries/statuses/queries.py index f0762f0e..6042528c 100644 --- a/db/queries/statuses/queries.py +++ b/db/queries/statuses/queries.py @@ -77,7 +77,9 @@ def update_application_status( ) form_statuses = [form.status.name for form in application_with_forms.forms] - if "IN_PROGRESS" in form_statuses: + if "CHANGES_REQUESTED" in form_statuses: + status = "CHANGES_REQUESTED" + elif "IN_PROGRESS" in form_statuses: status = "IN_PROGRESS" elif "COMPLETED" in form_statuses and ("NOT_STARTED" in form_statuses or not all_feedback_and_survey_completed): status = "IN_PROGRESS" @@ -129,7 +131,7 @@ def update_form_status( form_to_update.status = "COMPLETED" form_to_update.has_completed = True else: - form_to_update.status = "IN_PROGRESS" + form_to_update.status = "CHANGES_REQUESTED" if form_to_update.feedback_message else "IN_PROGRESS" form_to_update.has_completed = False else: form_to_update.status = "COMPLETED" @@ -138,7 +140,7 @@ def update_form_status( # All question pages have answers and form has previously completed form_to_update.status = "COMPLETED" else: - form_to_update.status = "IN_PROGRESS" + form_to_update.status = "CHANGES_REQUESTED" if form_to_update.feedback_message else "IN_PROGRESS" def _is_field_answered(field: dict) -> bool: diff --git a/db/schemas/form.py b/db/schemas/form.py index 41c60b08..96b13593 100644 --- a/db/schemas/form.py +++ b/db/schemas/form.py @@ -12,3 +12,5 @@ class Meta: status = Enum(Status) name = auto_field() questions = auto_field("json") + feedback_message = auto_field() + has_completed = auto_field()