Spaces:
Running
Running
Hozifa Elgharbawy
commited on
Commit
·
a99a515
1
Parent(s):
c822de3
add Serialization
Browse files- package-lock.json +6 -0
- package.json +1 -0
- src/common/serializers/user.serializtion.ts +68 -0
- src/helpers/serialize.ts +14 -0
- src/modules/console/common/models/admin.model.ts +7 -0
- src/modules/console/common/serializers/admin.serializtion.ts +27 -0
- src/modules/console/modules/admins/controllers/admins.controller.ts +7 -4
- src/modules/console/modules/users/controllers/users.controller.ts +4 -1
- src/modules/users/auth/controllers/auth.controller.ts +4 -2
package-lock.json
CHANGED
@@ -12,6 +12,7 @@
|
|
12 |
"@hapi/joi": "^17.1.1",
|
13 |
"@types/glob": "^8.1.0",
|
14 |
"bcrypt": "^5.1.1",
|
|
|
15 |
"cors": "^2.8.5",
|
16 |
"dotenv": "^16.3.1",
|
17 |
"express": "^4.18.2",
|
@@ -657,6 +658,11 @@
|
|
657 |
"node": ">=10"
|
658 |
}
|
659 |
},
|
|
|
|
|
|
|
|
|
|
|
660 |
"node_modules/color-convert": {
|
661 |
"version": "2.0.1",
|
662 |
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
|
|
|
12 |
"@hapi/joi": "^17.1.1",
|
13 |
"@types/glob": "^8.1.0",
|
14 |
"bcrypt": "^5.1.1",
|
15 |
+
"class-transformer": "^0.5.1",
|
16 |
"cors": "^2.8.5",
|
17 |
"dotenv": "^16.3.1",
|
18 |
"express": "^4.18.2",
|
|
|
658 |
"node": ">=10"
|
659 |
}
|
660 |
},
|
661 |
+
"node_modules/class-transformer": {
|
662 |
+
"version": "0.5.1",
|
663 |
+
"resolved": "https://registry.npmjs.org/class-transformer/-/class-transformer-0.5.1.tgz",
|
664 |
+
"integrity": "sha512-SQa1Ws6hUbfC98vKGxZH3KFY0Y1lm5Zm0SY8XX9zbK7FJCyVEac3ATW0RIpwzW+oOfmHE5PMPufDG9hCfoEOMw=="
|
665 |
+
},
|
666 |
"node_modules/color-convert": {
|
667 |
"version": "2.0.1",
|
668 |
"resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz",
|
package.json
CHANGED
@@ -22,6 +22,7 @@
|
|
22 |
"@hapi/joi": "^17.1.1",
|
23 |
"@types/glob": "^8.1.0",
|
24 |
"bcrypt": "^5.1.1",
|
|
|
25 |
"cors": "^2.8.5",
|
26 |
"dotenv": "^16.3.1",
|
27 |
"express": "^4.18.2",
|
|
|
22 |
"@hapi/joi": "^17.1.1",
|
23 |
"@types/glob": "^8.1.0",
|
24 |
"bcrypt": "^5.1.1",
|
25 |
+
"class-transformer": "^0.5.1",
|
26 |
"cors": "^2.8.5",
|
27 |
"dotenv": "^16.3.1",
|
28 |
"express": "^4.18.2",
|
src/common/serializers/user.serializtion.ts
ADDED
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { Expose, Transform } from "class-transformer";
|
2 |
+
import { serialize } from "@helpers/serialize";
|
3 |
+
|
4 |
+
class Preferences {
|
5 |
+
@Expose()
|
6 |
+
fitness_goal: string;
|
7 |
+
|
8 |
+
@Expose()
|
9 |
+
target_weight: number;
|
10 |
+
|
11 |
+
@Expose()
|
12 |
+
workout_frequency: number;
|
13 |
+
|
14 |
+
@Expose()
|
15 |
+
preferred_days: any;
|
16 |
+
|
17 |
+
@Expose()
|
18 |
+
workout_place: string;
|
19 |
+
|
20 |
+
@Expose()
|
21 |
+
preferred_equipment: any;
|
22 |
+
}
|
23 |
+
|
24 |
+
|
25 |
+
export class UserSerialization {
|
26 |
+
@Expose({ name: "_id" })
|
27 |
+
id: string;
|
28 |
+
|
29 |
+
@Expose()
|
30 |
+
name: string;
|
31 |
+
|
32 |
+
@Expose()
|
33 |
+
email: string;
|
34 |
+
|
35 |
+
@Expose()
|
36 |
+
image: object;
|
37 |
+
|
38 |
+
@Expose()
|
39 |
+
role: string;
|
40 |
+
|
41 |
+
@Expose()
|
42 |
+
gender: string;
|
43 |
+
|
44 |
+
@Expose({ name: "dob" })
|
45 |
+
@Transform(
|
46 |
+
({ value }) => new Date().getFullYear() - (value as Date).getFullYear()
|
47 |
+
)
|
48 |
+
age: number;
|
49 |
+
|
50 |
+
@Expose()
|
51 |
+
height: number;
|
52 |
+
|
53 |
+
@Expose()
|
54 |
+
weight: number;
|
55 |
+
|
56 |
+
@Expose()
|
57 |
+
fitness_level: string;
|
58 |
+
|
59 |
+
@Expose({ name: "preferences" })
|
60 |
+
@Transform(
|
61 |
+
({ value }) => serialize(value, Preferences)
|
62 |
+
)
|
63 |
+
preferences: object;
|
64 |
+
|
65 |
+
@Expose()
|
66 |
+
injuries: any;
|
67 |
+
|
68 |
+
}
|
src/helpers/serialize.ts
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { plainToClass } from "class-transformer";
|
2 |
+
|
3 |
+
export const serialize = <T>(
|
4 |
+
serializable: Record<string, any> | Record<string, any>[],
|
5 |
+
serializer: new () => T
|
6 |
+
): T | T[] => {
|
7 |
+
if (Array.isArray(serializable)) {
|
8 |
+
return serializable.map((item) => serialize(item, serializer)) as T[];
|
9 |
+
}
|
10 |
+
|
11 |
+
return plainToClass(serializer, serializable, {
|
12 |
+
excludeExtraneousValues: true,
|
13 |
+
}) as T;
|
14 |
+
};
|
src/modules/console/common/models/admin.model.ts
CHANGED
@@ -1,6 +1,8 @@
|
|
1 |
import mongoose from "mongoose";
|
2 |
import bcrypt from "bcrypt";
|
3 |
import { config } from "../../../../configs/config";
|
|
|
|
|
4 |
const { Schema } = mongoose;
|
5 |
|
6 |
export interface IAdmin {
|
@@ -10,6 +12,7 @@ export interface IAdmin {
|
|
10 |
image: object;
|
11 |
gender: string;
|
12 |
dob: Date;
|
|
|
13 |
}
|
14 |
|
15 |
const AdminSchema = new Schema({
|
@@ -19,6 +22,10 @@ const AdminSchema = new Schema({
|
|
19 |
image: { type: Object, default: {} },
|
20 |
gender: { type: String, required: true },
|
21 |
dob: { type: Date },
|
|
|
|
|
|
|
|
|
22 |
});
|
23 |
|
24 |
AdminSchema.pre("save", async function (next) {
|
|
|
1 |
import mongoose from "mongoose";
|
2 |
import bcrypt from "bcrypt";
|
3 |
import { config } from "../../../../configs/config";
|
4 |
+
import { Role } from "@common/enums/role.enum";
|
5 |
+
|
6 |
const { Schema } = mongoose;
|
7 |
|
8 |
export interface IAdmin {
|
|
|
12 |
image: object;
|
13 |
gender: string;
|
14 |
dob: Date;
|
15 |
+
role: Role;
|
16 |
}
|
17 |
|
18 |
const AdminSchema = new Schema({
|
|
|
22 |
image: { type: Object, default: {} },
|
23 |
gender: { type: String, required: true },
|
24 |
dob: { type: Date },
|
25 |
+
role: {
|
26 |
+
type: String,
|
27 |
+
enum: Role
|
28 |
+
},
|
29 |
});
|
30 |
|
31 |
AdminSchema.pre("save", async function (next) {
|
src/modules/console/common/serializers/admin.serializtion.ts
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { Expose, Transform } from "class-transformer";
|
2 |
+
|
3 |
+
export class AdminSerialization {
|
4 |
+
@Expose({ name: "_id" })
|
5 |
+
id: string;
|
6 |
+
|
7 |
+
@Expose()
|
8 |
+
name: string;
|
9 |
+
|
10 |
+
@Expose()
|
11 |
+
email: string;
|
12 |
+
|
13 |
+
@Expose()
|
14 |
+
image: object;
|
15 |
+
|
16 |
+
@Expose()
|
17 |
+
role: string;
|
18 |
+
|
19 |
+
@Expose()
|
20 |
+
gender: string;
|
21 |
+
|
22 |
+
@Expose({ name: "dob" })
|
23 |
+
@Transform(
|
24 |
+
({ value }) => new Date().getFullYear() - (value as Date).getFullYear()
|
25 |
+
)
|
26 |
+
age: number;
|
27 |
+
}
|
src/modules/console/modules/admins/controllers/admins.controller.ts
CHANGED
@@ -10,6 +10,8 @@ import { JsonResponse } from "@lib/responses/json-response";
|
|
10 |
import { ControllerMiddleware } from "@lib/decorators/controller-middleware.decorator";
|
11 |
import { AdminGuardMiddleware } from "src/modules/console/common/guards/admins.guard";
|
12 |
import { Role } from "@common/enums/role.enum";
|
|
|
|
|
13 |
|
14 |
@Prefix("/console/admins")
|
15 |
@ControllerMiddleware(AdminGuardMiddleware({ roles: [Role.SUPER_ADMIN] }))
|
@@ -43,8 +45,9 @@ export class AdminsController extends BaseController {
|
|
43 |
{},
|
44 |
paginationQuery
|
45 |
);
|
|
|
46 |
const response = new JsonResponse({
|
47 |
-
data: docs,
|
48 |
meta: paginationData,
|
49 |
});
|
50 |
return res.json(response);
|
@@ -55,7 +58,7 @@ export class AdminsController extends BaseController {
|
|
55 |
_id: req.params.id,
|
56 |
});
|
57 |
const response = new JsonResponse({
|
58 |
-
data,
|
59 |
});
|
60 |
res.json(response);
|
61 |
};
|
@@ -77,7 +80,7 @@ export class AdminsController extends BaseController {
|
|
77 |
);
|
78 |
|
79 |
const response = new JsonResponse({
|
80 |
-
data: admin,
|
81 |
});
|
82 |
|
83 |
res.json(response);
|
@@ -89,7 +92,7 @@ export class AdminsController extends BaseController {
|
|
89 |
});
|
90 |
|
91 |
const response = new JsonResponse({
|
92 |
-
data: admin,
|
93 |
});
|
94 |
|
95 |
res.json(response);
|
|
|
10 |
import { ControllerMiddleware } from "@lib/decorators/controller-middleware.decorator";
|
11 |
import { AdminGuardMiddleware } from "src/modules/console/common/guards/admins.guard";
|
12 |
import { Role } from "@common/enums/role.enum";
|
13 |
+
import { serialize } from "@helpers/serialize";
|
14 |
+
import { AdminSerialization } from "src/modules/console/common/serializers/admin.serializtion";
|
15 |
|
16 |
@Prefix("/console/admins")
|
17 |
@ControllerMiddleware(AdminGuardMiddleware({ roles: [Role.SUPER_ADMIN] }))
|
|
|
45 |
{},
|
46 |
paginationQuery
|
47 |
);
|
48 |
+
|
49 |
const response = new JsonResponse({
|
50 |
+
data: serialize(docs, AdminSerialization),
|
51 |
meta: paginationData,
|
52 |
});
|
53 |
return res.json(response);
|
|
|
58 |
_id: req.params.id,
|
59 |
});
|
60 |
const response = new JsonResponse({
|
61 |
+
data: serialize(data, AdminSerialization),
|
62 |
});
|
63 |
res.json(response);
|
64 |
};
|
|
|
80 |
);
|
81 |
|
82 |
const response = new JsonResponse({
|
83 |
+
data: serialize(admin, AdminSerialization),
|
84 |
});
|
85 |
|
86 |
res.json(response);
|
|
|
92 |
});
|
93 |
|
94 |
const response = new JsonResponse({
|
95 |
+
data: serialize(admin, AdminSerialization),
|
96 |
});
|
97 |
|
98 |
res.json(response);
|
src/modules/console/modules/users/controllers/users.controller.ts
CHANGED
@@ -6,6 +6,9 @@ import { asyncHandler } from "@helpers/async-handler";
|
|
6 |
import { bodyValidator } from "@helpers/validation.helper";
|
7 |
import { BaseController } from "@lib/controllers/controller.base";
|
8 |
import { Prefix } from "@lib/decorators/prefix.decorator";
|
|
|
|
|
|
|
9 |
|
10 |
@Prefix("/console/users")
|
11 |
export class AdminUsersController extends BaseController {
|
@@ -22,7 +25,7 @@ export class AdminUsersController extends BaseController {
|
|
22 |
create = async (req: Request, res: Response) => {
|
23 |
let user = await this.usersService.create(req.body);
|
24 |
const response = new JsonResponse({
|
25 |
-
data: user,
|
26 |
});
|
27 |
return res.json(response);
|
28 |
};
|
|
|
6 |
import { bodyValidator } from "@helpers/validation.helper";
|
7 |
import { BaseController } from "@lib/controllers/controller.base";
|
8 |
import { Prefix } from "@lib/decorators/prefix.decorator";
|
9 |
+
import { serialize } from "@helpers/serialize";
|
10 |
+
import { UserSerialization } from "@common/serializers/user.serializtion";
|
11 |
+
|
12 |
|
13 |
@Prefix("/console/users")
|
14 |
export class AdminUsersController extends BaseController {
|
|
|
25 |
create = async (req: Request, res: Response) => {
|
26 |
let user = await this.usersService.create(req.body);
|
27 |
const response = new JsonResponse({
|
28 |
+
data: serialize(user, UserSerialization),
|
29 |
});
|
30 |
return res.json(response);
|
31 |
};
|
src/modules/users/auth/controllers/auth.controller.ts
CHANGED
@@ -7,6 +7,8 @@ import { asyncHandler } from "@helpers/async-handler";
|
|
7 |
import { bodyValidator } from "@helpers/validation.helper";
|
8 |
import { BaseController } from "@lib/controllers/controller.base";
|
9 |
import { Prefix } from "@lib/decorators/prefix.decorator";
|
|
|
|
|
10 |
|
11 |
@Prefix("/users/auth")
|
12 |
export class UsersAuthController extends BaseController {
|
@@ -28,7 +30,7 @@ export class UsersAuthController extends BaseController {
|
|
28 |
register = async (req: Request, res: Response) => {
|
29 |
const user = await this.authService.register(req.body as IUserRegister);
|
30 |
const response = new JsonResponse({
|
31 |
-
data: user,
|
32 |
});
|
33 |
return res.json(response);
|
34 |
};
|
@@ -36,7 +38,7 @@ export class UsersAuthController extends BaseController {
|
|
36 |
login = async (req: Request, res: Response) => {
|
37 |
const { user, token } = await this.authService.login(req.body);
|
38 |
const response = new JsonResponse({
|
39 |
-
data: { user, token },
|
40 |
});
|
41 |
return res.json(response);
|
42 |
};
|
|
|
7 |
import { bodyValidator } from "@helpers/validation.helper";
|
8 |
import { BaseController } from "@lib/controllers/controller.base";
|
9 |
import { Prefix } from "@lib/decorators/prefix.decorator";
|
10 |
+
import { serialize } from "@helpers/serialize";
|
11 |
+
import { UserSerialization } from "@common/serializers/user.serializtion";
|
12 |
|
13 |
@Prefix("/users/auth")
|
14 |
export class UsersAuthController extends BaseController {
|
|
|
30 |
register = async (req: Request, res: Response) => {
|
31 |
const user = await this.authService.register(req.body as IUserRegister);
|
32 |
const response = new JsonResponse({
|
33 |
+
data: serialize(user, UserSerialization),
|
34 |
});
|
35 |
return res.json(response);
|
36 |
};
|
|
|
38 |
login = async (req: Request, res: Response) => {
|
39 |
const { user, token } = await this.authService.login(req.body);
|
40 |
const response = new JsonResponse({
|
41 |
+
data: { user: serialize(user, UserSerialization), token },
|
42 |
});
|
43 |
return res.json(response);
|
44 |
};
|