In this assignment, you are required to create a function in JavaScript that simulates the process of sending an email. This includes rendering an HTML template with context variables, creating a mock message object, and handling attachments. Since this is a simulated environment, you do not need to use real email services.
You must implement a function sendEmail
that adheres to the following specifications:
/**
* Calls send email service (simulated)
*
* @param {object} db - Session variable (not used in the current scope, but consider it exists)
* @param {string} recipientEmail - Email address of the recipient
* @param {object} contextData - Dictionary containing variables to be used in the template.
* E.g., if the template has {{first_name}} and the dictionary has 'first_name', then it should be replaced in the output.
* @param {string} templateCode - HTML template code
* @param {Array<string>} filePaths - Local paths of attachments
* @returns {object} - A dictionary containing the simulated message details
*/
function sendEmail(db, recipientEmail, contextData, templateCode, filePaths) {
// Implement your function here
}
-
Replace Template Variables:
- Parse the
templateCode
and replace any placeholders (e.g.,{{first_name}}
) with the corresponding values fromcontextData
.
- Parse the
-
Create Mock Message:
- Build a mock message object that includes the recipient email, rendered HTML, and a list of attachments.
-
Return the Mock Message:
- The function should return this mock message object as a dictionary.
Template Code:
<html>
<body>
<h1>Hello, {{first_name}} {{last_name}}!</h1>
<p>Thank you for registering with {{company}}.</p>
</body>
</html>
Context Data:
{
first_name: 'John',
last_name: 'Doe',
company: 'Tech Co.'
}
Expected Output HTML:
<html>
<body>
<h1>Hello, John Doe!</h1>
<p>Thank you for registering with Tech Co.</p>
</body>
</html>
Mock Message Object:
{
recipientEmail: '[email protected]',
renderedHtml: '<html><body><h1>Hello, John Doe!</h1><p>Thank you for registering with Tech Co.</p></body></html>',
attachments: ['path/to/attachment1.jpg', 'path/to/attachment2.pdf']
}
-
Error Handling (2 points)
- Implement robust error handling for various scenarios such as:
- Missing required context variables.
- Non-existent file paths.
- Ensure that meaningful error messages are returned or logged.
- Implement robust error handling for various scenarios such as:
-
Template Engine (3 points)
- Implement a simple template engine to support more advanced templating features such as loops and conditionals.
- E.g.,
{% if first_name %}Hello, {{first_name}}{% else %}Hello, Guest{% endif %}
.
-
Code Documentation (2 points)
- Provide detailed JSDoc comments for the function, and inline comments explaining complex logic.
- Maintain a separate README file explaining the function's purpose, usage, and example scenarios.
-
Email Validation (2 points)
- Implement email validation logic to ensure that the provided email addresses are correctly formatted.
- Utilize regular expressions or use a third-party validation library.
-
Attachment Validation (2 points)
- Verify that the file paths provided actually exist and that the files are accessible.
- Handle and report issues with inaccessible files.
-
Logging (2 points)
- Integrate logging to track the functioning of the
sendEmail
function. - Log message details along with timestamps, error messages, and processing duration.
- Integrate logging to track the functioning of the
-
Performance Optimization (3 points)
- Optimize the function for performance, especially in how it processes and renders the templates.
- Avoid redundant computations and optimize string operations.
Please submit your function implementation with error handling, and supporting comments. Ensure your code is well-documented and follows best practices.
Good luck!