Skip to content

Commit

Permalink
feat: fix errors with validation, don't delete stakeholder just relation
Browse files Browse the repository at this point in the history
  • Loading branch information
gwynndp committed May 10, 2022
1 parent 02007b1 commit 5327637
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 110 deletions.
114 changes: 38 additions & 76 deletions server/handlers/stakeholderHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,48 @@ const stakeholderGetQuerySchema = Joi.object({
}).unknown(false);

const stakeholderPostSchema = Joi.object({
type: Joi.string(),
email: Joi.string(),
first_name: Joi.string(),
last_name: Joi.string(),
org_name: Joi.string(),
logo_url: Joi.string(),
map: Joi.string(),
phone: Joi.string(),
website: Joi.string().uri(),
image_url: Joi.string().allow(''),
type: Joi.string(),
offering_pay_to_plant: Joi.boolean(),
relation: Joi.string(),
relation_id: Joi.string().uuid(),
}).unknown(false);

const stakeholderDeleteSchema = Joi.object({
id: Joi.string().uuid(),
type: Joi.string(),
linked: Joi.boolean(),
relation_id: Joi.string().uuid(),
}).unknown(false);

const updateStakeholderSchema = Joi.object({
id: Joi.string().uuid().required(),
email: Joi.string().email(),
org_name: Joi.string(),
first_name: Joi.string(),
last_name: Joi.string(),
logo_url: Joi.string(),
map: Joi.string(),
org_name: Joi.string(),
phone: Joi.string(),
website: Joi.string().uri(),
}).unknown(false);
children: Joi.array().items(Joi.object()),
parents: Joi.array().items(Joi.object()),
image_url: Joi.string().allow(''),
type: Joi.string(),
created_at: Joi.string(),
updated_at: Joi.string(),
})
.unknown(false)
.xor('org_name', 'first_name')
.xor('org_name', 'last_name');

