-
Notifications
You must be signed in to change notification settings - Fork 2
/
controller.ts
96 lines (87 loc) · 2.8 KB
/
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
import { NextFunction, Request, Response } from 'express';
import mongoose from 'mongoose';
import User from './User.js';
import * as leagues from './leagues.js';
import yahooAuth from './yahooAuth.js';
export async function index(_req: Request, res: Response) {
res.render('index');
}
export async function signup(req: Request, res: Response) {
const email = req.body.email && req.body.email.trim();
if (!email || !email.length) {
return res.render('index', { errorMessage: 'Email is required!' });
}
const user = await User.findOne({ email }).exec();
if (user) {
return res.render('index', {
errorMessage: `${email} is already signed up to receive notifications!`,
});
}
return res.redirect(yahooAuth.code.getUri({ state: email }));
}
interface IIndexContext {
errorMessage?: string;
successMessage?: string;
}
export async function authCallback(req: Request, res: Response) {
let context: IIndexContext = {};
if (req.query.error) {
throw new Error(req.query.error as string);
}
if (!Object.keys(req.query).length) {
context = {
errorMessage: 'Invalid callback, please try again.',
};
return res.render('index', context);
}
const authUser = await yahooAuth.code.getToken(req.originalUrl);
const user = new User({
email: req.query.state,
accessToken: authUser.accessToken,
expires: (authUser as any).expires, // eslint-disable-line @typescript-eslint/no-explicit-any
refreshToken: authUser.refreshToken,
});
await leagues.update(user);
if (!user.leagues?.length) {
context = {
errorMessage: 'No fantasy football leagues found for your account!',
};
} else {
context = {
successMessage: `All done! You'll start receiving transaction
notifications for ${user.leagues
.map((league) => league.name)
.join(', ')}.`,
};
await user.save();
}
return res.render('index', context);
}
export async function unsubscribe(req: Request, res: Response) {
if (!mongoose.Types.ObjectId.isValid(req.params.id)) {
return res.render('index', { errorMessage: 'Invalid user ID' });
}
const user = await User.findByIdAndDelete(req.params.id);
const context: IIndexContext = {};
if (!user) {
context.errorMessage = 'No email found matching this account';
} else {
context.successMessage = `${user.email} has been unsubscribed`;
}
return res.render('index', context);
}
export function handleError(
err: Error,
_req: Request,
res: Response,
_next: NextFunction,
) {
const context: IIndexContext = { errorMessage: err.message };
if (err.message === 'access_denied') {
context.errorMessage = `You must click "Allow" to authorize Fantasy Notify
to monitor your league's transactions.`;
} else {
console.error(err.stack);
}
res.render('index', context);
}