Spaces:
Running
Running
Hozifa Elgharbawy
commited on
Commit
·
c7ae7c3
1
Parent(s):
66780ff
feat: Add API endpoint to create user meal plan
Browse files
src/modules/users/modules/user-registered-meal-plans/controller/user-registered-meal-plans.controller.ts
CHANGED
@@ -9,6 +9,8 @@ import { GetMyMealPlanSerialization } from "@common/serializers/user-registered-
|
|
9 |
import { ControllerMiddleware } from "@lib/decorators/controller-middleware.decorator";
|
10 |
import { UsersGuardMiddleware } from "modules/users/common/guards/users.guard";
|
11 |
import { SwaggerGet } from "@lib/decorators/swagger-routes.decorator";
|
|
|
|
|
12 |
import { SwaggerResponse } from "@lib/decorators/swagger-response.decorator";
|
13 |
import { SwaggerSummary } from "@lib/decorators/swagger-summary.decorator";
|
14 |
import { SwaggerDescription } from "@lib/decorators/swagger-description.decorator";
|
@@ -23,6 +25,10 @@ export class UsersRegisteredMealPlansController extends BaseController {
|
|
23 |
|
24 |
setRoutes(): void {
|
25 |
this.router.get("/", asyncHandler(this.get));
|
|
|
|
|
|
|
|
|
26 |
}
|
27 |
|
28 |
@SwaggerGet()
|
@@ -46,4 +52,21 @@ export class UsersRegisteredMealPlansController extends BaseController {
|
|
46 |
res
|
47 |
);
|
48 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
}
|
|
|
9 |
import { ControllerMiddleware } from "@lib/decorators/controller-middleware.decorator";
|
10 |
import { UsersGuardMiddleware } from "modules/users/common/guards/users.guard";
|
11 |
import { SwaggerGet } from "@lib/decorators/swagger-routes.decorator";
|
12 |
+
import { SwaggerPost } from "@lib/decorators/swagger-routes.decorator";
|
13 |
+
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";
|
|
|
25 |
|
26 |
setRoutes(): void {
|
27 |
this.router.get("/", asyncHandler(this.get));
|
28 |
+
this.router.post(
|
29 |
+
"/",
|
30 |
+
asyncHandler(this.create)
|
31 |
+
);
|
32 |
}
|
33 |
|
34 |
@SwaggerGet()
|
|
|
52 |
res
|
53 |
);
|
54 |
};
|
55 |
+
|
56 |
+
|
57 |
+
@SwaggerPost()
|
58 |
+
@SwaggerResponse(GetMyMealPlanSerialization)
|
59 |
+
@SwaggerRequest({ mealPlan: "string" })
|
60 |
+
@SwaggerSummary("create my meal plan")
|
61 |
+
@SwaggerDescription("Create a new meal plan for the user")
|
62 |
+
create = async (req: userRequest, res: Response) => {
|
63 |
+
const data = await this.userRegisteredMealPlansService.createForUser(req.body, req.jwtPayload.id);
|
64 |
+
return JsonResponse.success(
|
65 |
+
{
|
66 |
+
status: 201,
|
67 |
+
data: serialize(data.toJSON(), GetMyMealPlanSerialization),
|
68 |
+
},
|
69 |
+
res
|
70 |
+
);
|
71 |
+
};
|
72 |
}
|
src/modules/users/modules/user-registered-meal-plans/services/user-registered-meal-plans.service.ts
CHANGED
@@ -1,10 +1,35 @@
|
|
1 |
import { UserRegisteredMealPlan } from "@common/models/user-registered-meal-plan.model";
|
2 |
import { CrudService } from "@lib/services/crud.service";
|
3 |
-
import {
|
4 |
import { HttpError } from "@lib/error-handling/http-error";
|
5 |
|
6 |
|
7 |
export class UserRegisteredMealPlansService extends CrudService(UserRegisteredMealPlan) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
async updateForUser(mealPlanProps: {urwId: string; dayNumber: number}, data: any, userId: string) {
|
9 |
// find workout
|
10 |
const mealPlan = await this.findOneOrFail({
|
|
|
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 |
|
6 |
|
7 |
export class UserRegisteredMealPlansService extends CrudService(UserRegisteredMealPlan) {
|
8 |
+
private mealPlansService: MealPlansService = new MealPlansService();
|
9 |
+
|
10 |
+
async unregisterCurrentMealPlan(userId: string) {
|
11 |
+
return await this.updateMany({
|
12 |
+
user: userId,
|
13 |
+
isActive: true,
|
14 |
+
}, {
|
15 |
+
isActive: false,
|
16 |
+
});
|
17 |
+
}
|
18 |
+
|
19 |
+
async createForUser(data: any, userId: string) {
|
20 |
+
const mealPlan = await this.mealPlansService.findOneOrFail({
|
21 |
+
_id: data.mealPlan,
|
22 |
+
});
|
23 |
+
|
24 |
+
await this.unregisterCurrentMealPlan(userId);
|
25 |
+
|
26 |
+
return await this.create({
|
27 |
+
...data,
|
28 |
+
user: userId,
|
29 |
+
days: mealPlan.days,
|
30 |
+
isActive: true,
|
31 |
+
});
|
32 |
+
}
|
33 |
async updateForUser(mealPlanProps: {urwId: string; dayNumber: number}, data: any, userId: string) {
|
34 |
// find workout
|
35 |
const mealPlan = await this.findOneOrFail({
|