moahmedwafy commited on
Commit
093279d
·
1 Parent(s): fe2d2df

refactor: update validations

Browse files
package-lock.json CHANGED
@@ -9,6 +9,7 @@
9
  "version": "1.0.0",
10
  "license": "ISC",
11
  "dependencies": {
 
12
  "@types/glob": "^8.1.0",
13
  "bcrypt": "^5.1.1",
14
  "cors": "^2.8.5",
@@ -18,7 +19,7 @@
18
  "glob": "^10.3.10",
19
  "http": "^0.0.1-security",
20
  "i": "^0.3.7",
21
- "joi": "^17.11.0",
22
  "jsonwebtoken": "^9.0.2",
23
  "mongoose": "^8.0.3",
24
  "npm": "^10.2.5",
@@ -45,11 +46,44 @@
45
  "node": ">=12"
46
  }
47
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  "node_modules/@hapi/hoek": {
49
  "version": "9.3.0",
50
  "resolved": "https://registry.npmjs.org/@hapi/hoek/-/hoek-9.3.0.tgz",
51
  "integrity": "sha512-/c6rf4UJlmHlC9b5BaNvzAcFv7HZ2QHaV0D4/HNlBdvFnvQq8RI4kYdhyPCl7Xj+oWvTWQ8ujhqS53LIgAe6KQ=="
52
  },
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  "node_modules/@hapi/topo": {
54
  "version": "5.1.0",
55
  "resolved": "https://registry.npmjs.org/@hapi/topo/-/topo-5.1.0.tgz",
@@ -228,9 +262,9 @@
228
  }
229
  },
230
  "node_modules/@sideway/address": {
231
- "version": "4.1.4",
232
- "resolved": "https://registry.npmjs.org/@sideway/address/-/address-4.1.4.tgz",
233
- "integrity": "sha512-7vwq+rOHVWjyXxVlR76Agnvhy8I9rpzjosTESvmhNeXOXdZZB15Fl+TI9x1SiHZH5Jv2wTGduSxFDIaq0m3DUw==",
234
  "dependencies": {
235
  "@hapi/hoek": "^9.0.0"
236
  }
@@ -1449,13 +1483,13 @@
1449
  }
1450
  },
1451
  "node_modules/joi": {
1452
- "version": "17.11.0",
1453
- "resolved": "https://registry.npmjs.org/joi/-/joi-17.11.0.tgz",
1454
- "integrity": "sha512-NgB+lZLNoqISVy1rZocE9PZI36bL/77ie924Ri43yEvi9GUUMPeyVIr8KdFTMUlby1p0PBYMk9spIxEUQYqrJQ==",
1455
  "dependencies": {
1456
- "@hapi/hoek": "^9.0.0",
1457
- "@hapi/topo": "^5.0.0",
1458
- "@sideway/address": "^4.1.3",
1459
  "@sideway/formula": "^3.0.1",
1460
  "@sideway/pinpoint": "^2.0.0"
1461
  }
 
9
  "version": "1.0.0",
10
  "license": "ISC",
11
  "dependencies": {
12
+ "@hapi/joi": "^17.1.1",
13
  "@types/glob": "^8.1.0",
14
  "bcrypt": "^5.1.1",
15
  "cors": "^2.8.5",
 
19
  "glob": "^10.3.10",
20
  "http": "^0.0.1-security",
21
  "i": "^0.3.7",
22
+ "joi": "^17.12.1",
23
  "jsonwebtoken": "^9.0.2",
24
  "mongoose": "^8.0.3",
25
  "npm": "^10.2.5",
 
46
  "node": ">=12"
47
  }
48
  },
49
+ "node_modules/@hapi/address": {
50
+ "version": "4.1.0",
51
+ "resolved": "https://registry.npmjs.org/@hapi/address/-/address-4.1.0.tgz",
52
+ "integrity": "sha512-SkszZf13HVgGmChdHo/PxchnSaCJ6cetVqLzyciudzZRT0jcOouIF/Q93mgjw8cce+D+4F4C1Z/WrfFN+O3VHQ==",
53
+ "deprecated": "Moved to 'npm install @sideway/address'",
54
+ "dependencies": {
55
+ "@hapi/hoek": "^9.0.0"
56
+ }
57
+ },
58
+ "node_modules/@hapi/formula": {
59
+ "version": "2.0.0",
60
+ "resolved": "https://registry.npmjs.org/@hapi/formula/-/formula-2.0.0.tgz",
61
+ "integrity": "sha512-V87P8fv7PI0LH7LiVi8Lkf3x+KCO7pQozXRssAHNXXL9L1K+uyu4XypLXwxqVDKgyQai6qj3/KteNlrqDx4W5A==",
62
+ "deprecated": "Moved to 'npm install @sideway/formula'"
63
+ },
64
  "node_modules/@hapi/hoek": {
65
  "version": "9.3.0",
66
  "resolved": "https://registry.npmjs.org/@hapi/hoek/-/hoek-9.3.0.tgz",
67
  "integrity": "sha512-/c6rf4UJlmHlC9b5BaNvzAcFv7HZ2QHaV0D4/HNlBdvFnvQq8RI4kYdhyPCl7Xj+oWvTWQ8ujhqS53LIgAe6KQ=="
68
  },
