Add BMR formula
This commit is contained in:
parent
56ca8d4ec2
commit
78cee39ca9
11
src/config/activities.js
Normal file
11
src/config/activities.js
Normal file
@ -0,0 +1,11 @@
|
||||
const activityTypes = {
|
||||
veryLow: 1.2,
|
||||
low: 1.375,
|
||||
medium: 1.55,
|
||||
high: 1.725,
|
||||
veryHigh: 1.9,
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
activityTypes,
|
||||
};
|
8
src/config/genders.js
Normal file
8
src/config/genders.js
Normal file
@ -0,0 +1,8 @@
|
||||
const genderTypes = {
|
||||
male: 'male',
|
||||
female: 'female',
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
genderTypes
|
||||
}
|
7
src/config/goals.js
Normal file
7
src/config/goals.js
Normal file
@ -0,0 +1,7 @@
|
||||
const goalTypes = {
|
||||
loseWeight: 'lose_weight',
|
||||
maintainWeight: 'maintain_weight',
|
||||
putOnWeight: 'put_on_weight',
|
||||
};
|
||||
|
||||
module.exports = { goalTypes };
|
@ -11,8 +11,8 @@ const createMeal = catchAsync(async (req, res) => {
|
||||
});
|
||||
|
||||
const getMeals = catchAsync(async (req, res) => {
|
||||
const result = await mealService.getMealsByDate(req.user._id, req.query.date);
|
||||
res.send(result);
|
||||
const meals = await mealService.getMealsByDate(req.user._id, req.query.date);
|
||||
res.send(meals);
|
||||
});
|
||||
|
||||
const getAllMeals = catchAsync(async (req, res) => {
|
||||
|
@ -17,7 +17,7 @@ const getProducts = catchAsync(async (req, res) => {
|
||||
});
|
||||
|
||||
const getProduct = catchAsync(async (req, res) => {
|
||||
const product = await productService.getProduct(req.params.productId);
|
||||
const product = await productService.getProductById(req.params.productId);
|
||||
if (!product) {
|
||||
throw new ApiError(httpStatus.NOT_FOUND, 'Product not found');
|
||||
}
|
||||
|
@ -9,7 +9,7 @@ const router = express.Router();
|
||||
router
|
||||
.route('/')
|
||||
.post(auth(), validate(profileValidation.createProfile), profileController.createProfile)
|
||||
.get(auth(), validate(profileValidation.getProfile), profileController.getProfile)
|
||||
.get(auth(), validate(profileValidation.getProfile), profileController.getProfile)
|
||||
.patch(auth(), validate(profileValidation.updateProfile), profileController.updateProfile)
|
||||
.delete(auth(), validate(profileValidation.deleteProfile), profileController.deleteProfile);
|
||||
|
||||
|
@ -19,7 +19,7 @@ const getMealsByDate = async (userId, date) => {
|
||||
$gte: new Date(new Date(date).setHours(0, 0, 0)),
|
||||
$lt: new Date(new Date(date).setHours(23, 59, 59)),
|
||||
},
|
||||
});
|
||||
}).populate('products.product');
|
||||
return meals;
|
||||
};
|
||||
|
||||
|
@ -1,6 +1,8 @@
|
||||
const httpStatus = require('http-status');
|
||||
const { Profile } = require('../models');
|
||||
const ApiError = require('../utils/ApiError');
|
||||
const calculateWeeksToGetGoalWeight = require('../utils/weeksToGetGoalWeight');
|
||||
const dailyCaloricRequirement = require('../utils/dailyCaloricRequirement');
|
||||
|
||||
const createProfile = async (profileBody) => {
|
||||
if (await Profile.hasUser(profileBody.userId)) {
|
||||
@ -16,7 +18,10 @@ const queryProfiles = async (filter, options) => {
|
||||
};
|
||||
|
||||
const getProfileById = async (id) => {
|
||||
return Profile.findById(id);
|
||||
const profile = await Profile.findById(id);
|
||||
console.log(dailyCaloricRequirement(profile));
|
||||
console.log(calculateWeeksToGetGoalWeight(profile));
|
||||
return profile
|
||||
};
|
||||
|
||||
const getProfileByUserId = async (userId) => {
|
||||
|
4
src/utils/birthdayToAge.js
Normal file
4
src/utils/birthdayToAge.js
Normal file
@ -0,0 +1,4 @@
|
||||
|
||||
const birthdayToAge = (birthday) => Math.floor((new Date() - new Date(birthday).getTime()) / 3.15576e10);
|
||||
|
||||
module.exports = birthdayToAge;
|
23
src/utils/dailyCaloricRequirement.js
Normal file
23
src/utils/dailyCaloricRequirement.js
Normal file
@ -0,0 +1,23 @@
|
||||
const birthdayToAge = require('./birthdayToAge');
|
||||
const { genderTypes } = require('../config/genders');
|
||||
const { goalTypes } = require('../config/goals');
|
||||
|
||||
const calculateDailyCaloricRequirementFoWomen = ({ age, currentWeight, height, activity, extraCalories }) =>
|
||||
Math.round((655 + (9.6 * currentWeight) + (1.8 * height) - (4.7 * age)) * activity + extraCalories)
|
||||
|
||||
|
||||
const calculateDailyCaloricRequirementFoMen = ({ age, currentWeight, height, activity, extraCalories }) =>
|
||||
Math.round((66 + (13.7 * currentWeight) + (5 * height) - (6.8 * age)) * activity + extraCalories)
|
||||
|
||||
const calculateDailyCaloricRequirement = ({ gender, goal, birthday, height, currentWeight, rateOfChange, activity }) => {
|
||||
const age = birthdayToAge(birthday);
|
||||
|
||||
const extraCalories = (goal === goalTypes.putOnWeight ? rateOfChange : -rateOfChange) * 100;
|
||||
|
||||
const dailyCaloricRequirement =
|
||||
gender === genderTypes.male ? calculateDailyCaloricRequirementFoMen : calculateDailyCaloricRequirementFoWomen;
|
||||
|
||||
return dailyCaloricRequirement({ age, currentWeight, height, activity, extraCalories });
|
||||
};
|
||||
|
||||
module.exports = calculateDailyCaloricRequirement;
|
8
src/utils/weeksToGetGoalWeight.js
Normal file
8
src/utils/weeksToGetGoalWeight.js
Normal file
@ -0,0 +1,8 @@
|
||||
|
||||
const calculateWeeksToGetGoalWeight = ({ currentWeight, goalWeight, rateOfChange }) => {
|
||||
const weightsDifference = Math.abs(goalWeight - currentWeight);
|
||||
const weeksToGetGoalWeight = Math.round(weightsDifference / rateOfChange);
|
||||
return weeksToGetGoalWeight;
|
||||
};
|
||||
|
||||
module.exports = calculateWeeksToGetGoalWeight;
|
@ -1,16 +1,19 @@
|
||||
const Joi = require('joi');
|
||||
const { objectId } = require('./custom.validation');
|
||||
const { genderTypes } = require('../config/genders');
|
||||
const { goalTypes } = require('../config/goals');
|
||||
const { activityTypes } = require('../config/activities');
|
||||
|
||||
const createProfile = {
|
||||
body: Joi.object().keys({
|
||||
gender: Joi.string().required().valid('male', 'female'),
|
||||
goal: Joi.string().required().valid('lose_weight', 'maintain_weight', 'put_on_weight'),
|
||||
gender: Joi.string().required().valid(Object.values(genderTypes)),
|
||||
goal: Joi.string().required().valid(Object.values(goalTypes)),
|
||||
birthday: Joi.date().required(),
|
||||
height: Joi.number().required(),
|
||||
currentWeight: Joi.number().required(),
|
||||
goalWeight: Joi.number().required(),
|
||||
rateOfChange: Joi.number().required(),
|
||||
activity: Joi.number().required().valid(1.2, 1.375, 1.55, 1.725, 1.9),
|
||||
activity: Joi.number().required().valid(Object.values(activityTypes)),
|
||||
}),
|
||||
};
|
||||
|
||||
@ -33,14 +36,14 @@ const updateProfile = {
|
||||
profileId: Joi.required().custom(objectId),
|
||||
}),
|
||||
body: Joi.object().keys({
|
||||
gender: Joi.string().required().valid('male', 'female'),
|
||||
goal: Joi.string().required().valid('lose_weight', 'maintain_weight', 'put_on_weight'),
|
||||
gender: Joi.string().required().valid(Object.values(genderTypes)),
|
||||
goal: Joi.string().required().valid(Object.values(goalTypes)),
|
||||
birthday: Joi.date().required(),
|
||||
height: Joi.number().required(),
|
||||
currentWeight: Joi.number().required(),
|
||||
goalWeight: Joi.number().required(),
|
||||
rateOfChange: Joi.number(),
|
||||
activity: Joi.number().required().valid(1.2, 1.375, 1.55, 1.725, 1.9),
|
||||
activity: Joi.number().required().valid(Object.values(activityTypes)),
|
||||
}).min(1),
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user