Skip to content

Commit

Permalink
put in backwards compatibility for accepting 'meterIdentifier' & 'met…
Browse files Browse the repository at this point in the history
…erName' using curl, tested and working
  • Loading branch information
mikedivine committed Aug 12, 2024
1 parent 7cdb87e commit d5d729f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
32 changes: 23 additions & 9 deletions src/server/services/csvPipeline/uploadReadings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 7 additions & 3 deletions src/server/services/csvPipeline/validateCsvUploadParams.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -111,7 +112,6 @@ const VALIDATION = {
},
readings: {
type: 'object',
required: ['meterIdentifier'],
properties: {
...COMMON_PROPERTIES,
cumulative: new EnumParam('cumulative', BooleanCheckArray),
Expand All @@ -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.
}
}
Expand Down

0 comments on commit d5d729f

Please sign in to comment.