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

feat: rest of stats

Browse files
src/modules/users/modules/home/services/user-home.service.ts CHANGED
@@ -8,11 +8,15 @@ 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
 
12
  export class UserHomeService {
13
  private userService = new UserService();
14
  private activitiesService = new UserActivitiesService();
15
  private exercisesService = new ExerciseService();
 
16
 
17
  private getDaysArray(startDate: Date, endDate: Date): string[] {
18
  const days = [];
@@ -57,19 +61,66 @@ export class UserHomeService {
57
  }
58
  }
59
 
60
- async getHomePageYourDailyIntake(_userId: string): Promise<UserHomeYourDailyIntakeSerialization>{
61
- const caloriesGoal = faker.number.int({ min: 29, max: 100 });
62
- const caloriesLeft = faker.number.int({ min: 0, max: caloriesGoal});
63
- const caloriesBurned = caloriesGoal - caloriesLeft;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
 
65
- const carbsGoal = faker.number.int({ min: 29, max: 100 });
66
- const carbsConsumed = faker.number.int({ min: 0, max: carbsGoal });
 
 
 
 
 
 
 
 
 
 
 
67
 
68
- const proteinGoal = faker.number.int({ min: 29, max: 100 });
69
- const proteinConsumed = faker.number.int({ min: 0, max: proteinGoal });
70
 
71
- const fatGoal = faker.number.int({ min: 29, max: 100 });
72
- const fatConsumed = faker.number.int({ min: 0, max: fatGoal });
 
 
 
 
 
 
73
 
74
  return {
75
  caloriesGoal,
@@ -84,21 +135,30 @@ export class UserHomeService {
84
  }
85
  }
86
 
87
- async getHomePageStreak(_userId: string, startDate: Date, endDate: Date): Promise<HomeStreakSerialization> {
88
  // list day names in between the start and end date
 
 
 
 
 
 
 
 
89
  const days = this.getDaysArray(startDate, endDate);
90
 
91
  return {
92
  days: days.map(day => ({
93
  day: day,
94
- points: faker.number.int({ min: 0, max: 100 }),
95
  })),
96
  }
97
  }
98
 
99
  async getNutriHomeDailyGoals(userId: string): Promise<UserNutriHomeDailyGoalsSerialization> {
100
- const sleepGoal = faker.number.int({ min:29, max: 100 })
101
- const sleepDone = faker.number.int({ min:0, max: sleepGoal })
 
102
 
103
  return {
104
  ...(await this.getDailyGoals(userId)),
 
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 = [];
 
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)),