fix: add function to hcnage variable string name to variable value for email body and subject

This commit is contained in:
RaktimaNXG
2024-04-08 18:02:03 +05:30
parent d30105299c
commit 55465a035f
4 changed files with 68 additions and 62 deletions
+22
View File
@@ -1840,3 +1840,25 @@ export const getTenantDetails = async (objectId) => {
return "user does not exist!";
}
};
//function to convert variable string name to variable value of email body and subject
export function replaceMailVaribles(subject, body, variables) {
let replacedSubject = subject;
let replacedBody = body;
for (const variable in variables) {
const regex = new RegExp(`{{${variable}}}`, "g");
if (subject) {
replacedSubject = replacedSubject.replace(regex, variables[variable]);
}
if (body) {
replacedBody = replacedBody.replace(regex, variables[variable]);
}
}
const result = {
subject: replacedSubject,
body: replacedBody
};
return result;
}
+2 -1
View File
@@ -9,4 +9,5 @@ export const modalSubmitBtnColor = "#17a2b8";
export const modalCancelBtnColor = "white";
export const themeColor = "#47a3ad";
export const iconColor = "#686968";
export const isEnableSubscription = process.env.REACT_APP_ENABLE_SUBSCRIPTION;
export const isEnableSubscription = false;
// process.env.REACT_APP_ENABLE_SUBSCRIPTION;
+22 -33
View File
@@ -21,7 +21,8 @@ import {
onSaveSign,
onSaveImage,
addDefaultSignatureImg,
radioButtonWidget
radioButtonWidget,
replaceMailVaribles
} from "../constant/Utils";
import Loader from "../primitives/LoaderWithMsg";
import HandleError from "../primitives/HandleError";
@@ -605,7 +606,6 @@ function PdfRequestFiles() {
let senderEmail = pdfDetails?.[0].ExtUserPtr.Email;
let senderPhone = pdfDetails?.[0]?.ExtUserPtr?.Phone;
const senderName = `${pdfDetails?.[0].ExtUserPtr.Name}`;
let emailBodyWithValue, emailSubjectWithValue;
try {
const imgPng =
@@ -633,6 +633,7 @@ function PdfRequestFiles() {
? pdfDetails[0].ExtUserPtr.Company
: "";
const themeBGcolor = themeColor;
let replaceVar;
if (
requestBody &&
requestSubject &&
@@ -647,46 +648,34 @@ function PdfRequestFiles() {
replacedRequestBody +
"</body> </html>";
emailBodyWithValue = htmlReqBody
.replace(/\{{sender_name}}/g, senderName)
.replace(/\{{document_title}}/g, pdfDetails?.[0].Name)
.replace(
/\{{signing_url}}/g,
`<a href=${signPdf}>Sign here</a>`
)
.replace(/\{{sender_mail}}/g, senderEmail)
.replace(/\{{sender_phone}}/g, senderPhone)
.replace(/\{{expiry_date}}}/g, localExpireDate)
.replace(/\{{company_name}}/g, orgName)
.replace(/\{{receiver_name}}/g, user.Name)
.replace(/\{{receiver_email}}/g, user.Email)
.replace(/\{{receiver_phone}}/g, user.Phone);
emailSubjectWithValue = requestSubject
.replace(/\{{sender_name}}/g, senderName)
.replace(/\{{document_title}}/g, pdfDetails?.[0].Name)
.replace(
/\{{signing_url}}/g,
`<a href=${signPdf}>Sign here</a>`
)
.replace(/\{{sender_mail}}/g, senderEmail)
.replace(/\{{sender_phone}}/g, senderPhone)
.replace(/\{{expiry_date}}}/g, localExpireDate)
.replace(/\{{company_name}}/g, orgName)
.replace(/\{{receiver_name}}/g, user.Name)
.replace(/\{{receiver_email}}/g, user.Email)
.replace(/\{{receiver_phone}}/g, user.Phone);
const variables = {
document_title: pdfDetails?.[0].Name,
sender_name: senderName,
sender_mail: senderEmail,
sender_phone: senderPhone,
receiver_name: user.Name,
receiver_email: user.Email,
receiver_phone: user.Phone,
expiry_date: localExpireDate,
company_name: orgName,
signing_url: `<a href=${signPdf}>Sign here</a>`
};
replaceVar = replaceMailVaribles(
requestSubject,
htmlReqBody,
variables
);
}
let params = {
extUserId: extUserId,
recipient: user.Email,
subject: requestSubject
? emailSubjectWithValue
? replaceVar?.subject
: `${pdfDetails?.[0].ExtUserPtr.Name} has requested you to sign ${pdfDetails?.[0].Name}`,
from: senderEmail,
html: requestBody
? emailBodyWithValue
? replaceVar?.body
: "<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8' /> </head> <body> <div style='background-color: #f5f5f5; padding: 20px'=> <div style=' box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 12px;background: white;padding-bottom: 20px;'> <div style='padding:10px 10px 0 10px'><img src=" +
imgPng +
" height='50' style='padding: 20px,width:170px,height:40px' /></div> <div style=' padding: 2px;font-family: system-ui;background-color:" +
+22 -28
View File
@@ -29,7 +29,8 @@ import {
textWidget,
radioButtonWidget,
color,
getTenantDetails
getTenantDetails,
replaceMailVaribles
} from "../constant/Utils";
import RenderPdf from "../components/pdf/RenderPdf";
import { useNavigate } from "react-router-dom";
@@ -915,7 +916,7 @@ function PlaceHolderSign() {
const themeBGcolor = themeColor;
const senderName = `${pdfDetails?.[0].ExtUserPtr.Name}`;
const documentName = `${pdfDetails?.[0].Name}`;
let emailBodyWithValue, emailSubjectWithValue;
let replaceVar;
if (
requestBody &&
requestSubject &&
@@ -928,40 +929,34 @@ function PlaceHolderSign() {
replacedRequestBody +
"</body> </html>";
emailBodyWithValue = htmlReqBody
.replace(/\{{sender_name}}/g, senderName)
.replace(/\{{document_title}}/g, documentName)
.replace(/\{{signing_url}}/g, `<a href=${signPdf}>Sign here</a>`)
.replace(/\{{sender_mail}}/g, senderEmail)
.replace(/\{{sender_phone}}/g, senderPhone)
.replace(/\{{expiry_date}}}/g, localExpireDate)
.replace(/\{{company_name}}/g, orgName)
.replace(/\{{receiver_name}}/g, signerMail[i].Name)
.replace(/\{{receiver_email}}/g, signerMail[i].Email)
.replace(/\{{receiver_phone}}/g, signerMail[i].Phone);
emailSubjectWithValue = requestSubject
.replace(/\{{sender_name}}/g, senderName)
.replace(/\{{document_title}}/g, documentName)
.replace(/\{{signing_url}}/g, `<a href=${signPdf}>Sign here</a>`)
.replace(/\{{sender_mail}}/g, senderEmail)
.replace(/\{{sender_phone}}/g, senderPhone)
.replace(/\{{expiry_date}}}/g, localExpireDate)
.replace(/\{{company_name}}/g, orgName)
.replace(/\{{receiver_name}}/g, signerMail[i].Name)
.replace(/\{{receiver_email}}/g, signerMail[i].Email)
.replace(/\{{receiver_phone}}/g, signerMail[i].Phone);
const variables = {
document_title: documentName,
sender_name: senderName,
sender_mail: senderEmail,
sender_phone: senderPhone,
receiver_name: signerMail[i].Name,
receiver_email: signerMail[i].Email,
receiver_phone: signerMail[i].Phone,
expiry_date: localExpireDate,
company_name: orgName,
signing_url: `<a href=${signPdf}>Sign here</a>`
};
replaceVar = replaceMailVaribles(
requestSubject,
htmlReqBody,
variables
);
}
let params = {
extUserId: extUserId,
recipient: signerMail[i].Email,
subject: isCustomize
? emailSubjectWithValue
? replaceVar?.subject
: `${senderName} has requested you to sign ${documentName}`,
from: senderEmail,
html: isCustomize
? emailBodyWithValue
? replaceVar?.body
: "<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8' /> </head> <body> <div style='background-color: #f5f5f5; padding: 20px'=> <div style=' box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 12px;background: white;padding-bottom: 20px;'> <div style='padding:10px 10px 0 10px'><img src=" +
imgPng +
" height='50' style='padding: 20px,width:170px,height:40px' /></div> <div style=' padding: 2px;font-family: system-ui;background-color:" +
@@ -990,7 +985,6 @@ function PlaceHolderSign() {
console.log("error", error);
}
}
if (sendMail.data.result.status === "success") {
setMailStatus("success");
const signers = signersdata?.map((x) => {