-
Notifications
You must be signed in to change notification settings - Fork 0
/
inviteInfo.ts
36 lines (28 loc) · 1.2 KB
/
inviteInfo.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
import * as DateFns from 'date-fns';
import * as express from 'express';
import { query, validationResult } from 'express-validator';
import { InviteResponse } from '../../types/endpoints';
import { ApiResponse } from '../apiResponse';
import ChatManager from '../managers/chatManager';
import InviteManager from '../managers/inviteManager';
import allowedMethods from '../middlewares/allowedMethods';
import ensureAuthenticated from '../middlewares/ensureAuthenticated';
const router = express.Router();
router.all('/',
allowedMethods('GET'),
ensureAuthenticated(),
query('key').isString(),
async (req, res) => {
const errors = validationResult(req);
if (!errors.isEmpty()) return new ApiResponse(res).validationError(errors);
const inviteKey = req.query.key;
const invite = await InviteManager.getInviteByKey(inviteKey);
const isExpired = invite && DateFns.isPast(DateFns.fromUnixTime(Number(invite.expireAt)));
if (!invite || isExpired) {
return new ApiResponse(res).badRequest('This invite is invalid or has expired');
}
const chat = await ChatManager.getChat(invite.chatId);
const result: InviteResponse = { invite, chat };
new ApiResponse(res).success(result);
});
export default router;