cover me email request routine

This commit is contained in:
Dobromir Popov
2024-04-06 11:09:22 +03:00
parent fa5d3f4f99
commit 09db5ca8b9
11 changed files with 433 additions and 246 deletions

View File

@ -525,7 +525,9 @@ exports.getCurrentYearMonth = () => {
const month = String(currentDate.getMonth() + 1).padStart(2, '0'); // Month is 0-indexed
return `${year}-${month}`;
}
exports.getTimeFormated = function (date) {
return this.formatTimeHHmm(date);
}
// format date to 'HH:mm' time string required by the time picker
exports.formatTimeHHmm = function (input) {
// Check if the input is a string or a Date object
@ -729,3 +731,7 @@ exports.getLocalStorage = function (key, defaultValue) {
}
return defaultValue;
};
exports.root = function (req) {
return process.env.NEXT_PUBLIC_PUBLIC_URL;
}

View File

@ -1,10 +1,12 @@
// helper module to send emails with nodemailer
const fs = require("fs");
const path = require('path');
const { MailtrapClient } = require("mailtrap");
const nodemailer = require("nodemailer");
const CON = require("./const");
const CAL = require("./calendar");
const Handlebars = require('handlebars');
// const { google } = require("googleapis");
// const OAuth2 = google.auth.OAuth2;
@ -12,14 +14,42 @@ const CAL = require("./calendar");
const { Shift, Publisher, PrismaClient } = require("@prisma/client");
const TOKEN = process.env.TOKEN || "a7d7147a530235029d74a4c2f228e6ad";
const SENDER_EMAIL = "pw@d-popov.com";
const sender = { name: "JW Cart: Shift Info", email: SENDER_EMAIL };
const SENDER_EMAIL = "sofia@mwitnessing.com";
const sender = { name: "Специално Свидетелстване София", email: SENDER_EMAIL };
const client = new MailtrapClient({ token: TOKEN });
const mailtrapTestClient = new MailtrapClient({
username: '8ec69527ff2104',//not working now
password: 'c7bc05f171c96c'
});
let mailtrapTestClient = null;
// const mailtrapTestClient = new MailtrapClient({
// username: '8ec69527ff2104',//not working now
// password: 'c7bc05f171c96c'
// });
//test
var transporter = nodemailer.createTransport({
host: "sandbox.smtp.mailtrap.io",
port: 2525,
auth: {
user: "8ec69527ff2104",
pass: "c7bc05f171c96c"
}
});
// production
// var transporter = nodemailer.createTransport({
// host: "live.smtp.mailtrap.io",
// port: 587,
// auth: {
// user: "api",
// pass: "1cfe82e747b8dc3390ed08bb16e0f48d"
// }
// });
var transporterBulk = nodemailer.createTransport({
host: "bulk.smtp.mailtrap.io",
port: 587,
auth: {
user: "api",
pass: "1cfe82e747b8dc3390ed08bb16e0f48d"
}
});
// ------------------ Email sending ------------------
var lastResult = null;
function setResult(result) {
@ -29,91 +59,173 @@ exports.GetLastResult = function () {
return lastResult;
};
exports.SendEmail = async function (to, subject, text, html) {
exports.SendEmail = async function (to, subject, text, html, attachments = []) {
let sender = '"Специално Свидетелстване София - тест" <demo@mwitnessing.com>';
to = Array.isArray(to) ? to : [to];
const emailAddresses = to.map(item => `"${item.name}" <${item.email}>`).join(', ');
const message = {
from: sender,
to,
to: emailAddresses,
subject,
text,
html,
attachments
};
if (mailtrapTestClient !== null) {
// Assuming mailtrapTestClient is correctly set up to send emails
await mailtrapTestClient
.send(message)
.then(console.log)
.catch(console.error);
} else {
let result = await transporter
.sendMail(message)
.then(console.log)
.catch(console.error);
return result;
}
};
exports.SendEmailHandlebars = async function (to, templateName, model) {
// Ensure the sender and mailtrapTestClient are correctly defined or imported
exports.SendEmailHandlebars = async function (to, templateName, model, attachments = []) {
try {
// 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);
// Load and compile the main template
const mainTemplateSource = fs.readFileSync(path.join(process.cwd(), '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');
// Dynamically load and compile the specified template
const templateSource = fs.readFileSync(path.join(process.cwd(), '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]*?) --}}/);
// 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;
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]*? --}}/, '');
// 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);
// Compile the cleaned template
const template = Handlebars.compile(cleanTemplateSource);
// Render the specified template with the provided model
const templateHtml = template(model);
// 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 });
// 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.
// 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,
};
let results = this.SendEmail(to, subject, text, html, attachments);
return results;
// Assuming mailtrapTestClient is correctly set up to send emails
await mailtrapTestClient
.send(message)
.then(console.log)
.catch(console.error);
} catch (error) {
console.error(error);
return new Error('Error sending email');
}
};
exports.SendEmail_Test = async function (to, subject, text, html) {
const message = {
from: sender,
to,
subject,
text,
html,
exports.SendEmail_NewShifts = async function (publisher, shifts) {
if (shifts.length === 0) return;
var date = new Date(shifts[0].startTime);
// Generate ICS calendar links for all shifts
const icsLink = CAL.GenerateICS(shifts);
// Prepare the shifts string
const shiftStr = shifts.map((s) => {
return `${CON.weekdaysBG[s.startTime.getDay()]} ${CON.GetDateFormat(s.startTime)} at ${s.cartEvent.location.name} from ${CON.GetTimeFormat(s.startTime)} to ${CON.GetTimeFormat(s.endTime)}`;
}).join("<br>");
// Define the model for the Handlebars template
const model = {
publisherFirstName: publisher.firstName,
publisherLastName: publisher.lastName,
month: CON.monthNamesBG[date.getMonth()],
shifts: shiftStr,
sentDate: new Date().toLocaleDateString() // Assuming you want to include the sent date in the email
};
await mailtrapTestClient
.send(message)
.then(console.log, console.error, setResult);
}
// Call the refactored function to send the email with Handlebars template rendering
await exports.SendEmailHandlebars(
publisher.email, // Assuming the publisher's email is to be used
"newShifts", // The name of your Handlebars template for new shifts notification
model,
[{
filename: "calendar.ics",
content: icsLink,
contentType: 'text/calendar' // Ensure this is correctly set for the ICS file
}]
).then(console.log).catch(console.error);
};
//----------------------- OLD -----------------------------
// exports.SendEmail_NewShifts = async function (publisher, shifts) {
// if (shifts.length == 0) return;
// var date = new Date(shifts[0].startTime);
// //generate ICS calendar links for all shifts
// const icsLink = CAL.GenerateICS(shifts);
// const shftStr = shifts
// .map((s) => {
// return ` ${CON.weekdaysBG[s.startTime.getDay()]
// } ${CON.GetDateFormat(s.startTime)} ${s.cartEvent.location.name
// } ${CON.GetTimeFormat(s.startTime)} - ${CON.GetTimeFormat(
// s.endTime
// )}`;
// })
// .join("\n");
// await client.send({
// from: sender,
// to: [
// {
// email: "dobromir.popov@gmail.com",//publisher.email,
// name: publisher.firstName + " " + publisher.lastName,
// },
// ],
// subject: "[CCC]: вашите смени през " + CON.monthNamesBG[date.getMonth()],
// text:
// "Здравейте, " + publisher.firstName + " " + publisher.lastName + "!\n\n" +
// "Ти регистриран да получавате известия за нови смени на количка.\n" +
// `За месец ${CON.monthNamesBG[date.getMonth()]} имате следните смени:\n` +
// ` ${shftStr} \n\n\n` +
// "Поздрави,\n" +
// "Специално Свидетелстване София",
// attachments: [
// {
// filename: "calendar.ics",
// content_id: "calendar.ics",
// disposition: "inline",
// content: icsLink,
// },
// ],
// })
// .then(console.log, console.error, setResult);
// };
// https://mailtrap.io/blog/sending-emails-with-nodemailer/
exports.SendTestEmail = async function (to) {
// await client
// .send({
// from: sender,
// to: [{ email: RECIPIENT_EMAIL }],
// subject: "Hello from Mailtrap!",
// text: "Welcome to Mailtrap Sending!",Shift Info"
// })
// .then(console.log, console.error, setResult);
// return lastResult;
exports.SendEmail_Example = async function (to) {
const welcomeImage = fs.readFileSync(
path.join(CON.contentPath, "welcome.png")
);
@ -160,50 +272,3 @@ exports.SendTestEmail = async function (to) {
})
.then(console.log, console.error, setResult);
};
exports.SendEmail_NewShifts = async function (publisher, shifts) {
if (shifts.length == 0) return;
var date = new Date(shifts[0].startTime);
//generate ICS calendar links for all shifts
const icsLink = CAL.GenerateICS(shifts);
const shftStr = shifts
.map((s) => {
return ` ${CON.weekdaysBG[s.startTime.getDay()]
} ${CON.GetDateFormat(s.startTime)} ${s.cartEvent.location.name
} ${CON.GetTimeFormat(s.startTime)} - ${CON.GetTimeFormat(
s.endTime
)}`;
})
.join("\n");
await client.send({
from: sender,
to: [
{
email: "dobromir.popov@gmail.com",//publisher.email,
name: publisher.firstName + " " + publisher.lastName,
},
],
subject: "[CCC]: вашите смени през " + CON.monthNamesBG[date.getMonth()],
text:
"Здравейте, " + publisher.firstName + " " + publisher.lastName + "!\n\n" +
"Ти регистриран да получавате известия за нови смени на количка.\n" +
`За месец ${CON.monthNamesBG[date.getMonth()]} имате следните смени:\n` +
` ${shftStr} \n\n\n` +
"Поздрави,\n" +
"Специално Свидетелстване София",
attachments: [
{
filename: "calendar.ics",
content_id: "calendar.ics",
disposition: "inline",
content: icsLink,
},
],
})
.then(console.log, console.error, setResult);
};

View File

@ -0,0 +1,24 @@
{{!-- Subject: ССС: Нужен е заместник--}}
{{!-- 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>Търси се зместник за смяна на {{placeName}} за {{dateStr}}!</h3>
<p>Здравейте,</p>
<p>{{prefix}} {{firstName}} {{lastName}} търси заместник.</p>
{{!-- <p><strong>Shift Details:</strong></p> --}}
<p>Дата: {{dateStr}} <br>Час: {{time}}<br>Място: {{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>

View File

@ -1,24 +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. --}}
{{!-- Subject: ССС: Нужен е заместник--}}
<section>
<h3>{{firstName}} {{lastName}} търси зместник за {{placeName}}!</h3>
<p>Здравейте,</p>
<p>{{prefix}} {{firstName}} {{lastName}} търси заместник за своята смяна на {{dateStr}} на {{placeName}}.</p>
<h3>Търси се зместник за смяна на {{placeName}} за {{dateStr}}!</h3>
<p>Здравей {{firstName}},</p>
<p>{{prefix}} {{user.firstName}} {{user.lastName}} търси заместник.</p>
{{!-- <p><strong>Shift Details:</strong></p> --}}
{{!-- <p>Date: {{date}}<br>Time: {{time}}<br>Location: {{placeName}}</p> --}}
<p>С натискането на бутона по-долу можете да премете да го замествате. Вие, той/тя и останалите участници в смяната
ще бъдат уведумени чрез имейл за промяната. Вашата помощ е високо ценена.</p>
<p>Дата: {{dateStr}} <br>Час: {{time}}<br>Място: {{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>
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>
<p>Изпратено до {{firstName}} {{lastName}} на {{sentDate}}</p>
</footer>

View File

@ -10,7 +10,7 @@
<body>
<header style="background-color: #f3f3f3; padding: 20px; text-align: center;">
<h2>Company Name</h2>
<h2>Cпециално Свидетелстване София</h2>
</header>
<main style="margin: 20px;">
@ -18,7 +18,7 @@
</main>
<footer style="background-color: #f3f3f3; padding: 20px; text-align: center;">
© 2024 Company Name. All rights reserved.
© 2024 ССС. All rights reserved.
</footer>
</body>

View File

@ -0,0 +1,14 @@
{{!-- Subject: ССС: Нужен е заместник--}}
<section>
<h2>Здравейте, {{publisherFirstName}} {{publisherLastName}}!</h2>
<p>Ти регистриран да получавате известия за нови смени на количка.</p>
<p>За месец {{month}} имате следните смени:</p>
<div>
{{{shifts}}}
</div>
</section>
<footer class="footer">
Изпратено на: {{sentDate}}
</footer>