diff --git a/src/server/services/csvPipeline/uploadReadings.js b/src/server/services/csvPipeline/uploadReadings.js index 080b4a015..1bdcfd947 100644 --- a/src/server/services/csvPipeline/uploadReadings.js +++ b/src/server/services/csvPipeline/uploadReadings.js @@ -17,21 +17,35 @@ const Meter = require('../../models/Meter'); * @returns */ async function uploadReadings(req, res, filepath, conn) { - const { meterIdentifier, headerRow, update, honorDst, relaxedParsing, useMeterZone } = req.body; // extract query parameters + const { meterIdentifier, meterName, headerRow, update, honorDst, relaxedParsing, useMeterZone } = req.body; // extract query parameters // The next few have no value in the DB for a meter so always use the value passed. const hasHeaderRow = normalizeBoolean(headerRow); const shouldUpdate = normalizeBoolean(update); let shouldHonorDst = normalizeBoolean(honorDst); let shouldRelaxedParsing = normalizeBoolean(relaxedParsing); let shouldUseMeterZone = normalizeBoolean(useMeterZone); - let meter = await Meter.getByIdentifier(meterIdentifier, conn) - .catch(async err => { - // If Meter does not exist, we do not know what to do with the readings so we error out. - throw new CSVPipelineError( - `User Error: Meter with identifier '${meterIdentifier}' not found.`, - err.message - ); - }); + // TODO: + // Allowing for backwards compatibility if any users are still using the 'meterName' parameter instead of + // the 'meterIdentifier' parameter to login. Developers need to decide in the future if we should deprecate + // using 'meterName' or continue to allow this backwards compatibility + let meter; + try { + if (meterIdentifier) { + meter = await Meter.getByIdentifier(meterIdentifier, conn); + } else { + meter = await Meter.getByName(meterName, conn); + } + } catch (error) { + // If Meter does not exist, we do not know what to do with the readings so we error out. + let errorMessage = meterIdentifier + ? `User Error: Meter with identifier '${meterIdentifier}' not found.` + : `User Error: Meter with name '${meterName}' not found.`; + + throw new CSVPipelineError( + errorMessage, + error.message + ); + } // Handle other parameter defaults let { timeSort, duplications, cumulative, cumulativeReset, cumulativeResetStart, cumulativeResetEnd, lengthGap, lengthVariation, endOnly } = req.body; diff --git a/src/server/services/csvPipeline/validateCsvUploadParams.js b/src/server/services/csvPipeline/validateCsvUploadParams.js index d0ad64605..d2bca74ad 100644 --- a/src/server/services/csvPipeline/validateCsvUploadParams.js +++ b/src/server/services/csvPipeline/validateCsvUploadParams.js @@ -90,8 +90,9 @@ const COMMON_PROPERTIES = { username: new StringParam('username', undefined, undefined), // TODO: // Allowing for backwards compatibility to allow for curl users to use the 'email' parameter instead of - // the 'username' parameter to login. Developers need to decide in the future if we should deprecate email - // or continue to allow this backwards compatibility + // the 'username' parameter to login. Also allowing to use 'meterName' vs 'meterIdentifier'. Developers need + // to decide in the future if we should deprecate email & meterName or continue to allow this backwards compatibility. + meterName: new StringParam('meterName', undefined, undefined), email: new StringParam('email', undefined, undefined), password: new StringParam('password', undefined, undefined), gzip: new EnumParam('gzip', BooleanCheckArray), @@ -111,7 +112,6 @@ const VALIDATION = { }, readings: { type: 'object', - required: ['meterIdentifier'], properties: { ...COMMON_PROPERTIES, cumulative: new EnumParam('cumulative', BooleanCheckArray), @@ -128,6 +128,10 @@ const VALIDATION = { timeSort: new EnumParam('timeSort', [MeterTimeSortTypesJS.increasing, MeterTimeSortTypesJS.decreasing]), useMeterZone: new EnumParam('useMeterZone', BooleanCheckArray), }, + anyOf: [ + { required: ['meterIdentifier'] }, + { required: ['meterName'] } + ], additionalProperties: false // This protects us from unintended parameters as well as typos. } }