Support special operators on intl string fields

This commit is contained in:
Elliot DeNolf
2019-03-16 15:04:03 -04:00
parent aea70ed7b4
commit d0bb871cc8
2 changed files with 4 additions and 11 deletions

View File

@@ -5,14 +5,11 @@ export default function buildQuery(schema) {
schema.statics.apiQuery = function (rawParams, locale, cb) {
const model = this;
const params = paramParser(this, rawParams, locale);
console.log('parsed params', params);
// Create the Mongoose Query object.
let query = model
.find(params.searchParams);
console.log('after query find');
if (params.sort)
query = query.sort(params.sort);
@@ -40,7 +37,7 @@ function paramParser(model, rawParams, locale) {
query = parseParam(key, rawParams[key], model, query, locale);
} else {
for (let i = 0; i < separatedParams.length; ++i) {
query = parseParam(key, separatedParams[i], model, query);
query = parseParam(key, separatedParams[i], model, query, locale);
}
}
}
@@ -105,7 +102,7 @@ function parseParam(key, val, model, query, locale) {
function parseSchemaForKey(schema, query, keyPrefix, lcKey, val, operator, locale) {
let paramType;
const key = keyPrefix + lcKey;
let key = keyPrefix + lcKey;
let matches = lcKey.match(/(.+)\.(.+)/);
if (matches) {
@@ -119,7 +116,8 @@ function parseSchemaForKey(schema, query, keyPrefix, lcKey, val, operator, local
}
} else if (typeof schema === 'object' ) {
if (schema.obj[lcKey].intl) {
query = addSearchParam(query, `${key}.${locale}`, val);
key = `${key}.${locale}`;
paramType = 'String'
}
} else if (typeof schema === 'undefined') {
paramType = 'String';

View File

@@ -1,14 +1,10 @@
import httpStatus from 'http-status';
const query = (req, res) => {
console.log('inside query');
req.model.apiQuery(req.query, req.locale, (err, result) => {
if (err) {
console.log('api query error', err);
return res.status(httpStatus.INTERNAL_SERVER_ERROR).json({ error: err });
}
console.log('no error inside api query');
return res.json(
result.map(doc => { //TODO: 'result.docs' will need to be used for the pagination plugin
if (req.locale) {
@@ -17,7 +13,6 @@ const query = (req, res) => {
return doc.toJSON({ virtuals: true })
})
);
});
};