Spaces:
Running
Running
Merge pull request #103 from Modarb-Ai-Trainer/stats
Browse files- src/common/enums/meal-type.enum.ts +1 -0
- src/lib/services/crud.service.ts +1 -1
- src/modules/users/modules/activities/services/user-activities.service.ts +4 -0
- src/modules/users/modules/{meals/events/eat-custom-meal.event.ts → exercises/events/exercise-done.event.ts} +8 -8
- src/modules/users/modules/exercises/events/exercises-done.event.ts +22 -0
- src/modules/users/modules/exercises/services/exercises.service.ts +33 -2
- src/modules/users/modules/home/services/user-home.service.ts +105 -21
- src/modules/users/modules/meals/events/meal-done.event.ts +18 -0
- src/modules/users/modules/meals/events/meals-done.event.ts +22 -0
- src/modules/users/modules/meals/services/meals.service.ts +19 -6
- src/modules/users/modules/user-registered-meal-plans/services/meal-plans-progress.service.ts +4 -0
- src/modules/users/modules/user-registered-workouts/services/workouts-progress.service.ts +4 -0
src/common/enums/meal-type.enum.ts
CHANGED
|
@@ -4,4 +4,5 @@ export enum MealType {
|
|
| 4 |
SNACKS = "snacks",
|
| 5 |
DRINKS_BEVERAGES = 'drinks beverages',
|
| 6 |
LUNCH = "lunch",
|
|
|
|
| 7 |
}
|
|
|
|
| 4 |
SNACKS = "snacks",
|
| 5 |
DRINKS_BEVERAGES = 'drinks beverages',
|
| 6 |
LUNCH = "lunch",
|
| 7 |
+
CUSTOM = "custom",
|
| 8 |
}
|
src/lib/services/crud.service.ts
CHANGED
|
@@ -9,7 +9,7 @@ export const CrudService = <ModelDoc extends Document>(
|
|
| 9 |
}
|
| 10 |
) => {
|
| 11 |
return class CrudServiceClass {
|
| 12 |
-
|
| 13 |
|
| 14 |
async create(data: AnyKeys<ModelDoc>): Promise<ModelDoc> {
|
| 15 |
return this.model.create(data);
|
|
|
|
| 9 |
}
|
| 10 |
) => {
|
| 11 |
return class CrudServiceClass {
|
| 12 |
+
public model: Model<ModelDoc> = model;
|
| 13 |
|
| 14 |
async create(data: AnyKeys<ModelDoc>): Promise<ModelDoc> {
|
| 15 |
return this.model.create(data);
|
src/modules/users/modules/activities/services/user-activities.service.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { Activity } from "@common/models/activity.model";
|
| 2 |
+
import { CrudService } from "@lib/services/crud.service";
|
| 3 |
+
|
| 4 |
+
export class UserActivitiesService extends CrudService(Activity) {}
|
src/modules/users/modules/{meals/events/eat-custom-meal.event.ts → exercises/events/exercise-done.event.ts}
RENAMED
|
@@ -1,18 +1,18 @@
|
|
| 1 |
import { ActivityType } from "@common/enums/activity-type.enum";
|
| 2 |
import { Activity, IActivity } from "@common/models/activity.model";
|
| 3 |
import { EventsManager } from "@lib/events/events-manager";
|
| 4 |
-
import {
|
| 5 |
|
| 6 |
-
export class
|
| 7 |
-
constructor(public userId: string, public
|
| 8 |
}
|
| 9 |
|
| 10 |
-
EventsManager.on(
|
| 11 |
-
console.log(`
|
| 12 |
|
| 13 |
Activity.create({
|
| 14 |
-
user_id: event.userId,
|
| 15 |
-
|
| 16 |
-
|
| 17 |
} satisfies IActivity).catch(console.error);
|
| 18 |
});
|
|
|
|
| 1 |
import { ActivityType } from "@common/enums/activity-type.enum";
|
| 2 |
import { Activity, IActivity } from "@common/models/activity.model";
|
| 3 |
import { EventsManager } from "@lib/events/events-manager";
|
| 4 |
+
import { Types } from "mongoose";
|
| 5 |
|
| 6 |
+
export class ExerciseDoneEvent {
|
| 7 |
+
constructor(public userId: string, public exercisesId: string){}
|
| 8 |
}
|
| 9 |
|
| 10 |
+
EventsManager.on(ExerciseDoneEvent.name, async (event: ExerciseDoneEvent) => {
|
| 11 |
+
console.log(`Exercise done event for user ${event.userId}`);
|
| 12 |
|
| 13 |
Activity.create({
|
| 14 |
+
user_id: new Types.ObjectId(event.userId),
|
| 15 |
+
related_id: new Types.ObjectId(event.exercisesId),
|
| 16 |
+
activity_type: ActivityType.EXERCISE,
|
| 17 |
} satisfies IActivity).catch(console.error);
|
| 18 |
});
|
src/modules/users/modules/exercises/events/exercises-done.event.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { ActivityType } from "@common/enums/activity-type.enum";
|
| 2 |
+
import { Activity, IActivity } from "@common/models/activity.model";
|
| 3 |
+
import { EventsManager } from "@lib/events/events-manager";
|
| 4 |
+
import { Types } from "mongoose";
|
| 5 |
+
|
| 6 |
+
export class ExercisesDoneEvent {
|
| 7 |
+
constructor(public userId: string, public exercisesIds: string[]){}
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
EventsManager.on(ExercisesDoneEvent.name, async (event: ExercisesDoneEvent) => {
|
| 11 |
+
console.log(`Exercise done event for user ${event.userId}`);
|
| 12 |
+
|
| 13 |
+
await Promise.all(
|
| 14 |
+
event.exercisesIds.map(eId => {
|
| 15 |
+
Activity.create({
|
| 16 |
+
user_id: new Types.ObjectId(event.userId),
|
| 17 |
+
related_id: new Types.ObjectId(eId),
|
| 18 |
+
activity_type: ActivityType.EXERCISE,
|
| 19 |
+
} satisfies IActivity).catch(console.error);
|
| 20 |
+
})
|
| 21 |
+
)
|
| 22 |
+
});
|
src/modules/users/modules/exercises/services/exercises.service.ts
CHANGED
|
@@ -1,4 +1,35 @@
|
|
| 1 |
-
import {
|
|
|
|
| 2 |
import { CrudService } from "@lib/services/crud.service";
|
| 3 |
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { ExerciseType } from "@common/enums/exercise-type.enum";
|
| 2 |
+
import { Exercise, ExerciseDocument } from "@common/models/exercise.model";
|
| 3 |
import { CrudService } from "@lib/services/crud.service";
|
| 4 |
|
| 5 |
+
const caloriesPerMinute = 5;
|
| 6 |
+
const caloriesPerRep = 0.5;
|
| 7 |
+
|
| 8 |
+
export class ExerciseService extends CrudService(Exercise) {
|
| 9 |
+
calculateCalories(exercise: ExerciseDocument): number {
|
| 10 |
+
if (exercise.isDeleted) {
|
| 11 |
+
return 0;
|
| 12 |
+
}
|
| 13 |
+
|
| 14 |
+
let calories = 0;
|
| 15 |
+
|
| 16 |
+
switch (exercise.exerciseType) {
|
| 17 |
+
case ExerciseType.DURATION:
|
| 18 |
+
if (exercise.duration) {
|
| 19 |
+
calories = exercise.duration * caloriesPerMinute;
|
| 20 |
+
}
|
| 21 |
+
break;
|
| 22 |
+
|
| 23 |
+
case ExerciseType.WEIGHT:
|
| 24 |
+
if (exercise.reps && exercise.sets) {
|
| 25 |
+
calories = exercise.reps * exercise.sets * caloriesPerRep;
|
| 26 |
+
}
|
| 27 |
+
break;
|
| 28 |
+
|
| 29 |
+
default:
|
| 30 |
+
throw new Error(`Unknown exercise type: ${exercise.exerciseType}`);
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
return calories;
|
| 34 |
+
}
|
| 35 |
+
}
|
src/modules/users/modules/home/services/user-home.service.ts
CHANGED
|
@@ -3,8 +3,21 @@ import { faker } from '@faker-js/faker';
|
|
| 3 |
import { UserHomeYourDailyIntakeSerialization } from "../responses/user-home-your-daily-intake.serialization";
|
| 4 |
import { UserHomeDailyGoalsSerialization } from "../responses/user-home-daily-goals.serialization";
|
| 5 |
import { UserNutriHomeDailyGoalsSerialization } from "../responses/user-nutri-home-daily-goals.serialization";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
export class UserHomeService {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
private getDaysArray(startDate: Date, endDate: Date): string[] {
|
| 9 |
const days = [];
|
| 10 |
for (let day = startDate; day <= endDate; day.setDate(day.getDate() + 1)) {
|
|
@@ -13,15 +26,30 @@ export class UserHomeService {
|
|
| 13 |
return days;
|
| 14 |
}
|
| 15 |
|
| 16 |
-
async getDailyGoals(
|
| 17 |
-
const
|
| 18 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
const stepsGoal =
|
| 21 |
-
const stepsDone =
|
| 22 |
|
| 23 |
-
const exercisesCals =
|
| 24 |
-
const exercisesHours =
|
| 25 |
|
| 26 |
return {
|
| 27 |
waterGoal,
|
|
@@ -33,19 +61,66 @@ export class UserHomeService {
|
|
| 33 |
}
|
| 34 |
}
|
| 35 |
|
| 36 |
-
async getHomePageYourDailyIntake(
|
| 37 |
-
const
|
| 38 |
-
const
|
| 39 |
-
const
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
-
const carbsGoal =
|
| 42 |
-
const carbsConsumed =
|
| 43 |
|
| 44 |
-
const proteinGoal =
|
| 45 |
-
const proteinConsumed =
|
| 46 |
|
| 47 |
-
const fatGoal =
|
| 48 |
-
const fatConsumed =
|
| 49 |
|
| 50 |
return {
|
| 51 |
caloriesGoal,
|
|
@@ -60,21 +135,30 @@ export class UserHomeService {
|
|
| 60 |
}
|
| 61 |
}
|
| 62 |
|
| 63 |
-
async getHomePageStreak(
|
| 64 |
// list day names in between the start and end date
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
const days = this.getDaysArray(startDate, endDate);
|
| 66 |
|
| 67 |
return {
|
| 68 |
days: days.map(day => ({
|
| 69 |
day: day,
|
| 70 |
-
points:
|
| 71 |
})),
|
| 72 |
}
|
| 73 |
}
|
| 74 |
|
| 75 |
async getNutriHomeDailyGoals(userId: string): Promise<UserNutriHomeDailyGoalsSerialization> {
|
| 76 |
-
const
|
| 77 |
-
const
|
|
|
|
| 78 |
|
| 79 |
return {
|
| 80 |
...(await this.getDailyGoals(userId)),
|
|
|
|
| 3 |
import { UserHomeYourDailyIntakeSerialization } from "../responses/user-home-your-daily-intake.serialization";
|
| 4 |
import { UserHomeDailyGoalsSerialization } from "../responses/user-home-daily-goals.serialization";
|
| 5 |
import { UserNutriHomeDailyGoalsSerialization } from "../responses/user-nutri-home-daily-goals.serialization";
|
| 6 |
+
import { UserService } from "../../users/services/users.service";
|
| 7 |
+
import { Types } from "mongoose";
|
| 8 |
+
import { UserActivitiesService } from "../../activities/services/user-activities.service";
|
| 9 |
+
import { ActivityType } from "@common/enums/activity-type.enum";
|
| 10 |
+
import { ExerciseService } from "../../exercises/services/exercises.service";
|
| 11 |
+
import { FitnessGoal } from "@common/enums/fitness-goal.enum";
|
| 12 |
+
import { MealsService } from "../../meals/services/meals.service";
|
| 13 |
+
import { calcAge } from "@lib/utils/age";
|
| 14 |
|
| 15 |
export class UserHomeService {
|
| 16 |
+
private userService = new UserService();
|
| 17 |
+
private activitiesService = new UserActivitiesService();
|
| 18 |
+
private exercisesService = new ExerciseService();
|
| 19 |
+
private mealsService = new MealsService();
|
| 20 |
+
|
| 21 |
private getDaysArray(startDate: Date, endDate: Date): string[] {
|
| 22 |
const days = [];
|
| 23 |
for (let day = startDate; day <= endDate; day.setDate(day.getDate() + 1)) {
|
|
|
|
| 26 |
return days;
|
| 27 |
}
|
| 28 |
|
| 29 |
+
async getDailyGoals(userId: string): Promise<UserHomeDailyGoalsSerialization> {
|
| 30 |
+
const user = await this.userService.findOneOrFail({_id: new Types.ObjectId(userId)});
|
| 31 |
+
const todaysExerciseActivities = await this.activitiesService.model.find({
|
| 32 |
+
user_id: new Types.ObjectId(userId),
|
| 33 |
+
activity_type: ActivityType.EXERCISE,
|
| 34 |
+
$and: [
|
| 35 |
+
{ created_at: { $gte: new Date(new Date().setHours(0, 0, 0, 0)) } },
|
| 36 |
+
{ created_at: { $lte: new Date(new Date().setHours(23, 59, 59, 999)) } },
|
| 37 |
+
],
|
| 38 |
+
});
|
| 39 |
+
const todaysExercisesIds = todaysExerciseActivities.map(a => a.related_id);
|
| 40 |
+
|
| 41 |
+
const todaysExercises = await this.exercisesService.model.find({
|
| 42 |
+
_id: { $in: todaysExercisesIds },
|
| 43 |
+
});
|
| 44 |
+
|
| 45 |
+
const waterGoal = (user.weight || 1) * 2;
|
| 46 |
+
const waterConsumed = Math.round(waterGoal * 0.72);
|
| 47 |
|
| 48 |
+
const stepsGoal = (user.height || 1) * 100;
|
| 49 |
+
const stepsDone = Math.round(stepsGoal * 0.78);
|
| 50 |
|
| 51 |
+
const exercisesCals = todaysExercises.reduce((acc, curr) => acc + this.exercisesService.calculateCalories(curr), 0);
|
| 52 |
+
const exercisesHours = todaysExercises.reduce((acc, curr) => acc + curr.duration || curr.expectedDurationRange?.min || 0, 0) /60;
|
| 53 |
|
| 54 |
return {
|
| 55 |
waterGoal,
|
|
|
|
| 61 |
}
|
| 62 |
}
|
| 63 |
|
| 64 |
+
async getHomePageYourDailyIntake(userId: string): Promise<UserHomeYourDailyIntakeSerialization>{
|
| 65 |
+
const user = await this.userService.findOneOrFail({_id: new Types.ObjectId(userId)});
|
| 66 |
+
const goalToCalsMap = {}
|
| 67 |
+
const goalToCarbsMap = {}
|
| 68 |
+
const goalToProteinMap = {}
|
| 69 |
+
const goalToFatMap = {}
|
| 70 |
+
goalToCalsMap[FitnessGoal.GET_FITTER] = 2000;
|
| 71 |
+
goalToCalsMap[FitnessGoal.LOSE_WEIGHT] = 1500;
|
| 72 |
+
goalToCalsMap[FitnessGoal.GAIN_MUSCLE] = 2500;
|
| 73 |
+
goalToCarbsMap[FitnessGoal.GET_FITTER] = 50;
|
| 74 |
+
goalToCarbsMap[FitnessGoal.LOSE_WEIGHT] = 30;
|
| 75 |
+
goalToCarbsMap[FitnessGoal.GAIN_MUSCLE] = 60;
|
| 76 |
+
goalToProteinMap[FitnessGoal.GET_FITTER] = 30;
|
| 77 |
+
goalToProteinMap[FitnessGoal.LOSE_WEIGHT] = 40;
|
| 78 |
+
goalToProteinMap[FitnessGoal.GAIN_MUSCLE] = 50;
|
| 79 |
+
goalToFatMap[FitnessGoal.GET_FITTER] = 20;
|
| 80 |
+
goalToFatMap[FitnessGoal.LOSE_WEIGHT] = 30;
|
| 81 |
+
goalToFatMap[FitnessGoal.GAIN_MUSCLE] = 20;
|
| 82 |
+
|
| 83 |
+
const caloriesGoal = goalToCalsMap[user.preferences?.fitness_goal] || 2000;
|
| 84 |
+
|
| 85 |
+
const todaysExerciseActivities = await this.activitiesService.model.find({
|
| 86 |
+
user_id: new Types.ObjectId(userId),
|
| 87 |
+
activity_type: ActivityType.EXERCISE,
|
| 88 |
+
$and: [
|
| 89 |
+
{ created_at: { $gte: new Date(new Date().setHours(0, 0, 0, 0)) } },
|
| 90 |
+
{ created_at: { $lte: new Date(new Date().setHours(23, 59, 59, 999)) } },
|
| 91 |
+
],
|
| 92 |
+
});
|
| 93 |
+
const todaysExercisesIds = todaysExerciseActivities.map(a => a.related_id);
|
| 94 |
+
|
| 95 |
+
const todaysExercises = await this.exercisesService.model.find({
|
| 96 |
+
_id: { $in: todaysExercisesIds },
|
| 97 |
+
});
|
| 98 |
+
|
| 99 |
+
const todaysMealsActivities = await this.activitiesService.model.find({
|
| 100 |
+
user_id: new Types.ObjectId(userId),
|
| 101 |
+
activity_type: ActivityType.MEAL,
|
| 102 |
+
$and: [
|
| 103 |
+
{ created_at: { $gte: new Date(new Date().setHours(0, 0, 0, 0)) } },
|
| 104 |
+
{ created_at: { $lte: new Date(new Date().setHours(23, 59, 59, 999)) } },
|
| 105 |
+
],
|
| 106 |
+
});
|
| 107 |
+
const todaysMealsIds = todaysMealsActivities.map(a => a.related_id);
|
| 108 |
+
|
| 109 |
+
const todaysMeals = await this.mealsService.model.find({
|
| 110 |
+
_id: { $in: todaysMealsIds },
|
| 111 |
+
});
|
| 112 |
+
|
| 113 |
+
const caloriesBurned = todaysExercises.reduce((acc, curr) => acc + this.exercisesService.calculateCalories(curr), 0);
|
| 114 |
+
const caloriesLeft = caloriesGoal - caloriesBurned;
|
| 115 |
|
| 116 |
+
const carbsGoal = goalToCarbsMap[user.preferences?.fitness_goal] || 50;
|
| 117 |
+
const carbsConsumed = todaysMeals.reduce((acc, curr) => acc + curr.carbs, 0);
|
| 118 |
|
| 119 |
+
const proteinGoal = goalToProteinMap[user.preferences?.fitness_goal] || 30;
|
| 120 |
+
const proteinConsumed = todaysMeals.reduce((acc, curr) => acc + curr.proteins, 0);
|
| 121 |
|
| 122 |
+
const fatGoal = goalToFatMap[user.preferences?.fitness_goal] || 20;
|
| 123 |
+
const fatConsumed = todaysMeals.reduce((acc, curr) => acc + curr.fats, 0);
|
| 124 |
|
| 125 |
return {
|
| 126 |
caloriesGoal,
|
|
|
|
| 135 |
}
|
| 136 |
}
|
| 137 |
|
| 138 |
+
async getHomePageStreak(userId: string, startDate: Date, endDate: Date): Promise<HomeStreakSerialization> {
|
| 139 |
// list day names in between the start and end date
|
| 140 |
+
//
|
| 141 |
+
const activities = await this.activitiesService.model.find({
|
| 142 |
+
user_id: new Types.ObjectId(userId),
|
| 143 |
+
$and: [
|
| 144 |
+
{ created_at: { $gte: startDate } },
|
| 145 |
+
{ created_at: { $lte: endDate } },
|
| 146 |
+
],
|
| 147 |
+
});
|
| 148 |
const days = this.getDaysArray(startDate, endDate);
|
| 149 |
|
| 150 |
return {
|
| 151 |
days: days.map(day => ({
|
| 152 |
day: day,
|
| 153 |
+
points: activities.filter(a => a.created_at.toLocaleString('en-US', { weekday: 'long' }).toLowerCase() === day).length,
|
| 154 |
})),
|
| 155 |
}
|
| 156 |
}
|
| 157 |
|
| 158 |
async getNutriHomeDailyGoals(userId: string): Promise<UserNutriHomeDailyGoalsSerialization> {
|
| 159 |
+
const user = await this.userService.findOneOrFail({_id: new Types.ObjectId(userId)});
|
| 160 |
+
const sleepGoal = calcAge(user.dob) < 18 ? 8 : 7;
|
| 161 |
+
const sleepDone = Math.round(sleepGoal * 0.8);
|
| 162 |
|
| 163 |
return {
|
| 164 |
...(await this.getDailyGoals(userId)),
|
src/modules/users/modules/meals/events/meal-done.event.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { ActivityType } from "@common/enums/activity-type.enum";
|
| 2 |
+
import { Activity, IActivity } from "@common/models/activity.model";
|
| 3 |
+
import { EventsManager } from "@lib/events/events-manager";
|
| 4 |
+
import { Types } from "mongoose";
|
| 5 |
+
|
| 6 |
+
export class MealDoneEvent {
|
| 7 |
+
constructor(public userId: string, public mealId: string){}
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
EventsManager.on(MealDoneEvent.name, async (event: MealDoneEvent) => {
|
| 11 |
+
console.log(`Meal done event for user ${event.userId}`);
|
| 12 |
+
|
| 13 |
+
Activity.create({
|
| 14 |
+
user_id: new Types.ObjectId(event.userId),
|
| 15 |
+
related_id: new Types.ObjectId(event.mealId),
|
| 16 |
+
activity_type: ActivityType.MEAL,
|
| 17 |
+
} satisfies IActivity).catch(console.error);
|
| 18 |
+
});
|
src/modules/users/modules/meals/events/meals-done.event.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { ActivityType } from "@common/enums/activity-type.enum";
|
| 2 |
+
import { Activity, IActivity } from "@common/models/activity.model";
|
| 3 |
+
import { EventsManager } from "@lib/events/events-manager";
|
| 4 |
+
import { Types } from "mongoose";
|
| 5 |
+
|
| 6 |
+
export class MealsDoneEvent {
|
| 7 |
+
constructor(public userId: string, public mealsIds: string[]){}
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
EventsManager.on(MealsDoneEvent.name, async (event: MealsDoneEvent) => {
|
| 11 |
+
console.log(`Meal done event for user ${event.userId}`);
|
| 12 |
+
|
| 13 |
+
await Promise.all(
|
| 14 |
+
event.mealsIds.map(mId => {
|
| 15 |
+
Activity.create({
|
| 16 |
+
user_id: new Types.ObjectId(event.userId),
|
| 17 |
+
related_id: new Types.ObjectId(mId),
|
| 18 |
+
activity_type: ActivityType.MEAL,
|
| 19 |
+
} satisfies IActivity).catch(console.error);
|
| 20 |
+
})
|
| 21 |
+
)
|
| 22 |
+
});
|
src/modules/users/modules/meals/services/meals.service.ts
CHANGED
|
@@ -4,14 +4,14 @@ import { IEatCustomMeal } from "../validations/eat-custom-meal.validation";
|
|
| 4 |
import { EventsManager } from "@lib/events/events-manager";
|
| 5 |
import { Ingredient } from "@common/models/ingredient.model";
|
| 6 |
import { HttpError } from "@lib/error-handling/http-error";
|
| 7 |
-
import { ActivityType } from "@common/enums/activity-type.enum";
|
| 8 |
-
import { EatCustomMealEvent } from "../events/eat-custom-meal.event";
|
| 9 |
import { Types } from "mongoose";
|
|
|
|
|
|
|
| 10 |
|
| 11 |
export class MealsService extends CrudService(Meal) {
|
| 12 |
-
async eatCustomMeal(
|
| 13 |
// Validate ingredients
|
| 14 |
-
await Promise.all(
|
| 15 |
body.ingredients.map(async i => {
|
| 16 |
const ing = await Ingredient.findOne({ _id: new Types.ObjectId(i.id) });
|
| 17 |
if(!ing) throw new HttpError(404, `Ingredient with id ${i} not found`);
|
|
@@ -20,8 +20,21 @@ export class MealsService extends CrudService(Meal) {
|
|
| 20 |
noServings: i.noServings,
|
| 21 |
};
|
| 22 |
})
|
| 23 |
-
)
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
}
|
| 27 |
}
|
|
|
|
| 4 |
import { EventsManager } from "@lib/events/events-manager";
|
| 5 |
import { Ingredient } from "@common/models/ingredient.model";
|
| 6 |
import { HttpError } from "@lib/error-handling/http-error";
|
|
|
|
|
|
|
| 7 |
import { Types } from "mongoose";
|
| 8 |
+
import { MealDoneEvent } from "../events/meal-done.event";
|
| 9 |
+
import { MealType } from "@common/enums/meal-type.enum";
|
| 10 |
|
| 11 |
export class MealsService extends CrudService(Meal) {
|
| 12 |
+
async eatCustomMeal(userId: string, body: IEatCustomMeal) {
|
| 13 |
// Validate ingredients
|
| 14 |
+
const customMealIngs = await Promise.all(
|
| 15 |
body.ingredients.map(async i => {
|
| 16 |
const ing = await Ingredient.findOne({ _id: new Types.ObjectId(i.id) });
|
| 17 |
if(!ing) throw new HttpError(404, `Ingredient with id ${i} not found`);
|
|
|
|
| 20 |
noServings: i.noServings,
|
| 21 |
};
|
| 22 |
})
|
| 23 |
+
);
|
| 24 |
|
| 25 |
+
// Create meal
|
| 26 |
+
const meal = await this.create({
|
| 27 |
+
name: `${(new Date()).toISOString()} - Custom Meal - ${userId}`,
|
| 28 |
+
image: "https://via.placeholder.com/150",
|
| 29 |
+
type: MealType.CUSTOM,
|
| 30 |
+
ingredients: customMealIngs.map(i => i.ingredient._id),
|
| 31 |
+
calories: customMealIngs.reduce((acc, curr) => acc + curr.ingredient.calories * curr.noServings, 0),
|
| 32 |
+
proteins: customMealIngs.reduce((acc, curr) => acc + curr.ingredient.proteins * curr.noServings, 0),
|
| 33 |
+
carbs: customMealIngs.reduce((acc, curr) => acc + curr.ingredient.carbs * curr.noServings, 0),
|
| 34 |
+
fats: customMealIngs.reduce((acc, curr) => acc + curr.ingredient.fats * curr.noServings, 0),
|
| 35 |
+
isDeleted: true,
|
| 36 |
+
});
|
| 37 |
+
|
| 38 |
+
EventsManager.emit(MealDoneEvent.name, new MealDoneEvent(userId, meal._id.toString()));
|
| 39 |
}
|
| 40 |
}
|
src/modules/users/modules/user-registered-meal-plans/services/meal-plans-progress.service.ts
CHANGED
|
@@ -2,6 +2,8 @@ import { UserRegisteredMealPlan } from "@common/models/user-registered-meal-plan
|
|
| 2 |
import { CrudService } from "@lib/services/crud.service";
|
| 3 |
import { HttpError } from "@lib/error-handling/http-error";
|
| 4 |
import { MealPlansService } from "../../meal-plans/services/meal-plans.service";
|
|
|
|
|
|
|
| 5 |
|
| 6 |
export class MealPlansProgressService extends CrudService(UserRegisteredMealPlan) {
|
| 7 |
private mealPlansService = new MealPlansService()
|
|
@@ -33,6 +35,8 @@ export class MealPlansProgressService extends CrudService(UserRegisteredMealPlan
|
|
| 33 |
this.mealPlansService.createModelMealPlan(userId)
|
| 34 |
}
|
| 35 |
|
|
|
|
|
|
|
| 36 |
return updatedMealPlan;
|
| 37 |
}
|
| 38 |
}
|
|
|
|
| 2 |
import { CrudService } from "@lib/services/crud.service";
|
| 3 |
import { HttpError } from "@lib/error-handling/http-error";
|
| 4 |
import { MealPlansService } from "../../meal-plans/services/meal-plans.service";
|
| 5 |
+
import { EventsManager } from "@lib/events/events-manager";
|
| 6 |
+
import { MealsDoneEvent } from "../../meals/events/meals-done.event";
|
| 7 |
|
| 8 |
export class MealPlansProgressService extends CrudService(UserRegisteredMealPlan) {
|
| 9 |
private mealPlansService = new MealPlansService()
|
|
|
|
| 35 |
this.mealPlansService.createModelMealPlan(userId)
|
| 36 |
}
|
| 37 |
|
| 38 |
+
EventsManager.emit(MealsDoneEvent.name, new MealsDoneEvent(userId, day.meals.map(e => e.toString())));
|
| 39 |
+
|
| 40 |
return updatedMealPlan;
|
| 41 |
}
|
| 42 |
}
|
src/modules/users/modules/user-registered-workouts/services/workouts-progress.service.ts
CHANGED
|
@@ -3,6 +3,8 @@ import { CrudService } from "@lib/services/crud.service";
|
|
| 3 |
import { IUpdateUserRegisteredWorkouts } from "../validations/update-user-registered-workouts.validation";
|
| 4 |
import { HttpError } from "@lib/error-handling/http-error";
|
| 5 |
import { WorkoutService } from "../../workouts/services/workouts.service";
|
|
|
|
|
|
|
| 6 |
|
| 7 |
export class WorkoutsProgressService extends CrudService(UserRegisteredWorkout, {
|
| 8 |
defaultFilter: {
|
|
@@ -38,6 +40,8 @@ export class WorkoutsProgressService extends CrudService(UserRegisteredWorkout,
|
|
| 38 |
if(weekIndex === workout.weeks.length - 1) {
|
| 39 |
await this.workoutsService.createModelWorkout(userId)
|
| 40 |
}
|
|
|
|
|
|
|
| 41 |
|
| 42 |
// save changes
|
| 43 |
workout.markModified('weeks');
|
|
|
|
| 3 |
import { IUpdateUserRegisteredWorkouts } from "../validations/update-user-registered-workouts.validation";
|
| 4 |
import { HttpError } from "@lib/error-handling/http-error";
|
| 5 |
import { WorkoutService } from "../../workouts/services/workouts.service";
|
| 6 |
+
import { EventsManager } from "@lib/events/events-manager";
|
| 7 |
+
import { ExercisesDoneEvent } from "../../exercises/events/exercises-done.event";
|
| 8 |
|
| 9 |
export class WorkoutsProgressService extends CrudService(UserRegisteredWorkout, {
|
| 10 |
defaultFilter: {
|
|
|
|
| 40 |
if(weekIndex === workout.weeks.length - 1) {
|
| 41 |
await this.workoutsService.createModelWorkout(userId)
|
| 42 |
}
|
| 43 |
+
|
| 44 |
+
EventsManager.emit(ExercisesDoneEvent.name, new ExercisesDoneEvent(userId, day.exercises.map(e => e.toString())));
|
| 45 |
|
| 46 |
// save changes
|
| 47 |
workout.markModified('weeks');
|