Skip to content

Commit

Permalink
validate field values
Browse files Browse the repository at this point in the history
  • Loading branch information
zandre-eng committed Feb 19, 2025
1 parent 80fb505 commit 24971cd
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
8 changes: 8 additions & 0 deletions corehq/apps/case_importer/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@
'user_or_case_id',
]

# Fields that must be absent or blank when importing cases with "payments" case type
MOMO_NO_EDIT_PAYMENT_FIELDS = [
'payment_verified',
'payment_submitted',
'payment_timestamp',
'payment_status',
]

MOMO_PAYMENT_CASE_TYPE = 'payment'


Expand Down
41 changes: 40 additions & 1 deletion corehq/apps/case_importer/do_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@
BULK_UPLOAD_DATE_OPENED,
CASE_IMPORT_DATA_DICTIONARY_VALIDATION,
DOMAIN_PERMISSIONS_MIRROR,
MOBILE_WORKER_VERIFICATION,
)
from corehq.util.metrics import metrics_counter, metrics_histogram
from corehq.util.metrics.load_counters import case_load_counter
from corehq.util.soft_assert import soft_assert
from corehq.util.timer import TimingContext

from .const import LookupErrors
from .const import (
LookupErrors,
MOMO_REQUIRED_PAYMENT_FIELDS,
MOMO_NO_EDIT_PAYMENT_FIELDS,
MOMO_PAYMENT_CASE_TYPE,
)
from .exceptions import (
BlankExternalId,
CaseGeneration,
Expand Down Expand Up @@ -169,6 +175,12 @@ def _do_import(self, spreadsheet):
def import_row(self, row_num, raw_row, import_context):
search_id = self._parse_search_id(raw_row)
fields_to_update = self._populate_updated_fields(raw_row)
if (
self.mobile_worker_verification_ff_enabled
and self.submission_handler.case_type == MOMO_PAYMENT_CASE_TYPE
):
self._validate_payment_fields(fields_to_update)

if self._has_custom_case_import_operations():
fields_to_update = self._perform_custom_case_import_operations(
row_num,
Expand Down Expand Up @@ -207,6 +219,29 @@ def import_row(self, row_num, raw_row, import_context):

self.submission_handler.add_caseblock(RowAndCase(row_num, caseblock))

def _validate_payment_fields(self, fields):
errors = []
for field_name in MOMO_NO_EDIT_PAYMENT_FIELDS:
if fields.get(field_name):
errors.append(CaseRowError(
column_name=field_name,
message=_(
'This field value cannot be set for cases with the '
'"{}" case type.'.format(MOMO_PAYMENT_CASE_TYPE)
),
))
for field_name in MOMO_REQUIRED_PAYMENT_FIELDS:
if field_name not in fields or not fields[field_name]:
errors.append(CaseRowError(
column_name=field_name,
message=_(
'This field requires a value for cases with the '
'"{}" case type.'.format(MOMO_PAYMENT_CASE_TYPE)
),
))
if any(errors):
raise CaseRowErrorList(errors)

def _has_custom_case_import_operations(self):
return any(
extension.should_call_for_domain(self.domain)
Expand All @@ -227,6 +262,10 @@ def _perform_custom_case_import_operations(self, row_num, raw_row, fields_to_upd
def user(self):
return CouchUser.get_by_user_id(self.config.couch_user_id)

@cached_property
def mobile_worker_verification_ff_enabled(self):
return MOBILE_WORKER_VERIFICATION.enabled(self.domain)

def _parse_search_id(self, row):
""" Find and convert the search id in an Excel row """

Expand Down

0 comments on commit 24971cd

Please sign in to comment.