Skip to content

Commit

Permalink
feat: add active column to stakeholder, remove deleteRelation endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
gwynndp committed May 10, 2022
1 parent 9515d89 commit 11ac043
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 33 deletions.
22 changes: 12 additions & 10 deletions server/handlers/stakeholderHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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,
Expand Down
25 changes: 19 additions & 6 deletions server/repositories/StakeholderRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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 };
}
Expand All @@ -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
Expand All @@ -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],
Expand All @@ -142,6 +147,7 @@ class StakeholderRepository extends BaseRepository {
.getDB()(this._tableName)
.select('*')
.whereIn('id', parentIds)
.andWhere('active', true)
.orderBy('org_name', 'asc');
}
return [];
Expand All @@ -166,6 +172,7 @@ class StakeholderRepository extends BaseRepository {
.getDB()(this._tableName)
.select('*')
.whereIn('id', childrenFound)
.andWhere('active', true)
.orderBy('org_name', 'asc');
}
return [];
Expand Down Expand Up @@ -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) {
Expand All @@ -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 };
}
Expand All @@ -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) {
Expand All @@ -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 };
}
Expand All @@ -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([
Expand Down Expand Up @@ -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 };
}
Expand Down
6 changes: 2 additions & 4 deletions server/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ const {
stakeholderGetAll,
// stakeholderGetRelations,
// stakeholderCreateRelation,
stakeholderDeleteRelation,
// stakeholderDeleteRelation,
stakeholderUpdate,
stakeholderCreate,
stakeholderDelete,
} = require('./handlers/stakeholderHandler');
const { handlerWrapper } = require('./utils/utils');

// router
// .route('/stakeholders/relations/:id')
// .route('/stakeholders/relations/:id');
// .get(handlerWrapper(stakeholderGetRelations))
// .post(handlerWrapper(stakeholderCreateRelation))
// .delete(handlerWrapper(stakeholderDeleteRelation));
Expand All @@ -23,15 +23,13 @@ router
.get(handlerWrapper(stakeholderGetAllById))
.post(handlerWrapper(stakeholderCreate))
.patch(handlerWrapper(stakeholderUpdate))
.put(handlerWrapper(stakeholderDeleteRelation))
.delete(handlerWrapper(stakeholderDelete));

router
.route('/stakeholders')
.get(handlerWrapper(stakeholderGetAll))
.post(handlerWrapper(stakeholderCreate))
.patch(handlerWrapper(stakeholderUpdate))
.put(handlerWrapper(stakeholderDeleteRelation))
.delete(handlerWrapper(stakeholderDelete));

module.exports = router;
26 changes: 13 additions & 13 deletions server/services/StakeholderService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 11ac043

Please sign in to comment.