diff --git a/bun.lockb b/bun.lockb index 4e6edb8..c0bfb78 100644 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/package.json b/package.json index 759968b..d0f4262 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/routes/email.ts b/src/routes/email.ts index d69d54a..ea65e5b 100644 --- a/src/routes/email.ts +++ b/src/routes/email.ts @@ -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,