69
+ "node_modules/@hapi/joi": {
70
+ "version": "17.1.1",
71
+ "resolved": "https://registry.npmjs.org/@hapi/joi/-/joi-17.1.1.tgz",
72
+ "integrity": "sha512-p4DKeZAoeZW4g3u7ZeRo+vCDuSDgSvtsB/NpfjXEHTUjSeINAi/RrVOWiVQ1isaoLzMvFEhe8n5065mQq1AdQg==",
73
+ "deprecated": "Switch to 'npm install joi'",
74
+ "dependencies": {
75
+ "@hapi/address": "^4.0.1",
76
+ "@hapi/formula": "^2.0.0",
77
+ "@hapi/hoek": "^9.0.0",
78
+ "@hapi/pinpoint": "^2.0.0",
79
+ "@hapi/topo": "^5.0.0"
80
+ }
81
+ },
82
+ "node_modules/@hapi/pinpoint": {
83
+ "version": "2.0.1",
84
+ "resolved": "https://registry.npmjs.org/@hapi/pinpoint/-/pinpoint-2.0.1.tgz",
85
+ "integrity": "sha512-EKQmr16tM8s16vTT3cA5L0kZZcTMU5DUOZTuvpnY738m+jyP3JIUj+Mm1xc1rsLkGBQ/gVnfKYPwOmPg1tUR4Q=="
86
+ },
87
  "node_modules/@hapi/topo": {
88
  "version": "5.1.0",
89
  "resolved": "https://registry.npmjs.org/@hapi/topo/-/topo-5.1.0.tgz",
 
262
  }
263
  },
264
  "node_modules/@sideway/address": {
265
+ "version": "4.1.5",
266
+ "resolved": "https://registry.npmjs.org/@sideway/address/-/address-4.1.5.tgz",
267
+ "integrity": "sha512-IqO/DUQHUkPeixNQ8n0JA6102hT9CmaljNTPmQ1u8MEhBo/R4Q8eKLN/vGZxuebwOroDB4cbpjheD4+/sKFK4Q==",
268
  "dependencies": {
269
  "@hapi/hoek": "^9.0.0"
270
  }
 
1483
  }
1484
  },
1485
  "node_modules/joi": {
1486
+ "version": "17.12.1",
1487
+ "resolved": "https://registry.npmjs.org/joi/-/joi-17.12.1.tgz",
1488
+ "integrity": "sha512-vtxmq+Lsc5SlfqotnfVjlViWfOL9nt/avKNbKYizwf6gsCfq9NYY/ceYRMFD8XDdrjJ9abJyScWmhmIiy+XRtQ==",
1489
  "dependencies": {
1490
+ "@hapi/hoek": "^9.3.0",
1491
+ "@hapi/topo": "^5.1.0",
1492
+ "@sideway/address": "^4.1.5",
1493
  "@sideway/formula": "^3.0.1",
1494
  "@sideway/pinpoint": "^2.0.0"
1495
  }
package.json CHANGED
@@ -19,6 +19,7 @@
19
  "typescript": "^5.3.3"
20
  },
21
  "dependencies": {
 
22
  "@types/glob": "^8.1.0",
23
  "bcrypt": "^5.1.1",
24
  "cors": "^2.8.5",
@@ -28,7 +29,7 @@
28
  "glob": "^10.3.10",
29
  "http": "^0.0.1-security",
30
  "i": "^0.3.7",
31
- "joi": "^17.11.0",
32
  "jsonwebtoken": "^9.0.2",
33
  "mongoose": "^8.0.3",
34
  "npm": "^10.2.5",
 
19
  "typescript": "^5.3.3"
20
  },
