Skip to content

Commit

Permalink
fix: minor updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Kpoke committed Apr 18, 2022
1 parent 73d13da commit 740a390
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 61 deletions.
87 changes: 43 additions & 44 deletions server/handlers/stakeholderHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ const stakeholderPostSchema = Joi.object({
type: Joi.string(),
email: Joi.string(),
phone: Joi.string(),
}).unknown();
}).unknown(false);

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

const updateStakeholderSchema = Joi.object({
id: Joi.string().uuid().required(),
type: Joi.string().required(),
email: Joi.string().required(),
phone: Joi.string().required(),
}).unknown(true);
type: Joi.string(),
email: Joi.string(),
phone: Joi.string(),
}).unknown(false);

const stakeholderGetAll = async (req, res) => {
await stakeholderGetQuerySchema.validateAsync(req.query, {
Expand Down Expand Up @@ -94,6 +94,43 @@ const stakeholderGetAllById = async (req, res) => {
});
};

const stakeholderCreate = async function (req, res) {
const requestObject = await stakeholderPostSchema.validateAsync(req.body, {
abortEarly: false,
});
const { id } = req.params || req.body.relation_id;

const stakeholderService = new StakeholderService();
const result = await stakeholderService.createStakeholder(id, requestObject);

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

const stakeholderDelete = async function (req, res) {
const requestObject = await stakeholderDeleteSchema.validateAsync(req.body, {
abortEarly: false,
});
const { id } = req.params || req.body.relation_id;

const stakeholderService = new StakeholderService();
const result = await stakeholderService.deleteStakeholder(id, requestObject);

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

const stakeholderUpdate = async function (req, res) {
const requestObject = await updateStakeholderSchema.validateAsync(req.body, {
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);
Expand Down Expand Up @@ -163,44 +200,6 @@ const stakeholderGetAllById = async (req, res) => {
// }
// };

const stakeholderCreate = async function (req, res) {
const requestObject = await stakeholderPostSchema.validateAsync(req.body, {
abortEarly: false,
});
const { id } = req.params || req.body.relation_id;

const stakeholderService = new StakeholderService();
const result = await stakeholderService.createStakeholder(id, requestObject);

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

const stakeholderDelete = async function (req, res) {
const requestObject = await stakeholderDeleteSchema.validateAsync(req.body, {
abortEarly: false,
});
const { id } = req.params || req.body.relation_id;

const stakeholderService = new StakeholderService();
const result = await stakeholderService.deleteStakeholder(id, requestObject);

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

const stakeholderUpdate = async function (req, res) {
const requestObject = await updateStakeholderSchema
.unknown(true)
.validateAsync(req.body, {
abortEarly: false,
});

const { id } = req.params;
const stakeholderService = new StakeholderService();
const result = await stakeholderService.updateStakeholder(id, requestObject);

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

module.exports = {
stakeholderGetAllById,
stakeholderGetAll,
Expand Down
2 changes: 1 addition & 1 deletion server/models/Stakeholder.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ class Stakeholder {
return stakeholderRelation;
}

async updateStakeholder(_id, data) {
async updateStakeholder(data) {
const editedStakeholder = this.stakeholderTree({ ...data });

// remove children and parents temporarily to update
Expand Down
11 changes: 0 additions & 11 deletions server/models/Stakeholder.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,5 @@ describe('Stakeholder Model', () => {
.property('id')
.eq('792a4eee-8e18-4750-a56f-91bdec383aa6');
});

it('should get stakeholders with filter -- id (integer)', async () => {
const stakeholder = new Stakeholder();
const result = await stakeholder.getAllStakeholders({
id: 1,
});

expect(result.stakeholders).to.have.length(1);
expect(result.totalCount).to.eql(1);
expect(result.stakeholders[0]).property('id').eq(1);
});
});
});
7 changes: 2 additions & 5 deletions server/services/StakeholderService.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,10 @@ class StakeholderService {
}
}

async updateStakeholder(id, requestObject) {
async updateStakeholder(requestObject) {
try {
await this._session.beginTransaction();
const result = await this._stakeholder.updateStakeholder(
id,
requestObject,
);
const result = await this._stakeholder.updateStakeholder(requestObject);
await this._session.commitTransaction();

return result;
Expand Down

0 comments on commit 740a390

Please sign in to comment.