moahmedwafy commited on
Commit
7e5807a
·
2 Parent(s): fe17679 58f0a1f

Merge branch 'main' into docs

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
@@ -30,6 +30,7 @@ export class userRegisteredWorkoutsController extends BaseController {
30
 
31
  setRoutes(): void {
32
  this.router.get("/:id", paramsValidator("id"), asyncHandler(this.get));
 
33
  this.router.get("/", asyncHandler(this.list));
34
  this.router.post(
35
  "/",
@@ -42,17 +43,16 @@ export class userRegisteredWorkoutsController extends BaseController {
42
  @SwaggerResponse([UserRegisteredWorkoutsSerialization])
43
  list = async (req: userRequest, res: Response) => {
44
  const paginationQuery = parsePaginationQuery(req.query);
45
- const { docs, paginationData } =
46
- await this.userRegisteredWorkoutsService.list(
47
- { user: req.jwtPayload.id },
48
- paginationQuery,
49
- {
50
- populateArray: [
51
- { path: "workout", select: "-templateWeeks -created_by" },
52
- { path: "weeks.days.exercises", select: "name media reps sets" },
53
- ],
54
- }
55
- );
56
 
57
  return JsonResponse.success(
58
  {
@@ -72,7 +72,26 @@ export class userRegisteredWorkoutsController extends BaseController {
72
  populateArray: [
73
  { path: "workout", select: "-template_weeks -created_by" },
74
  { path: "weeks.days.exercises", select: "name media reps sets" },
75
- ],
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
76
  }
77
  );
78
  return JsonResponse.success(
 
30
 
31
  setRoutes(): void {
32
  this.router.get("/:id", paramsValidator("id"), asyncHandler(this.get));
33
+ this.router.get("/home/:userId", paramsValidator("userId"), asyncHandler(this.getHomePage));
34
  this.router.get("/", asyncHandler(this.list));
35
  this.router.post(
36
  "/",
 
43
  @SwaggerResponse([UserRegisteredWorkoutsSerialization])
44
  list = async (req: userRequest, res: Response) => {
45
  const paginationQuery = parsePaginationQuery(req.query);
46
+ const { docs, paginationData } = await this.userRegisteredWorkoutsService.list(
47
+ { user: req.jwtPayload.id, is_active: true },
48
+ paginationQuery,
49
+ {
50
+ populateArray: [
51
+ { path: "workout", select: "-templateWeeks -created_by" },
52
+ { path: "weeks.days.exercises", select: "name media reps sets" },
53
+ ]
54
+ }
55
+ );
 
56
 
57
  return JsonResponse.success(
58
  {
 
72
  populateArray: [
73
  { path: "workout", select: "-template_weeks -created_by" },
74
  { path: "weeks.days.exercises", select: "name media reps sets" },
75
+ ]
76
+ }
77
+ );
78
+ return JsonResponse.success(
79
+ {
80
+ data: serialize(data.toJSON(), UserRegisteredWorkoutsSerialization),
81
+ },
82
+ res
83
+ );
84
+ };
85
+
86
+ getHomePage = async (req: userRequest, res: Response) => {
87
+ const data = await this.userRegisteredWorkoutsService.findOneOrFail(
88
+ { user: req.params.userId },
89
+ {
90
+ populateArray: [
91
+ { path: "workout", select: "-template_weeks -created_by" },
92
+ { path: "weeks.days.exercises", select: "name media reps sets" },
93
+ { path: "user", select: "name preferences" }
94
+ ]
95
  }
96
  );
97
  return JsonResponse.success(