-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mailing list token controller and routes
- Loading branch information
Showing
5 changed files
with
118 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,5 +20,6 @@ export default { | |
from: process.env.SMTP_BOOKING_FROM || process.env.SMTP_FROM || "[email protected]", | ||
pass: process.env.SMTP_BOOKING_PASS || process.env.SMTP_PASS || "password", | ||
secure: process.env.SMTP_BOOKING_SECURE || process.env.SMTP_SECURE || false, | ||
} | ||
}, | ||
subscribeLists: JSON.parse(process.env.SUBSCRIBE_LISTS || '[]') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import mailingListTokenModel from '../models/mailingListToken.model.js' | ||
import { addToMailinglist, sendNewsletterConfirmMail } from './mail.controller.js'; | ||
import mail from '../config/mail.js'; | ||
|
||
function generateToken(length) { | ||
let result = ''; | ||
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
const charactersLength = characters.length; | ||
let counter = 0; | ||
while (counter < length) { | ||
result += characters.charAt(Math.floor(Math.random() * charactersLength)); | ||
counter += 1; | ||
} | ||
return result; | ||
} | ||
|
||
export async function create (req, res) { | ||
if (!req.body || !req.body.mail || !req.body.list) { | ||
res.status(400).send('bad request') | ||
return; | ||
} | ||
if (!mail.subscribeLists.includes(req.body.list)) { | ||
res.status(403).send('you are not allowed to subscribe to this list') | ||
return; | ||
} | ||
const token = generateToken(32) | ||
await mailingListTokenModel.create({ | ||
token: token, | ||
mail: req.body.mail, | ||
list: req.body.list, | ||
valid: true | ||
}) | ||
sendNewsletterConfirmMail(req.body.mail, token) | ||
res.status(200).send() | ||
} | ||
|
||
export async function findOne (req, res) { | ||
if (!req.params || !req.params.token) { | ||
res.status(400).send('bad request') | ||
return; | ||
} | ||
const mailingListToken = await mailingListTokenModel.findByPk(req.params.token) | ||
if (!mailingListToken) { | ||
res.status(404).send('not found') | ||
return; | ||
} | ||
if (!mailingListToken.valid) { | ||
res.status(403).send('invalid mailingListToken') | ||
return; | ||
} | ||
if (!mail.subscribeLists.includes(mailingListToken.list)) { | ||
res.status(403).send('you are not allowed to subscribe to this list') | ||
return; | ||
} | ||
await addToMailinglist(mailingListToken.list, mailingListToken.mail) | ||
await mailingListToken.update({valid: false}) | ||
res.status(200).send("Du wurdest erfolgreich in den Verteiler eingetragen. Dieses Fenster kann geschlossen werden.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { DataTypes } from 'sequelize'; | ||
import sequelize from './db.model.js'; | ||
|
||
export default sequelize.define('MailingListToken', { | ||
token: { | ||
type: DataTypes.STRING, | ||
primaryKey: true, | ||
}, | ||
list: { | ||
type: DataTypes.STRING | ||
}, | ||
mail: { | ||
type: DataTypes.STRING | ||
}, | ||
valid: { | ||
type: DataTypes.BOOLEAN | ||
} | ||
}); |