From f5516edfb819962ab23cbe1cf973c9b2092ddd42 Mon Sep 17 00:00:00 2001 From: Gwynn Dandridge-Perry Date: Mon, 13 Nov 2023 13:14:36 -0800 Subject: [PATCH] feat: fix merge errors --- .../infra/database/OrganizationRepository.ts | 14 ++++++++++ .../database/OrganizationRepositoryV2.ts | 14 ++++++++++ server/models/Organization.ts | 12 +++++++-- server/models/OrganizationV2.ts | 6 +++++ server/routers/organizationsRouter.ts | 27 ++++++++++++++++--- server/routers/organizationsRouterV2.ts | 11 ++++++-- 6 files changed, 77 insertions(+), 7 deletions(-) diff --git a/server/infra/database/OrganizationRepository.ts b/server/infra/database/OrganizationRepository.ts index e0fb7d92..8d482eff 100644 --- a/server/infra/database/OrganizationRepository.ts +++ b/server/infra/database/OrganizationRepository.ts @@ -107,4 +107,18 @@ export default class OrganizationRepository extends BaseRepository ); return objectPatched; } + + async getByIds(ids: Array, options: FilterOptions) { + const { limit, offset } = options; + const sql = ` + SELECT + * + FROM entity + WHERE id IN (${ids}) + LIMIT ${limit} + OFFSET ${offset} + `; + const object = await this.session.getDB().raw(sql); + return object.rows; + } } diff --git a/server/infra/database/OrganizationRepositoryV2.ts b/server/infra/database/OrganizationRepositoryV2.ts index cd3e6c48..2ec5ff8b 100644 --- a/server/infra/database/OrganizationRepositoryV2.ts +++ b/server/infra/database/OrganizationRepositoryV2.ts @@ -73,6 +73,20 @@ export default class OrganizationRepositoryV2 extends BaseRepository, options: FilterOptions) { + const { limit = 20, offset = 0 } = options; + + const result = await this.session + .getDB() + .select('*') + .from(this.tableName) + .whereIn('id', ids) + .offset(offset) + .limit(limit); + + return result; + } + // async getByGrowerId(id: string | number) { // const object = await this.session // .getDB() diff --git a/server/models/Organization.ts b/server/models/Organization.ts index 1bd32c2c..aed9b57e 100644 --- a/server/models/Organization.ts +++ b/server/models/Organization.ts @@ -4,7 +4,11 @@ import Organization from 'interfaces/Organization'; import { delegateRepository } from '../infra/database/delegateRepository'; import OrganizationRepository from '../infra/database/OrganizationRepository'; -type Filter = Partial<{ planter_id: number; organization_id: number }>; +type Filter = Partial<{ + planter_id: number; + organization_id: number; + ids: Array; +}>; function getByFilter( organizationRepository: OrganizationRepository, @@ -18,7 +22,11 @@ function getByFilter( ); return trees; } - + if (filter?.ids?.length) { + log.warn('using ids filter...'); + const trees = await organizationRepository.getByIds(filter.ids, options); + return trees; + } const trees = await organizationRepository.getByFilter(filter, options); return trees; }; diff --git a/server/models/OrganizationV2.ts b/server/models/OrganizationV2.ts index ec1eeb3b..7e0a3352 100644 --- a/server/models/OrganizationV2.ts +++ b/server/models/OrganizationV2.ts @@ -8,6 +8,7 @@ type Filter = Partial<{ planter_id: number; organization_id: number; grower_id: string; + ids: Array; }>; function getByFilter( @@ -30,6 +31,11 @@ function getByFilter( ); return trees; } + if (filter?.ids?.length) { + log.warn('using ids filter...'); + const trees = await organizationRepository.getByIds(filter.ids, options); + return trees; + } const trees = await organizationRepository.getByFilter(filter, options); return trees; }; diff --git a/server/routers/organizationsRouter.ts b/server/routers/organizationsRouter.ts index a20eb608..07ca153b 100644 --- a/server/routers/organizationsRouter.ts +++ b/server/routers/organizationsRouter.ts @@ -5,7 +5,12 @@ import OrganizationRepository from '../infra/database/OrganizationRepository'; import Session from '../infra/database/Session'; import OrganizationModel from '../models/Organization'; -type Filter = Partial<{ planter_id: number; organization_id: number }>; +type Filter = Partial<{ + planter_id: number; + organization_id: number; + ids: Array; + stakeholder_uuid: string; +}>; const router = express.Router(); @@ -47,20 +52,36 @@ router.get( router.get( '/', handlerWrapper(async (req, res) => { + const query = { ...req.query }; + query.ids = JSON.parse(req.query.ids); Joi.assert( - req.query, + query, Joi.object().keys({ planter_id: Joi.number().integer().min(0), + ids: Joi.array(), + stakeholder_uuid: Joi.string().uuid(), limit: Joi.number().integer().min(1).max(1000), offset: Joi.number().integer().min(0), }), ); - const { limit = 20, offset = 0, planter_id } = req.query; + const { + limit = 20, + offset = 0, + planter_id, + ids = [], + stakeholder_uuid, + } = query; const repo = new OrganizationRepository(new Session()); const filter: Filter = {}; if (planter_id) { filter.planter_id = planter_id; } + if (stakeholder_uuid) { + filter.stakeholder_uuid = stakeholder_uuid; + } + if (ids.length) { + filter.ids = ids; + } const result = await OrganizationModel.getByFilter(repo)(filter, { limit, offset, diff --git a/server/routers/organizationsRouterV2.ts b/server/routers/organizationsRouterV2.ts index a1e26d63..3e311d22 100644 --- a/server/routers/organizationsRouterV2.ts +++ b/server/routers/organizationsRouterV2.ts @@ -9,6 +9,7 @@ type Filter = Partial<{ planter_id: number; organization_id: number; grower_id: string; + ids: Array; }>; const router = express.Router(); @@ -51,16 +52,19 @@ router.get( router.get( '/', handlerWrapper(async (req, res) => { + const query = { ...req.query }; + query.ids = JSON.parse(req.query.ids); Joi.assert( - req.query, + query, Joi.object().keys({ planter_id: Joi.number().integer().min(0), grower_id: Joi.string().guid(), + ids: Joi.array().items(Joi.string().uuid()), limit: Joi.number().integer().min(1).max(1000), offset: Joi.number().integer().min(0), }), ); - const { limit = 20, offset = 0, planter_id, grower_id } = req.query; + const { limit = 20, offset = 0, planter_id, grower_id, ids = [] } = query; const repo = new OrganizationRepositoryV2(new Session()); const filter: Filter = {}; if (planter_id) { @@ -68,6 +72,9 @@ router.get( } else if (grower_id) { filter.grower_id = grower_id; } + if (ids.length) { + filter.ids = ids; + } const result = await OrganizationModel.getByFilter(repo)(filter, { limit, offset,