From d8a04ead92959e6bbca41e20e06ede5fa6b9c4f8 Mon Sep 17 00:00:00 2001 From: Daniel Olojakpoke Date: Wed, 16 Feb 2022 01:45:23 +0100 Subject: [PATCH] feat: add organization_ids array query parameter for /captures --- package.json | 2 +- server/handlers/captureHandler.js | 11 +---------- server/infra/repositories/CaptureRepository.js | 11 ++++++++++- server/models/Capture.js | 15 +++++++++++---- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/package.json b/package.json index e8ecfc5b..69eaad58 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "eslint:fix": "npm run eslint -- --fix", "test": "npm run test-unit && npm run test-integration && npm run test-repository", "test-unit": "NODE_ENV=test mocha -r dotenv/config --env dotenv_config_path=.env.test --exit ./server/models/**/*.spec.js ./server/routes/**/*.spec.js", - "test-repository": "NODE_ENV=test mocha -r dotenv/config --exit ./server/infra/repositories/**/*.spec.js", + "test-repository": "NODE_ENV=test mocha -r dotenv/config --env dotenv_config_path=.env.test --exit ./server/infra/repositories/**/*.spec.js", "server": "nodemon server/server.js", "test-seedDB": "NODE_ENV=test mocha -r dotenv/config --env dotenv_config_path=.env.test --timeout 10000 './**/*.spec.js'", "test-integration": "NODE_ENV=test mocha -r dotenv/config --env dotenv_config_path=.env.test --exit --timeout 20000 --recursive './server/__tests__/'", diff --git a/server/handlers/captureHandler.js b/server/handlers/captureHandler.js index 66948070..d57bc68c 100644 --- a/server/handlers/captureHandler.js +++ b/server/handlers/captureHandler.js @@ -48,7 +48,7 @@ const capturePostSchema = Joi.object({ const captureGetQuerySchema = Joi.object({ tree_id: Joi.string().uuid(), tree_associated: Joi.boolean(), - planting_organization_id: Joi.string().uuid(), + organization_ids: Joi.array().items(Joi.string().uuid()), captured_at_start_date: Joi.date().iso(), captured_at_end_date: Joi.date().iso(), grower_account_id: Joi.string().uuid(), @@ -178,15 +178,6 @@ const captureHandlerSingleGet = async function (req, res) { id: req.params.capture_id, }, }); - const result = await captureRepo.getByFilter({ - parameters: { - id: req.params.capture_id, - }, - }); - - console.log('=======================\n\n\n\n\n'); - console.log(result); - console.log('\n\n\n\n\n\n========================='); res.send(Capture(capture)); }; diff --git a/server/infra/repositories/CaptureRepository.js b/server/infra/repositories/CaptureRepository.js index 8ebc4e7b..29ace901 100644 --- a/server/infra/repositories/CaptureRepository.js +++ b/server/infra/repositories/CaptureRepository.js @@ -10,7 +10,12 @@ class CaptureRepository extends BaseRepository { async getByFilter(filterCriteria, options = {}) { const whereBuilder = function (object, builder) { const result = builder; - const { parameters, whereNulls = [], whereNotNulls = [] } = { ...object }; + const { + parameters, + whereNulls = [], + whereNotNulls = [], + whereIns = [], + } = { ...object }; result.whereNot({ status: 'deleted' }); for (const whereNot of whereNotNulls) { result.whereNotNull(whereNot); @@ -20,6 +25,10 @@ class CaptureRepository extends BaseRepository { result.whereNull(whereNull); } + for (const whereIn of whereIns) { + result.whereIn(whereIn.field, whereIn.values); + } + const filterObject = { ...parameters }; if (filterObject.captured_at_start_date) { diff --git a/server/models/Capture.js b/server/models/Capture.js index 1993bf7e..bf8fa55f 100644 --- a/server/models/Capture.js +++ b/server/models/Capture.js @@ -26,7 +26,7 @@ const Capture = ({ ...(tree_id !== undefined && { tree_associated: !!tree_id }), tree_id, status, - tags: tag_array || [], + tags: tag_array || undefined, captured_at, }); @@ -110,15 +110,14 @@ const createCapture = (captureRepositoryImpl, eventRepositoryImpl) => async ( const FilterCriteria = ({ tree_id = undefined, tree_associated = undefined, - planting_organization_id = undefined, captured_at_start_date = undefined, captured_at_end_date = undefined, grower_account_id = undefined, species_id = undefined, + organization_ids = [], }) => { const parameters = Object.entries({ tree_id, - planting_organization_id, captured_at_start_date, captured_at_end_date, grower_account_id, @@ -132,13 +131,21 @@ const FilterCriteria = ({ const whereNulls = []; const whereNotNulls = []; + const whereIns = []; + + if (organization_ids.length) { + whereIns.push({ + field: 'planting_organization_id', + values: [...organization_ids], + }); + } if (tree_associated === 'true') { whereNotNulls.push('tree_id'); } else if (tree_associated === 'false') { whereNulls.push('tree_id'); } - return { parameters, whereNulls, whereNotNulls }; + return { parameters, whereNulls, whereNotNulls, whereIns }; }; const getCaptures = (captureRepositoryImpl) => async (