Open spelkey-ucd opened 2 months ago
I need some clarification and examples on this? Like examples and outcomes this would be used in?
// retrieved from settings table
content = `
Hi ${requesterFirstName},
Your travel, training, or professional development request has been successfully submitted.
${nextApproverFullName ? `
It has been sent to ${nextApproverFullName} for approval.
` : ``}
You may cancel, resubmit, or view the status of this request at anytime by going to the following url:
${approvalRequestUrl}
`
Then the email content class would look something like:
class EmaiContent {
constructor(approvalRequest, reimbursementRequest){
this.approvalRequest = approvalRequest;
this.reimbursementRequest = reimbursementRequest
}
_getContext(content){
// extract variables from content string
const variables = ['requesterFirstName', etc];
// get values from approvalRequest/reimbursmentRequest
const context = {};
for (v of variables) {
context[v] = this._getVariableFunction(v)
}
return context;
}
_getVariableFunction(variable){
// return method for getting data for variable
if (variable === 'requesterFirstName') return this._getRequesterFirstName;
// etc
return () => {return ''}
}
hydrate(content){
const context = this._getContext(content);
return this._evaluateTemplate(content, context);
}
_evaluateTemplate(template, context) {
const templateFunction = new Function(...Object.keys(context), `return \`${template}\`;`);
return templateFunction(...Object.values(context));
}
}
So then you would use the class to construct the email content you are sending:
emailContent = new EmaiContent(approvalRequest, reimbursementRequest);
emailSubject = emailContent.hydrate(subject);
emailBody = emailContent.hydrate(content);
emailService.send(recipients, emailSubject, emailBody);
when an admin saves an email body or subject line in the settings, it can contain special keywords representing variables such as the requester's name.
Need a class that will have methods that do all of this substitution, e.g.