From 7744898c09699777da3ada9a526ff6ddf01b6921 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sun, 24 Jan 2021 21:24:33 +0100 Subject: [PATCH] Return dailyCalories when getting meals --- TODO.md | 5 ----- src/config/config.js | 1 + src/controllers/meal.controller.js | 10 ++++++---- src/index.js | 2 +- src/validations/meal.validation.js | 4 ++-- 5 files changed, 10 insertions(+), 12 deletions(-) delete mode 100644 TODO.md diff --git a/TODO.md b/TODO.md deleted file mode 100644 index a5b9a48..0000000 --- a/TODO.md +++ /dev/null @@ -1,5 +0,0 @@ - [ ] formula - Women: BMR = 655 + (9.6 x weight in kg) + (1.8 x height in cm) - (4.7 x age in years) - Men: BMR = 66 + (13.7 x weight in kg) + (5 x height in cm) - (6.8 x age in years) - * activity - +- 100 kcal diff --git a/src/config/config.js b/src/config/config.js index 23dc1fa..39b3e71 100644 --- a/src/config/config.js +++ b/src/config/config.js @@ -7,6 +7,7 @@ dotenv.config({ path: path.join(__dirname, '../../.env') }); const envVarsSchema = Joi.object() .keys({ NODE_ENV: Joi.string().valid('production', 'development', 'test').required(), + HOSTNAME: Joi.string().default('0.0.0.0'), PORT: Joi.number().default(3000), MONGODB_URL: Joi.string().required().description('Mongo DB url'), JWT_SECRET: Joi.string().required().description('JWT secret key'), diff --git a/src/controllers/meal.controller.js b/src/controllers/meal.controller.js index 362e8b6..a057f76 100644 --- a/src/controllers/meal.controller.js +++ b/src/controllers/meal.controller.js @@ -2,17 +2,19 @@ const httpStatus = require('http-status'); const pick = require('../utils/pick'); const ApiError = require('../utils/ApiError'); const catchAsync = require('../utils/catchAsync'); -const { mealService } = require('../services'); +const { mealService, profileService } = require('../services'); const createMeal = catchAsync(async (req, res) => { req.body.user = req.user._id; const meal = await mealService.createMeal(req.body); - res.status(httpStatus.CREATED).send(meal); + const { dailyCalories } = await profileService.getProfileByUserId(req.user._id); + res.status(httpStatus.CREATED).send({ meal, dailyCalories }); }); const getMeals = catchAsync(async (req, res) => { const meals = await mealService.getMealsByDate(req.user._id, req.query.date); - res.send(meals); + const { dailyCalories } = await profileService.getProfileByUserId(req.user._id); + res.send({ meals, dailyCalories }); }); const getAllMeals = catchAsync(async (req, res) => { @@ -38,7 +40,7 @@ const updateMeal = catchAsync(async (req, res) => { const updateMealProducts = catchAsync(async (req, res) => { const meal = await mealService.updateMealProductsById(req.params.mealId, req.body); res.send(meal); -}) +}); const deleteMeal = catchAsync(async (req, res) => { await mealService.deleteMealById(req.params.mealId); diff --git a/src/index.js b/src/index.js index bbfeb3f..96bafca 100644 --- a/src/index.js +++ b/src/index.js @@ -6,7 +6,7 @@ const logger = require('./config/logger'); let server; mongoose.connect(config.mongoose.url, config.mongoose.options).then(() => { logger.info('Connected to MongoDB'); - server = app.listen(config.port, () => { + server = app.listen(config.port, config.hostname,() => { logger.info(`Listening to port ${config.port}`); }); }); diff --git a/src/validations/meal.validation.js b/src/validations/meal.validation.js index 8102894..f2e622f 100644 --- a/src/validations/meal.validation.js +++ b/src/validations/meal.validation.js @@ -9,7 +9,7 @@ const products = Joi.object().keys({ const createMeal = { body: Joi.object().keys({ - label: Joi.string().required().valid('breakfast', 'lunch', 'dinner', 'supper', 'snack I', 'snack II', 'snack III'), + label: Joi.string().required().valid('breakfast', 'lunch', 'dinner', 'supper'), products: Joi.array().items(products).required(), date: Joi.date(), }), @@ -34,7 +34,7 @@ const updateMeal = { body: Joi.object() .keys({ user: Joi.string().custom(objectId).required(), - label: Joi.string().required().valid('breakfast', 'lunch', 'dinner', 'supper', 'snack I', 'snack II', 'snack III'), + label: Joi.string().required().valid('breakfast', 'lunch', 'dinner', 'supper'), products: Joi.array().items(products).required(), date: Joi.date().required(), })