Added logging service and emailing function

This commit is contained in:
veyselboybay committed 2024-05-13 19:20:56 -04:00
1 parent cca454b447
commit ad2b48bb41
5 files changed
+79 -7

No files matched your search

+2
View File
@@ -37,6 +37,8 @@ Configure the server with the following environmental variables
| ENV Varialbe Name | Required/Optional | Type | Description |
| -------------------- | ----------------- | ------ | --------------------------------- |
| DB_CONNECTION_STRING | Required | string | Specfies URL for MongoDB Database |
| MAILERSEND_API_KEY | Required | string | Specfies API KEY for MailerSend service |
| SYSTEM_EMAIL_ADDRESS | Required | string | Specfies System email to be used in emailing service |
---
+1 -3
View File
@@ -1,4 +1,2 @@
node_modules
.env
utils/sendEmail.js
utils/logger.js
.env
+7 -4
View File
@@ -5,6 +5,7 @@ const authRouter = require("./routes/authRoute");
const monitorRouter = require("./routes/monitorRoute");
const { connectDbAndRunServer } = require("./configs/db");
require("dotenv").config();
const logger = require('./utils/logger');
// const { sendEmail } = require('./utils/sendEmail')
// **************************
@@ -53,15 +54,17 @@ app.use("/api/v1/monitors", monitorRouter);
// Testing email service
// app.use('/sendEmail', async (req, res) => {
// const response = sendEmail('veysel.boybay@outlook.com', ['veysel.boybay@bluewavelabs.ca'], 'Testing email service', '<h1>Testing Bluewavelabs</h1>');
// const response = sendEmail(['veysel.boybay@bluewavelabs.ca'], 'Testing email service', '<h1>Testing Bluewavelabs</h1>');
// console.log(response);
// })
//health check
app.use("/api/v1/healthy", (req, res) => {
try {
return res.status(200).json({ message: "Healthy" });
} catch (error) {
try {
logger.info("Checking Health of the server.")
return res.status(200).json({ message: "Healthy" });
} catch (error) {
logger.error(error.message)
return res.status(500).json({ message: error.message });
}
});
+26
View File
@@ -0,0 +1,26 @@
const winston = require('winston');
/**
* @module
* @example
* logger.info("Registered a new user!")
* logger.warn("User not found!")
* logger.error("Cannot save")
* @example
* "Specify service and ID in the log if applicable."
* logger.error("Descriptive Message",{"service":"monitor","monitorId":"123456"})
* logger.error("Incorrect Credentials",{"service":"Auth","userId":"654321"})
* logger.error("User not found!",{"service":"Auth"})
*/
const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.json()
),
transports: [
new winston.transports.Console(),
new winston.transports.File({ filename: 'app.log' }),
],
});
module.exports = logger;
+43
View File
@@ -0,0 +1,43 @@
const { MailerSend, EmailParams, Sender, Recipient } = require('mailersend')
const logger = require('../utils/logger')
const mailersend = new MailerSend({
apiKey: process.env.MAILERSEND_API_KEY,
})
/**
* @function
* @param {[string]} receivers - takes an array of strings
* @param {string} subject - takes a single string
* @param {string} contentHTML - takes a single string that contains HTML
* @returns {JSON}
* @example
* sendEmail(['veysel@bluewavelabs.ca','alex@bluewavelabs.ca','monzer@bluewavelabs.ca'],'Testing Email Servide','<h1>BlueWaveLabs</h1>')
*/
// TODO: from email should be in .env file
const sendEmail = async (receivers,subject,contentHTML) => {
// Sender
const from = process.env.SYSTEM_EMAIL_ADDRESS;
const sender = new Sender(from);
// receivers
let recipients = []
receivers.map(email => recipients.push(new Recipient(email)));
// Set params
const emailParams = new EmailParams()
.setFrom(sender)
.setTo(recipients)
.setSubject(subject)
.setHtml(contentHTML);
try {
const response = await mailersend.email.send(emailParams);
logger.info("Email sent to receivers!",{"service":"Email"})
return response;
} catch (error) {
logger.error(error.body,{"service":"email"})
console.log(error.body)
}
}
module.exports = {sendEmail}