Spaces:
Running
Running
Commit
·
6d6248c
1
Parent(s):
15e7f40
feat: home daily goals stats
Browse files
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/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,17 @@ 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 +22,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,
|
|
|
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 |
|
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 = [];
|
19 |
for (let day = startDate; day <= endDate; day.setDate(day.getDate() + 1)) {
|
|
|
22 |
return days;
|
23 |
}
|
24 |
|
25 |
+
async getDailyGoals(userId: string): Promise<UserHomeDailyGoalsSerialization> {
|
26 |
+
const user = await this.userService.findOneOrFail({_id: new Types.ObjectId(userId)});
|
27 |
+
const todaysExerciseActivities = await this.activitiesService.model.find({
|
28 |
+
user_id: new Types.ObjectId(userId),
|
29 |
+
activity_type: ActivityType.EXERCISE,
|
30 |
+
$and: [
|
31 |
+
{ created_at: { $gte: new Date(new Date().setHours(0, 0, 0, 0)) } },
|
32 |
+
{ created_at: { $lte: new Date(new Date().setHours(23, 59, 59, 999)) } },
|
33 |
+
],
|
34 |
+
});
|
35 |
+
const todaysExercisesIds = todaysExerciseActivities.map(a => a.related_id);
|
36 |
+
|
37 |
+
const todaysExercises = await this.exercisesService.model.find({
|
38 |
+
_id: { $in: todaysExercisesIds },
|
39 |
+
});
|
40 |
+
|
41 |
+
const waterGoal = (user.weight || 1) * 2;
|
42 |
+
const waterConsumed = Math.round(waterGoal * 0.72);
|
43 |
|
44 |
+
const stepsGoal = (user.height || 1) * 100;
|
45 |
+
const stepsDone = Math.round(stepsGoal * 0.78);
|
46 |
|
47 |
+
const exercisesCals = todaysExercises.reduce((acc, curr) => acc + this.exercisesService.calculateCalories(curr), 0);
|
48 |
+
const exercisesHours = todaysExercises.reduce((acc, curr) => acc + curr.duration || curr.expectedDurationRange?.min || 0, 0) /60;
|
49 |
|
50 |
return {
|
51 |
waterGoal,
|