email template fo rreplacements
This commit is contained in:
@ -4,8 +4,10 @@ import { getToken } from "next-auth/jwt";
|
|||||||
import type { NextApiRequest, NextApiResponse } from 'next';
|
import type { NextApiRequest, NextApiResponse } from 'next';
|
||||||
import { createRouter, expressWrapper } from "next-connect";
|
import { createRouter, expressWrapper } from "next-connect";
|
||||||
const common = require('../../src/helpers/common');
|
const common = require('../../src/helpers/common');
|
||||||
const email = require('../../src/helpers/');
|
const emailHelper = require('../../src/helpers/email');
|
||||||
|
|
||||||
|
import fs from 'fs';
|
||||||
|
import path from 'path';
|
||||||
const handlebars = require("handlebars");
|
const handlebars = require("handlebars");
|
||||||
|
|
||||||
const router = createRouter<NextApiRequest, NextApiResponse>();
|
const router = createRouter<NextApiRequest, NextApiResponse>();
|
||||||
@ -51,7 +53,7 @@ export default async function handler(req, res) {
|
|||||||
if (!user) {
|
if (!user) {
|
||||||
return res.status(404).json({ message: "User not found" });
|
return res.status(404).json({ message: "User not found" });
|
||||||
}
|
}
|
||||||
|
var emailaction = req.query.emailaction;
|
||||||
switch (action) {
|
switch (action) {
|
||||||
case "send_coverme_request":
|
case "send_coverme_request":
|
||||||
// Send CoverMe request to the user
|
// Send CoverMe request to the user
|
||||||
@ -72,13 +74,52 @@ export default async function handler(req, res) {
|
|||||||
//send email to all subscribed publishers
|
//send email to all subscribed publishers
|
||||||
for (let i = 0; i < subscribedPublishers.length; i++) {
|
for (let i = 0; i < subscribedPublishers.length; i++) {
|
||||||
//send email to subscribed publisher
|
//send email to subscribed publisher
|
||||||
//send email to subscribed publisher
|
let shift = await prisma.shift.findUnique({
|
||||||
const emailResponse = await common.sendEmail(subscribedPublishers[i].email, "CoverMe Request",
|
where: {
|
||||||
"User: " + user.email + " sent a CoverMe request: " +
|
id: parseInt(shiftId)
|
||||||
shiftId + " " + assignmentId + " " + date);
|
},
|
||||||
|
include: {
|
||||||
|
cartEvent: {
|
||||||
|
include: {
|
||||||
|
location: true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
let acceptUrl = process.env.NEXTAUTH_URL + "/api/emailActions?action=coverme_accept&userId=" + user.id + "&shiftId=" + shiftId + "&assignmentId=" + assignmentId;
|
||||||
|
|
||||||
|
let model = {
|
||||||
|
user: user,
|
||||||
|
shiftId: shiftId,
|
||||||
|
assignmentId: assignmentId,
|
||||||
|
acceptUrl: acceptUrl,
|
||||||
|
prefix: subscribedPublishers[i].isMale ? "Брат" : "Сестра",
|
||||||
|
firstName: subscribedPublishers[i].firstName,
|
||||||
|
lastName: subscribedPublishers[i].lastName,
|
||||||
|
placeName: shift.cartEvent.location.name,
|
||||||
|
dateStr: date.toLocaleDateString(),
|
||||||
|
sentDate: date.toLocaleTimeString()
|
||||||
|
};
|
||||||
|
emailHelper.SendEmailHandlebars(subscribedPublishers[i].email, "coverMe", model);
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
case "email_response":
|
||||||
|
//get email action
|
||||||
|
if (!emailaction) {
|
||||||
|
return res.status(400).json({ message: "Email action is not provided" });
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return res.status(400).json({ message: "Invalid action" });
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if (action !== "email_response") {
|
||||||
|
switch (emailaction) {
|
||||||
case "coverme_accept":
|
case "coverme_accept":
|
||||||
// Update the user status to accepted
|
// Update the user status to accepted
|
||||||
console.log("User: " + user.firstName + " " + user.lastName + " accepted the CoverMe request");
|
console.log("User: " + user.firstName + " " + user.lastName + " accepted the CoverMe request");
|
||||||
@ -102,9 +143,6 @@ export default async function handler(req, res) {
|
|||||||
return res.status(400).json({ message: "Shift ID does not match" });
|
return res.status(400).json({ message: "Shift ID does not match" });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// await prisma.user.update({
|
// await prisma.user.update({
|
||||||
// where: {
|
// where: {
|
||||||
// id: parseInt(userId)
|
// id: parseInt(userId)
|
||||||
@ -146,9 +184,10 @@ export default async function handler(req, res) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
default:
|
// //send email response to the user
|
||||||
return res.status(400).json({ message: "Invalid action" });
|
// const emailResponse = await common.sendEmail(user.email, "Email Action Processed",
|
||||||
|
// "Your email action was processed successfully");
|
||||||
}
|
}
|
||||||
return res.status(200).json({ message: "User action processed" });
|
return res.status(200).json({ message: "User action processed" });
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,53 @@ exports.SendEmail = async function (to, subject, text, html) {
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.SendEmailHandlebars = async function (to, templateName, model) {
|
||||||
|
// Ensure the sender and mailtrapTestClient are correctly defined or imported
|
||||||
|
|
||||||
|
// Load and compile the main template
|
||||||
|
const mainTemplateSource = fs.readFileSync(path.join(__dirname, 'src', 'templates', 'emails', 'main.hbs'), 'utf8');
|
||||||
|
const mainTemplate = Handlebars.compile(mainTemplateSource);
|
||||||
|
|
||||||
|
// Dynamically load and compile the specified template
|
||||||
|
const templateSource = fs.readFileSync(path.join(__dirname, 'src', 'templates', 'emails', `${templateName}.hbs`), 'utf8');
|
||||||
|
|
||||||
|
// Extract subject and optional text version from the template source
|
||||||
|
const subjectMatch = templateSource.match(/{{!-- Subject: (.*) --}}/);
|
||||||
|
const textMatch = templateSource.match(/{{!-- Text: ([\s\S]*?) --}}/);
|
||||||
|
|
||||||
|
const subject = subjectMatch ? subjectMatch[1].trim() : 'Default Subject';
|
||||||
|
const textVersion = textMatch ? textMatch[1].trim() : null;
|
||||||
|
|
||||||
|
// Remove the subject and text annotations from the template source
|
||||||
|
const cleanTemplateSource = templateSource.replace(/{{!-- Subject: .* --}}/, '').replace(/{{!-- Text: [\s\S]*? --}}/, '');
|
||||||
|
|
||||||
|
// Compile the cleaned template
|
||||||
|
const template = Handlebars.compile(cleanTemplateSource);
|
||||||
|
|
||||||
|
// Render the specified template with the provided model
|
||||||
|
const templateHtml = template(model);
|
||||||
|
|
||||||
|
// Render the main template, inserting the specific template HTML
|
||||||
|
const html = mainTemplate({ body: templateHtml });
|
||||||
|
|
||||||
|
// Generate a plain text version if not explicitly provided
|
||||||
|
const text = textVersion || html.replace(/<[^>]*>?/gm, ''); // Simple regex to strip HTML tags. Might need refinement.
|
||||||
|
|
||||||
|
const message = {
|
||||||
|
from: sender, // Ensure 'sender' is defined
|
||||||
|
to,
|
||||||
|
subject,
|
||||||
|
text,
|
||||||
|
html,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Assuming mailtrapTestClient is correctly set up to send emails
|
||||||
|
await mailtrapTestClient
|
||||||
|
.send(message)
|
||||||
|
.then(console.log)
|
||||||
|
.catch(console.error);
|
||||||
|
};
|
||||||
|
|
||||||
exports.SendEmail_Test = async function (to, subject, text, html) {
|
exports.SendEmail_Test = async function (to, subject, text, html) {
|
||||||
const message = {
|
const message = {
|
||||||
from: sender,
|
from: sender,
|
||||||
|
@ -0,0 +1,24 @@
|
|||||||
|
{{!-- Subject: Your email subject here --}}
|
||||||
|
{{!-- Text: Plain text version of your email. If not provided, HTML tags will be stripped from the HTML version for the
|
||||||
|
text version. --}}
|
||||||
|
|
||||||
|
<section>
|
||||||
|
<h3>{{firstName}} {{lastName}} търси зместник за {{placeName}}!</h3>
|
||||||
|
<p>Здравейте,</p>
|
||||||
|
<p>{{prefix}} {{firstName}} {{lastName}} търси заместник за своята смяна на {{dateStr}} на {{placeName}}.</p>
|
||||||
|
{{!-- <p><strong>Shift Details:</strong></p> --}}
|
||||||
|
{{!-- <p>Date: {{date}}<br>Time: {{time}}<br>Location: {{placeName}}</p> --}}
|
||||||
|
<p>С натискането на бутона по-долу можете да премете да го замествате. Вие, той/тя и останалите участници в смяната
|
||||||
|
ще бъдат уведумени чрез имейл за промяната. Вашата помощ е високо ценена.</p>
|
||||||
|
<p style="text-align: center;">
|
||||||
|
<a href="{{acceptUrl}}"
|
||||||
|
target="_blank"
|
||||||
|
style="background-color: #4CAF50; color: white; padding: 10px 20px; text-decoration: none; display: inline-block; border-radius: 5px;">Приеми
|
||||||
|
смяната</a>
|
||||||
|
</p>
|
||||||
|
{{!-- <p>Thank you very much for considering my request.</p>
|
||||||
|
<p>Best regards,<br>{{name}}</p> --}}
|
||||||
|
</section>
|
||||||
|
<footer style="margin-top: 20px; text-align: center;">
|
||||||
|
<p>Изпратено на: {{sentDate}}</p>
|
||||||
|
</footer>
|
25
src/templates/emails/main.hbs
Normal file
25
src/templates/emails/main.hbs
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8">
|
||||||
|
<meta name="viewport"
|
||||||
|
content="width=device-width, initial-scale=1.0">
|
||||||
|
<title>Email Template</title>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<header style="background-color: #f3f3f3; padding: 20px; text-align: center;">
|
||||||
|
<h2>Company Name</h2>
|
||||||
|
</header>
|
||||||
|
|
||||||
|
<main style="margin: 20px;">
|
||||||
|
{{{body}}}
|
||||||
|
</main>
|
||||||
|
|
||||||
|
<footer style="background-color: #f3f3f3; padding: 20px; text-align: center;">
|
||||||
|
© 2024 Company Name. All rights reserved.
|
||||||
|
</footer>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
Reference in New Issue
Block a user