From a7c59f767c71000dc165270b85ad0609542a3c78 Mon Sep 17 00:00:00 2001 From: Morgan Thompson Date: Tue, 4 May 2021 16:27:31 -0700 Subject: [PATCH] NRPT-733: ensure timezone is set on date fields during fme imports --- api/src/utils/constants/csv/flnro-inspections-csv.js | 4 ++-- api/src/utils/csv-helper.test.js | 3 ++- api/src/utils/csv-helpers.js | 11 +++++++++-- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/api/src/utils/constants/csv/flnro-inspections-csv.js b/api/src/utils/constants/csv/flnro-inspections-csv.js index 84b958c54..a5d337727 100644 --- a/api/src/utils/constants/csv/flnro-inspections-csv.js +++ b/api/src/utils/constants/csv/flnro-inspections-csv.js @@ -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' } ]; /** @@ -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' }]; diff --git a/api/src/utils/csv-helper.test.js b/api/src/utils/csv-helper.test.js index 9ea0f9626..e00dc441a 100644 --- a/api/src/utils/csv-helper.test.js +++ b/api/src/utils/csv-helper.test.js @@ -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'); @@ -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'); }); }) diff --git a/api/src/utils/csv-helpers.js b/api/src/utils/csv-helpers.js index e86ab0602..70feb19c0 100644 --- a/api/src/utils/csv-helpers.js +++ b/api/src/utils/csv-helpers.js @@ -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'); @@ -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; } /** @@ -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;