Implement sending of first translated templates

This commit is contained in:
Wolfgang Huß 2021-08-11 10:29:19 +02:00
parent 0c8287ab03
commit 7e72a0bf03
7 changed files with 169 additions and 11 deletions

View File

@ -14,10 +14,12 @@ let sendMailCallback = async (templateArgs) => {
if (!hasEmailConfig) {
if (!CONFIG.TEST) {
// eslint-disable-next-line no-console
console.log('Warning: Email middleware will not try to send mails.') // Wolle
console.log('Warning: Middlewares will not try to send mails.') // Wolle
}
} else {
sendMailCallback = async (templateArgs) => {
// Wolle
console.log('templateArgs: ', templateArgs)
const transporter = nodemailer.createTransport({
host: CONFIG.SMTP_HOST,
port: CONFIG.SMTP_PORT,

View File

@ -3,6 +3,8 @@ import CONFIG from '../../../config'
import logosWebapp from '../../../config/logos.js'
import * as templates from './templates'
import * as templatesEN from './templates/en'
import * as templatesDE from './templates/de'
const from = CONFIG.EMAIL_DEFAULT_SENDER
const welcomeImageUrl = new URL(logosWebapp.LOGO_WELCOME_PATH, CONFIG.CLIENT_URI)
@ -90,3 +92,33 @@ export const wrongAccountTemplate = ({ email }) => {
),
}
}
export const notificationTemplate = ({ email, notification }) => {
const subject = `${CONFIG.APPLICATION_NAME} Benachrichtigung | Notification`
const actionUrl = new URL('/notifications', CONFIG.CLIENT_URI)
// TODO Wolle language
let content
switch (notification.to.locale) {
case 'de':
content = templatesDE.notification
break
case 'en':
content = templatesEN.notification
break
default:
content = templatesEN.notification
break
}
return {
from,
to: email,
subject,
html: mustache.render(
templates.layout,
{ ...defaultParams, actionUrl, supportUrl: /* Wolle */ CONFIG.SUPPORT_URL, welcomeImageUrl },
{ content },
),
}
}

View File

@ -0,0 +1,6 @@
import fs from 'fs'
import path from 'path'
const readFile = (fileName) => fs.readFileSync(path.join(__dirname, fileName), 'utf-8')
export const notification = readFile('./notification.html')

View File

@ -0,0 +1,55 @@
<!-- Email Body German : BEGIN -->
<table class="email-german" align="center" role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%"
style="margin: auto;">
<!-- Hero Image, Flush : BEGIN -->
<tr>
<td style="background-color: #ffffff;">
<img
src="{{{ welcomeImageUrl }}}"
width="300" height="" alt="Welcome image" border="0"
style="width: 100%; max-width: 300px; height: auto; background: #ffffff; font-family: Lato, sans-serif; font-size: 16px; line-height: 15px; color: #555555; margin: auto; display: block; padding: 20px;"
class="g-img">
</td>
</tr>
<!-- Hero Image, Flush : END -->
<!-- 1 Column Text + Button : BEGIN -->
<tr>
<td style="background-color: #ffffff; padding: 0 20px;">
<table role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%">
<tr>
<td
style="padding: 20px; padding-top: 0; font-family: Lato, sans-serif; font-size: 16px; line-height: 22px; color: #555555;">
<h1
style="margin: 0 0 10px 0; font-family: Lato, sans-serif; font-size: 25px; line-height: 30px; color: #333333; font-weight: normal;">
Hallo {{ name }}!</h1>
<p style="margin: 0;">Du hast mindestens eine Banachrichtigung bekommen! Mit Klick auf diesen Button
kannst Du deine Nachrichten ansehen:</p>
</td>
</tr>
<tr>
<td style="padding: 0 20px;">
<!-- Button : BEGIN -->
<table align="center" role="presentation" cellspacing="0" cellpadding="0" border="0" style="margin: auto;">
<tr>
<td class="button-td button-td-primary" style="border-radius: 4px; background: #17b53e;">
<a class="button-a button-a-primary" href="{{{ actionUrl }}}"
style="background: #17b53e; font-family: Lato, sans-serif; font-size: 16px; line-height: 15px; text-decoration: none; padding: 13px 17px; color: #ffffff; display: block; border-radius: 4px;">Banachrichtigungen
abrufen</a>
</td>
</tr>
</table>
<!-- Button : END -->
</td>
<td style="display: none;">
<p></p>
</td>
</tr>
</table>
</td>
</tr>
<!-- 1 Column Text : END -->
</table>
<!-- Email Body German : END -->

View File

@ -0,0 +1,6 @@
import fs from 'fs'
import path from 'path'
const readFile = (fileName) => fs.readFileSync(path.join(__dirname, fileName), 'utf-8')
export const notification = readFile('./notification.html')

View File

@ -0,0 +1,55 @@
<!-- Email Body German : BEGIN -->
<table class="email-german" align="center" role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%"
style="margin: auto;">
<!-- Hero Image, Flush : BEGIN -->
<tr>
<td style="background-color: #ffffff;">
<img
src="{{{ welcomeImageUrl }}}"
width="300" height="" alt="Welcome image" border="0"
style="width: 100%; max-width: 300px; height: auto; background: #ffffff; font-family: Lato, sans-serif; font-size: 16px; line-height: 15px; color: #555555; margin: auto; display: block; padding: 20px;"
class="g-img">
</td>
</tr>
<!-- Hero Image, Flush : END -->
<!-- 1 Column Text + Button : BEGIN -->
<tr>
<td style="background-color: #ffffff; padding: 0 20px;">
<table role="presentation" cellspacing="0" cellpadding="0" border="0" width="100%">
<tr>
<td
style="padding: 20px; padding-top: 0; font-family: Lato, sans-serif; font-size: 16px; line-height: 22px; color: #555555;">
<h1
style="margin: 0 0 10px 0; font-family: Lato, sans-serif; font-size: 25px; line-height: 30px; color: #333333; font-weight: normal;">
Hello {{ name }}!</h1>
<p style="margin: 0;">You have received at least one notification! By clicking on this button
you can view your messages:</p>
</td>
</tr>
<tr>
<td style="padding: 0 20px;">
<!-- Button : BEGIN -->
<table align="center" role="presentation" cellspacing="0" cellpadding="0" border="0" style="margin: auto;">
<tr>
<td class="button-td button-td-primary" style="border-radius: 4px; background: #17b53e;">
<a class="button-a button-a-primary" href="{{{ actionUrl }}}"
style="background: #17b53e; font-family: Lato, sans-serif; font-size: 16px; line-height: 15px; text-decoration: none; padding: 13px 17px; color: #ffffff; display: block; border-radius: 4px;">Retrieve
notifications</a>
</td>
</tr>
</table>
<!-- Button : END -->
</td>
<td style="display: none;">
<p></p>
</td>
</tr>
</table>
</td>
</tr>
<!-- 1 Column Text : END -->
</table>
<!-- Email Body German : END -->

View File

@ -1,6 +1,8 @@
import { pubsub, NOTIFICATION_ADDED } from '../../server'
import extractMentionedUsers from './mentions/extractMentionedUsers'
import { validateNotifyUsers } from '../validation/validationMiddleware'
import { pubsub, NOTIFICATION_ADDED } from '../../server'
import { sendMail } from '../helpers/email/sendMail'
import { notificationTemplate } from '../helpers/email/templateBuilder'
const queryNotificationsEmails = async (context, notificationUserIds) => {
if (!(notificationUserIds && notificationUserIds.length)) return []
@ -29,12 +31,6 @@ const queryNotificationsEmails = async (context, notificationUserIds) => {
}
}
const sendNotificationEmails = async (notification, email) => {
// Wolle
console.log('sendNotificationEmails !!! notification.to.slug: ', notification.to.slug)
console.log('sendNotificationEmails !!! email: ', email)
}
const publishNotifications = async (context, promises) => {
let notifications = await Promise.all(promises)
notifications = notifications.flat()
@ -48,10 +44,16 @@ const publishNotifications = async (context, promises) => {
console.log('notificationsEmailAddresses: ', notificationsEmailAddresses)
notifications.forEach((notificationAdded, index) => {
pubsub.publish(NOTIFICATION_ADDED, { notificationAdded })
// Wolle
// console.log('notificationAdded: ', notificationAdded)
sendNotificationEmails(notificationAdded, notificationsEmailAddresses[index].email)
// Wolle await
sendMail(
notificationTemplate({
email: notificationsEmailAddresses[index].email,
notification: notificationAdded,
}),
)
})
// Wolle
// return XXX successful?
}
const handleContentDataOfPost = async (resolve, root, args, context, resolveInfo) => {