Skip to content

Commit

Permalink
feat: add generic filter for paginated results
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Jan 6, 2023
1 parent ed1ff7c commit 58cb535
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,26 @@ export const findAll = async (req, res) => {
marketplaceIdentifiers,
hasMarketplaceIdentifier,
includeProjectInfoInSearch = false,
filter,
} = req.query;

let where = orgUid != null && orgUid !== 'all' ? { orgUid } : undefined;

if (filter) {
if (!where) {
where = {};
}

const matches = filter.match(/(\w+):(.+):(in|eq|not)/);
// check if the value param is an array so we can parse it
const valueMatches = matches[2].match(/\[.+\]/);
where[matches[1]] = {
[Sequelize.Op[matches[3]]]: valueMatches
? JSON.parse(matches[2])
: matches[2],
};
}

const includes = Unit.getAssociatedModels();

if (columns) {
Expand Down
1 change: 1 addition & 0 deletions src/validations/units.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export const unitsGetQuerySchema = Joi.object()
marketplaceIdentifiers: Joi.array().items(Joi.string()).single(),
hasMarketplaceIdentifier: Joi.boolean(),
includeProjectInfoInSearch: Joi.boolean(),
filter: Joi.string().regex(/(\w+):(.+):(in|eq|not)/),
})
.with('page', 'limit');

Expand Down

0 comments on commit 58cb535

Please sign in to comment.