2019-11-16 19:25:44 -05:00
|
|
|
/* eslint-disable consistent-return */
|
|
|
|
import { Server } from '..';
|
|
|
|
import { Route } from '../../class';
|
2019-11-16 22:55:25 -05:00
|
|
|
import { Req } from '../interfaces';
|
2019-11-16 19:25:44 -05:00
|
|
|
|
|
|
|
export default class Account extends Route {
|
|
|
|
constructor(server: Server) {
|
|
|
|
super(server, { path: '/account', deprecated: false });
|
|
|
|
}
|
|
|
|
|
2019-11-16 19:51:29 -05:00
|
|
|
public bind() {
|
2019-11-16 19:25:44 -05:00
|
|
|
this.router.use(async (req, res, next) => {
|
2019-11-16 22:55:25 -05:00
|
|
|
const account = await this.server.security.checkBearer(this.server.security.extractBearer(req));
|
|
|
|
if (!account) return res.status(401).json({ code: this.constants.codes.UNAUTHORIZED, message: 'BEARER_TOKEN_INVALID' });
|
|
|
|
Object.defineProperty(req, 'account', { value: account, writable: true, enumerable: true, configurable: true });
|
2019-11-16 19:25:44 -05:00
|
|
|
next();
|
|
|
|
});
|
2019-11-16 19:37:45 -05:00
|
|
|
|
2019-11-16 22:55:25 -05:00
|
|
|
this.router.get('/', async (req: Req, res) => {
|
2019-11-16 19:37:45 -05:00
|
|
|
const acc: any = {};
|
2019-11-16 22:55:25 -05:00
|
|
|
acc.username = req.account.username;
|
|
|
|
acc.userID = req.account.userID;
|
|
|
|
acc.email = req.account.emailAddress;
|
|
|
|
acc.locked = req.account.locked;
|
|
|
|
acc.root = req.account.root;
|
|
|
|
acc.createdAt = req.account.createdAt;
|
|
|
|
acc.createdBy = req.account.createdBy;
|
|
|
|
acc.permissions = req.account.permissions;
|
2019-11-16 19:37:45 -05:00
|
|
|
res.status(200).json({ code: this.constants.codes.SUCCESS, message: acc });
|
|
|
|
});
|
2019-11-16 22:55:25 -05:00
|
|
|
|
|
|
|
this.router.get('/moderations/:id', async (req: Req, res) => {
|
|
|
|
const moderations = await this.server.client.db.Moderation.find({ username: req.account.username });
|
|
|
|
if (!moderations.length) res.sendStatus(204);
|
|
|
|
if (req.params.id) {
|
|
|
|
const filtered = moderations.filter((moderation) => moderation.logID === req.params.id);
|
|
|
|
res.status(200).json({ code: this.constants.codes.SUCCESS, message: { filtered } });
|
|
|
|
} else {
|
|
|
|
res.status(200).json({ code: this.constants.codes.SUCCESS, message: moderations });
|
|
|
|
}
|
|
|
|
});
|
2019-11-16 19:25:44 -05:00
|
|
|
}
|
|
|
|
}
|