Spaces:
Running
Running
Merge pull request #96 from Modarb-Ai-Trainer/models
Browse files- src/modules/users/modules/home/controllers/home-nutriguide.controller.ts +3 -3
- src/modules/users/modules/meal-plans/services/meal-plans.service.ts +8 -1
- src/modules/users/modules/user-registered-meal-plans/controller/user-registered-meal-plans.controller.ts +4 -6
- src/modules/users/modules/user-registered-meal-plans/services/meal-plans-progress.service.ts +38 -0
- src/modules/users/modules/user-registered-meal-plans/services/user-registered-meal-plans.service.ts +1 -33
- src/modules/users/modules/user-registered-workouts/controllers/user-registered-workouts.controller.ts +3 -1
- src/modules/users/modules/user-registered-workouts/services/user-registered-workouts.service.ts +0 -30
- src/modules/users/modules/user-registered-workouts/services/workouts-progress.service.ts +46 -0
- src/modules/users/modules/workouts/services/workouts.service.ts +8 -1
src/modules/users/modules/home/controllers/home-nutriguide.controller.ts
CHANGED
@@ -10,14 +10,13 @@ import { SwaggerGet, SwaggerPatch } from "@lib/decorators/swagger-routes.decorat
|
|
10 |
import { SwaggerSummary } from "@lib/decorators/swagger-summary.decorator";
|
11 |
import { SwaggerDescription } from "@lib/decorators/swagger-description.decorator";
|
12 |
import { SwaggerResponse } from "@lib/decorators/swagger-response.decorator";
|
13 |
-
import { SwaggerQuery } from "@lib/decorators/swagger-query.decorator";
|
14 |
import { UserHomeService } from "../services/user-home.service";
|
15 |
import { IUserRequest } from "@common/interfaces/user-request.interface";
|
16 |
import { UserHomeYourDailyIntakeSerialization } from "../responses/user-home-your-daily-intake.serialization";
|
17 |
import { UserNutriHomeDailyGoalsSerialization } from "../responses/user-nutri-home-daily-goals.serialization";
|
18 |
import { GetMyMealPlanSerialization } from "@common/serializers/user-registered-meal-planPopulate.serialization";
|
19 |
import { UserRegisteredMealPlansService } from "../../user-registered-meal-plans/services/user-registered-meal-plans.service";
|
20 |
-
import {
|
21 |
|
22 |
|
23 |
@Controller("/user/nutri-guide")
|
@@ -25,6 +24,7 @@ import { SwaggerRequest } from "@lib/decorators/swagger-request.decorator";
|
|
25 |
export class homeNutriGuideController extends BaseController {
|
26 |
private userHomeService = new UserHomeService();
|
27 |
private userRegisteredMealPlansService = new UserRegisteredMealPlansService();
|
|
|
28 |
|
29 |
setRoutes(): void {
|
30 |
this.router.get("/todays-intake", asyncHandler(this.getHomePageYourDailyIntake));
|
@@ -88,7 +88,7 @@ export class homeNutriGuideController extends BaseController {
|
|
88 |
updateProgress = async (req: IUserRequest, res: Response) => {
|
89 |
const urwId: string = req.params.id;
|
90 |
const dayNumber: number = Number(req.params.day);
|
91 |
-
await this.
|
92 |
urwId, dayNumber
|
93 |
}, req.body, req.jwtPayload.id);
|
94 |
return JsonResponse.success(
|
|
|
10 |
import { SwaggerSummary } from "@lib/decorators/swagger-summary.decorator";
|
11 |
import { SwaggerDescription } from "@lib/decorators/swagger-description.decorator";
|
12 |
import { SwaggerResponse } from "@lib/decorators/swagger-response.decorator";
|
|
|
13 |
import { UserHomeService } from "../services/user-home.service";
|
14 |
import { IUserRequest } from "@common/interfaces/user-request.interface";
|
15 |
import { UserHomeYourDailyIntakeSerialization } from "../responses/user-home-your-daily-intake.serialization";
|
16 |
import { UserNutriHomeDailyGoalsSerialization } from "../responses/user-nutri-home-daily-goals.serialization";
|
17 |
import { GetMyMealPlanSerialization } from "@common/serializers/user-registered-meal-planPopulate.serialization";
|
18 |
import { UserRegisteredMealPlansService } from "../../user-registered-meal-plans/services/user-registered-meal-plans.service";
|
19 |
+
import { MealPlansProgressService } from "../../user-registered-meal-plans/services/meal-plans-progress.service";
|
20 |
|
21 |
|
22 |
@Controller("/user/nutri-guide")
|
|
|
24 |
export class homeNutriGuideController extends BaseController {
|
25 |
private userHomeService = new UserHomeService();
|
26 |
private userRegisteredMealPlansService = new UserRegisteredMealPlansService();
|
27 |
+
private mealPlansProgressService = new MealPlansProgressService();
|
28 |
|
29 |
setRoutes(): void {
|
30 |
this.router.get("/todays-intake", asyncHandler(this.getHomePageYourDailyIntake));
|
|
|
88 |
updateProgress = async (req: IUserRequest, res: Response) => {
|
89 |
const urwId: string = req.params.id;
|
90 |
const dayNumber: number = Number(req.params.day);
|
91 |
+
await this.mealPlansProgressService.updateForUser({
|
92 |
urwId, dayNumber
|
93 |
}, req.body, req.jwtPayload.id);
|
94 |
return JsonResponse.success(
|
src/modules/users/modules/meal-plans/services/meal-plans.service.ts
CHANGED
@@ -5,12 +5,19 @@ import { calcAge } from "@lib/utils/age";
|
|
5 |
import { MealsService } from "../../meals/services/meals.service";
|
6 |
import { UserRegisteredMealPlansService } from "../../user-registered-meal-plans/services/user-registered-meal-plans.service";
|
7 |
import { UserDocument } from "@common/models/user.model";
|
|
|
|
|
8 |
|
9 |
export class MealPlansService extends CrudService(MealPlan) {
|
10 |
private mealsService = new MealsService();
|
11 |
private userRegisteredMealPlansService = new UserRegisteredMealPlansService();
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
|
13 |
-
public async createModelMealPlan(user: UserDocument) {
|
14 |
let caloriesPerDay = 0;
|
15 |
if (user.gender === "male") {
|
16 |
caloriesPerDay = 10 * user.weight + 6.25 * user.height - 5 * calcAge(user.dob) + 5;
|
|
|
5 |
import { MealsService } from "../../meals/services/meals.service";
|
6 |
import { UserRegisteredMealPlansService } from "../../user-registered-meal-plans/services/user-registered-meal-plans.service";
|
7 |
import { UserDocument } from "@common/models/user.model";
|
8 |
+
import { UsersService } from "modules/console/modules/users/services/users.service";
|
9 |
+
import { Types } from "mongoose";
|
10 |
|
11 |
export class MealPlansService extends CrudService(MealPlan) {
|
12 |
private mealsService = new MealsService();
|
13 |
private userRegisteredMealPlansService = new UserRegisteredMealPlansService();
|
14 |
+
private usersService = new UsersService();
|
15 |
+
|
16 |
+
public async createModelMealPlan(userOrId: UserDocument | string) {
|
17 |
+
const user: UserDocument = typeof userOrId === 'string' ?
|
18 |
+
await this.usersService.findOneOrFail({_id: new Types.ObjectId(userOrId)}) :
|
19 |
+
userOrId;
|
20 |
|
|
|
21 |
let caloriesPerDay = 0;
|
22 |
if (user.gender === "male") {
|
23 |
caloriesPerDay = 10 * user.weight + 6.25 * user.height - 5 * calcAge(user.dob) + 5;
|
src/modules/users/modules/user-registered-meal-plans/controller/user-registered-meal-plans.controller.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import { UserRegisteredMealPlansService } from "../services/user-registered-meal-plans.service";
|
2 |
-
import {
|
3 |
import { JsonResponse } from "@lib/responses/json-response";
|
4 |
import { asyncHandler } from "@helpers/async-handler";
|
5 |
import { BaseController } from "@lib/controllers/controller.base";
|
@@ -14,9 +14,7 @@ import { SwaggerRequest } from "@lib/decorators/swagger-request.decorator";
|
|
14 |
import { SwaggerResponse } from "@lib/decorators/swagger-response.decorator";
|
15 |
import { SwaggerSummary } from "@lib/decorators/swagger-summary.decorator";
|
16 |
import { SwaggerDescription } from "@lib/decorators/swagger-description.decorator";
|
17 |
-
|
18 |
-
jwtPayload?: any;
|
19 |
-
}
|
20 |
|
21 |
@Controller("/user/myMealPlan")
|
22 |
@ControllerMiddleware(UsersGuardMiddleware())
|
@@ -35,7 +33,7 @@ export class UsersRegisteredMealPlansController extends BaseController {
|
|
35 |
@SwaggerResponse(GetMyMealPlanSerialization)
|
36 |
@SwaggerSummary("get my meal plan")
|
37 |
@SwaggerDescription("Get the meal plan that the user is currently using")
|
38 |
-
get = async (req:
|
39 |
const data = await this.userRegisteredMealPlansService.findOneOrFail(
|
40 |
{ user: req.jwtPayload.id, isActive: true },
|
41 |
{
|
@@ -59,7 +57,7 @@ export class UsersRegisteredMealPlansController extends BaseController {
|
|
59 |
@SwaggerRequest({ meal_plan: "string" })
|
60 |
@SwaggerSummary("create my meal plan")
|
61 |
@SwaggerDescription("Create a new meal plan for the user")
|
62 |
-
create = async (req:
|
63 |
const data = await this.userRegisteredMealPlansService.createForUser(req.body, req.jwtPayload.id);
|
64 |
return JsonResponse.success(
|
65 |
{
|
|
|
1 |
import { UserRegisteredMealPlansService } from "../services/user-registered-meal-plans.service";
|
2 |
+
import { Response } from "express";
|
3 |
import { JsonResponse } from "@lib/responses/json-response";
|
4 |
import { asyncHandler } from "@helpers/async-handler";
|
5 |
import { BaseController } from "@lib/controllers/controller.base";
|
|
|
14 |
import { SwaggerResponse } from "@lib/decorators/swagger-response.decorator";
|
15 |
import { SwaggerSummary } from "@lib/decorators/swagger-summary.decorator";
|
16 |
import { SwaggerDescription } from "@lib/decorators/swagger-description.decorator";
|
17 |
+
import { IUserRequest } from "@common/interfaces/user-request.interface";
|
|
|
|
|
18 |
|
19 |
@Controller("/user/myMealPlan")
|
20 |
@ControllerMiddleware(UsersGuardMiddleware())
|
|
|
33 |
@SwaggerResponse(GetMyMealPlanSerialization)
|
34 |
@SwaggerSummary("get my meal plan")
|
35 |
@SwaggerDescription("Get the meal plan that the user is currently using")
|
36 |
+
get = async (req: IUserRequest, res: Response): Promise<Response> => {
|
37 |
const data = await this.userRegisteredMealPlansService.findOneOrFail(
|
38 |
{ user: req.jwtPayload.id, isActive: true },
|
39 |
{
|
|
|
57 |
@SwaggerRequest({ meal_plan: "string" })
|
58 |
@SwaggerSummary("create my meal plan")
|
59 |
@SwaggerDescription("Create a new meal plan for the user")
|
60 |
+
create = async (req: IUserRequest, res: Response) => {
|
61 |
const data = await this.userRegisteredMealPlansService.createForUser(req.body, req.jwtPayload.id);
|
62 |
return JsonResponse.success(
|
63 |
{
|
src/modules/users/modules/user-registered-meal-plans/services/meal-plans-progress.service.ts
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { UserRegisteredMealPlan } from "@common/models/user-registered-meal-plan.model";
|
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()
|
8 |
+
|
9 |
+
async updateForUser(mealPlanProps: { urwId: string; dayNumber: number }, _data: any, userId: string) {
|
10 |
+
// find mealPlan
|
11 |
+
const mealPlan = await this.findOneOrFail({
|
12 |
+
_id: mealPlanProps.urwId,
|
13 |
+
user: userId,
|
14 |
+
});
|
15 |
+
|
16 |
+
// find day
|
17 |
+
const day = mealPlan.days.find(d => d.day_number === mealPlanProps.dayNumber);
|
18 |
+
if (!day) throw new HttpError(404, 'Workout Day Not Found');
|
19 |
+
const dayIndex = mealPlan.days.indexOf(day)
|
20 |
+
|
21 |
+
// update day
|
22 |
+
day.is_eaten = true;
|
23 |
+
mealPlan.days[dayIndex] = day;
|
24 |
+
|
25 |
+
// save changes
|
26 |
+
mealPlan.markModified('days');
|
27 |
+
|
28 |
+
const updatedMealPlan = await mealPlan.save();
|
29 |
+
|
30 |
+
// check if it's the last day
|
31 |
+
const lastDay = mealPlan.days[mealPlan.days.length - 1];
|
32 |
+
if (lastDay.day_number === mealPlanProps.dayNumber) {
|
33 |
+
this.mealPlansService.createModelMealPlan(userId)
|
34 |
+
}
|
35 |
+
|
36 |
+
return updatedMealPlan;
|
37 |
+
}
|
38 |
+
}
|
src/modules/users/modules/user-registered-meal-plans/services/user-registered-meal-plans.service.ts
CHANGED
@@ -1,7 +1,5 @@
|
|
1 |
import { UserRegisteredMealPlan } from "@common/models/user-registered-meal-plan.model";
|
2 |
import { CrudService } from "@lib/services/crud.service";
|
3 |
-
import { MealPlansService } from "../../meal-plans/services/meal-plans.service";
|
4 |
-
import { HttpError } from "@lib/error-handling/http-error";
|
5 |
import { MealPlan } from "@common/models/meal-plan.model";
|
6 |
|
7 |
|
@@ -32,34 +30,4 @@ export class UserRegisteredMealPlansService extends CrudService(UserRegisteredMe
|
|
32 |
isActive: true,
|
33 |
});
|
34 |
}
|
35 |
-
|
36 |
-
// find mealPlan
|
37 |
-
|
38 |
-
const mealPlan = await this.findOneOrFail({
|
39 |
-
_id: mealPlanProps.urwId,
|
40 |
-
user: userId,
|
41 |
-
});
|
42 |
-
|
43 |
-
// find day
|
44 |
-
const day = mealPlan.days.find(d => d.day_number === mealPlanProps.dayNumber);
|
45 |
-
if (!day) throw new HttpError(404, 'Workout Day Not Found');
|
46 |
-
const dayIndex = mealPlan.days.indexOf(day)
|
47 |
-
|
48 |
-
// update day
|
49 |
-
day.is_eaten = true;
|
50 |
-
mealPlan.days[dayIndex] = day;
|
51 |
-
|
52 |
-
// save changes
|
53 |
-
mealPlan.markModified('days');
|
54 |
-
|
55 |
-
const updatedMealPlan = await mealPlan.save();
|
56 |
-
|
57 |
-
// check if it's the last day
|
58 |
-
const lastDay = mealPlan.days[mealPlan.days.length - 1];
|
59 |
-
if (lastDay.day_number === mealPlanProps.dayNumber) {
|
60 |
-
console.log('This is the last day that was updated.');
|
61 |
-
}
|
62 |
-
|
63 |
-
return updatedMealPlan;
|
64 |
-
}
|
65 |
-
}
|
|
|
1 |
import { UserRegisteredMealPlan } from "@common/models/user-registered-meal-plan.model";
|
2 |
import { CrudService } from "@lib/services/crud.service";
|
|
|
|
|
3 |
import { MealPlan } from "@common/models/meal-plan.model";
|
4 |
|
5 |
|
|
|
30 |
isActive: true,
|
31 |
});
|
32 |
}
|
33 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
src/modules/users/modules/user-registered-workouts/controllers/user-registered-workouts.controller.ts
CHANGED
@@ -22,6 +22,7 @@ import { SwaggerDescription } from "@lib/decorators/swagger-description.decorato
|
|
22 |
import { SwaggerResponse } from "@lib/decorators/swagger-response.decorator";
|
23 |
import { SwaggerRequest } from "@lib/decorators/swagger-request.decorator";import { updateUserRegisteredWorkoutsSchema } from "../validations/update-user-registered-workouts.validation";
|
24 |
import { IUserRequest } from "@common/interfaces/user-request.interface";
|
|
|
25 |
4
|
26 |
|
27 |
|
@@ -29,6 +30,7 @@ import { IUserRequest } from "@common/interfaces/user-request.interface";
|
|
29 |
@ControllerMiddleware(UsersGuardMiddleware())
|
30 |
export class userRegisteredWorkoutsController extends BaseController {
|
31 |
private userRegisteredWorkoutsService = new UserRegisteredWorkoutsService();
|
|
|
32 |
|
33 |
setRoutes(): void {
|
34 |
this.router.get("/:id", asyncHandler(this.get));
|
@@ -124,7 +126,7 @@ export class userRegisteredWorkoutsController extends BaseController {
|
|
124 |
const urwId: string = req.params.id;
|
125 |
const weekNumber: number = Number(req.params.week);
|
126 |
const dayNumber: number = Number(req.params.day);
|
127 |
-
await this.
|
128 |
urwId, weekNumber, dayNumber
|
129 |
}, req.body, req.jwtPayload.id);
|
130 |
return JsonResponse.success(
|
|
|
22 |
import { SwaggerResponse } from "@lib/decorators/swagger-response.decorator";
|
23 |
import { SwaggerRequest } from "@lib/decorators/swagger-request.decorator";import { updateUserRegisteredWorkoutsSchema } from "../validations/update-user-registered-workouts.validation";
|
24 |
import { IUserRequest } from "@common/interfaces/user-request.interface";
|
25 |
+
import { WorkoutsProgressService } from "../services/workouts-progress.service";
|
26 |
4
|
27 |
|
28 |
|
|
|
30 |
@ControllerMiddleware(UsersGuardMiddleware())
|
31 |
export class userRegisteredWorkoutsController extends BaseController {
|
32 |
private userRegisteredWorkoutsService = new UserRegisteredWorkoutsService();
|
33 |
+
private workoutsProgressService = new WorkoutsProgressService();
|
34 |
|
35 |
setRoutes(): void {
|
36 |
this.router.get("/:id", asyncHandler(this.get));
|
|
|
126 |
const urwId: string = req.params.id;
|
127 |
const weekNumber: number = Number(req.params.week);
|
128 |
const dayNumber: number = Number(req.params.day);
|
129 |
+
await this.workoutsProgressService.updateForUser({
|
130 |
urwId, weekNumber, dayNumber
|
131 |
}, req.body, req.jwtPayload.id);
|
132 |
return JsonResponse.success(
|
src/modules/users/modules/user-registered-workouts/services/user-registered-workouts.service.ts
CHANGED
@@ -1,8 +1,6 @@
|
|
1 |
import { UserRegisteredWorkout } from "@common/models/user-registered-workout.model";
|
2 |
import { CrudService } from "@lib/services/crud.service";
|
3 |
import { ICreateUserRegisteredWorkouts } from "../validations/create-user-registered-workouts.validation";
|
4 |
-
import { IUpdateUserRegisteredWorkouts } from "../validations/update-user-registered-workouts.validation";
|
5 |
-
import { HttpError } from "@lib/error-handling/http-error";
|
6 |
import { Workout } from "@common/models/workout.model";
|
7 |
|
8 |
export class UserRegisteredWorkoutsService extends CrudService(UserRegisteredWorkout, {
|
@@ -35,32 +33,4 @@ export class UserRegisteredWorkoutsService extends CrudService(UserRegisteredWor
|
|
35 |
is_active: true,
|
36 |
});
|
37 |
}
|
38 |
-
|
39 |
-
async updateForUser(workoutProps: {urwId: string; weekNumber: number; dayNumber: number}, data: IUpdateUserRegisteredWorkouts, userId: string) {
|
40 |
-
// find workout
|
41 |
-
const workout = await this.findOneOrFail({
|
42 |
-
_id: workoutProps.urwId,
|
43 |
-
user: userId,
|
44 |
-
});
|
45 |
-
|
46 |
-
// find week
|
47 |
-
const week = workout.weeks.find(w => w.week_number === workoutProps.weekNumber);
|
48 |
-
if(!week) throw new HttpError(404, 'Workout Week Not Found');
|
49 |
-
const weekIndex = workout.weeks.indexOf(week);
|
50 |
-
|
51 |
-
// find day
|
52 |
-
const day = week.days.find(d => d.day_number === workoutProps.dayNumber);
|
53 |
-
if(!day) throw new HttpError(404, 'Workout Day Not Found');
|
54 |
-
const dayIndex = week.days.indexOf(day)
|
55 |
-
|
56 |
-
// update day and week
|
57 |
-
day.is_done = true;
|
58 |
-
week.days[dayIndex] = day;
|
59 |
-
week.is_done = week.days.every(d => d.is_done);
|
60 |
-
workout.weeks[weekIndex] = week;
|
61 |
-
|
62 |
-
// save changes
|
63 |
-
workout.markModified('weeks');
|
64 |
-
return workout.save()
|
65 |
-
}
|
66 |
}
|
|
|
1 |
import { UserRegisteredWorkout } from "@common/models/user-registered-workout.model";
|
2 |
import { CrudService } from "@lib/services/crud.service";
|
3 |
import { ICreateUserRegisteredWorkouts } from "../validations/create-user-registered-workouts.validation";
|
|
|
|
|
4 |
import { Workout } from "@common/models/workout.model";
|
5 |
|
6 |
export class UserRegisteredWorkoutsService extends CrudService(UserRegisteredWorkout, {
|
|
|
33 |
is_active: true,
|
34 |
});
|
35 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
}
|
src/modules/users/modules/user-registered-workouts/services/workouts-progress.service.ts
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { UserRegisteredWorkout } from "@common/models/user-registered-workout.model";
|
2 |
+
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: {
|
9 |
+
is_active: true,
|
10 |
+
},
|
11 |
+
}) {
|
12 |
+
private workoutsService = new WorkoutService();
|
13 |
+
|
14 |
+
async updateForUser(workoutProps: {urwId: string; weekNumber: number; dayNumber: number}, _data: IUpdateUserRegisteredWorkouts, userId: string) {
|
15 |
+
// find workout
|
16 |
+
const workout = await this.findOneOrFail({
|
17 |
+
_id: workoutProps.urwId,
|
18 |
+
user: userId,
|
19 |
+
});
|
20 |
+
|
21 |
+
// find week
|
22 |
+
const week = workout.weeks.find(w => w.week_number === workoutProps.weekNumber);
|
23 |
+
if(!week) throw new HttpError(404, 'Workout Week Not Found');
|
24 |
+
const weekIndex = workout.weeks.indexOf(week);
|
25 |
+
|
26 |
+
// find day
|
27 |
+
const day = week.days.find(d => d.day_number === workoutProps.dayNumber);
|
28 |
+
if(!day) throw new HttpError(404, 'Workout Day Not Found');
|
29 |
+
const dayIndex = week.days.indexOf(day)
|
30 |
+
|
31 |
+
// update day and week
|
32 |
+
day.is_done = true;
|
33 |
+
week.days[dayIndex] = day;
|
34 |
+
week.is_done = week.days.every(d => d.is_done);
|
35 |
+
workout.weeks[weekIndex] = week;
|
36 |
+
|
37 |
+
// if last week
|
38 |
+
if(weekIndex === workout.weeks.length - 1) {
|
39 |
+
await this.workoutsService.createModelWorkout(userId)
|
40 |
+
}
|
41 |
+
|
42 |
+
// save changes
|
43 |
+
workout.markModified('weeks');
|
44 |
+
return workout.save()
|
45 |
+
}
|
46 |
+
}
|
src/modules/users/modules/workouts/services/workouts.service.ts
CHANGED
@@ -6,12 +6,19 @@ import { CrudService } from "@lib/services/crud.service";
|
|
6 |
import { calcAge } from "@lib/utils/age";
|
7 |
import { ExerciseService } from "../../exercises/services/exercises.service";
|
8 |
import { UserRegisteredWorkoutsService } from "../../user-registered-workouts/services/user-registered-workouts.service";
|
|
|
|
|
9 |
|
10 |
export class WorkoutService extends CrudService(Workout) {
|
11 |
private exerciseService = new ExerciseService();
|
12 |
private userRegisteredWorkoutsService = new UserRegisteredWorkoutsService();
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
-
public async createModelWorkout(user: UserDocument) {
|
15 |
const params: IFWParams = {
|
16 |
home_or_gym: user.preferences.workout_place === WorkoutPlace.GYM ? 1 : 0,
|
17 |
level: user.fitness_level,
|
|
|
6 |
import { calcAge } from "@lib/utils/age";
|
7 |
import { ExerciseService } from "../../exercises/services/exercises.service";
|
8 |
import { UserRegisteredWorkoutsService } from "../../user-registered-workouts/services/user-registered-workouts.service";
|
9 |
+
import { UserService } from "../../users/services/users.service";
|
10 |
+
import { Types } from "mongoose";
|
11 |
|
12 |
export class WorkoutService extends CrudService(Workout) {
|
13 |
private exerciseService = new ExerciseService();
|
14 |
private userRegisteredWorkoutsService = new UserRegisteredWorkoutsService();
|
15 |
+
private usersService = new UserService()
|
16 |
+
|
17 |
+
public async createModelWorkout(userOrId: UserDocument | string) {
|
18 |
+
const user: UserDocument = typeof userOrId === 'string' ?
|
19 |
+
await this.usersService.findOneOrFail({_id: new Types.ObjectId(userOrId)}) :
|
20 |
+
userOrId;
|
21 |
|
|
|
22 |
const params: IFWParams = {
|
23 |
home_or_gym: user.preferences.workout_place === WorkoutPlace.GYM ? 1 : 0,
|
24 |
level: user.fitness_level,
|