21
  "dependencies": {
22
+ "@hapi/joi": "^17.1.1",
23
  "@types/glob": "^8.1.0",
24
  "bcrypt": "^5.1.1",
25
  "cors": "^2.8.5",
 
29
  "glob": "^10.3.10",
30
  "http": "^0.0.1-security",
31
  "i": "^0.3.7",
32
+ "joi": "^17.12.1",
33
  "jsonwebtoken": "^9.0.2",
34
  "mongoose": "^8.0.3",
35
  "npm": "^10.2.5",
src/helpers/create-schema.ts ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ import * as joi from "joi";
2
+
3
+ export const createSchema = <T>(
4
+ schema: joi.SchemaMap<T>
5
+ ): joi.ObjectSchema<T> => {
6
+ return joi.object().required().keys(schema);
7
+ };
src/modules/common/users/enums/roles.enum.ts CHANGED
@@ -36,7 +36,7 @@ export enum PreferredEquipment {
36
  RESISTANCE_BAND = "resistance band",
37
  BODYWEIGHT = "bodyweight"
38
  }
39
- export enum Injurie {
40
  NECK = "neck",
41
  SHOULDERS = "shoulders",
42
  BACK = "back",
 
36
  RESISTANCE_BAND = "resistance band",
37
  BODYWEIGHT = "bodyweight"
38
  }
39
+ export enum Injury {
40
  NECK = "neck",
41
  SHOULDERS = "shoulders",
42
  BACK = "back",
src/modules/common/users/models/user.model.ts CHANGED
@@ -1,7 +1,7 @@
1
  import mongoose from "mongoose";
2
  import bcrypt from "bcrypt";
3
  export const saltrounds = 5;
4
- import { Role, Gender, FitnessLevel, FitnessGoal, WorkoutPlace, PreferredDay, PreferredEquipment, Injurie } from "../enums/roles.enum";
5
  const { Schema } = mongoose;
6
 
7
  export interface IUser {
@@ -23,7 +23,7 @@ export interface IUser {
23
  workout_place: WorkoutPlace;
24
  preferred_equipment: [PreferredEquipment];
25
  };
26
- injuries: [Injurie];
27
  }
28
 
29
  const userSchema = new Schema({
@@ -69,7 +69,7 @@ const userSchema = new Schema({
69
  },
70
  injuries: [{
71
  type: String,
72
- enum: Injurie,
73
  required: true
74
  }],
75
  dob: { type: Date },
 
1
  import mongoose from "mongoose";
2
  import bcrypt from "bcrypt";
3
  export const saltrounds = 5;
4
+ import { Role, Gender, FitnessLevel, FitnessGoal, WorkoutPlace, PreferredDay, PreferredEquipment, Injury } from "../enums/roles.enum";
5
  const { Schema } = mongoose;
6
 
7
  export interface IUser {
 
23
  workout_place: WorkoutPlace;
24
  preferred_equipment: [PreferredEquipment];
25
  };
26
+ injuries: [Injury];
27
  }
28
 
29
  const userSchema = new Schema({
 
69
  },
70
  injuries: [{
71
  type: String,
72
+ enum: Injury,
73
  required: true
74
  }],
75
  dob: { type: Date },
src/modules/common/users/validation/user-register.validation.ts CHANGED
@@ -1,140 +1,196 @@
1
  import * as joi from "joi";
2
- import { Role, Gender, FitnessLevel, FitnessGoal, WorkoutPlace, PreferredDay, PreferredEquipment, Injurie } from "../enums/roles.enum";
 
 
 
 
 
 
 
 
 
 
3
 
4
- export const userRegisterValidation = joi
5
- .object()
6
- .required()
7
- .keys({
8
- name: joi.string().empty().required().messages({
9
- "string.base": "please enter a valid name",
10
- "any.required": "name is required",
11
- "string.empty": "name can not be empty",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  }),
13
- email: joi
14
- .string()
15
- .required()
16
- .email({
17
- minDomainSegments: 2,
18
- tlds: { allow: ["com", "net", "org", "eg", "io"] },
19
- })
20
- .empty()
21
- .messages({
22
- "string.email": "please enter a valid email",
23
- "any.required": "email must be entered",
24
- "string.empty": "email can not be empty",
 
 
 
 
 
 
25
  }),
26
- password: joi.string().empty().min(8).required().messages({
27
- "string.base": "please enter a valid password",
28
- "any.required": "password must be entered",
29
- "string.empty": "password cannot be empty",
30
- "string.min": "password must be at least 8 characters",
31
- }),
32
- confirmPassword: joi.string().empty().min(8).required().messages({
33
- "string.base": "please enter a valid password",
34
- "any.required": "password must be entered",
35
- "string.empty": "password cannot be empty",
36
- "string.min": "password must be at least 8 characters",
37
- }),
38
- image: joi
39
- .object()
40
- .optional()
41
- .keys({
42
- url: joi.string().optional().messages({
43
- "string.base": "please enter a valid url",
44
- }),
45
- public_id: joi.string().optional().messages({
46
- "string.base": "please enter a valid public_id",
47
- }),
48
  }),
49
- gender: joi.string().valid(...Object.values(Gender)).empty().required().messages({
 
 
 
 
 
 
50
  "string.base": "please enter a valid gender",
51
  "any.required": "gender must be entered",
52
  "string.empty": "gender cannot be empty",
53
  }),
54
- height: joi.number().empty().required().messages({
55
- "number.base": "please enter a valid height number",
56
- "any.required": "height must be entered",
57
- "number.empty": "height cannot be empty",
58
- }),
59
- weight: joi.number().empty().required().messages({
60
- "number.base": "please enter a valid weight number",
61
- "any.required": "weight must be entered",
62
- "number.empty": "weight cannot be empty",
63
- }),
64
- fitness_level: joi.string().valid(...Object.values(FitnessLevel)).empty().required().messages({
 
 
 
 
 
65
  "string.base": "please enter a valid fitness_level",
66
  "any.required": "fitness_level must be entered",
67
  "string.empty": "fitness_level cannot be empty",
68
  }),
69
- preferences: joi
70
- .object()
71
- .optional()
72
- .keys({
73
- fitness_goal: joi.string().valid(...Object.values(FitnessGoal)).empty().required().messages({
 
 
 
 
 
74
  "string.base": "please enter a valid fitness_goal",
75
  "any.required": "fitness_goal must be entered",
76
  "string.empty": "fitness_goal cannot be empty",
77
  }),
78
- target_weight: joi.number().empty().required().messages({
79
- "number.base": "please enter a valid target_weight number",
80
- "any.required": "target_weight must be entered",
81
- "number.empty": "target_weight cannot be empty",
82
- }),
83
- workout_frequency: joi.number().empty().required().messages({
84
- "number.base": "please enter a valid workout_frequency number",
85
- "any.required": "workout_frequency must be entered",
86
- "number.empty": "workout_frequency cannot be empty",
87
- }),
88
- preferred_days: joi
89
- .array()
90
- .valid(...Object.values(PreferredDay))
91
- .empty()
92
- .required()
93
- .items(
94
- joi.string().empty().required().messages({
95
- "string.base": "please enter a valid preferred_days",
96
- "any.required": "preferred_days must be entered",
97
- "string.empty": "preferred_days cannot be empty",
98
- })
99
- ),
100
- workout_place: joi.string().valid(...Object.values(WorkoutPlace)).empty().required().messages({
 
 
 
 
 
101
  "string.base": "please enter a valid workout_place",
102
  "any.required": "workout_place must be entered",
103
  "string.empty": "workout_place cannot be empty",
104
  }),
105
- preferred_equipment: joi
106
- .array()
107
- .valid(...Object.values(PreferredEquipment))
108
- .empty()
109
- .required()
110
- .items(
111
- joi.string().empty().required().messages({
112
- "string.base": "please enter a valid preferred_equipment",
113
- "any.required": "preferred_equipment must be entered",
114
- "string.empty": "preferred_equipment cannot be empty",
115
- })
116
- ),
117
- }),
118
- injuries: joi
119
- .array()
120
- .valid(...Object.values(Injurie))
121
- .empty()
122
- .required()
123
- .items(
124
- joi.string().empty().required().messages({
125
- "string.base": "please enter a valid injuries",
126
- "any.required": "injuries must be entered",
127
- "string.empty": "injuries cannot be empty",
128
- })
129
- ),
130
- dob: joi.date().empty().optional().messages({
131
- "date.base": "please enter a valid date",
132
  }),
133
- role: joi
134
- .string()
135
- .valid(...Object.values(Role))
136
- .optional().messages({
137
- "string.base": "please enter a valid role",
138
- "string.empty": "role cannot be empty"
139
- }),
140
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import * as joi from "joi";
2
+ import {
3
+ Role,
4
+ Gender,
5
+ FitnessLevel,
6
+ FitnessGoal,
7
+ WorkoutPlace,
8
+ PreferredDay,
9
+ PreferredEquipment,
10
+ Injury,
11
+ } from "../enums/roles.enum";
12
+ import { createSchema } from "src/helpers/create-schema";
13
 
14
+ export interface IUserRegister {
15
+ name: string;
16
+ email: string;
17
+ password: string;
18
+ confirmPassword: string;
19
+ image?: {
20
+ url: string;
21
+ public_id: string;
22
+ };
23
+ gender: string;
24
+ height: number;
25
+ weight: number;
26
+ fitness_level: string;
27
+ preferences?: {
28
+ fitness_goal: string;
29
+ target_weight: number;
30
+ workout_frequency: number;
31
+ preferred_days: string[];
32
+ workout_place: string;
33
+ preferred_equipment: string[];
34
+ };
35
+ injuries: string[];
36
+ dob?: Date;
37
+ role?: Role;
38
+ }
39
+
40
+ export const userRegisterKeys = {
41
+ name: joi.string().empty().required().messages({
42
+ "string.base": "please enter a valid name",
43
+ "any.required": "name is required",
44
+ "string.empty": "name can not be empty",
45
+ }),
46
+ email: joi
47
+ .string()
48
+ .required()
49
+ .email({
50
+ minDomainSegments: 2,
51
+ tlds: { allow: ["com", "net", "org", "eg", "io"] },
52
+ })
53
+ .empty()
54
+ .messages({
55
+ "string.email": "please enter a valid email",
56
+ "any.required": "email must be entered",
57
+ "string.empty": "email can not be empty",
58
  }),
59
+ password: joi.string().empty().min(8).required().messages({
60
+ "string.base": "please enter a valid password",
61
+ "any.required": "password must be entered",
62
+ "string.empty": "password cannot be empty",
63
+ "string.min": "password must be at least 8 characters",
64
+ }),
65
+ confirmPassword: joi.string().empty().min(8).required().messages({
66
+ "string.base": "please enter a valid password",
67
+ "any.required": "password must be entered",
68
+ "string.empty": "password cannot be empty",
69
+ "string.min": "password must be at least 8 characters",
70
+ }),
71
+ image: joi
72
+ .object()
73
+ .optional()
74
+ .keys({
75
+ url: joi.string().optional().messages({
76
+ "string.base": "please enter a valid url",
77
  }),
78
+ public_id: joi.string().optional().messages({
79
+ "string.base": "please enter a valid public_id",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
80
  }),
81
+ }),
82
+ gender: joi
83
+ .string()
84
+ .valid(...Object.values(Gender))
85
+ .empty()
86
+ .required()
87
+ .messages({
88
  "string.base": "please enter a valid gender",
89
  "any.required": "gender must be entered",
90
  "string.empty": "gender cannot be empty",
91
  }),
92
+ height: joi.number().empty().required().messages({
93
+ "number.base": "please enter a valid height number",
94
+ "any.required": "height must be entered",
95
+ "number.empty": "height cannot be empty",
96
+ }),
97
+ weight: joi.number().empty().required().messages({
98
+ "number.base": "please enter a valid weight number",
99
+ "any.required": "weight must be entered",
100
+ "number.empty": "weight cannot be empty",
101
+ }),
102
+ fitness_level: joi
103
+ .string()
104
+ .valid(...Object.values(FitnessLevel))
105
+ .empty()
106
+ .required()
107
+ .messages({
108
  "string.base": "please enter a valid fitness_level",
109
  "any.required": "fitness_level must be entered",
110
  "string.empty": "fitness_level cannot be empty",
111
  }),
112
+ preferences: joi
113
+ .object()
114
+ .optional()
115
+ .keys({
116
+ fitness_goal: joi
117
+ .string()
118
+ .valid(...Object.values(FitnessGoal))
119
+ .empty()
120
+ .required()
121
+ .messages({
122
  "string.base": "please enter a valid fitness_goal",
123
  "any.required": "fitness_goal must be entered",
124
  "string.empty": "fitness_goal cannot be empty",
125
  }),
126
+ target_weight: joi.number().empty().required().messages({
127
+ "number.base": "please enter a valid target_weight number",
128
+ "any.required": "target_weight must be entered",
129
+ "number.empty": "target_weight cannot be empty",
130
+ }),
131
+ workout_frequency: joi.number().empty().required().messages({
132
+ "number.base": "please enter a valid workout_frequency number",
133
+ "any.required": "workout_frequency must be entered",
134
+ "number.empty": "workout_frequency cannot be empty",
135
+ }),
136
+ preferred_days: joi
137
+ .array()
138
+ .valid(...Object.values(PreferredDay))
139
+ .empty()
140
+ .required()
141
+ .items(
142
+ joi.string().empty().required().messages({
143
+ "string.base": "please enter a valid preferred_days",
144
+ "any.required": "preferred_days must be entered",
145
+ "string.empty": "preferred_days cannot be empty",
146
+ })
147
+ ),
148
+ workout_place: joi
149
+ .string()
150
+ .valid(...Object.values(WorkoutPlace))
151
+ .empty()
152
+ .required()
153
+ .messages({
154
  "string.base": "please enter a valid workout_place",
155
  "any.required": "workout_place must be entered",
156
  "string.empty": "workout_place cannot be empty",
157
  }),
158
+ preferred_equipment: joi
159
+ .array()
160
+ .valid(...Object.values(PreferredEquipment))
161
+ .empty()
162
+ .required()
163
+ .items(
164
+ joi.string().empty().required().messages({
165
+ "string.base": "please enter a valid preferred_equipment",
166
+ "any.required": "preferred_equipment must be entered",
167
+ "string.empty": "preferred_equipment cannot be empty",
168
+ })
169
+ ),
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
  }),
171
+ injuries: joi
172
+ .array()
173
+ .valid(...Object.values(Injury))
174
+ .empty()
175
+ .required()
176
+ .items(
177
+ joi.string().empty().required().messages({
178
+ "string.base": "please enter a valid injuries",
179
+ "any.required": "injuries must be entered",
180
+ "string.empty": "injuries cannot be empty",
181
+ })
182
+ ),
183
+ dob: joi.date().empty().optional().messages({
184
+ "date.base": "please enter a valid date",
185
+ }),
186
+ role: joi
187
+ .string()
188
+ .valid(...Object.values(Role))
189
+ .optional()
190
+ .messages({
191
+ "string.base": "please enter a valid role",
192
+ "string.empty": "role cannot be empty",
193
+ }),
194
+ };
195
+
196
+ export const userRegisterSchema = createSchema<IUserRegister>(userRegisterKeys);
src/modules/console/admins/validations/create-admin.validation.ts CHANGED
@@ -1,47 +1,55 @@
1
  import * as joi from "joi";
2
  import { Role } from "../enums/roles.enum";
 
3
 
4
- export const createAdminSchema = joi
5
- .object()
6
- .required()
7
- .keys({
8
- name: joi.string().empty().required().messages({
9
- "string.base": "please enter a valid name",
10
- "any.required": "name is required",
11
- "string.empty": "name can not be empty",
12
- }),
13
- email: joi
14
- .string()
15
- .required()
16
- .email({
17
- minDomainSegments: 2,
18
- tlds: { allow: ["com", "net", "org", "eg", "io"] },
19
- })
20
- .empty()
21
- .messages({
22
- "string.email": "please enter a valid email",
23
- "any.required": "email must be entered",
24
- "string.empty": "email can not be empty",
25
- }),
26
- password: joi.string().empty().min(8).required().messages({
27
- "string.base": "please enter a valid password",
28
- "any.required": "password must be entered",
29
- "string.empty": "password cannot be empty",
30
- "string.min": "password must be at least 8 characters",
31
  }),
32
- dob: joi.date().empty().optional().messages({
33
- "date.base": "please enter a valid date",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
  }),
35
- role: joi
36
- .string()
37
- .valid(...Object.values(Role))
38
- .optional().messages({
39
- "string.base": "please enter a valid role",
40
- "string.empty": "role cannot be empty"
41
- }),
42
- gender: joi.string().empty().required().messages({
43
- "string.base": "please enter a valid gender",
44
- "any.required": "gender must be entered",
45
- "string.empty": "gender cannot be empty"
46
- })
47
- });
 
1
  import * as joi from "joi";
2
  import { Role } from "../enums/roles.enum";
3
+ import { createSchema } from "src/helpers/create-schema";
4
 
5
+ export interface ICreateAdmin {
6
+ name: string;
7
+ email: string;
8
+ password: string;
9
+ dob: Date;
10
+ role?: Role;
11
+ gender: string;
12
+ }
13
+
14
+ export const createAdminSchema = createSchema<ICreateAdmin>({
15
+ name: joi.string().empty().required().messages({
16
+ "string.base": "please enter a valid name",
17
+ "any.required": "name is required",
18
+ "string.empty": "name can not be empty",
19
+ }),
20
+ email: joi
21
+ .string()
22
+ .required()
23
+ .email({
24
+ minDomainSegments: 2,
25
+ tlds: { allow: ["com", "net", "org", "eg", "io"] },
26
+ })
27
+ .empty()
28
+ .messages({
29
+ "string.email": "please enter a valid email",
30
+ "any.required": "email must be entered",
31
+ "string.empty": "email can not be empty",
32
  }),
33
+ password: joi.string().empty().min(8).required().messages({
34
+ "string.base": "please enter a valid password",
35
+ "any.required": "password must be entered",
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()
44
+ .valid(...Object.values(Role))
45
+ .optional()
46
+ .messages({
47
+ "string.base": "please enter a valid role",
48
+ "string.empty": "role cannot be empty",
49
  }),
50
+ gender: joi.string().empty().required().messages({
51
+ "string.base": "please enter a valid gender",
52
+ "any.required": "gender must be entered",
53
+ "string.empty": "gender cannot be empty",
54
+ }),
55
+ });
 
 
 
 
 
 
 
src/modules/console/users/controllers/users.controller.ts CHANGED
@@ -1,9 +1,9 @@
 
1
  import { asyncHandler } from "../../../../helpers/async-handler";
2
  import { jwtHelper } from "../../../../helpers/jwt.helper";
3
  import { bodyValidator } from "../../../../helpers/validation.helper";
4
  import { BaseController } from "../../../../lib/controllers/controller.base";
5
  import { Prefix } from "../../../../lib/decorators/prefix.decorator";
6
- import { userRegisterValidation } from "../../../common/users/validation/user-register.validation";
7
  import { UsersService } from "../services/users.service";
8
 
9
  const allowedRoles = ["superAdmin", "admin"];
@@ -16,7 +16,7 @@ export class AdminUsersController extends BaseController {
16
  this.router.post(
17
  "/create",
18
  jwtHelper.verifyToken(allowedRoles),
19
- bodyValidator(userRegisterValidation),
20
  asyncHandler(this.create)
21
  );
22
  }
 
1
+ import { userRegisterSchema } from "src/modules/common/users/validation/user-register.validation";
2
  import { asyncHandler } from "../../../../helpers/async-handler";
3
  import { jwtHelper } from "../../../../helpers/jwt.helper";
4
  import { bodyValidator } from "../../../../helpers/validation.helper";
5
  import { BaseController } from "../../../../lib/controllers/controller.base";
6
  import { Prefix } from "../../../../lib/decorators/prefix.decorator";
 
7
  import { UsersService } from "../services/users.service";
8
 
9
  const allowedRoles = ["superAdmin", "admin"];
 
16
  this.router.post(
17
  "/create",
18
  jwtHelper.verifyToken(allowedRoles),
19
+ bodyValidator(userRegisterSchema),
20
  asyncHandler(this.create)
21
  );
22
  }
src/modules/user/auth/validation/user.Validation.ts DELETED
@@ -1,26 +0,0 @@
1
- import joi from "joi";
2
-
3
- export const loginValidation = joi
4
- .object()
5
- .required()
6
- .keys({
7
- email: joi
8
- .string()
9
- .required()
10
- .email({
11
- minDomainSegments: 2,
12
- tlds: { allow: ["com", "net", "org", "eg", "io"] },
13
- })
14
- .empty()
15
- .messages({
16
- "string.email": "please enter a valid email",
17
- "any.required": "email must be entered",
18
- "string.empty": "email can not be empty",
19
- }),
20
- password: joi.string().empty().min(8).required().messages({
21
- "string.base": "please enter a valid password",
22
- "any.required": "password must be entered",
23
- "string.empty": "password cannot be empty",
24
- "string.min": "password must be at least 8 characters",
25
- }),
26
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
src/modules/{user → users}/auth/controllers/auth.controller.ts RENAMED
@@ -1,25 +1,25 @@
1
- import { UsersService } from "../services/users.service";
2
  import { jwtHelper } from "../../../../helpers/jwt.helper";
3
  import { BaseController } from "../../../../lib/controllers/controller.base";
4
  import { bodyValidator } from "../../../../helpers/validation.helper";
5
- import { userRegisterValidation } from "../../../common/users/validation/user-register.validation";
6
- import { loginValidation } from "../validation/user.Validation";
7
  import { asyncHandler } from "../../../../helpers/async-handler";
8
  import { Prefix } from "../../../../lib/decorators/prefix.decorator";
 
 
9
 
10
- @Prefix("/user/auth")
11
- export class AuthController extends BaseController {
12
- private usersService = new UsersService();
13
 
14
  setRoutes(): void {
15
  this.router.post(
16
  "/register",
17
- bodyValidator(userRegisterValidation),
18
  asyncHandler(this.register)
19
  );
20
  this.router.post(
21
  "/login",
22
- bodyValidator(loginValidation),
23
  asyncHandler(this.login)
24
  );
25
  }
 
1
+ import { UsersAuthService } from "../services/users.service";
2
  import { jwtHelper } from "../../../../helpers/jwt.helper";
3
  import { BaseController } from "../../../../lib/controllers/controller.base";
4
  import { bodyValidator } from "../../../../helpers/validation.helper";
 
 
5
  import { asyncHandler } from "../../../../helpers/async-handler";
6
  import { Prefix } from "../../../../lib/decorators/prefix.decorator";
7
+ import { loginValidationSchema } from "../validation/user.Validation";
8
+ import { userRegisterSchema } from "src/modules/common/users/validation/user-register.validation";
9
 
10
+ @Prefix("/users/auth")
11
+ export class UsersAuthController extends BaseController {
12
+ private usersService = new UsersAuthService();
13
 
14
  setRoutes(): void {
15
  this.router.post(
16
  "/register",
17
+ bodyValidator(userRegisterSchema),
18
  asyncHandler(this.register)
19
  );
20
  this.router.post(
21
  "/login",
22
+ bodyValidator(loginValidationSchema),
23
  asyncHandler(this.login)
24
  );
25
  }
src/modules/{user → users}/auth/services/users.service.ts RENAMED
@@ -1,7 +1,7 @@
1
  import { UsersBaseService } from "../../../common/users/services/users.base.service";
2
  import bcrypt from "bcrypt";
3
 
4
- export class UsersService extends UsersBaseService {
5
  async comparePassword(email: string, password: string) {
6
  try {
7
  if (email != undefined) {
@@ -34,54 +34,4 @@ export class UsersService extends UsersBaseService {
34
  };
35
  }
36
  }
37
-
38
- // async resetPassword(_id: any, currentPassword: string, newPassword: string, confirmPassword: string) {
39
- // try {
40
- // let user = await UserBaseService.find({ _id })
41
- // let saltrouds = 5;
42
- // let oldPassword = user.record.password;
43
- // let ok = await bcrypt.compare(currentPassword, oldPassword)
44
-
45
- // if (user.success) {
46
-
47
- // if (!ok) return {
48
- // success: false,
49
- // code: 409,
50
- // message: "current password isn't correct"
51
- // };
52
-
53
- // if (newPassword == currentPassword) return {
54
- // success: false,
55
- // code: 409,
56
- // message: "new password must be different from current password"
57
- // };
58
-
59
- // if (newPassword != confirmPassword) return {
60
- // success: false,
61
- // code: 409,
62
- // message: "passwords don't match"
63
- // };
64
-
65
- // const hashedPassword = await bcrypt.hash(newPassword, saltrouds)
66
- // await userModel.findByIdAndUpdate(_id, { password: hashedPassword })
67
- // return {
68
- // success: true,
69
- // code: 200,
70
- // message: "password changed successfully"
71
- // };
72
- // }
73
- // else return {
74
- // success: false,
75
- // code: 404,
76
- // error: user.error
77
- // };
78
- // } catch (err) {
79
- // console.log(`err.message`, err.message);
80
- // return {
81
- // success: false,
82
- // code: 500,
83
- // error: err.message
84
- // };
85
- // }
86
- // }
87
  }
 
1
  import { UsersBaseService } from "../../../common/users/services/users.base.service";
2
  import bcrypt from "bcrypt";
3
 
4
+ export class UsersAuthService extends UsersBaseService {
5
  async comparePassword(email: string, password: string) {
6
  try {
7
  if (email != undefined) {
 
34
  };
35
  }
36
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  }
src/modules/users/auth/validation/user.Validation.ts ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import joi from "joi";
2
+ import { createSchema } from "src/helpers/create-schema";
3
+
4
+ export interface ILogin {
5
+ email: string;
6
+ password: string;
7
+ }
8
+
9
+ export const loginValidationKeys = {
10
+ email: joi
11
+ .string()
12
+ .required()
13
+ .email({
14
+ minDomainSegments: 2,
15
+ tlds: { allow: ["com", "net", "org", "eg", "io"] },
16
+ })
17
+ .empty()
18
+ .messages({
19
+ "string.email": "please enter a valid email",
20
+ "any.required": "email must be entered",
21
+ "string.empty": "email can not be empty",
22
+ }),
23
+ password: joi.string().empty().min(8).required().messages({
24
+ "string.base": "please enter a valid password",
25
+ "any.required": "password must be entered",
26
+ "string.empty": "password cannot be empty",
27
+ "string.min": "password must be at least 8 characters",
28
+ }),
29
+ };
30
+
31
+ export const loginValidationSchema = createSchema<ILogin>(loginValidationKeys);