Add calc marcors

This commit is contained in:
= 2021-01-24 23:32:52 +01:00
parent 7744898c09
commit f9cf60f530
5 changed files with 46 additions and 5 deletions

View File

@ -7,14 +7,13 @@ const { mealService, profileService } = require('../services');
const createMeal = catchAsync(async (req, res) => {
req.body.user = req.user._id;
const meal = await mealService.createMeal(req.body);
const { dailyCalories } = await profileService.getProfileByUserId(req.user._id);
res.status(httpStatus.CREATED).send({ meal, dailyCalories });
res.status(httpStatus.CREATED).send(meal);
});
const getMeals = catchAsync(async (req, res) => {
const meals = await mealService.getMealsByDate(req.user._id, req.query.date);
const { dailyCalories } = await profileService.getProfileByUserId(req.user._id);
res.send({ meals, dailyCalories });
const { dailyCalories, dailyFats, dailyProteins, dailyCarbs } = await profileService.getProfileByUserId(req.user._id);
res.send({ meals, dailyCalories, dailyFats, dailyProteins, dailyCarbs });
});
const getAllMeals = catchAsync(async (req, res) => {

View File

@ -20,6 +20,18 @@ const profileSchema = mongoose.Schema(
type: Number,
required: true,
},
dailyFats: {
type: Number,
required: true,
},
dailyProteins: {
type: Number,
required: true,
},
dailyCarbs: {
type: Number,
required: true,
},
weeksToGoal: {
type: Number,
required: true,

View File

@ -86,6 +86,21 @@ const data = [
protein: 0.3,
salt: 0.14,
},
{
label: `Deser mleczny Monte`,
brand: `Zott`,
verified: true,
eco: false,
barcode: 4014500043326,
unit: 'g',
servingCapacity: 55,
calories: 195,
fat: 13.3,
carbohydrates: 15.9,
protein: 2.8,
salt: 0,
},
];
class ProductSeeder extends Seeder {

View File

@ -50,7 +50,8 @@ const updateMealProductsById = async (mealId, updateBody) => {
}
meal.products.push(...updateBody.products);
await meal.save();
return meal;
const mealWithProducts = await meal.populate('products.product').execPopulate();
return mealWithProducts;
};
const deleteMealById = async (mealId) => {

View File

@ -12,10 +12,17 @@ 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 profile = await Profile.create({
...profileBody,
dailyCalories,
weeksToGoal,
dailyFats,
dailyProteins,
dailyCarbs,
});
return profile;
};
@ -43,10 +50,17 @@ 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;
Object.assign(profile, {
...updateBody,
dailyCalories,
weeksToGoal,
dailyFats,
dailyProteins,
dailyCarbs,
});
await profile.save();
return profile;