Skip to content

Commit

Permalink
Merge pull request #41 from tieme-ndo/fix-date-update
Browse files Browse the repository at this point in the history
Fix updating dates for farmers
  • Loading branch information
Pav0l committed Sep 17, 2019
2 parents b22c844 + d7741c4 commit b3ec336
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
4 changes: 3 additions & 1 deletion controllers/farmer/convertToDotNotationObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ to a dot notation object in order to make updates
in deep objects using Mongoose
*/
function convertToDotNotationObject(obj) {
let dateObject = {};
if (obj.personalInfo.date_of_birth) dateObject = { "personalInfo.date_of_birth" : obj.personalInfo.date_of_birth };
const dotNotationObject = dotize.convert(obj);
const arraysObject = {};
Object.keys(dotNotationObject).map(key => {
Expand All @@ -18,7 +20,7 @@ function convertToDotNotationObject(obj) {
}
});

return { ...dotNotationObject, ...arraysObject };
return { ...dotNotationObject, ...arraysObject, ...dateObject };
}

module.exports = convertToDotNotationObject;
4 changes: 2 additions & 2 deletions controllers/farmer/updateFarmer.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const updateFarmer = async (req, res, next) => {
if (farmer.staff === username || isAdmin) {
if (isAdmin) {
const convertedObject = convertToDotNotationObject(farmerDetails);
const farmer = await models.Farmer.findOneAndUpdate(
const updatedFarmer = await models.Farmer.findOneAndUpdate(
{ _id: farmerId },
convertedObject,
{ new: true, runValidators: true }
Expand All @@ -40,7 +40,7 @@ const updateFarmer = async (req, res, next) => {
return res.status(201).json({
success: true,
message: 'Farmer details updated successfully',
farmer
farmer: updatedFarmer
});
}

Expand Down

0 comments on commit b3ec336

Please sign in to comment.