Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
WIP: moves patching logic down to the service layer
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sfount committed Nov 25, 2024
1 parent aee7aed commit b3ab627
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 38 deletions.
38 changes: 3 additions & 35 deletions api/routes/application/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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)
27 changes: 27 additions & 0 deletions db/queries/application/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
8 changes: 5 additions & 3 deletions db/queries/statuses/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand All @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions db/schemas/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ class Meta:
status = Enum(Status)
name = auto_field()
questions = auto_field("json")
feedback_message = auto_field()
has_completed = auto_field()

0 comments on commit b3ab627

Please sign in to comment.