welcome mails: barcode_content to barcode_url generation with bwip-js

This commit is contained in:
Philipp Dormann 2024-12-02 17:14:18 +01:00
parent 5cf33146fe
commit 75866d9a8d
Signed by: philipp
GPG Key ID: 3BB9ADD52DCA4314
3 changed files with 20 additions and 3 deletions

BIN
bun.lockb

Binary file not shown.

View File

@ -13,6 +13,7 @@
"@hono/zod-openapi": "0.18.0",
"@hono/zod-validator": "0.4.1",
"bullmq": "5.29.0",
"bwip-js": "^4.5.1",
"handlebars": "4.7.8",
"hono": "4.6.11",
"ioredis": "5.4.1",

View File

@ -4,6 +4,7 @@ import { z } from 'zod'
import { EmailService } from '../services/email'
import { getEmailTemplate } from '../templates'
import { Language } from '../types'
import { toBuffer } from 'bwip-js/node'
const emailRouter = new Hono()
const emailService = new EmailService()
@ -16,15 +17,30 @@ const sendEmailSchema = z.object({
data: z.record(z.any())
})
async function generateBarcodeDataURL(data) {
const buffer = await toBuffer({
bcid: 'code128',
text: data,
scale: 3,
height: 10,
includetext: true,
textxalign: 'center',
});
const base64Data = buffer.toString('base64');
const dataURL = `data:image/png;base64,${base64Data}`;
return dataURL;
}
emailRouter.post('/', zValidator('json', sendEmailSchema), async (c) => {
let { to, subject, templateName, language, data } = c.req.valid('json')
try {
const template = getEmailTemplate(templateName, language as Language)
if (templateName === "welcome") {
if (data.barcode_content === "0123456789") {
data.barcode_url = "https://barcodeapi.org/api/128/0123456789?"
}
const dataURL = await generateBarcodeDataURL(data.barcode_content);
data.barcode_url = dataURL;
}
await emailService.sendEmail({
to,