moahmedwafy commited on
Commit
22f0230
·
1 Parent(s): ac9dbd5

refactor: stop using static services

Browse files
src/modules/common/users/services/users.base.service.ts CHANGED
@@ -1,8 +1,7 @@
1
- import { userModel, } from '../models/user.model'
2
 
3
-
4
- export class UsersBaseService {
5
- static async find(filterObject) {
6
  try {
7
  const resultObject = await userModel.findOne(filterObject).lean();
8
 
@@ -28,33 +27,40 @@ export class UsersBaseService {
28
  }
29
  }
30
 
31
- static async create(form: any) {
32
  try {
33
- if (form.email) {
34
- form.email = form.email.toLowerCase()
35
- let user = await this.find({ email: form.email });
36
- if (user.success) return { success: false, error: "This email already exists", code: 409 };
37
- }
38
- let newUser = new userModel(form);
39
- await newUser.save();
40
- return {
41
- success: true,
42
- code: 201
43
- };
44
-
45
- } catch (err) {
46
- console.log(`err.message`, err.message);
47
- return {
48
  success: false,
49
- code: 500,
50
- error: err.message
51
- };
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  }
53
- }
54
 
55
- static async get(filterObject) {
56
  try {
57
- const resultObject = await userModel.findOne(filterObject).lean().select("-password");
 
 
 
58
  if (!resultObject)
59
  return {
60
  success: false,
@@ -76,7 +82,7 @@ export class UsersBaseService {
76
  }
77
  }
78
 
79
- static async list(filterObject) {
80
  try {
81
  const resultArray = await userModel
82
  .find(filterObject)
@@ -105,8 +111,4 @@ export class UsersBaseService {
105
  };
106
  }
107
  }
108
-
109
  }
110
-
111
-
112
-
 
1
+ import { userModel } from "../models/user.model";
2
 
3
+ export abstract class UsersBaseService {
4
+ async find(filterObject) {
 
5
  try {
6
  const resultObject = await userModel.findOne(filterObject).lean();
7
 
 
27
  }
28
  }
29
 
30
+ async create(form: any) {
31
  try {
32
+ if (form.email) {
33
+ form.email = form.email.toLowerCase();
34
+ let user = await this.find({ email: form.email });
35
+ if (user.success)
36
+ return {
 
 
 
 
 
 
 
 
 
 
37
  success: false,
38
+ error: "This email already exists",
39
+ code: 409,
40
+ };
41
+ }
42
+ let newUser = new userModel(form);
43
+ await newUser.save();
44
+ return {
45
+ success: true,
46
+ code: 201,
47
+ };
48
+ } catch (err) {
49
+ console.log(`err.message`, err.message);
50
+ return {
51
+ success: false,
52
+ code: 500,
53
+ error: err.message,
54
+ };
55
  }
56
+ }
57
 
58
+ async get(filterObject) {
59
  try {
60
+ const resultObject = await userModel
61
+ .findOne(filterObject)
62
+ .lean()
63
+ .select("-password");
64
  if (!resultObject)
65
  return {
66
  success: false,
 
82
  }
83
  }
84
 
85
+ async list(filterObject) {
86
  try {
87
  const resultArray = await userModel
88
  .find(filterObject)
 
111
  };
112
  }
113
  }
 
114
  }
 
 
 
src/modules/console/admins/controllers/admins.controller.ts CHANGED
@@ -10,6 +10,8 @@ import {
10
 
11
  @Prefix("/console/admins")
12
  export class AdminsController extends BaseController {
 
 
13
  setRoutes() {
14
  this.router.get("/", this.list);
15
  this.router.get("/:id", paramsValidator("id"), this.get);
@@ -23,35 +25,36 @@ export class AdminsController extends BaseController {
23
  this.router.delete("/:id", paramsValidator("id"), this.delete);
24
  }
25
 
26
- list(_, res: Response) {
27
- AdminsService.list({})
 
28
  .then((result) => {
29
  res.status(result.code).json(result);
30
  })
31
  .catch((err) => {
32
  res.status(500).json(err);
33
  });
34
- }
35
 
36
- async get(req: Request, res: Response) {
37
- const data = await AdminsService.get({
38
  _id: req.params.id,
39
  });
40
  res.json(data);
41
- }
42
 
43
- async create(req: Request, res: Response) {
44
- const data = await AdminsService.create(req.body);
45
  res.json(data);
46
- }
47
 
48
- async update(req: Request, res: Response) {
49
- const data = await AdminsService.update(req.params.id, req.body);
50
  res.json(data);
51
- }
52
 
53
- async delete(req: Request, res: Response) {
54
- const data = await AdminsService.remove(req.params.id);
55
  res.json(data);
56
- }
57
  }
 
10
 
11
  @Prefix("/console/admins")
12
  export class AdminsController extends BaseController {
13
+ private adminsService = new AdminsService();
14
+
15
  setRoutes() {
16
  this.router.get("/", this.list);
17
  this.router.get("/:id", paramsValidator("id"), this.get);
 
25
  this.router.delete("/:id", paramsValidator("id"), this.delete);
26
  }
27
 
28
+ list = (_, res: Response) => {
29
+ this.adminsService
30
+ .list({})
31
  .then((result) => {
32
  res.status(result.code).json(result);
33
  })
34
  .catch((err) => {
35
  res.status(500).json(err);
36
  });
37
+ };
38
 
39
+ get = async (req: Request, res: Response) => {
40
+ const data = await this.adminsService.get({
41
  _id: req.params.id,
42
  });
43
  res.json(data);
44
+ };
45
 
46
+ create = async (req: Request, res: Response) => {
47
+ const data = await this.adminsService.create(req.body);
48
  res.json(data);
49
+ };
50
 
51
+ update = async (req: Request, res: Response) => {
52
+ const data = await this.adminsService.update(req.params.id, req.body);
53
  res.json(data);
54
+ };
55
 
56
+ delete = async (req: Request, res: Response) => {
57
+ const data = await this.adminsService.remove(req.params.id);
58
  res.json(data);
59
+ };
60
  }
src/modules/console/admins/services/admins.service.ts CHANGED
@@ -4,7 +4,7 @@ import { config } from "../../../../configs/config";
4
  import { FilterQuery } from "mongoose";
5
 
6
  export class AdminsService {
7
- static async find(filterObject) {
8
  try {
9
  const resultObject = await Admin.findOne(filterObject).lean();
10
 
@@ -30,7 +30,7 @@ export class AdminsService {
30
  }
31
  }
32
 
33
- static async get(filterObject: FilterQuery<IAdmin>) {
34
  try {
35
  const resultObject = await Admin.findOne(filterObject)
36
  .lean()
@@ -56,7 +56,7 @@ export class AdminsService {
56
  }
57
  }
58
 
59
- static async list(filterObject) {
60
  try {
61
  const resultArray = await Admin.find(filterObject)
62
  .lean()
@@ -85,7 +85,7 @@ export class AdminsService {
85
  }
86
  }
87
 
88
- static async create(formObject) {
89
  try {
90
  if (formObject.email) formObject.email = formObject.email.toLowerCase();
91
  const resultObject = new Admin(formObject);
@@ -113,7 +113,7 @@ export class AdminsService {
113
  }
114
  }
115
 
116
- static async update(_id, formObject) {
117
  try {
118
  const existingObject = await this.find({ _id });
119
  if (!existingObject.success)
@@ -161,7 +161,7 @@ export class AdminsService {
161
  }
162
  }
163
 
164
- static async remove(_id) {
165
  try {
166
  const resultObject = await Admin.findByIdAndDelete({ _id });
167
  if (!resultObject)
@@ -185,7 +185,7 @@ export class AdminsService {
185
  }
186
  }
187
 
188
- static async comparePassword(emailString, passwordString) {
189
  try {
190
  emailString = emailString.toLowerCase();
191
  const existingObject = await this.find({ email: emailString });
@@ -223,7 +223,7 @@ export class AdminsService {
223
  }
224
  }
225
 
226
- static async resetPassword(emailString, newPasswordString) {
227
  try {
228
  emailString = emailString.toLowerCase();
229
  const existingObject = await this.find({ email: emailString });
 
4
  import { FilterQuery } from "mongoose";
5
 
6
  export class AdminsService {
7
+ async find(filterObject) {
8
  try {
9
  const resultObject = await Admin.findOne(filterObject).lean();
10
 
 
30
  }
31
  }
32
 
33
+ async get(filterObject: FilterQuery<IAdmin>) {
34
  try {
35
  const resultObject = await Admin.findOne(filterObject)
36
  .lean()
 
56
  }
57
  }
58
 
59
+ async list(filterObject) {
60
  try {
61
  const resultArray = await Admin.find(filterObject)
62
  .lean()
 
85
  }
86
  }
87
 
88
+ async create(formObject) {
89
  try {
90
  if (formObject.email) formObject.email = formObject.email.toLowerCase();
91
  const resultObject = new Admin(formObject);
 
113
  }
114
  }
115
 
116
+ async update(_id, formObject) {
117
  try {
118
  const existingObject = await this.find({ _id });
119
  if (!existingObject.success)
 
161
  }
162
  }
163
 
164
+ async remove(_id) {
165
  try {
166
  const resultObject = await Admin.findByIdAndDelete({ _id });
167
  if (!resultObject)
 
185
  }
186
  }
187
 
188
+ async comparePassword(emailString, passwordString) {
189
  try {
190
  emailString = emailString.toLowerCase();
191
  const existingObject = await this.find({ email: emailString });
 
223
  }
224
  }
225
 
226
+ async resetPassword(emailString, newPasswordString) {
227
  try {
228
  emailString = emailString.toLowerCase();
229
  const existingObject = await this.find({ email: emailString });
src/modules/console/users/controllers/users.controller.ts CHANGED
@@ -3,12 +3,14 @@ import { bodyValidator } from "../../../../helpers/validation.helper";
3
  import { BaseController } from "../../../../lib/controllers/controller.base";
4
  import { Prefix } from "../../../common/decorators/prefix.decorator";
5
  import { userRegisterValidation } from "../../../common/users/validation/user.baseValidation";
6
- import { usersService } from "../services/users.service";
7
 
8
  const allowedRoles = ["superAdmin", "admin"];
9
 
10
  @Prefix("/console/users")
11
  export class AdminUsersController extends BaseController {
 
 
12
  setRoutes() {
13
  this.router.post(
14
  "/create",
@@ -18,9 +20,9 @@ export class AdminUsersController extends BaseController {
18
  );
19
  }
20
 
21
- async create(req, res) {
22
  try {
23
- let result = await usersService.create(req.body);
24
  return res.status(result.code).json(result);
25
  } catch (err) {
26
  console.log(`err.message`, err.message);
@@ -30,5 +32,5 @@ export class AdminUsersController extends BaseController {
30
  error: err.message,
31
  });
32
  }
33
- }
34
  }
 
3
  import { BaseController } from "../../../../lib/controllers/controller.base";
4
  import { Prefix } from "../../../common/decorators/prefix.decorator";
5
  import { userRegisterValidation } from "../../../common/users/validation/user.baseValidation";
6
+ import { UsersService } from "../services/users.service";
7
 
8
  const allowedRoles = ["superAdmin", "admin"];
9
 
10
  @Prefix("/console/users")
11
  export class AdminUsersController extends BaseController {
12
+ private usersService: UsersService = new UsersService();
13
+
14
  setRoutes() {
15
  this.router.post(
16
  "/create",
 
20
  );
21
  }
22
 
23
+ create = async (req, res) => {
24
  try {
25
+ let result = await this.usersService.create(req.body);
26
  return res.status(result.code).json(result);
27
  } catch (err) {
28
  console.log(`err.message`, err.message);
 
32
  error: err.message,
33
  });
34
  }
35
+ };
36
  }
src/modules/console/users/services/users.service.ts CHANGED
@@ -1,3 +1,3 @@
1
  import { UsersBaseService } from "../../../common/users/services/users.base.service";
2
 
3
- export class usersService extends UsersBaseService {}
 
1
  import { UsersBaseService } from "../../../common/users/services/users.base.service";
2
 
3
+ export class UsersService extends UsersBaseService {}
src/modules/user/auth/controllers/auth.controller.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { usersService } from "../services/users.service";
2
  import { jwtHelper } from "../../../../helpers/jwt.helper";
3
  import { BaseController } from "../../../../lib/controllers/controller.base";
4
  import { Prefix } from "../../../common/decorators/prefix.decorator";
@@ -8,6 +8,8 @@ import { loginValidation } from "../validation/user.Validation";
8
 
9
  @Prefix("/user/auth")
10
  export class AuthController extends BaseController {
 
 
11
  setRoutes(): void {
12
  this.router.post(
13
  "/register",
@@ -17,9 +19,9 @@ export class AuthController extends BaseController {
17
  this.router.post("/login", bodyValidator(loginValidation), this.login);
18
  }
19
 
20
- async register(req, res) {
21
  try {
22
- let result = await usersService.create(req.body);
23
  return res.status(result.code).json(result);
24
  } catch (err) {
25
  console.log(`err.message`, err.message);
@@ -29,9 +31,9 @@ export class AuthController extends BaseController {
29
  error: err.message,
30
  });
31
  }
32
- }
33
 
34
- async login(req, res) {
35
  try {
36
  const { email, password } = req.body;
37
  let result: {
@@ -39,7 +41,7 @@ export class AuthController extends BaseController {
39
  code: number;
40
  record?: any;
41
  message?: string;
42
- } = await usersService.comparePassword(email, password);
43
  if (!result.success) return res.status(result.code).json(result);
44
  let payload = {
45
  _id: result.record?._id,
@@ -62,5 +64,5 @@ export class AuthController extends BaseController {
62
  message: err.message,
63
  });
64
  }
65
- }
66
  }
 
1
+ import { UsersService } from "../services/users.service";
2
  import { jwtHelper } from "../../../../helpers/jwt.helper";
3
  import { BaseController } from "../../../../lib/controllers/controller.base";
4
  import { Prefix } from "../../../common/decorators/prefix.decorator";
 
8
 
9
  @Prefix("/user/auth")
10
  export class AuthController extends BaseController {
11
+ private usersService = new UsersService();
12
+
13
  setRoutes(): void {
14
  this.router.post(
15
  "/register",
 
19
  this.router.post("/login", bodyValidator(loginValidation), this.login);
20
  }
21
 
22
+ register = async (req, res) => {
23
  try {
24
+ let result = await this.usersService.create(req.body);
25
  return res.status(result.code).json(result);
26
  } catch (err) {
27
  console.log(`err.message`, err.message);
 
31
  error: err.message,
32
  });
33
  }
34
+ };
35
 
36
+ login = async (req, res) => {
37
  try {
38
  const { email, password } = req.body;
39
  let result: {
 
41
  code: number;
42
  record?: any;
43
  message?: string;
44
+ } = await this.usersService.comparePassword(email, password);
45
  if (!result.success) return res.status(result.code).json(result);
46
  let payload = {
47
  _id: result.record?._id,
 
64
  message: err.message,
65
  });
66
  }
67
+ };
68
  }
src/modules/user/auth/services/users.service.ts CHANGED
@@ -1,92 +1,87 @@
1
  import { UsersBaseService } from "../../../common/users/services/users.base.service";
2
- import { userModel, } from "../../../common/users/models/user.model";
3
  import bcrypt from "bcrypt";
4
 
5
- export class usersService extends UsersBaseService {
 
 
 
 
 
 
 
6
 
7
- static async comparePassword(email: string, password: string) {
8
- try {
9
- if (email != undefined) {
10
- email = email.toLowerCase();
11
- }
12
- let result = await UsersBaseService.find({ email })
13
- if (!result.success) return result;
14
 
15
- let match = await bcrypt.compare(password, result.record.password)
16
- delete result.record.password;
 
 
 
 
17
 
18
- if (!match) return {
19
- success: false,
20
- code: 409,
21
- message: "password isn't correct"
22
- }
23
-
24
- return {
25
- success: true,
26
- code: 200,
27
- record: result.record
28
- }
29
-
30
- } catch (err) {
31
- console.log(`err.message`, err.message);
32
- return {
33
- success: false,
34
- code: 500,
35
- error: err.message
36
- };
37
- }
38
  }
 
39
 
40
- // static async resetPassword(_id: any, currentPassword: string, newPassword: string, confirmPassword: string) {
41
- // try {
42
- // let user = await UserBaseService.find({ _id })
43
- // let saltrouds = 5;
44
- // let oldPassword = user.record.password;
45
- // let ok = await bcrypt.compare(currentPassword, oldPassword)
46
 
47
- // if (user.success) {
48
 
49
- // if (!ok) return {
50
- // success: false,
51
- // code: 409,
52
- // message: "current password isn't correct"
53
- // };
54
 
55
- // if (newPassword == currentPassword) return {
56
- // success: false,
57
- // code: 409,
58
- // message: "new password must be different from current password"
59
- // };
60
 
61
- // if (newPassword != confirmPassword) return {
62
- // success: false,
63
- // code: 409,
64
- // message: "passwords don't match"
65
- // };
66
 
67
- // const hashedPassword = await bcrypt.hash(newPassword, saltrouds)
68
- // await userModel.findByIdAndUpdate(_id, { password: hashedPassword })
69
- // return {
70
- // success: true,
71
- // code: 200,
72
- // message: "password changed successfully"
73
- // };
74
- // }
75
- // else return {
76
- // success: false,
77
- // code: 404,
78
- // error: user.error
79
- // };
80
- // } catch (err) {
81
- // console.log(`err.message`, err.message);
82
- // return {
83
- // success: false,
84
- // code: 500,
85
- // error: err.message
86
- // };
87
- // }
88
- // }
89
  }
90
-
91
-
92
-
 
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) {
8
+ email = email.toLowerCase();
9
+ }
10
+ let result = await this.find({ email });
11
+ if (!result.success) return result;
12
 
13
+ let match = await bcrypt.compare(password, result.record.password);
14
+ delete result.record.password;
 
 
 
 
 
15
 
16
+ if (!match)
17
+ return {
18
+ success: false,
19
+ code: 409,
20
+ message: "password isn't correct",
21
+ };
22
 
23
+ return {
24
+ success: true,
25
+ code: 200,
26
+ record: result.record,
27
+ };
28
+ } catch (err) {
29
+ console.log(`err.message`, err.message);
30
+ return {
31
+ success: false,
32
+ code: 500,
33
+ error: err.message,
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
  }