diff --git a/src/controllers/meal.controller.js b/src/controllers/meal.controller.js index 5867b85..ba06bb3 100644 --- a/src/controllers/meal.controller.js +++ b/src/controllers/meal.controller.js @@ -41,9 +41,9 @@ const updateMealProducts = catchAsync(async (req, res) => { res.send(meal); }); -const deleteMeal = catchAsync(async (req, res) => { - await mealService.deleteMealById(req.params.mealId); - res.status(httpStatus.NO_CONTENT).send(); +const removeProductByMeal = catchAsync(async (req, res) => { + const meal = await mealService.removeProductByMealId(req.params.mealId, req.body.productId); + res.send(meal); }); module.exports = { @@ -53,5 +53,5 @@ module.exports = { getMeal, updateMeal, updateMealProducts, - deleteMeal, + removeProductByMeal, }; diff --git a/src/routes/v1/meal.route.js b/src/routes/v1/meal.route.js index dfbba47..1ef616f 100644 --- a/src/routes/v1/meal.route.js +++ b/src/routes/v1/meal.route.js @@ -19,7 +19,10 @@ router .route('/:mealId') .get(auth(), validate(mealValidation.getMeal), mealController.getMeal) .put(auth(), validate(mealValidation.updateMeal), mealController.updateMeal) - .patch(auth(), validate(mealValidation.updateMealProducts), mealController.updateMealProducts) - .delete(auth(), validate(mealValidation.deleteMeal), mealController.deleteMeal); + .patch(auth(), validate(mealValidation.updateMealProducts), mealController.updateMealProducts); + +router + .route('/:mealId/products') + .patch(auth(), validate(mealValidation.removeProductByMeal), mealController.removeProductByMeal); module.exports = router; diff --git a/src/services/meal.service.js b/src/services/meal.service.js index 7dec55e..5ccf1c2 100644 --- a/src/services/meal.service.js +++ b/src/services/meal.service.js @@ -54,13 +54,15 @@ const updateMealProductsById = async (mealId, updateBody) => { return mealWithProducts; }; -const deleteMealById = async (mealId) => { +const removeProductByMealId = async (mealId, productId) => { const meal = await getMealById(mealId); if (!meal) { throw new ApiError(httpStatus.NOT_FOUND, 'Meal not found'); } - await meal.remove(); - return meal; + meal.products.pull({ _id: productId }); + await meal.save(); + const mealWithProducts = await meal.populate('products.product').execPopulate() + return mealWithProducts; }; module.exports = { @@ -71,5 +73,5 @@ module.exports = { getMealByUserId, updateMealById, updateMealProductsById, - deleteMealById, + removeProductByMealId, }; diff --git a/src/services/profile.service.js b/src/services/profile.service.js index 0ca565d..26870c8 100644 --- a/src/services/profile.service.js +++ b/src/services/profile.service.js @@ -12,9 +12,9 @@ const createProfile = async (profileBody) => { const dailyCalories = dailyCaloricRequirement(profileBody); const weeksToGoal = calculateWeeksToGetGoalWeight(profileBody); - const dailyFats = (dailyCalories * 0.4) / 9; - const dailyProteins = (dailyCalories * 0.3) / 4; - const dailyCarbs = (dailyCalories * 0.4) / 4; + const dailyFats = Math.floor((dailyCalories * 0.4) / 9); + const dailyProteins = Math.floor((dailyCalories * 0.3) / 4); + const dailyCarbs = Math.floor((dailyCalories * 0.4) / 4); const profile = await Profile.create({ ...profileBody, @@ -50,9 +50,9 @@ const updateProfileById = async (profileId, updateBody) => { const dailyCalories = dailyCaloricRequirement(updateBody); const weeksToGoal = calculateWeeksToGetGoalWeight(updateBody); - const dailyFats = (dailyCalories * 0.4) / 9; - const dailyProteins = (dailyCalories * 0.3) / 4; - const dailyCarbs = (dailyCalories * 0.4) / 4; + const dailyFats = Math.floor((dailyCalories * 0.4) / 9); + const dailyProteins = Math.floor((dailyCalories * 0.3) / 4); + const dailyCarbs = Math.floor((dailyCalories * 0.4) / 4); Object.assign(profile, { ...updateBody, diff --git a/src/validations/meal.validation.js b/src/validations/meal.validation.js index f2e622f..f716780 100644 --- a/src/validations/meal.validation.js +++ b/src/validations/meal.validation.js @@ -52,10 +52,15 @@ const updateMealProducts = { .min(1), }; -const deleteMeal = { +const removeProductByMeal = { params: Joi.object().keys({ mealId: Joi.string().custom(objectId).required(), }), + body: Joi.object() + .keys({ + productId: Joi.string().custom(objectId).required(), + }) + .min(1), }; module.exports = { @@ -64,5 +69,5 @@ module.exports = { getMeal, updateMeal, updateMealProducts, - deleteMeal, + removeProductByMeal, };