feat: add ical

This commit is contained in:
Philipp Dormann 2025-03-18 23:57:01 +01:00
parent 658b8d4dd8
commit 2f25937a94
Signed by: philipp
GPG Key ID: 3BB9ADD52DCA4314
3 changed files with 15 additions and 2 deletions

View File

@ -2,11 +2,13 @@ import { createTransport } from "nodemailer";
import { Queue, Worker, QueueEvents } from "bullmq"; import { Queue, Worker, QueueEvents } from "bullmq";
import { config } from "../config/env"; import { config } from "../config/env";
import Redis from "ioredis"; import Redis from "ioredis";
import { Attachment } from "nodemailer/lib/mailer";
interface EmailJob { interface EmailJob {
to: string; to: string;
subject: string; subject: string;
html: string; html: string;
attachments: Attachment[];
text: string; text: string;
} }
@ -40,12 +42,13 @@ const worker = new Worker<EmailJob>(
QUEUE_NAME, QUEUE_NAME,
async (job) => { async (job) => {
await transporter.sendMail({ await transporter.sendMail({
from: config.email.from, from: { address: config.email.from, name: "Lauf für Kaya!" },
replyTo: config.email.replyTo, replyTo: config.email.replyTo,
to: job.data.to, to: job.data.to,
subject: job.data.subject, subject: job.data.subject,
text: job.data.text, text: job.data.text,
html: job.data.html, html: job.data.html,
attachments: job.data.attachments,
}); });
}, },
{ {

View File

@ -5,6 +5,7 @@ import { z } from 'zod'
import { EmailService } from '../services/email' import { EmailService } from '../services/email'
import { getEmailTemplate } from '../templates' import { getEmailTemplate } from '../templates'
import { Language } from '../types' import { Language } from '../types'
import { Attachment } from 'nodemailer/lib/mailer'
const emailRouter = new Hono() const emailRouter = new Hono()
const emailService = new EmailService() const emailService = new EmailService()
@ -22,7 +23,7 @@ async function generateBarcodeDataURL(data) {
emailRouter.post('/', bearerAuth({ token: process.env.AUTHKEY }), zValidator('json', sendEmailSchema), async (c) => { emailRouter.post('/', bearerAuth({ token: process.env.AUTHKEY }), zValidator('json', sendEmailSchema), async (c) => {
let { to, templateName, language, data } = c.req.valid('json') let { to, templateName, language, data } = c.req.valid('json')
const attachments: Attachment[] = []
try { try {
const template = getEmailTemplate(templateName, language as Language) const template = getEmailTemplate(templateName, language as Language)
if (templateName === "welcome") { if (templateName === "welcome") {
@ -32,6 +33,12 @@ emailRouter.post('/', bearerAuth({ token: process.env.AUTHKEY }), zValidator('js
} else { } else {
return c.json({ success: false, error: "required params 'data.name', 'data.link', 'data.barcode_content' not provided" }, 406) return c.json({ success: false, error: "required params 'data.name', 'data.link', 'data.barcode_content' not provided" }, 406)
} }
const attachment: Attachment = {
filename: 'invite.ics',
content: `BEGIN:VCALENDAR\nVERSION:2.0\nPRODID:-//ICS Generator//NONSGML ICS Generator//DE\nBEGIN:VEVENT\nUID:1742337822408-5ghrzyi@icsgenerator.local\nDTSTAMP:20250318T224342Z\nSUMMARY:Lauf für Kaya! 2025\nDTSTART:20250523T110000Z\nDTEND:20250523T160000Z\nDESCRIPTION:Der Lauf für Kaya! 2025 findet am 23.05.2025 auf dem Sportplatz des Gymnasium Herzogenaurach statt. Zur Anmeldung einfach zum Infozelt kommen.\nLOCATION:Sportplatz Gymnasium Herzogenaurach\nBEGIN:VALARM\nACTION:DISPLAY\nDESCRIPTION:Erinnerung: Lauf für Kaya! 2025\nTRIGGER:-PT1440M\nEND:VALARM\nEND:VEVENT\nEND:VCALENDAR`,
contentType: 'text/calendar; method=REQUEST',
}
attachments.push(attachment)
} }
if (templateName === "password-reset") { if (templateName === "password-reset") {
if (data.token) { if (data.token) {
@ -45,6 +52,7 @@ emailRouter.post('/', bearerAuth({ token: process.env.AUTHKEY }), zValidator('js
data.event_name = process.env.EVENT_NAME data.event_name = process.env.EVENT_NAME
await emailService.sendEmail({ await emailService.sendEmail({
to, to,
attachments,
subject: template.subject(data), subject: template.subject(data),
html: template.html(data), html: template.html(data),
text: template.text(data) text: template.text(data)

View File

@ -1,10 +1,12 @@
import { emailQueue } from '../queues/email.queue' import { emailQueue } from '../queues/email.queue'
import { config } from '../config/env' import { config } from '../config/env'
import { Attachment } from 'nodemailer/lib/mailer'
interface EmailOptions { interface EmailOptions {
to: string to: string
subject: string subject: string
html: string html: string
attachments: Attachment[]
text: string text: string
} }