Skip to content

Commit

Permalink
feat: add project generic filter and generic sort
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Jan 9, 2023
1 parent 58cb535 commit f1332db
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 14 deletions.
45 changes: 43 additions & 2 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import { Staging, Project, Organization, ModelKeys } from '../models';

import { logger } from '../config/logger.cjs';

import {
genericFilterRegex,
genericSortColumnRegex,
isArrayRegex,
} from '../utils/string-utils';

import {
columnsToInclude,
optionallyPaginatedResponse,
Expand All @@ -26,6 +32,7 @@ import {
} from '../utils/data-assertions';

import { createProjectRecordsFromCsv } from '../utils/csv-utils';

import {
tableDataFromXlsx,
createXlsFromSequelizeResults,
Expand Down Expand Up @@ -108,9 +115,35 @@ export const create = async (req, res) => {

export const findAll = async (req, res) => {
try {
let { page, limit, search, orgUid, columns, xls, projectIds } = req.query;
let {
page,
limit,
search,
orgUid,
columns,
xls,
projectIds,
filter,
order,
} = req.query;

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

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

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

if (orgUid === 'all') {
// 'ALL' orgUid is just a UI concept but they keep forgetting this and send it
// So delete this value if its sent so nothing breaks
Expand Down Expand Up @@ -180,10 +213,18 @@ export const findAll = async (req, res) => {
...pagination,
};

// default to DESC
let resultOrder = [['timeStaged', 'DESC']];

if (order?.match(genericSortColumnRegex)) {
const matches = order.match(genericSortColumnRegex);
resultOrder = [[matches[1], matches[2]]];
}

const results = await Project.findAndCountAll({
distinct: true,
where,
order: [['timeStaged', 'DESC']],
order: resultOrder,
...query,
});

Expand Down
24 changes: 18 additions & 6 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ import {
transformMetaUid,
} from '../utils/xls';

import {
genericFilterRegex,
genericSortColumnRegex,
isArrayRegex,
} from '../utils/string-utils';

import { formatModelAssociationName } from '../utils/model-utils.js';
import { logger } from '../config/logger.cjs';

Expand Down Expand Up @@ -134,9 +140,9 @@ export const findAll = async (req, res) => {
where = {};
}

const matches = filter.match(/(\w+):(.+):(in|eq|not)/);
const matches = filter.match(genericFilterRegex);
// check if the value param is an array so we can parse it
const valueMatches = matches[2].match(/\[.+\]/);
const valueMatches = matches[2].match(isArrayRegex);
where[matches[1]] = {
[Sequelize.Op[matches[3]]]: valueMatches
? JSON.parse(matches[2])
Expand Down Expand Up @@ -223,10 +229,16 @@ export const findAll = async (req, res) => {
// default to DESC
let resultOrder = [['timeStaged', 'DESC']];

if (order && order === 'SERIALNUMBER') {
resultOrder = [['serialNumberBlock', 'ASC']];
} else if (order && order === 'ASC') {
resultOrder = [['timeStaged', 'ASC']];
if (order?.match(genericSortColumnRegex)) {
const matches = order.match(genericSortColumnRegex);
resultOrder = [[matches[1], matches[2]]];
} else {
// backwards compatibility for old order usage
if (order && order === 'SERIALNUMBER') {
resultOrder = [['serialNumberBlock', 'ASC']];
} else if (order && order === 'ASC') {
resultOrder = [['timeStaged', 'ASC']];
}
}

const results = await Unit.findAndCountAll({
Expand Down
12 changes: 8 additions & 4 deletions src/utils/string-utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
export function isPluralized(name) {
if (name == null || typeof name !== 'string') return false;
return name.endsWith('s');
}
export function isPluralized(name) {
if (name == null || typeof name !== 'string') return false;
return name.endsWith('s');
}

export const genericFilterRegex = /(\w+):(.+):(in|eq|not|lt|gt|lte|gte)/;
export const isArrayRegex = /\[.+\]/;
export const genericSortColumnRegex = /(\w+):(ASC|DESC)/;
7 changes: 7 additions & 0 deletions src/validations/projects.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import {
estimationSchema,
} from '../validations';

import {
genericFilterRegex,
genericSortColumnRegex,
} from '../utils/string-utils';

import { pickListValidation } from '../utils/validation-utils';

export const baseSchema = {
Expand Down Expand Up @@ -65,6 +70,8 @@ export const projectsGetQuerySchema = Joi.object()
warehouseProjectId: Joi.string(),
xls: Joi.boolean(),
projectIds: Joi.array().items(Joi.string()).single(),
order: Joi.string().regex(genericSortColumnRegex),
filter: Joi.string().regex(genericFilterRegex),
})
.with('page', 'limit')
.with('limit', 'page');
Expand Down
13 changes: 11 additions & 2 deletions src/validations/units.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import { issuanceSchema } from './issuances.validation';
import { labelSchema } from './labels.validations';

import { pickListValidation } from '../utils/validation-utils';
import {
genericFilterRegex,
genericSortColumnRegex,
} from '../utils/string-utils';

const unitsBaseSchema = {
// warehouseUnitId - derived upon unit creation
Expand Down Expand Up @@ -56,12 +60,17 @@ export const unitsGetQuerySchema = Joi.object()
warehouseUnitId: Joi.string(),
columns: Joi.array().items(Joi.string()).single(),
orgUid: Joi.string(),
order: Joi.string().valid('SERIALNUMBER', 'ASC', 'DESC'),
order: Joi.alternatives().try(
// backwards compatibility for old order usage
Joi.string().valid('SERIALNUMBER', 'ASC', 'DESC'),
// new order usage
Joi.string().regex(genericSortColumnRegex),
),
xls: Joi.boolean(),
marketplaceIdentifiers: Joi.array().items(Joi.string()).single(),
hasMarketplaceIdentifier: Joi.boolean(),
includeProjectInfoInSearch: Joi.boolean(),
filter: Joi.string().regex(/(\w+):(.+):(in|eq|not)/),
filter: Joi.string().regex(genericFilterRegex),
})
.with('page', 'limit');

Expand Down

0 comments on commit f1332db

Please sign in to comment.