From 11ac043ebd53f30a3aeb3b9ca23f0d6b0101e6e9 Mon Sep 17 00:00:00 2001 From: Gwynn Dandridge-Perry Date: Tue, 10 May 2022 14:52:18 -0700 Subject: [PATCH] feat: add active column to stakeholder, remove deleteRelation endpoint --- server/handlers/stakeholderHandler.js | 22 +++++++++-------- server/repositories/StakeholderRepository.js | 25 ++++++++++++++----- server/routes.js | 6 ++--- server/services/StakeholderService.js | 26 ++++++++++---------- 4 files changed, 46 insertions(+), 33 deletions(-) diff --git a/server/handlers/stakeholderHandler.js b/server/handlers/stakeholderHandler.js index bc2cf9c..ae0f789 100644 --- a/server/handlers/stakeholderHandler.js +++ b/server/handlers/stakeholderHandler.js @@ -17,6 +17,7 @@ const stakeholderGetQuerySchema = Joi.object({ phone: Joi.string(), website: Joi.string(), search: Joi.string(), + active: Joi.boolean(), }).unknown(false); const stakeholderPostSchema = Joi.object({ @@ -58,6 +59,7 @@ const updateStakeholderSchema = Joi.object({ type: Joi.string(), created_at: Joi.string(), updated_at: Joi.string(), + active: Joi.boolean(), }) .unknown(false) .xor('org_name', 'first_name') @@ -155,24 +157,24 @@ const stakeholderUpdate = async function (req, res) { res.status(200).json(result); }; -const stakeholderDeleteRelation = async function (req, res) { - const requestObject = await stakeholderDeleteSchema.validateAsync(req.body, { - abortEarly: false, - }); - const { id } = req.params; +// const stakeholderDeleteRelation = async function (req, res) { +// const requestObject = await stakeholderDeleteSchema.validateAsync(req.body, { +// abortEarly: false, +// }); +// const { id } = req.params; - const stakeholderService = new StakeholderService(); - const result = await stakeholderService.deleteRelation(id, requestObject); +// const stakeholderService = new StakeholderService(); +// const result = await stakeholderService.deleteRelation(id, requestObject); - res.status(200).json(result); -}; +// res.status(200).json(result); +// }; module.exports = { stakeholderGetAllById, stakeholderGetAll, // stakeholderGetRelations, // stakeholderCreateRelation, - stakeholderDeleteRelation, + // stakeholderDeleteRelation, stakeholderCreate, stakeholderDelete, stakeholderUpdate, diff --git a/server/repositories/StakeholderRepository.js b/server/repositories/StakeholderRepository.js index 9e3c315..fb35ad5 100644 --- a/server/repositories/StakeholderRepository.js +++ b/server/repositories/StakeholderRepository.js @@ -53,6 +53,7 @@ class StakeholderRepository extends BaseRepository { .select('s.*') .leftJoin('stakeholder_relation as sr', 's.id', 'sr.child_id') .whereNull('sr.child_id') + .andWhere('active', true) .orderBy('s.org_name', 'asc'); if (limitOptions?.limit) { @@ -81,6 +82,7 @@ class StakeholderRepository extends BaseRepository { .select('s.*') .leftJoin('stakeholder_relation as sr', 's.id', 'sr.child_id') .where('s.id', id) + .andWhere('active', true) .orderBy('s.org_name', 'asc'); if (limitOptions?.limit) { @@ -96,7 +98,8 @@ class StakeholderRepository extends BaseRepository { const count = await this._session .getDB()(this._tableName) .count('*') - .where('id', id); + .where('id', id) + .andWhere('active', true); return { stakeholders, count: +count[0].count }; } @@ -107,6 +110,7 @@ class StakeholderRepository extends BaseRepository { .getDB()(this._tableName) .select('*') .where('id', id) + .andWhere('active', true) .first(); // only get one step generation difference, no recursion @@ -116,7 +120,8 @@ class StakeholderRepository extends BaseRepository { const count = await this._session .getDB()(this._tableName) .count('*') - .where('id', id); + .where('id', id) + .andWhere('active', true); return { stakeholders: [stakeholder], @@ -142,6 +147,7 @@ class StakeholderRepository extends BaseRepository { .getDB()(this._tableName) .select('*') .whereIn('id', parentIds) + .andWhere('active', true) .orderBy('org_name', 'asc'); } return []; @@ -166,6 +172,7 @@ class StakeholderRepository extends BaseRepository { .getDB()(this._tableName) .select('*') .whereIn('id', childrenFound) + .andWhere('active', true) .orderBy('org_name', 'asc'); } return []; @@ -195,6 +202,7 @@ class StakeholderRepository extends BaseRepository { .getDB()(this._tableName) .select('*') .where((builder) => whereBuilder(filter, builder)) + .andWhere('active', true) .orderBy('org_name', 'asc'); if (limitOptions?.limit) { @@ -209,7 +217,8 @@ class StakeholderRepository extends BaseRepository { const count = await this._session .getDB()(this._tableName) .count('*') - .where((builder) => whereBuilder(filter, builder)); + .where((builder) => whereBuilder(filter, builder)) + .andWhere('active', true); return { stakeholders, count: +count[0].count }; } @@ -222,6 +231,7 @@ class StakeholderRepository extends BaseRepository { .select('*') .where((builder) => builder.whereIn('id', relatedIds)) .andWhere({ ...filter }) + .andWhere('active', true) .orderBy('org_name', 'asc'); if (limitOptions?.limit) { @@ -237,7 +247,8 @@ class StakeholderRepository extends BaseRepository { .getDB()(this._tableName) .count('*') .where((builder) => builder.whereIn('id', relatedIds)) - .andWhere({ ...filter }); + .andWhere({ ...filter }) + .andWhere('active', true); return { stakeholders, count: +count[0].count }; } @@ -261,7 +272,7 @@ class StakeholderRepository extends BaseRepository { const deleted = await this._session .getDB()(this._tableName) .where('id', id) - .del() + .update('active', false) .returning('*'); expect(deleted).match([ @@ -318,13 +329,15 @@ class StakeholderRepository extends BaseRepository { .getDB()(this._tableName) .select('*') .whereIn('id', [...ids, id]) + .andWhere('active', true) .orWhereNull('id') .orderBy('org_name', 'asc'); const count = await this._session .getDB()(this._tableName) .count('*') - .whereIn('id', [...ids, id]); + .whereIn('id', [...ids, id]) + .andWhere('active', true); return { stakeholders, count: +count[0].count }; } diff --git a/server/routes.js b/server/routes.js index 0ba17b4..182655b 100644 --- a/server/routes.js +++ b/server/routes.js @@ -5,7 +5,7 @@ const { stakeholderGetAll, // stakeholderGetRelations, // stakeholderCreateRelation, - stakeholderDeleteRelation, + // stakeholderDeleteRelation, stakeholderUpdate, stakeholderCreate, stakeholderDelete, @@ -13,7 +13,7 @@ const { const { handlerWrapper } = require('./utils/utils'); // router -// .route('/stakeholders/relations/:id') +// .route('/stakeholders/relations/:id'); // .get(handlerWrapper(stakeholderGetRelations)) // .post(handlerWrapper(stakeholderCreateRelation)) // .delete(handlerWrapper(stakeholderDeleteRelation)); @@ -23,7 +23,6 @@ router .get(handlerWrapper(stakeholderGetAllById)) .post(handlerWrapper(stakeholderCreate)) .patch(handlerWrapper(stakeholderUpdate)) - .put(handlerWrapper(stakeholderDeleteRelation)) .delete(handlerWrapper(stakeholderDelete)); router @@ -31,7 +30,6 @@ router .get(handlerWrapper(stakeholderGetAll)) .post(handlerWrapper(stakeholderCreate)) .patch(handlerWrapper(stakeholderUpdate)) - .put(handlerWrapper(stakeholderDeleteRelation)) .delete(handlerWrapper(stakeholderDelete)); module.exports = router; diff --git a/server/services/StakeholderService.js b/server/services/StakeholderService.js index 106d052..b44bc42 100644 --- a/server/services/StakeholderService.js +++ b/server/services/StakeholderService.js @@ -71,20 +71,20 @@ class StakeholderService { } } - async deleteRelation(id, requestObject) { - try { - await this._session.beginTransaction(); - await this._stakeholder.deleteRelation(id, requestObject); - await this._session.commitTransaction(); + // async deleteRelation(id, requestObject) { + // try { + // await this._session.beginTransaction(); + // await this._stakeholder.deleteRelation(id, requestObject); + // await this._session.commitTransaction(); - return id ? this.getAllStakeholdersById(id) : this.getAllStakeholders(); - } catch (e) { - if (this._session.isTransactionInProgress()) { - await this._session.rollbackTransaction(); - } - throw e; - } - } + // return id ? this.getAllStakeholdersById(id) : this.getAllStakeholders(); + // } catch (e) { + // if (this._session.isTransactionInProgress()) { + // await this._session.rollbackTransaction(); + // } + // throw e; + // } + // } } module.exports = StakeholderService;