Skip to content

Commit

Permalink
Add custom validation to parsed vehicles
Browse files Browse the repository at this point in the history
  • Loading branch information
jmccollum-woolpert committed Sep 17, 2024
1 parent 9b37b70 commit 915b77a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1240,7 +1240,24 @@ export class CsvUploadDialogComponent implements OnDestroy, OnInit {
shipments = shipmentsResults.map((result) => result.shipment);
}
if (this.vehicleFile) {
vehicles = this.service.csvToVehicles(res[1].data, this.mappingFormVehicles.value);
const vehiclesResults = this.service.csvToVehicles(
res[1].data,
this.mappingFormVehicles.value
);

if (vehiclesResults.some((result) => result.errors.length)) {
vehiclesResults.forEach((result, index) => {
this.validationErrors.push(
...result.errors.map(
(error) =>
`Vehicle ${result.vehicle.label || ''} at index ${index}: ${error.message}`
)
);
});
throw Error('Vehicle validation error');
}

vehicles = vehiclesResults.map((result) => result.vehicle);
}

// geocode all shipments, then all vehicles
Expand Down
16 changes: 14 additions & 2 deletions application/frontend/src/app/core/services/csv.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ export class CsvService {
return errors;
}

csvToVehicles(csvVehicles: any[], mapping: { [key: string]: string }): IVehicle[] {
csvToVehicles(
csvVehicles: any[],
mapping: { [key: string]: string }
): { vehicle: IVehicle; errors: ValidationErrorResponse[] }[] {
return csvVehicles.map((vehicle) => {
// Conditionally add each field to the vehicle object, converting from csv strings as needed
const parsedVehicle = {
Expand Down Expand Up @@ -305,10 +308,19 @@ export class CsvService {
...this.mapToLoadLimits(vehicle, mapping),
...this.mapToVehicleTimeWindows(vehicle, mapping),
};
return parsedVehicle;
return {
vehicle: parsedVehicle,
errors: this.validateVehicle(parsedVehicle),
};
});
}

private validateVehicle(vehicle: IVehicle): ValidationErrorResponse[] {

Check warning on line 318 in application/frontend/src/app/core/services/csv.service.ts

View workflow job for this annotation

GitHub Actions / check-frontend

'vehicle' is defined but never used. Allowed unused args must match /^_/u
const errors = [];

return errors;
}

private mapToPickup(shipment: any, mapping: { [key: string]: string }, timeWindow: any): any {
const pickup = {
...this.mapKeyToModelValue('arrivalWaypoint', 'PickupArrivalWaypoint', shipment, mapping),
Expand Down

0 comments on commit 915b77a

Please sign in to comment.