diff --git a/src/controllers/issuance.controller.js b/src/controllers/issuance.controller.js index bc2d03f4..d5457c61 100644 --- a/src/controllers/issuance.controller.js +++ b/src/controllers/issuance.controller.js @@ -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, + }); + } +}; diff --git a/src/controllers/project.controller.js b/src/controllers/project.controller.js index 448d5906..50b47bfe 100644 --- a/src/controllers/project.controller.js +++ b/src/controllers/project.controller.js @@ -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') { @@ -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, diff --git a/src/controllers/units.controller.js b/src/controllers/units.controller.js index fb7eecc0..4f6b48cb 100644 --- a/src/controllers/units.controller.js +++ b/src/controllers/units.controller.js @@ -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; @@ -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]: '', }; } diff --git a/src/validations/projects.validations.js b/src/validations/projects.validations.js index 97db3a84..f6c19f2f 100644 --- a/src/validations/projects.validations.js +++ b/src/validations/projects.validations.js @@ -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'); diff --git a/src/validations/units.validations.js b/src/validations/units.validations.js index a62679ac..3f92c362 100644 --- a/src/validations/units.validations.js +++ b/src/validations/units.validations.js @@ -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');