Skip to content

Commit

Permalink
feat: get projects by project ids
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Sep 19, 2022
1 parent e333a16 commit 8eb8904
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 26 deletions.
54 changes: 33 additions & 21 deletions src/controllers/issuance.controller.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,33 @@
import { Issuance, Organization } from '../models';

import { assertHomeOrgExists } from '../utils/data-assertions';

export const findAll = async (req, res) => {
try {
await assertHomeOrgExists();
const homeOrg = await Organization.getHomeOrg();

return res.json(
await Issuance.findAll({
where: { orgUid: homeOrg.orgUid },
}),
);
} catch (error) {
res.status(400).json({
message: 'Can not retreive issuances',
error: error.message,
});
}
};
import { Issuance, Organization } from '../models';
import { Sequelize } from 'sequelize';
import { assertHomeOrgExists } from '../utils/data-assertions';
import { _ } from 'sequelize-mock/src/utils';

export const findAll = async (req, res) => {
try {
await assertHomeOrgExists();
const homeOrg = await Organization.getHomeOrg();

let { issuanceIds } = req.query;

let where = {};

if (issuanceIds) {
where = {
id: {
[Sequelize.Op.in]: _.flatten([issuanceIds]),
},
};
} else {
where = { orgUid: homeOrg.orgUid };
}

return res.json(await Issuance.findAll({ where }));
} catch (error) {
console.trace(error);
res.status(400).json({
message: 'Can not retreive issuances',
error: error.message,
});
}
};
12 changes: 11 additions & 1 deletion src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export const create = async (req, res) => {

export const findAll = async (req, res) => {
try {
let { page, limit, search, orgUid, columns, xls } = req.query;
let { page, limit, search, orgUid, columns, xls, projectIds } = req.query;
let where = orgUid != null && orgUid !== 'all' ? { orgUid } : undefined;

if (orgUid === 'all') {
Expand Down Expand Up @@ -165,6 +165,16 @@ export const findAll = async (req, res) => {
};
}

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

where.warehouseProjectId = {
[Sequelize.Op.in]: _.flatten([projectIds]),
};
}

const query = {
...columnsToInclude(columns, includes),
...pagination,
Expand Down
18 changes: 15 additions & 3 deletions src/controllers/units.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ export const findAll = async (req, res) => {
search,
xls,
order,
marketplaceIdentifier,
marketplaceIdentifiers,
hasMarketplaceIdentifier,
} = req.query;

let where = orgUid != null && orgUid !== 'all' ? { orgUid } : undefined;
Expand Down Expand Up @@ -169,13 +170,24 @@ export const findAll = async (req, res) => {
};
}

if (marketplaceIdentifier) {
if (marketplaceIdentifiers) {
if (!where) {
where = {};
}

where.marketplaceIdentifier = {
[Sequelize.Op.in]: marketplaceIdentifier,
[Sequelize.Op.in]: _.flatten([marketplaceIdentifiers]),
};
}

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

where.marketplaceIdentifier = {
[Sequelize.Op.not]: null,
[Sequelize.Op.not]: '',
};
}

Expand Down
1 change: 1 addition & 0 deletions src/validations/projects.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export const projectsGetQuerySchema = Joi.object()
orgUid: Joi.string(),
warehouseProjectId: Joi.string(),
xls: Joi.boolean(),
projectIds: Joi.array().items(Joi.string()).single(),
})
.with('page', 'limit')
.with('limit', 'page');
Expand Down
3 changes: 2 additions & 1 deletion src/validations/units.validations.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export const unitsGetQuerySchema = Joi.object()
orgUid: Joi.string(),
order: Joi.string().valid('SERIALNUMBER', 'ASC', 'DESC'),
xls: Joi.boolean(),
marketplaceIdentifier: Joi.array().items(Joi.string()).single(),
marketplaceIdentifiers: Joi.array().items(Joi.string()).single(),
hasMarketplaceIdentifier: Joi.boolean(),
})
.with('page', 'limit');

Expand Down

0 comments on commit 8eb8904

Please sign in to comment.