Spaces:
Sleeping
Sleeping
Hozifa Elgharbawy
commited on
Commit
·
ebc986f
1
Parent(s):
169a0ee
update
Browse files
src/modules/users/modules/home/controllers/home.controller.ts
CHANGED
@@ -21,7 +21,9 @@ import { UserHomeService } from "../services/user-home.service";
|
|
21 |
import { IUserRequest } from "@common/interfaces/user-request.interface";
|
22 |
import { UserHomeYourDailyIntakeSerialization } from "../responses/user-home-your-daily-intake.serialization";
|
23 |
import { UserHomeDailyGoalsSerialization } from "../responses/user-home-daily-goals.serialization";
|
24 |
-
|
|
|
|
|
25 |
|
26 |
@Controller("/user/homePage")
|
27 |
@ControllerMiddleware(UsersGuardMiddleware())
|
@@ -29,6 +31,7 @@ export class homePageController extends BaseController {
|
|
29 |
private userRegisteredWorkoutsService = new UserRegisteredWorkoutsService();
|
30 |
private userService = new UserService();
|
31 |
private userHomeService = new UserHomeService();
|
|
|
32 |
|
33 |
setRoutes(): void {
|
34 |
this.router.get("/", asyncHandler(this.getHomePage));
|
@@ -101,7 +104,7 @@ export class homePageController extends BaseController {
|
|
101 |
res
|
102 |
);
|
103 |
}
|
104 |
-
|
105 |
|
106 |
@SwaggerGet()
|
107 |
@SwaggerResponse(HomeSerialization)
|
@@ -122,12 +125,50 @@ export class homePageController extends BaseController {
|
|
122 |
selectArray: ["-weeks.week_description", "-weeks.week_name", "-weeks.days.exercises", "-user"],
|
123 |
},
|
124 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
const data = {
|
126 |
user: user,
|
127 |
myWorkout: myWorkout,
|
128 |
-
myMealPlan: {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
}
|
130 |
|
|
|
|
|
131 |
return JsonResponse.success(
|
132 |
{
|
133 |
data: serialize(data, HomeSerialization)
|
|
|
21 |
import { IUserRequest } from "@common/interfaces/user-request.interface";
|
22 |
import { UserHomeYourDailyIntakeSerialization } from "../responses/user-home-your-daily-intake.serialization";
|
23 |
import { UserHomeDailyGoalsSerialization } from "../responses/user-home-daily-goals.serialization";
|
24 |
+
import { GetMyMealPlanSerialization } from "@common/serializers/user-registered-meal-planPopulate.serialization";
|
25 |
+
import { UserRegisteredMealPlansService } from "../../user-registered-meal-plans/services/user-registered-meal-plans.service";
|
26 |
+
import { number } from "joi";
|
27 |
|
28 |
@Controller("/user/homePage")
|
29 |
@ControllerMiddleware(UsersGuardMiddleware())
|
|
|
31 |
private userRegisteredWorkoutsService = new UserRegisteredWorkoutsService();
|
32 |
private userService = new UserService();
|
33 |
private userHomeService = new UserHomeService();
|
34 |
+
private userRegisteredMealPlansService = new UserRegisteredMealPlansService();
|
35 |
|
36 |
setRoutes(): void {
|
37 |
this.router.get("/", asyncHandler(this.getHomePage));
|
|
|
104 |
res
|
105 |
);
|
106 |
}
|
107 |
+
|
108 |
|
109 |
@SwaggerGet()
|
110 |
@SwaggerResponse(HomeSerialization)
|
|
|
125 |
selectArray: ["-weeks.week_description", "-weeks.week_name", "-weeks.days.exercises", "-user"],
|
126 |
},
|
127 |
)
|
128 |
+
|
129 |
+
let myMealPlan = await this.userRegisteredMealPlansService.findOneOrFail(
|
130 |
+
{
|
131 |
+
user: req.jwtPayload.id, isActive: true
|
132 |
+
},
|
133 |
+
{
|
134 |
+
populateArray: [
|
135 |
+
{ path: "meal_plan", select: "-days" },
|
136 |
+
{
|
137 |
+
path: "days.meals",
|
138 |
+
populate: { path: "ingredients" }
|
139 |
+
}
|
140 |
+
],
|
141 |
+
}
|
142 |
+
);
|
143 |
+
|
144 |
+
|
145 |
+
const dayToEat = myMealPlan.days.find(day => day.is_eaten === false);
|
146 |
+
const totalCalories = dayToEat.meals.reduce((sum, meal: any) => sum + meal.calories, 0);
|
147 |
+
|
148 |
+
const totals = dayToEat.meals.reduce((totals, meal: any) => {
|
149 |
+
if (meal.type === "breakfast" || meal.type === "lunch" || meal.type === "dinner") {
|
150 |
+
totals.meals += 1;
|
151 |
+
} else if (meal.type === "snacks") {
|
152 |
+
totals.snacks += 1;
|
153 |
+
}
|
154 |
+
return totals;
|
155 |
+
}, { meals: 0, snacks: 0 });
|
156 |
+
|
157 |
const data = {
|
158 |
user: user,
|
159 |
myWorkout: myWorkout,
|
160 |
+
myMealPlan: {
|
161 |
+
id: myMealPlan._id,
|
162 |
+
today: {
|
163 |
+
numberOfMeals: totals.meals,
|
164 |
+
numberOfSnacks: totals.snacks,
|
165 |
+
totalCalories: totalCalories,
|
166 |
+
},
|
167 |
+
}
|
168 |
}
|
169 |
|
170 |
+
|
171 |
+
|
172 |
return JsonResponse.success(
|
173 |
{
|
174 |
data: serialize(data, HomeSerialization)
|
src/modules/users/modules/home/responses/home.serialization.ts
CHANGED
@@ -117,6 +117,31 @@ class UserHome {
|
|
117 |
injuries: any;
|
118 |
}
|
119 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
export class HomeSerialization {
|
121 |
@Expose()
|
122 |
@SwaggerResponseProperty({ type: UserHome })
|
@@ -129,6 +154,9 @@ export class HomeSerialization {
|
|
129 |
myWorkout: any;
|
130 |
|
131 |
@Expose({ name: "myMealPlan" })
|
132 |
-
@SwaggerResponseProperty({ type:
|
|
|
133 |
myMealPlan: any;
|
134 |
}
|
|
|
|
|
|
117 |
injuries: any;
|
118 |
}
|
119 |
|
120 |
+
class TodayMealPlanHome {
|
121 |
+
@Expose()
|
122 |
+
@SwaggerResponseProperty({ type: "number" })
|
123 |
+
numberOfMeals: number;
|
124 |
+
|
125 |
+
@Expose()
|
126 |
+
@SwaggerResponseProperty({ type: "number" })
|
127 |
+
numberOfSnacks: number;
|
128 |
+
|
129 |
+
@Expose()
|
130 |
+
@SwaggerResponseProperty({ type: "number" })
|
131 |
+
totalCalories: number;
|
132 |
+
}
|
133 |
+
|
134 |
+
class MyMealPlanHome {
|
135 |
+
@Expose({ name: "id" })
|
136 |
+
@SwaggerResponseProperty({ type: "string" })
|
137 |
+
id: string;
|
138 |
+
|
139 |
+
@Expose()
|
140 |
+
@SwaggerResponseProperty({ type: TodayMealPlanHome })
|
141 |
+
@Transform(({ value }) => serialize(value, TodayMealPlanHome))
|
142 |
+
today: any;
|
143 |
+
}
|
144 |
+
|
145 |
export class HomeSerialization {
|
146 |
@Expose()
|
147 |
@SwaggerResponseProperty({ type: UserHome })
|
|
|
154 |
myWorkout: any;
|
155 |
|
156 |
@Expose({ name: "myMealPlan" })
|
157 |
+
@SwaggerResponseProperty({ type: MyMealPlanHome })
|
158 |
+
@Transform(({ value }) => serialize(value, MyMealPlanHome))
|
159 |
myMealPlan: any;
|
160 |
}
|
161 |
+
|
162 |
+
|