Skip to content

Commit

Permalink
Refactor validation functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
oddsson committed Dec 23, 2024
1 parent 9afc55f commit 6f5ec34
Showing 1 changed file with 17 additions and 32 deletions.
49 changes: 17 additions & 32 deletions apps/judicial-system/web/src/utils/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,47 +308,32 @@ export const isTrafficViolationStepValidIndictments = (
return false
}

const validateTrafficViolation = (indictmentCount: IndictmentCount) => {
return (
Boolean(indictmentCount.policeCaseNumber) &&
Boolean(
indictmentCount.offenses && indictmentCount.offenses?.length > 0,
) &&
Boolean(indictmentCount.vehicleRegistrationNumber) &&
Boolean(indictmentCount.lawsBroken) &&
Boolean(indictmentCount.incidentDescription) &&
Boolean(indictmentCount.legalArguments)
)
}

const validateNonTrafficViolation = (indictmentCount: IndictmentCount) =>
Boolean(indictmentCount.incidentDescription) &&
Boolean(indictmentCount.legalArguments)
const isValidIndictmentCount = (indictmentCount: IndictmentCount) =>
isTrafficViolation(indictmentCount)
? Boolean(
indictmentCount.policeCaseNumber &&
indictmentCount.offenses?.length &&
indictmentCount.vehicleRegistrationNumber &&
indictmentCount.lawsBroken &&
indictmentCount.incidentDescription &&
indictmentCount.legalArguments,
)
: Boolean(
indictmentCount.incidentDescription && indictmentCount.legalArguments,
)

const isTrafficViolation = (indictmentCount: IndictmentCount) =>
indictmentCount.indictmentCountSubtypes?.includes(
IndictmentSubtype.TRAFFIC_VIOLATION,
)

console.log(isTrafficViolationCase(workingCase))

// All indictment counts are traffic violations
// If all indictment counts are traffic violations, validate them all
if (isTrafficViolationCase(workingCase)) {
const hasValidTrafficViolationIndictmentCounts =
workingCase.indictmentCounts?.every(validateTrafficViolation) ?? false

return hasValidTrafficViolationIndictmentCounts
}

let isValid = false

for (const indictmentCount of workingCase.indictmentCounts || []) {
isValid = isTrafficViolation(indictmentCount)
? validateTrafficViolation(indictmentCount)
: validateNonTrafficViolation(indictmentCount)
return workingCase.indictmentCounts?.every(isValidIndictmentCount) ?? false
}

return isValid
// Otherwise, validate each indictment count
return workingCase.indictmentCounts?.some(isValidIndictmentCount) ?? false
}

export const isPoliceDemandsStepValidRC = (workingCase: Case): boolean => {
Expand Down

0 comments on commit 6f5ec34

Please sign in to comment.