Hozifa Elgharbawy commited on
Commit
490d8a0
·
1 Parent(s): 4bad94c

refactor: Update admin model and serialization

Browse files
src/modules/console/common/models/admin.model.ts CHANGED
@@ -9,9 +9,8 @@ export interface IAdmin {
9
  name: string;
10
  email: string;
11
  password: string;
12
- image: object;
13
  gender: string;
14
- dob: Date;
15
  role: Role;
16
  }
17
 
@@ -19,9 +18,8 @@ const AdminSchema = new Schema({
19
  name: { type: String, required: true },
20
  email: { type: String, required: true, unique: true, dropDups: true },
21
  password: { type: String, required: true },
22
- image: { type: Object, default: {} },
23
  gender: { type: String, required: true },
24
- dob: { type: Date },
25
  role: {
26
  type: String,
27
  enum: Role
 
9
  name: string;
10
  email: string;
11
  password: string;
12
+ image: String;
13
  gender: string;
 
14
  role: Role;
15
  }
16
 
 
18
  name: { type: String, required: true },
19
  email: { type: String, required: true, unique: true, dropDups: true },
20
  password: { type: String, required: true },
21
+ image: { type: String, default: {} },
22
  gender: { type: String, required: true },
 
23
  role: {
24
  type: String,
25
  enum: Role
src/modules/console/common/serializers/admin.serialization.ts CHANGED
@@ -27,11 +27,4 @@ export class AdminSerialization {
27
  @Expose()
28
  @SwaggerResponseProperty("string")
29
  gender: string;
30
-
31
- @Expose({ name: "dob" })
32
- @SwaggerResponseProperty("number")
33
- @Transform(
34
- ({ value }) => new Date().getFullYear() - new Date(value).getFullYear()
35
- )
36
- age: number;
37
  }
 
27
  @Expose()
28
  @SwaggerResponseProperty("string")
29
  gender: string;
 
 
 
 
 
 
 
30
  }
src/modules/console/modules/admins/controllers/admins.controller.ts CHANGED
@@ -8,6 +8,7 @@ import {
8
  ICreateAdmin,
9
  createAdminSchema,
10
  } from "../validations/create-admin.validation";
 
11
  import { parsePaginationQuery } from "@helpers/pagination";
12
  import { JsonResponse } from "@lib/responses/json-response";
13
  import { ControllerMiddleware } from "@lib/decorators/controller-middleware.decorator";
@@ -38,13 +39,13 @@ export class AdminsController extends BaseController {
38
  this.router.get("/:id", paramsValidator("id"), asyncHandler(this.get));
39
  this.router.post(
40
  "/",
41
-
42
  this.create
43
  );
44
  this.router.patch(
45
  "/:id",
46
  paramsValidator("id"),
47
- bodyValidator(createAdminSchema),
48
  asyncHandler(this.update)
49
  );
50
  this.router.delete(
@@ -123,7 +124,7 @@ export class AdminsController extends BaseController {
123
 
124
  @SwaggerPatch("/:id")
125
  @SwaggerResponse(AdminSerialization)
126
- @SwaggerRequest(createAdminSchema)
127
  @SwaggerSummary("Update admin")
128
  @SwaggerDescription("Updates an admin by id")
129
  update = async (
 
8
  ICreateAdmin,
9
  createAdminSchema,
10
  } from "../validations/create-admin.validation";
11
+ import { updateAdminSchema } from "../validations/update-admin.validation";
12
  import { parsePaginationQuery } from "@helpers/pagination";
13
  import { JsonResponse } from "@lib/responses/json-response";
14
  import { ControllerMiddleware } from "@lib/decorators/controller-middleware.decorator";
 
39
  this.router.get("/:id", paramsValidator("id"), asyncHandler(this.get));
40
  this.router.post(
41
  "/",
42
+ bodyValidator(createAdminSchema),
43
  this.create
44
  );
45
  this.router.patch(
46
  "/:id",
47
  paramsValidator("id"),
48
+ bodyValidator(updateAdminSchema),
49
  asyncHandler(this.update)
50
  );
51
  this.router.delete(
 
124
 
125
  @SwaggerPatch("/:id")
126
  @SwaggerResponse(AdminSerialization)
127
+ @SwaggerRequest(updateAdminSchema)
128
  @SwaggerSummary("Update admin")
129
  @SwaggerDescription("Updates an admin by id")
130
  update = async (
src/modules/console/modules/admins/validations/create-admin.validation.ts CHANGED
@@ -6,7 +6,7 @@ export interface ICreateAdmin {
6
  name: string;
7
  email: string;
8
  password: string;
9
- dob: Date;
10
  role?: Role;
11
  gender: string;
12
  }
@@ -36,8 +36,9 @@ export const createAdminSchema = createSchema<ICreateAdmin>({
36
  "string.empty": "password cannot be empty",
37
  "string.min": "password must be at least 8 characters",
38
  }),
39
- dob: joi.date().empty().optional().messages({
40
- "date.base": "please enter a valid date",
 
41
  }),
42
  role: joi
43
  .string()
 
6
  name: string;
7
  email: string;
8
  password: string;
9
+ image?: string;
10
  role?: Role;
11
  gender: string;
12
  }
 
36
  "string.empty": "password cannot be empty",
37
  "string.min": "password must be at least 8 characters",
38
  }),
39
+ image: joi.string().empty().optional().messages({
40
+ "string.base": "please enter a valid image",
41
+ "string.empty": "image cannot be empty",
42
  }),
43
  role: joi
44
  .string()
src/modules/console/modules/admins/validations/update-admin.validation.ts ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { Role } from "@common/enums/role.enum";
2
+ import * as joi from "joi";
3
+ import { createSchema } from "@helpers/create-schema";
4
+
5
+ export interface IUpdateAdmin {
6
+ name?: string;
7
+ email?: string;
8
+ password?: string;
9
+ image?: string;
10
+ role?: Role;
11
+ gender?: string;
12
+ }
13
+
14
+ export const updateAdminSchema = createSchema<IUpdateAdmin>({
15
+ name: joi.string().empty().optional().messages({
16
+ "string.base": "please enter a valid name",
17
+ "string.empty": "name can not be empty",
18
+ }),
19
+ email: joi
20
+ .string()
21
+ .required()
22
+ .email({
23
+ minDomainSegments: 2,
24
+ tlds: { allow: ["com", "net", "org", "eg", "io"] },
25
+ })
26
+ .empty()
27
+ .messages({
28
+ "string.email": "please enter a valid email",
29
+ "any.required": "email must be entered",
30
+ "string.empty": "email can not be empty",
31
+ }),
32
+ password: joi.string().empty().min(8).optional().messages({
33
+ "string.base": "please enter a valid password",
34
+ "string.empty": "password cannot be empty",
35
+ "string.min": "password must be at least 8 characters",
36
+ }),
37
+ image: joi.string().empty().optional().messages({
38
+ "string.base": "please enter a valid image",
39
+ "string.empty": "image cannot be empty",
40
+ }),
41
+ role: joi
42
+ .string()
43
+ .valid(...Object.values(Role))
44
+ .optional()
45
+ .messages({
46
+ "string.base": "please enter a valid role",
47
+ "string.empty": "role cannot be empty",
48
+ }),
49
+ gender: joi.string().empty().optional().messages({
50
+ "string.base": "please enter a valid gender",
51
+ "string.empty": "gender cannot be empty",
52
+ }),
53
+ });