From 749a247673ee96f205177d898cd2ea1a194bab38 Mon Sep 17 00:00:00 2001 From: RamuniN Date: Wed, 18 Oct 2023 13:17:03 +0100 Subject: [PATCH] added cyp report mapping --- .../cof_r3w2_key_report_mapping.py | 6 +++ .../cyp_r1_key_report_mapping.py | 18 +++++++++ config/key_report_mappings/mappings.py | 13 +++++++ db/queries/feedback/queries.py | 37 +++++++++---------- tests/test_queries.py | 11 ++++++ 5 files changed, 65 insertions(+), 20 deletions(-) create mode 100644 config/key_report_mappings/cyp_r1_key_report_mapping.py diff --git a/config/key_report_mappings/cof_r3w2_key_report_mapping.py b/config/key_report_mappings/cof_r3w2_key_report_mapping.py index ef4b7282..130d3c8a 100644 --- a/config/key_report_mappings/cof_r3w2_key_report_mapping.py +++ b/config/key_report_mappings/cof_r3w2_key_report_mapping.py @@ -6,6 +6,12 @@ COF_R3W2_KEY_REPORT_MAPPING = KeyReportMapping( round_id="6af19a5e-9cae-4f00-9194-cf10d2d7c8a7", mapping=[ + FormMappingItem( + form_name="applicant-information-cof-r3-w2", + form_name_cy="gwybodaeth-am-yr-ymgeisydd-cof-r3-w2", + key="NlHSBg", + return_field="applicant_email", + ), FormMappingItem( form_name="organisation-information-cof-r3-w2", form_name_cy="gwybodaeth-am-y-sefydliad-cof-r3-w2", diff --git a/config/key_report_mappings/cyp_r1_key_report_mapping.py b/config/key_report_mappings/cyp_r1_key_report_mapping.py new file mode 100644 index 00000000..31f48ac9 --- /dev/null +++ b/config/key_report_mappings/cyp_r1_key_report_mapping.py @@ -0,0 +1,18 @@ +from config.key_report_mappings.model import FormMappingItem +from config.key_report_mappings.model import KeyReportMapping + +CYP_R1_KEY_REPORT_MAPPING = KeyReportMapping( + round_id="888aae3d-7e2c-4523-b9c1-95952b3d1644", + mapping=[ + FormMappingItem( + form_name="applicant-information-cyp", + key="BKOHaM", + return_field="applicant_email", + ), + FormMappingItem( + form_name="about-your-organisation-cyp", + key="JbmcJE", + return_field="organisation_name", + ), + ], +) diff --git a/config/key_report_mappings/mappings.py b/config/key_report_mappings/mappings.py index fd3b63c2..293f6d4b 100644 --- a/config/key_report_mappings/mappings.py +++ b/config/key_report_mappings/mappings.py @@ -6,10 +6,14 @@ from config.key_report_mappings.cof_r3w2_key_report_mapping import ( COF_R3W2_KEY_REPORT_MAPPING, ) +from config.key_report_mappings.cyp_r1_key_report_mapping import ( + CYP_R1_KEY_REPORT_MAPPING, +) MAPPINGS = ( COF_R2_KEY_REPORT_MAPPING, COF_R3W2_KEY_REPORT_MAPPING, + CYP_R1_KEY_REPORT_MAPPING, ) ROUND_ID_TO_KEY_REPORT_MAPPING = defaultdict( @@ -17,3 +21,12 @@ lambda: COF_R2_KEY_REPORT_MAPPING.mapping, {m.round_id: m.mapping for m in MAPPINGS}, ) + + +def get_report_mapping_for_round(round_id): + if round_id == COF_R2_KEY_REPORT_MAPPING.round_id: + return COF_R2_KEY_REPORT_MAPPING + elif round_id == COF_R3W2_KEY_REPORT_MAPPING.round_id: + return COF_R3W2_KEY_REPORT_MAPPING + elif round_id == CYP_R1_KEY_REPORT_MAPPING.round_id: + return CYP_R1_KEY_REPORT_MAPPING diff --git a/db/queries/feedback/queries.py b/db/queries/feedback/queries.py index 8a0adaa3..607c069d 100644 --- a/db/queries/feedback/queries.py +++ b/db/queries/feedback/queries.py @@ -1,13 +1,15 @@ from datetime import datetime import jsonpath_rw_ext +from config.key_report_mappings.mappings import get_report_mapping_for_round from db import db from db.models import Applications from db.models import Feedback from db.models.feedback import EndOfApplicationSurveyFeedback from db.queries.application.queries import get_applications +from db.queries.reporting.queries import map_application_key_fields +from db.schemas.application import ApplicationSchema from db.schemas.end_of_application_survey import EndOfApplicationSurveyFeedbackSchema -from db.schemas.form import FormsRunnerSchema from external_services.data import get_application_sections @@ -116,35 +118,30 @@ def get_answer_value(form, search_key, search_value): filters.append(Applications.status == status) applications = get_applications(filters=filters, include_forms=True) + mapping_report = get_report_mapping_for_round(round_id) + # get section id & names map application_sections = get_application_sections(fund_id, round_id, language="en") for section in application_sections: section_names[str(section["id"])] = section["title"] # extract section feedbacks & end of survey feedbacks for all applications - serialiser = FormsRunnerSchema() eoas_serialiser = EndOfApplicationSurveyFeedbackSchema() + applicant_serialiser = ApplicationSchema() + for application in applications: # extract applicant email & organisation - for form in application.forms: - form_dict = serialiser.dump(form) - try: - if "applicant-information" in form.name: - applicant_email = get_answer_value( - form_dict, "title", "Lead contact email address" - ) - except Exception as e: - print(f"Coudn't extract applicant email. Exception :{e}") - applicant_email = "" - try: - if "organisation-information" in form.name: - applicant_organisation = get_answer_value( - form_dict, "title", "Organisation name" - ) - except Exception as e: - print(f"Coudn't extract applicant organisation. Exception :{e}") - applicant_organisation = "" + try: + result = map_application_key_fields( + applicant_serialiser.dump(application), mapping_report.mapping, round_id + ) + applicant_email = result["applicant_email"] + applicant_organisation = result["organisation_name"] + except Exception as e: + print(f"Coudn't extract applicant email & organisation. Exception :{e}") + applicant_email = "" + applicant_organisation = "" # extract sections feedback for feedback in application.feedbacks: diff --git a/tests/test_queries.py b/tests/test_queries.py index e2bfebb2..2f963fc5 100644 --- a/tests/test_queries.py +++ b/tests/test_queries.py @@ -383,6 +383,16 @@ def test_extract_postcode(input_str, expected_output): { "language": "en", "forms": [ + { + "name": "applicant-information-cof-r3-w2", + "questions": [ + { + "fields": [ + {"key": "NlHSBg", "answer": "test@test.com"}, + ] + } + ], + }, { "name": "organisation-information-cof-r3-w2", "questions": [ @@ -441,6 +451,7 @@ def test_extract_postcode(input_str, expected_output): }, { "eoi_reference": "Ref1234", + "applicant_email": "test@test.com", "organisation_name": "OrgName", "organisation_type": "Non-Profit", "asset_type": "Building",