welcome mails: barcode_content to barcode_url generation with bwip-js
This commit is contained in:
parent
5cf33146fe
commit
75866d9a8d
@ -13,6 +13,7 @@
|
|||||||
"@hono/zod-openapi": "0.18.0",
|
"@hono/zod-openapi": "0.18.0",
|
||||||
"@hono/zod-validator": "0.4.1",
|
"@hono/zod-validator": "0.4.1",
|
||||||
"bullmq": "5.29.0",
|
"bullmq": "5.29.0",
|
||||||
|
"bwip-js": "^4.5.1",
|
||||||
"handlebars": "4.7.8",
|
"handlebars": "4.7.8",
|
||||||
"hono": "4.6.11",
|
"hono": "4.6.11",
|
||||||
"ioredis": "5.4.1",
|
"ioredis": "5.4.1",
|
||||||
|
@ -4,6 +4,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 { toBuffer } from 'bwip-js/node'
|
||||||
|
|
||||||
const emailRouter = new Hono()
|
const emailRouter = new Hono()
|
||||||
const emailService = new EmailService()
|
const emailService = new EmailService()
|
||||||
@ -16,15 +17,30 @@ const sendEmailSchema = z.object({
|
|||||||
data: z.record(z.any())
|
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) => {
|
emailRouter.post('/', zValidator('json', sendEmailSchema), async (c) => {
|
||||||
let { to, subject, templateName, language, data } = c.req.valid('json')
|
let { to, subject, templateName, language, data } = c.req.valid('json')
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const template = getEmailTemplate(templateName, language as Language)
|
const template = getEmailTemplate(templateName, language as Language)
|
||||||
if (templateName === "welcome") {
|
if (templateName === "welcome") {
|
||||||
if (data.barcode_content === "0123456789") {
|
const dataURL = await generateBarcodeDataURL(data.barcode_content);
|
||||||
data.barcode_url = "https://barcodeapi.org/api/128/0123456789?"
|
data.barcode_url = dataURL;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
await emailService.sendEmail({
|
await emailService.sendEmail({
|
||||||
to,
|
to,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user