From a1f74f8021553a58bf15955df0c9976310f41d0d Mon Sep 17 00:00:00 2001 From: Zaven Arra Date: Wed, 2 Mar 2022 10:09:22 -0800 Subject: [PATCH] feat: remove legacy ground_user API endpoint --- server/handlers/groundUserHandler.js | 32 ---- .../repositories/GroundUserRepository.js | 23 --- .../repositories/GroundUserRepository.spec.js | 68 ------- server/models/GroundUser.js | 80 --------- server/models/GroundUser.spec.js | 168 ------------------ server/routes.js | 6 - 6 files changed, 377 deletions(-) delete mode 100644 server/handlers/groundUserHandler.js delete mode 100644 server/infra/repositories/GroundUserRepository.js delete mode 100644 server/infra/repositories/GroundUserRepository.spec.js delete mode 100644 server/models/GroundUser.js delete mode 100644 server/models/GroundUser.spec.js diff --git a/server/handlers/groundUserHandler.js b/server/handlers/groundUserHandler.js deleted file mode 100644 index 04a0f65f..00000000 --- a/server/handlers/groundUserHandler.js +++ /dev/null @@ -1,32 +0,0 @@ -const Joi = require('joi'); -const Session = require('../infra/database/Session'); -const GroundUserRepository = require('../infra/repositories/GroundUserRepository'); -const { getGroundUsers } = require('../models/GroundUser'); - -const groundUserQuerySchema = Joi.object({ - organization_id: Joi.string().guid(), - id: Joi.string().guid(), - limit: Joi.number().integer().greater(0).less(101), - offset: Joi.number().integer().greater(-1), -}).unknown(false); - -const groundUserHandlerGet = async function (req, res) { - await groundUserQuerySchema.validateAsync(req.query, { abortEarly: false }); - const session = new Session(); - const groundUserRepo = new GroundUserRepository(session); - const { organization_id } = req.query; - - const url = `groundUsers${ - organization_id ? `?organization_id=${organization_id}` : '' - }`; - - const executeGetGroundUsers = getGroundUsers(groundUserRepo); - const result = await executeGetGroundUsers(req.query, url); - - res.send(result); - res.end(); -}; - -module.exports = { - groundUserHandlerGet, -}; diff --git a/server/infra/repositories/GroundUserRepository.js b/server/infra/repositories/GroundUserRepository.js deleted file mode 100644 index 520dd1dc..00000000 --- a/server/infra/repositories/GroundUserRepository.js +++ /dev/null @@ -1,23 +0,0 @@ -const BaseRepository = require('./BaseRepository'); - -class GroundUserRepository extends BaseRepository { - constructor(session) { - super('planter', session); - this._tableName = 'planter'; - this._session = session; - } - - async getGroundUsersByOrganization(organization_id, options) { - const limit = options?.limit; - const offset = options?.offset; - const groundUsers = await this._session.getDB().raw( - `SELECT planter.* FROM public.planter JOIN public.entity ON planter.organization_id = entity.id WHERE stakeholder_uuid = ? - ${limit ? `LIMIT ${limit}` : ''} ${offset ? `OFFSET ${offset}` : ''}`, - [organization_id], - ); - - return groundUsers.rows; - } -} - -module.exports = GroundUserRepository; diff --git a/server/infra/repositories/GroundUserRepository.spec.js b/server/infra/repositories/GroundUserRepository.spec.js deleted file mode 100644 index 57a99475..00000000 --- a/server/infra/repositories/GroundUserRepository.spec.js +++ /dev/null @@ -1,68 +0,0 @@ -const { expect } = require('chai'); -const mockKnex = require('mock-knex'); -const knex = require('knex'); -const GroundUserRepository = require('./GroundUserRepository'); - -const tracker = mockKnex.getTracker(); -const Session = require('../database/Session'); - -describe('GroundUserRepository', () => { - let groundUserRepository; - const knexDB = knex({ client: 'pg' }); - - beforeEach(() => { - mockKnex.mock(knexDB); - tracker.install(); - const session = new Session(); - groundUserRepository = new GroundUserRepository(session); - }); - - afterEach(() => { - tracker.uninstall(); - mockKnex.unmock(knexDB); - }); - - it('getGroundUsersByOrganization', async () => { - tracker.uninstall(); - tracker.install(); - tracker.on('query', (query) => { - expect(query.sql).match( - /.*planter.*.*planter.organization_id.*.*entity\.id.*.*stakeholder_uuid.*/, - ); - - query.response({ rows: [{ id: 1 }] }); - }); - const result = await groundUserRepository.getGroundUsersByOrganization(1); - expect(result[0]).property('id').eq(1); - }); - - it('getGroundUsersByOrganization with LIMIT', async () => { - tracker.uninstall(); - tracker.install(); - tracker.on('query', (query) => { - expect(query.sql).match( - /.*planter.*.*planter.organization_id.*.*entity\.id.*.*stakeholder_uuid.*\s*LIMIT.*/, - ); - query.response({ rows: [{ id: 1 }] }); - }); - const result = await groundUserRepository.getGroundUsersByOrganization(1, { - limit: 1, - }); - expect(result[0]).property('id').eq(1); - }); - - it('getGroundUsersByOrganization with OFFSET', async () => { - tracker.uninstall(); - tracker.install(); - tracker.on('query', (query) => { - expect(query.sql).match( - /.*planter.*.*planter.organization_id.*.*entity\.id.*.*stakeholder_uuid.*\s*OFFSET.*/, - ); - query.response({ rows: [{ id: 1 }] }); - }); - const result = await groundUserRepository.getGroundUsersByOrganization(1, { - offset: 1, - }); - expect(result[0]).property('id').eq(1); - }); -}); diff --git a/server/models/GroundUser.js b/server/models/GroundUser.js deleted file mode 100644 index c6382faa..00000000 --- a/server/models/GroundUser.js +++ /dev/null @@ -1,80 +0,0 @@ -const { PaginationQueryOptions } = require('./helper'); - -const GroundUser = ({ - id, - first_name, - last_name, - email, - organization, - phone, - pwd_reset_required, - image_url, - person_id, - organization_id, - image_rotation, -}) => { - return Object.freeze({ - id, - first_name, - last_name, - email, - organization, - phone, - pwd_reset_required, - image_url, - person_id, - organization_id, - image_rotation, - }); -}; - -const getGroundUsers = (groundUserRepo) => async (filterCriteria, url) => { - const organization_id = filterCriteria?.organization_id; - const filter = { ...filterCriteria }; - delete filter.limit; - delete filter.offset; - let options = { ...PaginationQueryOptions({ ...filterCriteria }) }; - - if (!filterCriteria?.limit && !organization_id) { - options = { ...options, limit: 100, offset: 0 }; - } - - if (!filterCriteria?.limit && organization_id) { - options = {}; - } - - let query = `${url}?`; - let next = ''; - let prev = ''; - - if (options.limit) { - query = `${query}limit=${options.limit}&`; - } - - if (options.offset || options.offset === 0) { - next = `${query}offset=${+options.offset + +options.limit}`; - if (options.offset - +options.limit >= 0) { - prev = `${query}offset=${+options.offset - +options.limit}`; - } - } - - const groundUsers = organization_id - ? await groundUserRepo.getGroundUsersByOrganization( - organization_id, - options, - ) - : await groundUserRepo.getByFilter(filter, options); - - return { - ground_users: groundUsers.map((row) => GroundUser(row)), - links: { - prev, - next, - }, - }; -}; - -module.exports = { - getGroundUsers, - GroundUser, -}; diff --git a/server/models/GroundUser.spec.js b/server/models/GroundUser.spec.js deleted file mode 100644 index 252b92ba..00000000 --- a/server/models/GroundUser.spec.js +++ /dev/null @@ -1,168 +0,0 @@ -const { expect } = require('chai'); -const sinon = require('sinon'); -const { getGroundUsers, GroundUser } = require('./GroundUser'); - -describe('GroundUser Model', () => { - it('GroundUser Model should return defined fields', () => { - const groundUser = GroundUser({}); - expect(groundUser).to.have.keys([ - 'id', - 'first_name', - 'last_name', - 'email', - 'organization', - 'phone', - 'pwd_reset_required', - 'image_url', - 'person_id', - 'organization_id', - 'image_rotation', - ]); - }); - - describe('getGroundUsers', () => { - it('should get groundUsers with organization_id', async () => { - const getByFilter = sinon.mock(); - const getGroundUsersByOrganization = sinon.mock(); - const executeGetGroundUsers = getGroundUsers({ - getByFilter, - getGroundUsersByOrganization, - }); - getGroundUsersByOrganization.resolves([{ id: 1 }]); - const result = await executeGetGroundUsers({ organization_id: 'uuid' }); - expect(getByFilter.notCalled); - expect(getGroundUsersByOrganization.calledWith('uuid', {})).to.be.true; - expect(result.ground_users).to.have.length(1); - expect(result.links).to.have.keys(['prev', 'next']); - expect(result.ground_users[0]).property('id').eq(1); - }); - - it('should get groundUsers with organization_id -- with limit', async () => { - const getByFilter = sinon.mock(); - const getGroundUsersByOrganization = sinon.mock(); - const executeGetGroundUsers = getGroundUsers({ - getByFilter, - getGroundUsersByOrganization, - }); - getGroundUsersByOrganization.resolves([{ id: 1 }]); - const result = await executeGetGroundUsers({ - organization_id: 'uuid', - limit: 2, - }); - expect(getByFilter.notCalled); - expect(getGroundUsersByOrganization.calledWith('uuid', { limit: 2 })).to - .be.true; - expect(result.ground_users).to.have.length(1); - expect(result.links).to.have.keys(['prev', 'next']); - expect(result.ground_users[0]).property('id').eq(1); - }); - - it('should get groundUsers with organization_id -- with offset', async () => { - const getByFilter = sinon.mock(); - const getGroundUsersByOrganization = sinon.mock(); - const executeGetGroundUsers = getGroundUsers({ - getByFilter, - getGroundUsersByOrganization, - }); - getGroundUsersByOrganization.resolves([{ id: 1 }]); - const result = await executeGetGroundUsers({ - organization_id: 'uuid', - offset: 2, - }); - expect(getByFilter.notCalled); - expect(getGroundUsersByOrganization.calledWith('uuid', {})).to.be.true; - expect(result.ground_users).to.have.length(1); - expect(result.links).to.have.keys(['prev', 'next']); - expect(result.ground_users[0]).property('id').eq(1); - }); - - it('should get groundUsers with organization_id -- with limit and offset', async () => { - const getByFilter = sinon.mock(); - const getGroundUsersByOrganization = sinon.mock(); - const executeGetGroundUsers = getGroundUsers({ - getByFilter, - getGroundUsersByOrganization, - }); - getGroundUsersByOrganization.resolves([{ id: 1 }]); - const result = await executeGetGroundUsers({ - organization_id: 'uuid', - offset: 2, - limit: 4, - }); - expect(getByFilter.notCalled); - expect( - getGroundUsersByOrganization.calledWith('uuid', { - limit: 4, - offset: 2, - }), - ).to.be.true; - expect(result.ground_users).to.have.length(1); - expect(result.links).to.have.keys(['prev', 'next']); - expect(result.ground_users[0]).property('id').eq(1); - }); - - it('should get groundUsers without organization_id', async () => { - const getByFilter = sinon.mock(); - const getGroundUsersByOrganization = sinon.mock(); - const executeGetGroundUsers = getGroundUsers({ - getByFilter, - getGroundUsersByOrganization, - }); - getByFilter.resolves([{ id: 1 }]); - const result = await executeGetGroundUsers(); - expect(getGroundUsersByOrganization.notCalled); - expect(getByFilter.calledWith({}, { limit: 100, offset: 0 })).to.be.true; - expect(result.ground_users).to.have.length(1); - expect(result.links).to.have.keys(['prev', 'next']); - expect(result.ground_users[0]).property('id').eq(1); - }); - - it('should get groundUsers without organization_id -- with limit', async () => { - const getByFilter = sinon.mock(); - const getGroundUsersByOrganization = sinon.mock(); - const executeGetGroundUsers = getGroundUsers({ - getByFilter, - getGroundUsersByOrganization, - }); - getByFilter.resolves([{ id: 1 }]); - const result = await executeGetGroundUsers({ limit: 2 }); - expect(getGroundUsersByOrganization.notCalled); - expect(getByFilter.calledWith({}, { limit: 2 })).to.be.true; - expect(result.ground_users).to.have.length(1); - expect(result.links).to.have.keys(['prev', 'next']); - expect(result.ground_users[0]).property('id').eq(1); - }); - - it('should get groundUsers without organization_id -- with offset', async () => { - const getByFilter = sinon.mock(); - const getGroundUsersByOrganization = sinon.mock(); - const executeGetGroundUsers = getGroundUsers({ - getByFilter, - getGroundUsersByOrganization, - }); - getByFilter.resolves([{ id: 1 }]); - const result = await executeGetGroundUsers({ offset: 2 }); - expect(getGroundUsersByOrganization.notCalled); - expect(getByFilter.calledWith({}, { limit: 100, offset: 0 })).to.be.true; - expect(result.ground_users).to.have.length(1); - expect(result.links).to.have.keys(['prev', 'next']); - expect(result.ground_users[0]).property('id').eq(1); - }); - - it('should get groundUsers without organization_id -- with limit and offset', async () => { - const getByFilter = sinon.mock(); - const getGroundUsersByOrganization = sinon.mock(); - const executeGetGroundUsers = getGroundUsers({ - getByFilter, - getGroundUsersByOrganization, - }); - getByFilter.resolves([{ id: 1 }]); - const result = await executeGetGroundUsers({ limit: 4, offset: 2 }); - expect(getGroundUsersByOrganization.notCalled); - expect(getByFilter.calledWith({}, { limit: 4, offset: 2 })).to.be.true; - expect(result.ground_users).to.have.length(1); - expect(result.links).to.have.keys(['prev', 'next']); - expect(result.ground_users[0]).property('id').eq(1); - }); - }); -}); diff --git a/server/routes.js b/server/routes.js index fb33643d..3f6ad3f8 100644 --- a/server/routes.js +++ b/server/routes.js @@ -41,7 +41,6 @@ const { tagHandlerSingleGet, tagHandlerPatch, } = require('./handlers/tagHandler'); -const { groundUserHandlerGet } = require('./handlers/groundUserHandler'); router .route('/trees') @@ -68,11 +67,6 @@ router .patch(validateRequest, handlerWrapper(treeHandlerSingleTagPatch)) .delete(validateRequest, handlerWrapper(treeHandlerSingleTagDelete)); -// Legacy Planters Table -router - .route('/ground_users') - .get(validateRequest, handlerWrapper(groundUserHandlerGet)); - router .route('/grower_accounts') .get(validateRequest, handlerWrapper(growerAccountHandlerGet))