Skip to content

Commit

Permalink
Merge pull request #46 from tieme-ndo/no-change-archived-farmers
Browse files Browse the repository at this point in the history
Don't change archived farmers
  • Loading branch information
Pav0l committed Sep 19, 2019
2 parents 22c738e + 6ac9a29 commit 3de0eab
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 2 deletions.
27 changes: 25 additions & 2 deletions controllers/changeRequest/approveChangeRequest.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { models } = require('../../models');
const { createError, GENERIC_ERROR } = require('../../helpers/error.js');
const { createError, GENERIC_ERROR, FORBIDDEN } = require('../../helpers/error.js');
const convertToDotNotationObject = require('../farmer/convertToDotNotationObject');

/**
Expand All @@ -15,16 +15,39 @@ const approveChangeRequest = async (req, res, next) => {
const changeRequestEntry = await models.ChangeRequest.findOne({
_id: req.params.id
});

if (changeRequestEntry) {
const convertedObject = convertToDotNotationObject(
changeRequestEntry.requested_changes
);

const farmer = await models.Farmer.findOne({ _id: changeRequestEntry.farmer_id }).lean();

if (!farmer) {
return next(
createError({
message: 'Farmer does not exist',
status: NOT_FOUND
})
);
}

if(farmer.archived){
await models.ChangeRequest.findOneAndRemove({ _id: req.params.id });
return next({
message: 'This Farmer is archived and can not be updated',
status: FORBIDDEN
})
}

await models.Farmer.findOneAndUpdate(
{ _id: changeRequestEntry.farmer_id },
convertedObject,
{ new: true, runValidators: true }
);

await models.ChangeRequest.findOneAndRemove({ _id: req.params.id });

return res.status(200).json({
success: true,
message: 'ChangeRequest approved',
Expand All @@ -34,7 +57,7 @@ const approveChangeRequest = async (req, res, next) => {
return res.status(404).json({
success: false,
message:
'There is no saved changeRequest with this ID, please subit a valid changeRequest-ID'
'There is no saved changeRequest with this ID, please submit a valid changeRequest-ID'
});
} catch (err) {
return next(
Expand Down
8 changes: 8 additions & 0 deletions controllers/farmer/updateFarmer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const convertToDotNotationObject = require('./convertToDotNotationObject');
const {
createError,
GENERIC_ERROR,
FORBIDDEN,
NOT_FOUND
} = require('../../helpers/error');

Expand All @@ -29,6 +30,13 @@ const updateFarmer = async (req, res, next) => {
);
}

if(farmer.archived){
return next({
message: 'This Farmer is archived and can not be updated',
status: FORBIDDEN
})
}

if (isAdmin) {
const convertedObject = convertToDotNotationObject(farmerDetails);
const updatedFarmer = await models.Farmer.findOneAndUpdate(
Expand Down
42 changes: 42 additions & 0 deletions tests/changeRequest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,46 @@ describe('Request change route', () => {
done(err);
});
});

it('It creates a changerequest if farmer is updated by staff. Copy of test to have a changerequest in DB', (done) => {
chai
.request(server)
.patch(`/api/v1/farmers/${idCreatedByStaff}/update`)
.set('Authorization', staffToken)
.send({ personalInfo: { title : 'Chief' } })
.end(async (err, res) => {
res.should.have.status(201);
res.body.message.should.equal(
'Your change was created and is ready for admin approval'
);
const changeRequests = await models.ChangeRequest.find();
chai.expect(changeRequests).to.have.lengthOf(1);
changeRequestId = changeRequests[0]._id;
done(err);
});
});

it('It sets the farmer to archived, necessary for next test', (done) => {
chai
.request(server)
.delete(`/api/v1/farmers/${idCreatedByStaff}/delete`)
.set('Authorization', token)
.end((err, res) => {
res.should.have.status(200);
res.body.message.should.equal('Farmer details deleted successfully');
done(err);
});
});

it('It does not accept an update of an archived farmer details', (done) => {
chai
.request(server)
.post(`/api/v1/change-requests/${changeRequestId}/approve`)
.set('Authorization', token)
.end((err, res) => {
res.should.have.status(403);
res.body.message.should.equal('This Farmer is archived and can not be updated');
done(err);
});
});
});
27 changes: 27 additions & 0 deletions tests/farmers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,33 @@ describe('Farmer route', () => {
done(err);
});
});
it('It does not update archived farmer details when done by admin', (done) => {
farmerInput.personalInfo.title = 'Chief';
chai
.request(server)
.patch(`/api/v1/farmers/${id}/update`)
.set('Authorization', token)
.send(farmerInput)
.end((err, res) => {
res.should.have.status(403);
res.body.message.should.equal('This Farmer is archived and can not be updated');
done(err);
});
});
it('It does not update archived farmer details if done by staff', (done) => {
farmerInput.personalInfo.title = 'Chief';

chai
.request(server)
.patch(`/api/v1/farmers/${idCreatedByStaff}/update`)
.set('Authorization', staffToken)
.send(farmerInput)
.end((err, res) => {
res.should.have.status(403);
res.body.message.should.equal('This Farmer is archived and can not be updated');
done(err);
});
});
it('It should return 404 if there are no farmers in the DB', (done) => {
chai
.request(server)
Expand Down

0 comments on commit 3de0eab

Please sign in to comment.