-
Notifications
You must be signed in to change notification settings - Fork 1
/
super.controller.ts
108 lines (97 loc) · 4.83 KB
/
super.controller.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import { Router, Request, Response, NextFunction } from 'express';
import userModel from '../model/user.model';
import IUser from '../interface/user.interface';
import Controller from '../interface/controller.interface';
import authMiddleware from '../middleware/auth.middleware';
import UserNotFoundException from '../exception/UserNotFoundException';
import HttpException from '../exception/HttpException';
import superMiddleware from '../middleware/super.middleware';
import AuthService from '../service/auth.service';
import logger from '../util/logger';
import { ANCHOR_PATH, COUNT_PATH, DISABLE_PATH, ID_PATH, REGISTER_PATH, SUPER_PATH, USER_PATH } from '../util/common';
import RequestWithUser from '../interface/requestWithUser.interface';
import { ANCHOR_LIMIT_MAX } from '../util/constants';
import AnchorService from '../service/anchor.service';
class SuperController implements Controller {
public path = `/${SUPER_PATH}`;
public router = Router();
private userModel = userModel;
private anchorService = new AnchorService();
constructor() {
this.initializeRoutes();
}
private initializeRoutes() {
this.router.get(`${this.path}/${USER_PATH}/${ID_PATH}/:id`, authMiddleware, superMiddleware, this.getUserById);
this.router.get(`${this.path}/${USER_PATH}`, authMiddleware, superMiddleware, this.getUsers);
this.router.get(`${this.path}/${USER_PATH}/${COUNT_PATH}`, authMiddleware, superMiddleware, this.getUserCount);
this.router.post(`${this.path}/${REGISTER_PATH}`, authMiddleware, superMiddleware, this.register);
this.router.put(`${this.path}/${USER_PATH}/${ID_PATH}/:id`, authMiddleware, superMiddleware, this.updateUser);
this.router.post(`${this.path}/${USER_PATH}/${DISABLE_PATH}/${ID_PATH}/:id`, authMiddleware, superMiddleware, this.disableUser);
this.router.delete(`${this.path}/${USER_PATH}/${ID_PATH}/:id`, authMiddleware, superMiddleware, this.deleteUser);
this.router.post(`${this.path}/${ANCHOR_PATH}`, authMiddleware, superMiddleware, this.anchor);
}
private getUserById = async (req: Request, res: Response, next: NextFunction) => {
const user = await this.userModel.findById(req.params.id).populate('roles', '-_id -__v')
.catch(() => {
return next(new UserNotFoundException(req.params.id));
});
return res.status(200).json(user);
}
private getUsers = async (req: Request, res: Response, next: NextFunction) => {
const users = await this.userModel.find({}).populate('roles', '-_id -__v')
.catch((error: string) => {
next(new UserNotFoundException(error));
});
return res.status(200).json(users);
}
private getUserCount = async (req: Request, res: Response, next: NextFunction) => {
const userCount = await this.userModel.countDocuments({})
.catch((error: string) => {
next(new HttpException(400, error));
});
return res.status(200).json(userCount);
}
private register = async (req: Request, res: Response, next: NextFunction) => {
const userData: IUser = req.body;
const authService: AuthService = await AuthService.getInstance();
const user = await authService.register(userData)
.catch((error: string) => {
next(new HttpException(400, error));
});
return res.status(201).json(user);
}
private updateUser = async (req: Request, res: Response, next: NextFunction) => {
const userData: IUser = req.body;
const user = await this.userModel.findOneAndUpdate({ _id: req.params.id }, { username: userData.username, password: userData.password })
.catch((error: string) => {
next(new HttpException(400, error));
});
return res.status(200).json(user);
}
private disableUser = async (req: Request, res: Response, next: NextFunction) => {
const user = await this.userModel.findOneAndUpdate({ _id: req.params.id }, { enabled: false })
.catch((error: string) => {
next(new HttpException(400, error));
});
logger.log({ level: 'info', message: `user disabled: ${user}` });
return res.status(200).json(user);
}
private deleteUser = async (req: Request, res: Response, next: NextFunction) => {
const user = await this.userModel.findOneAndRemove({ _id: req.params.id })
.catch((error: string) => {
next(new HttpException(400, error));
});
return res.status(200).json(user);
}
private anchor = async (req: RequestWithUser, res: Response, next: NextFunction) => {
if (!req.body.limit || isNaN(+req.body.limit) || req.body.limit < 1 || req.body.limit > ANCHOR_LIMIT_MAX) {
return next(new HttpException(400, `Limit must be between 1 - ${ANCHOR_LIMIT_MAX}`));
}
const ret = await this.anchorService.anchor(req.body.provider, req.body.limit)
.catch((error: Error) => {
next(new HttpException(400, error.message));
});
res.status(200).json(ret);
};
}
export default SuperController;