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 6f5ec34 commit e7264b0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
19 changes: 19 additions & 0 deletions apps/judicial-system/web/src/utils/formHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,22 @@ export const findFirstInvalidStep = (steps: string[], theCase: Case) => {

return key
}

export const isTrafficViolationIndictmentCount = (
policeCaseNumber?: string | null,
indictmentSubtypes?: IndictmentSubtypeMap,
) => {
if (!policeCaseNumber || !indictmentSubtypes) {
return false
}

if (
indictmentSubtypes[policeCaseNumber].length === 1 &&
indictmentSubtypes[policeCaseNumber][0] ===
IndictmentSubtype.TRAFFIC_VIOLATION
) {
return true
}

return false
}
41 changes: 24 additions & 17 deletions apps/judicial-system/web/src/utils/validate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,32 +308,39 @@ export const isTrafficViolationStepValidIndictments = (
return false
}

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 validateTrafficViolation = (indictmentCount: IndictmentCount) =>
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 isTrafficViolation = (indictmentCount: IndictmentCount) =>
indictmentCount.indictmentCountSubtypes?.includes(
IndictmentSubtype.TRAFFIC_VIOLATION,
)

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

let isValid = false

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

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

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

0 comments on commit e7264b0

Please sign in to comment.