Skip to content

Commit

Permalink
CM-378: fixing response output for endpoint with validations
Browse files Browse the repository at this point in the history
  • Loading branch information
sniedzielski committed Jan 3, 2024
1 parent f88016d commit bce4990
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions social_protection/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,53 +148,49 @@ def _validate_possible_beneficiaries(self, dataframe: DataFrame, benefit_plan: B
validated_dataframe = []

def validate_row(row):
field_validation = {'row': row, 'validations': {}}
field_validation = {'row': row.to_dict(), 'validations': {}}
for field, field_properties in properties.items():
if "uniqueness" in field_properties:
field_validation['validations'][f'{field}_uniqueness'] = self._handle_uniqueness(row, field, field_properties, benefit_plan)

field_validation['validations'][f'{field}_uniqueness'] = self._handle_uniqueness(
row, field, field_properties, benefit_plan, dataframe
)
if "validationCalculation" in field_properties:
field_validation['validations'][f'{field}'] = self._handle_validation_calculation(row, field, field_properties)
field_validation['validations'][f'{field}'] = self._handle_validation_calculation(
row, field, field_properties
)

validated_dataframe.append(field_validation)
return row

dataframe.apply(validate_row, axis='columns')
return validated_dataframe

def _handle_uniqueness(self, row, field, field_properties, benefit_plan):
def _handle_uniqueness(self, row, field, field_properties, benefit_plan, dataframe):
unique_class_validation = 'DeduplicationValidationStrategy'

calculation_uuid = SocialProtectionConfig.validation_calculation_uuid

calculation = get_calculation_object(calculation_uuid)

result_row = calculation.calculate_if_active_for_object(
unique_class_validation,
calculation_uuid,
field,
row[field],
benefit_plan=benefit_plan.id
benefit_plan=benefit_plan.id,
incoming_data=dataframe
)
return result_row

def _handle_validation_calculation(self, row, field, field_properties):
validation_calculation = field_properties.get("validationCalculation", {}).get("name")

if not validation_calculation:
raise ValueError("Missing validation name")

calculation_uuid = SocialProtectionConfig.validation_calculation_uuid

calculation = get_calculation_object(calculation_uuid)

result_row = calculation.calculate_if_active_for_object(
validation_calculation,
calculation_uuid,
field,
row[field]
)

return result_row

def _create_upload_entry(self, filename):
Expand Down

0 comments on commit bce4990

Please sign in to comment.