youssef Qatry commited on
Commit
58f0a1f
·
unverified ·
2 Parent(s): a8279cb 184c874

Merge pull request #51 from Modarb-Ai-Trainer/homePage

Browse files
src/lib/services/crud.service.ts CHANGED
@@ -1,7 +1,7 @@
1
  import { HttpError } from "@lib/error-handling/http-error";
2
- import { populate } from "dotenv";
3
  import { AnyKeys, Document, FilterQuery, Model } from "mongoose";
4
 
 
5
  export const CrudService = <ModelDoc extends Document>(
6
  model: Model<ModelDoc>
7
  ) => {
@@ -42,11 +42,11 @@ export const CrudService = <ModelDoc extends Document>(
42
  skip?: number;
43
  } = {
44
  limit: 10,
45
- skip: 0,
46
  },
47
  options?: {
48
  populateArray: any
49
- }
50
  ): Promise<{
51
  docs: ModelDoc[];
52
  paginationData: {
@@ -58,10 +58,10 @@ export const CrudService = <ModelDoc extends Document>(
58
  const queryInstruction = this.model
59
  .find(filter)
60
  .limit(paginationOptions.limit)
61
- .skip(paginationOptions.skip)
62
  if (options?.populateArray) queryInstruction.populate(options.populateArray);
63
 
64
- const docs = await queryInstruction
65
  const total = await this.model.countDocuments(filter);
66
  const paginationData = {
67
  total: total,
@@ -90,8 +90,9 @@ export const CrudService = <ModelDoc extends Document>(
90
  }
91
  ): Promise<ModelDoc> {
92
  await this.existsOrThrow(filter);
93
- let optionsObject = options ? options : { populateArray: [{}] }
94
- const document = await this.findOne(filter, optionsObject);
 
95
  return document;
96
  }
97
 
 
1
  import { HttpError } from "@lib/error-handling/http-error";
 
2
  import { AnyKeys, Document, FilterQuery, Model } from "mongoose";
3
 
4
+
5
  export const CrudService = <ModelDoc extends Document>(
6
  model: Model<ModelDoc>
7
  ) => {
 
42
  skip?: number;
43
  } = {
44
  limit: 10,
45
+ skip: 1,
46
  },
47
  options?: {
48
  populateArray: any
49
+ },
50
  ): Promise<{
51
  docs: ModelDoc[];
52
  paginationData: {
 
58
  const queryInstruction = this.model
59
  .find(filter)
60
  .limit(paginationOptions.limit)
61
+ .skip(paginationOptions.skip);
62
  if (options?.populateArray) queryInstruction.populate(options.populateArray);
63
 
64
+ const docs = await queryInstruction;
65
  const total = await this.model.countDocuments(filter);
66
  const paginationData = {
67
  total: total,
 
90
  }
91
  ): Promise<ModelDoc> {
92
  await this.existsOrThrow(filter);
93
+ const queryInstruction = this.model.findOne(filter);
94
+ if (options?.populateArray) queryInstruction.populate(options.populateArray);
95
+ const document = await queryInstruction;
96
  return document;
97
  }
98
 
src/modules/users/modules/user-registered-workouts/controllers/user-registered-workouts.controller.ts CHANGED
@@ -24,6 +24,7 @@ export class userRegisteredWorkoutsController extends BaseController {
24
 
25
  setRoutes(): void {
26
  this.router.get("/:id", paramsValidator("id"), asyncHandler(this.get));
 
27
  this.router.get("/", asyncHandler(this.list));
28
  this.router.post("/",
29
  bodyValidator(createUserRegisteredWorkoutsSchema),
@@ -33,7 +34,7 @@ export class userRegisteredWorkoutsController extends BaseController {
33
  list = async (req: userRequest, res: Response) => {
34
  const paginationQuery = parsePaginationQuery(req.query);
35
  const { docs, paginationData } = await this.userRegisteredWorkoutsService.list(
36
- { user: req.jwtPayload.id },
37
  paginationQuery,
38
  {
39
  populateArray: [
@@ -70,6 +71,25 @@ export class userRegisteredWorkoutsController extends BaseController {
70
  );
71
  };
72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
  create = async (req: userRequest, res: Response) => {
74
  const data = await this.userRegisteredWorkoutsService.create(req.body);
75
  return JsonResponse.success(
 
24
 
25
  setRoutes(): void {
26
  this.router.get("/:id", paramsValidator("id"), asyncHandler(this.get));
27
+ this.router.get("/home/:userId", paramsValidator("userId"), asyncHandler(this.getHomePage));
28
  this.router.get("/", asyncHandler(this.list));
29
  this.router.post("/",
30
  bodyValidator(createUserRegisteredWorkoutsSchema),
 
34
  list = async (req: userRequest, res: Response) => {
35
  const paginationQuery = parsePaginationQuery(req.query);
36
  const { docs, paginationData } = await this.userRegisteredWorkoutsService.list(
37
+ { user: req.jwtPayload.id, is_active: true },
38
  paginationQuery,
39
  {
40
  populateArray: [
 
71
  );
72
  };
73
 
74
+ getHomePage = async (req: userRequest, res: Response) => {
75
+ const data = await this.userRegisteredWorkoutsService.findOneOrFail(
76
+ { user: req.params.userId },
77
+ {
78
+ populateArray: [
79
+ { path: "workout", select: "-template_weeks -created_by" },
80
+ { path: "weeks.days.exercises", select: "name media reps sets" },
81
+ { path: "user", select: "name preferences" }
82
+ ]
83
+ }
84
+ );
85
+ return JsonResponse.success(
86
+ {
87
+ data: serialize(data.toJSON(), UserRegisteredWorkoutsSerialization),
88
+ },
89
+ res
90
+ );
91
+ };
92
+
93
  create = async (req: userRequest, res: Response) => {
94
  const data = await this.userRegisteredWorkoutsService.create(req.body);
95
  return JsonResponse.success(