Skip to content

Commit

Permalink
Fix: named query date parameters aren't behaving as expected (#2694)
Browse files Browse the repository at this point in the history
* Initial commit. Added userQueryFields configuration option that will allow us to pass the logged in user's attributes in a namedQuery

* Added logged in user parameter support for solr queries

* Added mocha tests for the parameter building functions for solr and named queries inside the vocab service.
Added a feature to expose all service methods when in mocha testing mode

* Fixed issue where default values for date params were not being set.
Fixed issue where date parameters with values of type days were not being set correctly on the request.

* Fixed PopulateExportedMethods decorator so that it doesn't override the mochaTesting exportedMethods

* Added mocha tests to ensure the parameter setting logic works correctly for named queries

* Fixed format parameter for ISODate
Added default format check to ensure it uses ISODate

---------

Co-authored-by: Mark Cottman-Fields <[email protected]>
  • Loading branch information
andrewbrazzatti and cofiem authored Dec 18, 2024
1 parent 307f92c commit 771d58c
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 20 deletions.
160 changes: 160 additions & 0 deletions test/unit/services/NamedQueryService.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
describe('The Named Query Service', function () {
before(function (done) {
done()
})

it("Build a valid query with default values", function (done) {

const namedQueryConfig = {
collectionName: "record",
brandIdFieldPath: "metaMetadata.brandId",
resultObjectMapping: {

},
mongoQuery: {
"daysDatePath": null,
"isoDatePath": null,
"defaultIsoDatePath": null,
"stringPath": null,
"numberPath": null
},
queryParams: {

daysDateParam: {

type: "date",
path: "daysDatePath",
queryType: "<=",
format: "days",
whenUndefined: "defaultValue",
defaultValue: "-1"
},
isoDateParam: {
type: "date",
queryType: "=>",
path: "isoDatePath",
format: "ISODate",
whenUndefined: "defaultValue",
defaultValue: "2024-12-17T00:00:00.000Z"
},
defaultIsoDateParam: {
type: "date",
queryType: "=>",
path: "defaultIsoDatePath",
whenUndefined: "defaultValue",
defaultValue: "2024-12-18T00:00:00.000Z"
},
stringParam: {
type: "string",
path: "stringPath",
queryType: 'contains',
whenUndefined: "defaultValue",
defaultValue: "defaultString"
},
//TODO: number doesn't support queryType of default value like the others
numberParam: {
type: "number",
path: "numberPath"
}

}

};

const numberParamValue = 1;
let paramMap = {numberParam: numberParamValue};

NamedQueryService.setParamsInQuery(namedQueryConfig.mongoQuery, namedQueryConfig.queryParams, paramMap);
expect(namedQueryConfig.mongoQuery.numberPath).to.equal(numberParamValue);
expect(namedQueryConfig.mongoQuery.isoDatePath).to.deep.equal({ "=>": '2024-12-17T00:00:00.000Z'});
expect(namedQueryConfig.mongoQuery.defaultIsoDatePath).to.deep.equal({ "=>": '2024-12-18T00:00:00.000Z'});
expect(namedQueryConfig.mongoQuery.stringPath).to.deep.equal({contains: 'defaultString'});

const daysDateString = namedQueryConfig.mongoQuery.daysDatePath["<="]
expect(daysDateString).to.be.a('string');
let yesterdayIsoString = moment().subtract(1, 'days').format('YYYY-MM-DD');
expect(daysDateString).to.contain(yesterdayIsoString);

done()
})


it("Build a valid query with passed values", function (done) {

const namedQueryConfig = {
collectionName: "record",
brandIdFieldPath: "metaMetadata.brandId",
resultObjectMapping: {

},
mongoQuery: {
"daysDatePath": null,
"isoDatePath": null,
"stringPath": null,
"numberPath": null
},
queryParams: {

daysDateParam: {

type: "date",
path: "daysDatePath",
queryType: "<=",
format: "days",
whenUndefined: "defaultValue",
defaultValue: "-1"
},
isoDateParam: {
type: "date",
queryType: "=>",
path: "isoDatePath",
format: "ISODate",
whenUndefined: "defaultValue",
defaultValue: "2024-12-17T00:00:00.000Z"
},
defaultIsoDateParam: {
type: "date",
queryType: "=>",
path: "defaultIsoDatePath",
whenUndefined: "defaultValue",
defaultValue: "2024-12-18T00:00:00.000Z"
},
stringParam: {
type: "string",
path: "stringPath",
queryType: 'contains',
whenUndefined: "defaultValue",
defaultValue: "defaultString"
},
numberParam: {
type: "number",
path: "numberPath"
}

}

};

const daysDateParamValue = -2;
const isoDateParamValue = "2024-12-18T00:00:00.000Z";
const defaultIsoDateParamValue = "2024-12-18T00:00:00.000Z";
const stringParamValue = "passedString";
const numberParamValue = 2;
let paramMap = {numberParam: numberParamValue, isoDateParam: isoDateParamValue, stringParam: stringParamValue, daysDateParam: daysDateParamValue, defaultIsoDateParamValue: defaultIsoDateParamValue};

NamedQueryService.setParamsInQuery(namedQueryConfig.mongoQuery, namedQueryConfig.queryParams, paramMap);
expect(namedQueryConfig.mongoQuery.numberPath).to.equal(numberParamValue);
expect(namedQueryConfig.mongoQuery.isoDatePath).to.deep.equal({ "=>": isoDateParamValue});
expect(namedQueryConfig.mongoQuery.defaultIsoDatePath).to.deep.equal({ "=>": defaultIsoDateParamValue});
expect(namedQueryConfig.mongoQuery.stringPath).to.deep.equal({contains: stringParamValue});

const daysDateString = namedQueryConfig.mongoQuery.daysDatePath["<="]
expect(daysDateString).to.be.a('string');
let dayBeforeYesterdayIsoString = moment().subtract(2, 'days').format('YYYY-MM-DD');
expect(daysDateString).to.contain(dayBeforeYesterdayIsoString);

done()
})


})
41 changes: 21 additions & 20 deletions typescript/api/services/NamedQueryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,26 +253,27 @@ export module Services {
let query = {};
if (_.isUndefined(value)) {
if (queryParam.whenUndefined == NamedQueryWhenUndefinedOptions.defaultValue) {
if(queryParam.format == NamedQueryFormatOptions.days) {
let days = _.toInteger(queryParam.defaultValue);
let nowDateAddOrSubtract = moment();
if (days > 0) {
//Going forward in time X number of days
nowDateAddOrSubtract = nowDateAddOrSubtract.add(days, 'days');
} else if(days < 0) {
//This "additional" step makes the code self explanatory
days = days * -1;
//Going backwards in time X number of days
nowDateAddOrSubtract = nowDateAddOrSubtract.subtract(days, 'days');
}
value = nowDateAddOrSubtract.toISOString();
} else if(queryParam.format == NamedQueryFormatOptions.ISODate) {
value = queryParam.defaultValue;
}
query[queryParam.queryType] = value;
value = query;
value = queryParam.defaultValue;
}
}
if(queryParam.format == NamedQueryFormatOptions.days) {
let days = _.toInteger(value);
let nowDateAddOrSubtract = moment();
if (days > 0) {
//Going forward in time X number of days
nowDateAddOrSubtract = nowDateAddOrSubtract.add(days, 'days');
} else if(days < 0) {
//This "additional" step makes the code self explanatory
days = days * -1;
//Going backwards in time X number of days
nowDateAddOrSubtract = nowDateAddOrSubtract.subtract(days, 'days');
}
value = nowDateAddOrSubtract.toISOString();
}

query[queryParam.queryType] = value;
value = query;

}
}

Expand Down Expand Up @@ -314,8 +315,8 @@ enum NamedQueryWhenUndefinedOptions {
}

enum NamedQueryFormatOptions {
days,
ISODate
days = 'days',
ISODate = 'ISODate'
}

class QueryParameterDefinition {
Expand Down

0 comments on commit 771d58c

Please sign in to comment.