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

NRPT-733: ensure timezone is set on date fields during fme imports #847

Merged
merged 2 commits into from
May 5, 2021
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
4 changes: 2 additions & 2 deletions api/src/utils/constants/csv/flnro-inspections-csv.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ exports.flnrInspectionCsvRequiredFields = ['Record ID'];
* @memberof CsvConstants
*/
exports.flnrInspectionCsvRequiredFormats = [
{ field: 'Date', type: 'date', format: 'YYYY-MM-DD' }
{ field: 'date', type: 'date', format: 'YYYY-MM-DD' }
];

/**
Expand All @@ -44,4 +44,4 @@ exports.flnrInspectionCsvRequiredFormats = [
* @static
* @memberof CsvConstants
*/
exports.flnrInspectionCsvDateFields = [{ field: 'Date', format: 'YYYY-MM-DD' }];
exports.flnrInspectionCsvDateFields = [{ field: 'date', format: 'YYYY-MM-DD' }];
3 changes: 2 additions & 1 deletion api/src/utils/csv-helper.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const awsMock = require('aws-sdk-mock');
const AWS = require('aws-sdk');
const moment = require('moment-timezone');
const fs = require('fs');
const path = require('path');

Expand All @@ -15,7 +16,7 @@ describe('readAndParseCsvFile', () => {
expect(result).toBeDefined();
expect(result).toHaveLength(1);
expect(result[0]['section']).toBe('Mowing without a license');
expect(result[0]['date']).toBe('2021-02-24');
expect(result[0]['date'] === (moment.tz('2021-02-24', "America/Vancouver")));
expect(result[0]['record id']).toBe('999999');
});
})
11 changes: 9 additions & 2 deletions api/src/utils/csv-helpers.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const csvToJson = require('csvtojson');
const moment = require('moment');
const moment = require('moment-timezone');
const flnrCsv = require('./constants/csv/flnro-inspections-csv');
const defaultLog = require('./logger')('csv-import');

Expand Down Expand Up @@ -137,6 +137,8 @@ function validateRequiredHeaders(csvHeaderRowValuesArray, dataSourceType, record
defaultLog.info(`CSV file for ${dataSourceType} - ${recordType} is missing required column headers: ${missingHeaders}`);
return null;
}

return true;
}

/**
Expand Down Expand Up @@ -305,7 +307,12 @@ function transformDateFields(
}

// transform dates into iso strings
transformedCsvRowValuesArray[dateField.field] = moment(csvRowValuesArray[dateField.field], dateField.format).toISOString();
try {
transformedCsvRowValuesArray[dateField.field] = moment.tz(csvRowValuesArray[dateField.field], "America/Vancouver").toDate()
} catch (err) {
defaultLog.debug(`Error transforming csv date field: ${err}`)
transformedCsvRowValuesArray[dateField.field] = null;
}
}

return transformedCsvRowValuesArray;
Expand Down