Skip to content

Commit

Permalink
Merge pull request #113 from Kpoke/fix/remove_unknowns
Browse files Browse the repository at this point in the history
fix: remove unknowns
  • Loading branch information
Kpoke authored Jul 20, 2022
2 parents b0e76b3 + e9c12c4 commit d55a26d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
3 changes: 2 additions & 1 deletion server/handlers/earningsHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
};

Expand Down
15 changes: 11 additions & 4 deletions server/services/EarningsService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit d55a26d

Please sign in to comment.