const stakeholderGetAll = async (req, res) => {
await stakeholderGetQuerySchema.validateAsync(req.query, {
Expand Down Expand Up @@ -103,7 +124,7 @@ const stakeholderCreate = async function (req, res) {
const requestObject = await stakeholderPostSchema.validateAsync(req.body, {
abortEarly: false,
});
const { id } = req.params || req.body.relation_id;
const { id } = req.params;

const stakeholderService = new StakeholderService();
const result = await stakeholderService.createStakeholder(id, requestObject);
Expand All @@ -115,7 +136,7 @@ const stakeholderDelete = async function (req, res) {
const requestObject = await stakeholderDeleteSchema.validateAsync(req.body, {
abortEarly: false,
});
const { id } = req.params || req.body.relation_id;
const { id } = req.params;

const stakeholderService = new StakeholderService();
const result = await stakeholderService.deleteStakeholder(id, requestObject);
Expand All @@ -128,89 +149,30 @@ const stakeholderUpdate = async function (req, res) {
abortEarly: false,
});

// NOT CURRENTLY IN USE
// const { id } = req.params;
const stakeholderService = new StakeholderService();
const result = await stakeholderService.updateStakeholder(requestObject);

res.status(200).json(result);
};

// const stakeholderGetRelations = async function (req, res, next) {
// const { id } = req.params;
// const session = new Session(false);
// const repo = new StakeholderRepository(session);

// const executeGetRelations = getRelations(repo, id);
// try {
// const result = await executeGetRelations();
// res.send(result);
// res.end();
// } catch (e) {
// next(e);
// }
// };

// const stakeholderCreateRelation = async function (req, res, next) {
// const { id } = req.params;
// const session = new Session();
// const repo = new StakeholderRepository(session);
// const executeCreateRelation = createRelation(repo, id);

// const createStakeholderSchema = Joi.object({
// type: Joi.string().required(),
// data: Joi.object().required(),
// });

// try {
// const value = await createStakeholderSchema
// .unknown(true)
// .validateAsync(req.body, {
// abortEarly: false,
// });

// const result = await executeCreateRelation(value);
// res.send(result);
// res.end();
// } catch (e) {
// next(e);
// }
// };

// const stakeholderDeleteRelation = async function (req, res, next) {
// const { id } = req.params;
// const session = new Session();
// const repo = new StakeholderRepository(session);
// const executeDeleteRelation = deleteRelation(repo, id);
// const executeDeleteStakeholder = deleteStakeholder(repo, id);

// const deleteStakeholderSchema = Joi.object({
// type: Joi.string().required(),
// data: Joi.object().required(),
// });

// try {
// const value = await deleteStakeholderSchema
// .unknown(true)
// .validateAsync(req.body, {
// abortEarly: false,
// });

// await executeDeleteRelation(value);
// const result = await executeDeleteStakeholder(value);
// res.send(result);
// res.end();
// } catch (e) {
// next(e);
// }
// };
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);

res.status(200).json(result);
};

module.exports = {
stakeholderGetAllById,
stakeholderGetAll,
// stakeholderGetRelations,
// stakeholderCreateRelation,
// stakeholderDeleteRelation,
stakeholderDeleteRelation,
stakeholderCreate,
stakeholderDelete,
stakeholderUpdate,
Expand Down
27 changes: 10 additions & 17 deletions server/models/Stakeholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,9 +234,9 @@ class Stakeholder {
};
}

async getRelations(current_id) {
async getRelations(id) {
const { stakeholders, count } =
await this._stakeholderRepository.getRelations(current_id);
await this._stakeholderRepository.getRelations(id);

return {
stakeholders:
Expand All @@ -251,14 +251,14 @@ class Stakeholder {
};
}

async deleteRelation(current_id, stakeholder) {
async deleteRelation(current_id, data) {
const id = await this.getUUID(current_id);
const { type, data } = stakeholder;
const { type, relation_id } = data;
const removeObj = {};

if (type === 'parents' || type === 'children') {
removeObj.parent_id = type === 'parents' ? data.id : id;
removeObj.child_id = type === 'children' ? data.id : id;
removeObj.parent_id = type === 'parents' ? relation_id : id;
removeObj.child_id = type === 'children' ? relation_id : id;
}

const stakeholderRelation =
Expand All @@ -279,25 +279,18 @@ class Stakeholder {
return this.stakeholderTree({ ...stakeholder, children, parents });
}

async createStakeholder(org_id = null, newStakeholder) {
const id = await this.getUUID(org_id);
const stakeholderObj = this.constructor.StakeholderPostObject({
...newStakeholder,
});
async createStakeholder(data) {
const stakeholderObj = this.constructor.StakeholderPostObject(data);

// not sure what the id here is meant for but provision was not made for it in the repository
const stakeholder = await this._stakeholderRepository.createStakeholder(
stakeholderObj,
id,
);

return this.stakeholderTree({ ...stakeholder });
}

async deleteStakeholder(removeStakeholder) {
const stakeholder = await this._stakeholderRepository.deleteStakeholder(
removeStakeholder,
);
async deleteStakeholder(id) {
const stakeholder = await this._stakeholderRepository.deleteStakeholder(id);

return this.stakeholderTree({ ...stakeholder });
}
Expand Down
28 changes: 14 additions & 14 deletions server/repositories/StakeholderRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,16 @@ class StakeholderRepository extends BaseRepository {
}

// not currently being used but may be useful later
async getStakeholderTreeById(id, options) {
async getStakeholderTreeById(id = null) {
const stakeholder = await this._session
.getDB()(this._tableName)
.select('*')
.where('id', id)
.first();

// only get one step generation difference, no recursion
stakeholder.parents = await this.getParents(stakeholder, options);
stakeholder.children = await this.getChildren(stakeholder, options);
stakeholder.parents = await this.getParents(stakeholder);
stakeholder.children = await this.getChildren(stakeholder);

const count = await this._session
.getDB()(this._tableName)
Expand Down Expand Up @@ -242,10 +242,10 @@ class StakeholderRepository extends BaseRepository {
return { stakeholders, count: +count[0].count };
}

async createStakeholder(object) {
async createStakeholder(stakeholder) {
const created = await this._session
.getDB()(this._tableName)
.insert(object)
.insert(stakeholder)
.returning('*');

expect(created).match([
Expand All @@ -257,10 +257,10 @@ class StakeholderRepository extends BaseRepository {
return created[0];
}

async deleteStakeholder(object) {
async deleteStakeholder(id) {
const deleted = await this._session
.getDB()(this._tableName)
.where('id', object.id)
.where('id', id)
.del()
.returning('*');

Expand All @@ -273,11 +273,11 @@ class StakeholderRepository extends BaseRepository {
return deleted[0];
}

async updateStakeholder(object) {
async updateStakeholder(stakeholder) {
const updated = await this._session
.getDB()(this._tableName)
.where('id', object.id)
.update(object, ['*']);
.where('id', stakeholder.id)
.update(stakeholder, ['*']);

expect(updated).match([
{
Expand Down Expand Up @@ -350,21 +350,21 @@ class StakeholderRepository extends BaseRepository {
// return { stakeholders, count: +count[0].count };
// }

async createRelation(stakeholder) {
async createRelation(relationData) {
const linkedStakeholders = await this._session
.getDB()('stakeholder_relation')
.insert(stakeholder)
.insert(relationData)
.returning('*');

expect(linkedStakeholders[0]).to.have.property('parent_id');

return linkedStakeholders[0];
}

async deleteRelation(stakeholder) {
async deleteRelation(relationData) {
const linkedStakeholders = await this._session
.getDB()('stakeholder_relation')
.where(stakeholder)
.where(relationData)
.del()
.returning('*');

Expand Down
4 changes: 3 additions & 1 deletion server/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const {
stakeholderGetAll,
// stakeholderGetRelations,
// stakeholderCreateRelation,
// stakeholderDeleteRelation,
stakeholderDeleteRelation,
stakeholderUpdate,
stakeholderCreate,
stakeholderDelete,
Expand All @@ -23,13 +23,15 @@ 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;
19 changes: 17 additions & 2 deletions server/services/StakeholderService.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class StakeholderService {
try {
await this._session.beginTransaction();
const createdStakeholder = await this._stakeholder.createStakeholder(
id,
requestObject,
);
await this._stakeholder.createRelation(id, {
Expand All @@ -40,10 +39,11 @@ class StakeholderService {
async deleteStakeholder(id, requestObject) {
try {
await this._session.beginTransaction();
await this._stakeholder.deleteStakeholder(requestObject.data);
await this._stakeholder.deleteStakeholder(requestObject.relation_id);
await this._stakeholder.deleteRelation(id, {
type: requestObject.type,
data: requestObject.data,
relation_id: requestObject.relation_id,
});
await this._session.commitTransaction();

Expand All @@ -70,6 +70,21 @@ class StakeholderService {
throw e;
}
}

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;
}
}
}

module.exports = StakeholderService;

0 comments on commit 5327637

Please sign in to comment.