Created
December 10, 2020 15:47
-
-
Save urbanisierung/b525a158371ea900212b4266de39bed5 to your computer and use it in GitHub Desktop.
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
const nodemailer = require('nodemailer') | |
import * as functions from 'firebase-functions' | |
import { User } from '../../types/User.type' | |
import { MoodController } from '../mood.controller' | |
import { StorageController } from '../storage.controller' | |
import { ZeebeController } from '../zeebe.controller' | |
const PORT = 465 | |
export class MailWorkerController { | |
constructor( | |
private zeebeController: ZeebeController, | |
private store: StorageController, | |
) {} | |
public createWorker(taskType: string) { | |
this.zeebeController.getZeebeClient().createWorker({ | |
taskType, | |
taskHandler: async (job: any, complete: any, worker: any) => { | |
const user: User = { | |
name: job.customHeaders.name, | |
email: job.customHeaders.email, | |
team: job.customHeaders.team, | |
} | |
try { | |
await this.sendMail(user) | |
complete.success() | |
} catch (error) { | |
complete.failure('Failed to send email') | |
} | |
}, | |
}) | |
} | |
private async sendMail(user: User) { | |
const smtpUser = functions.config().email.user | |
const smtpPass = functions.config().email.pass | |
const smtpHost = functions.config().email.host | |
const baseUrl = functions.config().email.baseurl | |
const transporter = nodemailer.createTransport({ | |
host: smtpHost, | |
port: PORT, | |
secure: true, | |
auth: { | |
user: smtpUser, | |
pass: smtpPass, | |
}, | |
}) | |
const subject = `How are you today?` | |
const moodController = new MoodController(this.store) | |
const moodRequest = moodController.createRequest(user) | |
await Promise.all([ | |
moodController.saveMoodRequest(moodRequest), | |
transporter.sendMail({ | |
from: `"Adam" <zeebe@urbanisierung.dev>`, | |
to: user.email, | |
subject, | |
text: `Please consider a modern email client.`, | |
html: this.createEmailHtml(user, baseUrl, moodRequest.uuid), | |
}), | |
]) | |
} | |
private createEmailHtml(user: User, baseUrl: string, moodId: string) { | |
let html = `<p>Hello ${user.name},</p> | |
<p>How do you feel about your day today?</p> | |
<p>Please click in the term which better describes your day:</p> | |
<ul> | |
<li><a href="${baseUrl}/mood/${moodId}/5">Excellent day</a></li> | |
<li><a href="${baseUrl}/mood/${moodId}/4">Good day</a></li> | |
<li><a href="${baseUrl}/mood/${moodId}/3">Average day</a></li> | |
<li><a href="${baseUrl}/mood/${moodId}/2">Hard day</a></li> | |
<li><a href="${baseUrl}/mood/${moodId}/1">Bad day</a></li> | |
</ul> | |
<p>By giving your today's feeling, you'll be able to see the average mood of your teammates.</p> | |
<p>Cheers,</p> | |
<p>Adam</p> | |
` | |
return html | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment