diff --git a/server/handlers/earningsHandler.js b/server/handlers/earningsHandler.js index 7ef4967..2a0c71b 100644 --- a/server/handlers/earningsHandler.js +++ b/server/handlers/earningsHandler.js @@ -120,8 +120,9 @@ const earningsBatchPatch = async (req, res) => { throw new HttpError(406, 'Only text/csv is supported'); const validateRow = async (row) => { - await earningsPatchSchema.validateAsync(row, { + return earningsPatchSchema.validateAsync(row, { abortEarly: false, + stripUnknown: true, }); }; diff --git a/server/services/EarningsService.js b/server/services/EarningsService.js index 373baa2..b0a1f6f 100644 --- a/server/services/EarningsService.js +++ b/server/services/EarningsService.js @@ -38,8 +38,15 @@ class EarningsService { // check the first line, headers of fields const firstLine = fileBuffer.toString().split('\n')[0]; - if (!firstLine.match(/.*earnings_id.*,.*worker_id.*,.*phone.*,.*currency.*,.*amount.*,.*captures_count.*,.*payment_confirmation_id.*,.*payment_method.*,.*paid_at.*/)) { - throw new HttpError(422, 'Seems the CVS file is not in the correct format, make sure the CSS file has fields: "earnings_id", "worker_id", "phone", "currency", "amount", "captures_count", "payment_confirmation_id", "payment_method", "paid_at", and the fields is separated by a comma'); + if ( + !firstLine.match( + /.*earnings_id.*,.*worker_id.*,.*phone.*,.*currency.*,.*amount.*,.*captures_count.*,.*payment_confirmation_id.*,.*payment_method.*,.*paid_at.*/, + ) + ) { + throw new HttpError( + 422, + 'Seems the CSV file is not in the correct format, make sure the CSV file has fields: "earnings_id", "worker_id", "phone", "currency", "amount", "captures_count", "payment_confirmation_id", "payment_method", "paid_at", and the fields is separated by a comma', + ); } // Don't want to roll back batch creation if it errors out @@ -54,8 +61,8 @@ class EarningsService { csv() .fromStream(csvReadStream) .subscribe( - async (json) => { - await validateRow(json); + async (row) => { + const json = await validateRow(row); await this._earnings.updateEarnings({ ...json, batch_id,