Skip to content

Commit

Permalink
Add mailing list token controller and routes
Browse files Browse the repository at this point in the history
  • Loading branch information
Mipronimo committed Sep 29, 2024
1 parent 0414571 commit d88bb02
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 4 deletions.
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ import { registration } from './src/controllers/registration.controller.js';
import { findAll as getAllEvents } from './src/controllers/event.controller.js';
import { findAll as getAllSettings } from './src/controllers/setting.controller.js';
import { create as createSupporterYear } from './src/controllers/supporterYear.controller.js';
import { findOne, create } from './src/controllers/mailingListToken.controller.js';

app.get("/", (req, res) => {res.json({ message: "up" });});
app.post('/registration', registration)
app.get('/event', getAllEvents);
app.get('/setting', getAllSettings);
app.post('/supporterYear', createSupporterYear)
app.get('/mailingListToken/:token', findOne)
app.post('/mailingListToken', create)

// protected routes
import keycloak from './src/config/keycloak.js'
Expand Down
3 changes: 2 additions & 1 deletion src/config/mail.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || '[]')
};
40 changes: 37 additions & 3 deletions src/controllers/mail.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,16 @@ export async function addToMailinglist(mailingList, uuids) {
}
uuids.forEach(async (uuid) => {
console.log(uuid);
const user = await userModel.findByPk(uuid);
let user = {};
if (uuid.includes('@')) {
user = {
mail: uuid
}
} else {
user = await userModel.findByPk(uuid);
}
console.log(user);
if (!user) return;
if (!user.mail) return;
mg.lists.members.createMember(mailingList, {
address: user.mail || '',
name: user.firstName || '' + ' ' + user.lastName || '',
Expand All @@ -165,7 +172,34 @@ export async function addToTeamMailinglist(uuids, year) {
addToMailinglist('team' + year + '@' + (process.env.MAIL_LIST_DOMAIN || 'verteiler.lippesola.de'), uuids);
}


export async function sendNewsletterConfirmMail(mailAddress, token) {
const confirmLink = process.env.LAMA_API_URL + '/mailingListToken/' + token;
const html = '<p>'
+ 'Vielen Dank für deine Anmeldung zum Lippesola Newsletter.<br>'
+ 'Bitte bestätige deine Anmeldung durch Klick auf den folgenden Link: <br>'
+ '<a href="' + confirmLink + '">' + confirmLink + '</a>'
+ '</p>'
+ '<p>'
+ 'Solltest du keine Anmeldung vorgenommen haben, kannst du diese E-Mail ignorieren.'
+ '</p>';
const transporter = nodemailer.createTransport({
host: mail.default.host,
port: mail.default.port,
secure: mail.default.secure,
auth: {
user: mail.default.user,
pass: mail.default.pass
}
});
transporter.sendMail({
from: '"Lippesola Newsletter" <' + mail.default.from + '>',
to: mailAddress,
subject: 'Newsletter Anmeldung bestätigen',
text: convert(html),
html: html
});

}

export async function sendMailToParents(orderId, positionId, type) {
const participator = await findOneParticipator({params: {
Expand Down
58 changes: 58 additions & 0 deletions src/controllers/mailingListToken.controller.js
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.")
}
18 changes: 18 additions & 0 deletions src/models/mailingListToken.model.js
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
}
});

0 comments on commit d88bb02

Please sign in to comment.