moahmedwafy commited on
Commit
e5ce633
·
1 Parent(s): b3c9b55

feat: store activities

Browse files
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/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 { IEatCustomMeal } from "../validations/eat-custom-meal.validation";
5
 
6
- export class EatCustomMealEvent {
7
- constructor(public userId: string, public ingredients: IEatCustomMeal['ingredients']) {}
8
  }
9
 
10
- EventsManager.on(ActivityType.EAT_CUSTOM_MEAL, (event: EatCustomMealEvent) => {
11
- console.log(`User with id ${event.userId} ate a custom meal with ingredients: ${event.ingredients.map(i => i.id).join(", ")}`);
12
 
13
  Activity.create({
14
- user_id: event.userId,
15
- activity_type: ActivityType.EAT_CUSTOM_MEAL,
16
- related_item: { ingredients: event.ingredients },
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/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(id: string, body: IEatCustomMeal) {
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
- EventsManager.emit(ActivityType.EAT_CUSTOM_MEAL, new EatCustomMealEvent(id, body.ingredients));
 
 
 
 
 
 
 
 
 
 
 
 
 
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');