Skip to content

Commit 32b1b49

Browse files
committedMay 4, 2022
Event Notification remainder Overall Done
1 parent fa18520 commit 32b1b49

File tree

9 files changed

+122
-8
lines changed

9 files changed

+122
-8
lines changed
 

‎res/emails/confirmEmail.html

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ <h3 style="text-align:center; padding-bottom:5px;">
1010
Hey <b>${data.username}</b>,
1111
<br />
1212
Please validate your email address on fairfieldprogramming.org by clicking
13+
<!-- <a href="http://localhost:8080/user/confirmEmail/${id_token}">this link</a>. -->
1314
<a href="https://fairfield-programming.herokuapp.com/user/confirmEmail/${id_token}">this link</a>.
1415
<br />
1516
</p>

‎res/emails/eventNotification.html

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<img src="https://raw.githubusercontent.com/fairfield-programming/.github/main/spread.png"
2+
style="width:90%; margin-left:5%;" />
3+
<hr />
4+
<h3 style="text-align:center; padding-bottom:5px;">
5+
Notification From Fairfield Programming Association
6+
</h3>
7+
<hr />
8+
<p>
9+
<br />
10+
Hey <b>${sub.username}</b>,
11+
<br />
12+
The event named <b> ${event.name}</b> will take place soon. Consider <a href="https://fairfieldprogramming.org/login/">login</a> in the platform to stay up to date now !
13+
<br />
14+
</p>
15+
<p>
16+
<br />
17+
Thanks for joining us !
18+
<br />
19+
Kind Regards.
20+
<br />
21+
<address>fairfieldprogramming.org <b> team </b></address>
22+
</p>
23+
<hr />
24+
<footer style="color:grey">
25+
fairfieldprogramming.org is an open-source,
26+
non-profit association dedicated to the education of children in the world of computer science.
27+
We host competitions, events, and websites in order to forward the learning experience of highschool and college
28+
students.
29+
Since we are a non-profit and an open-source organization, we would love it if you contribute or donate,
30+
but that is fully up to you!
31+
</footer>
32+
<hr />

‎src/constants.js

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
22
MAX_UNCONFIRMED_ACCOUNT_AGE: 30 * 24 * 60 * 60 * 1000, // 30 days
33
EMAIL_CONFIRMATION_REMAINDER_TIMEOUT: 14 * 24 * 60 * 60 * 1000, // 14 days
4+
EVENT_REMAINDER: 1 * 24 * 60 * 60 * 1000, // 1 day
45
};

‎src/controllers/Events/rsvpEvent.js

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const Events = require('../../models/Events');
2-
31

42
/**
53
* @module RSVP Events Controller

‎src/controllers/User/Account/signup.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ module.exports.signup = async (req, res) => {
3737
{
3838
[Op.or]: [
3939
{ username: req.body.username },
40-
{ email: req.body.email }],
40+
{ email: req.body.email }
41+
],
4142
},
4243
},
4344
)

‎src/index.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ require('dotenv').config();
33
// Configure Imports
44
const express = require('express');
55
const { Sequelize } = require('sequelize');
6-
const models = require('./models');
76
const schedule = require('node-schedule');
87
const { removeUnconfirmedAccounts, emailConfirmationRemainder } = require('./jobs/accountCleanup');
8+
const { eventRemainder } = require("./jobs/eventNotifications");
99

1010
// Configure Local Variables
1111
const app = express();
@@ -17,7 +17,7 @@ app.use(express.static('public'));
1717
app.use(express.urlencoded({ extended: true }));
1818
app.use(express.json());
1919
app.use(require("cors")({
20-
origin: "*"
20+
origin: "*"
2121
}));
2222

2323
// Programs
@@ -62,8 +62,18 @@ app.use('/user', require('./routes/userRoutes'));
6262
removeUnconfirmedAccounts();
6363
},
6464
)
65+
schedule.scheduleJob(
66+
"remaind users about the events they are subscribed to",
67+
"0 8 * * *",
68+
() => {
69+
eventRemainder();
70+
},
71+
)
72+
6573
})()
6674

75+
76+
6777
// Start Server
6878
if (process.env.NODE_ENV !== 'test') {
6979
app.listen(port, () => {

‎src/jobs/eventNotifications.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const { mailer } = require('../helpers/mailer');
2+
const fs = require('fs');
3+
const path = require('path');
4+
5+
6+
const {
7+
EVENT_REMAINDER
8+
} = require('../constants');
9+
10+
11+
12+
module.exports.eventRemainder = async () => {
13+
14+
// find events that will take place soon.
15+
16+
const events = await Events.findAll({
17+
where: {
18+
date: {
19+
[Op.lte]: Date.now() + EVENT_REMAINDER,
20+
}
21+
},
22+
attributes: ["id", "ownerId", "name", "date"],
23+
})
24+
25+
26+
if (!events?.length) {
27+
return;
28+
}
29+
30+
events.forEach(async event => {
31+
32+
// find all the subscribers.
33+
const subs = await event.getUsers();
34+
35+
// send an email to all of them.
36+
37+
subs.forEach(sub => {
38+
39+
// build-up the mail markup
40+
let emailData = fs.readFileSync(path.join(process.cwd(), "/res/emails/eventNotification.html"), 'ascii');
41+
42+
emailData = emailData.replace("${event.name}", event.name);
43+
emailData = emailData.replace("${sub.username}", sub.username);
44+
45+
// send the email
46+
mailer(emailData, String(sub.email), "Event Notification | Fairfield Programming Association");
47+
48+
})
49+
50+
51+
});
52+
53+
54+
55+
56+
}

‎src/middleware/verifyLogin.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,28 @@ const { verify } = require('jsonwebtoken');
99
*/
1010

1111
module.exports.verifyLogin = (req, res, next) => {
12-
if (req.cookies.token) {
13-
verify(req.cookies.token, process.env.JWT_KEY, (err, userData) => {
12+
let tokenCookie = req.headers.cookie.split(";").filter(el => el.includes("token"));
13+
console.log("token cookies are equal to", tokenCookie);
14+
tokenCookie = tokenCookie[tokenCookie.length - 1];
15+
console.log("then token cookies is equal to", tokenCookie);
16+
17+
let token = tokenCookie.split("=")[1];
18+
console.log("token is equal to", token);
19+
20+
21+
if (token) {
22+
23+
verify(token, process.env.JWT_KEY, (err, userData) => {
1424
if (err) {
1525
return res.status(400).send(err.message);
1626
}
1727
req.user = userData;
1828
return next();
1929
});
30+
31+
} else {
32+
return res.redirect('/login');
33+
2034
}
2135

22-
return res.redirect('/login');
2336
};

‎src/models/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,6 @@ global.User.belongsToMany(global.User, {
7171
foreignKey: 'blockedId',
7272
});
7373

74+
75+
7476
global.sequelize = sequelize;

0 commit comments

Comments
 (0)
Please sign in to comment.