Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove unknowns #113

Merged
merged 2 commits into from
Jul 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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