From 348786eeaa64c99e2982f9b402465b537c7d63ed Mon Sep 17 00:00:00 2001
From: Nanditha K <106753017+nanditha938@users.noreply.github.com>
Date: Fri, 23 Feb 2024 15:11:13 +0530
Subject: [PATCH 01/12] Edits to the service name and changed app and component
name to Mentor.
Added editorial changes
---
README.md | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/README.md b/README.md
index e3661576..541108bc 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# Setup Options
-Elevate notification services can be setup in local using two methods:
+Elevate notification services can be set in local using two methods:
Dockerized service with local dependencies(Intermediate)
@@ -196,9 +196,9 @@ npm test
# Used in
-This project was built to be used with [Mentoring Service](https://github.com/ELEVATE-Project/mentoring.git) and [User Service](https://github.com/ELEVATE-Project/user.git).
+This project was built to be used with [Mentor Service](https://github.com/ELEVATE-Project/mentoring.git) and [User Service](https://github.com/ELEVATE-Project/user.git).
-The frontend/mobile application [repo](https://github.com/ELEVATE-Project/mentoring-mobile-app).
+The PWA [repo](https://github.com/ELEVATE-Project/mentoring-mobile-app).
You can learn more about the full implementation of MentorEd [here](https://elevate-docs.shikshalokam.org/.mentorEd/intro) .
From ba647a86327927eab36e8fbd49f5a8a9f3f02774 Mon Sep 17 00:00:00 2001
From: joffinjoy
Date: Wed, 27 Mar 2024 16:00:25 +0530
Subject: [PATCH 02/12] Updated 2.5 README
---
README.md | 301 ++++++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 260 insertions(+), 41 deletions(-)
diff --git a/README.md b/README.md
index 541108bc..c7529084 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
+# System Requirements
+
+- **Operating System:** Ubuntu 22
+- **Node.js:** v20
+- **PostgreSQL:** 16
+- **Citus:** 12.1
+- **Apache Kafka:** 3.5.0
+
# Setup Options
-Elevate notification services can be set in local using two methods:
+Elevate notification services can be setup in local using two methods:
Dockerized service with local dependencies(Intermediate)
@@ -108,75 +116,286 @@ Elevate notification services can be set in local using two methods:
**Expectation**: Run single service with existing local dependencies in host (**Non-Docker Implementation**).
-### Steps
+## Installations
+
+### Install Node.js LTS
+
+Refer to the [NodeSource distributions installation scripts](https://github.com/nodesource/distributions#installation-scripts) for Node.js installation.
+
+```bash
+$ curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash - &&\
+sudo apt-get install -y nodejs
+```
+
+### Install Build Essential
-1. Install required tools & dependencies
+```bash
+$ sudo apt-get install build-essential
+```
- Install any IDE (eg: VScode)
+### Install Kafka
- Install Nodejs: https://nodejs.org/en/download/
+Refer to [Kafka Ubuntu 22.04 setup guide](https://www.fosstechnix.com/install-apache-kafka-on-ubuntu-22-04-lts/)
-2. Clone the **Notification service** repository.
+1. Install OpenJDK 11:
+ ```bash
+ $ sudo apt install openjdk-11-jdk
```
- git clone https://github.com/ELEVATE-Project/notification.git
+
+2. Download and extract Kafka:
+
+ ```bash
+ $ sudo wget https://downloads.apache.org/kafka/3.5.0/kafka_2.12-3.5.0.tgz
+ $ sudo tar xzf kafka_2.12-3.5.0.tgz
+ $ sudo mv kafka_2.12-3.5.0 /opt/kafka
```
-3. Add **.env** file to the project directory
+3. Configure Zookeeper:
+
+ ```bash
+ $ sudo nano /etc/systemd/system/zookeeper.service
+ ```
- Create a **.env** file in **src** directory of the project and copy these environment variables into it.
+ Paste the following lines into the `zookeeper.service` file:
+ ```ini
+ /etc/systemd/system/zookeeper.service
+ [Unit]
+ Description=Apache Zookeeper service
+ Documentation=http://zookeeper.apache.org
+ Requires=network.target remote-fs.target
+ After=network.target remote-fs.target
+
+ [Service]
+ Type=simple
+ ExecStart=/opt/kafka/bin/zookeeper-server-start.sh /opt/kafka/config/zookeeper.properties
+ ExecStop=/opt/kafka/bin/zookeeper-server-stop.sh
+ Restart=on-abnormal
+
+ [Install]
+ WantedBy=multi-user.target
```
- # Notification Service Config
- #Port on which service runs
- APPLICATION_PORT = 3000
+ Save and exit.
- #Application environment
- APPLICATION_ENV = development
+4. Reload systemd:
- #Route after base url
- APPLICATION_BASE_URL = /notification/
+ ```bash
+ $ sudo systemctl daemon-reload
+ ```
- #Kafka endpoint
- KAFKA_HOST = "localhost:9092"
+5. Configure Kafka:
- #kafka topic name
- KAFKA_TOPIC ="testTopic"
+ ```bash
+ $ sudo nano /etc/systemd/system/kafka.service
+ ```
- #kafka consumer group id
- KAFKA_GROUP_ID = "notification"
+ Paste the following lines into the `kafka.service` file:
- #sendgrid api key
- SENDGRID_API_KEY = "SG.sdssd.dsdsd.XVSDGFEBGEB.sddsd"
+ ```ini
+ [Unit]
+ Description=Apache Kafka Service
+ Documentation=http://kafka.apache.org/documentation.html
+ Requires=zookeeper.service
- #sendgrid sender email address
- SENDGRID_FROM_MAIL = "test@gmail.com"
+ [Service]
+ Type=simple
+ Environment="JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64"
+ ExecStart=/opt/kafka/bin/kafka-server-start.sh /opt/kafka/config/server.properties
+ ExecStop=/opt/kafka/bin/kafka-server-stop.sh
+ [Install]
+ WantedBy=multi-user.target
```
-4. Install Npm packages
+ Save and exit.
+
+6. Reload systemd:
+ ```bash
+ $ sudo systemctl daemon-reload
```
- ELEVATE/notification/src$ npm install
+
+7. Start Zookeeper:
+
+ ```bash
+ $ sudo systemctl start zookeeper
```
-5. Start Notification server
+ Check status:
+ ```bash
+ $ sudo systemctl status zookeeper
```
- ELEVATE/notification/src$ npm start
+
+ Zookeeper service status should be shown as active (running).
+
+8. Start Kafka:
+
+ ```bash
+ $ sudo systemctl start kafka
```
-
+ Check status:
-
+ ```bash
+ $ sudo systemctl status kafka
+ ```
+
+ Kafka status should be shown as active (running).
+
+### Install PM2
+
+Refer to [How To Set Up a Node.js Application for Production on Ubuntu 22.04](https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-22-04).
+
+**Run the following command**
+
+```bash
+$ sudo npm install pm2@latest -g
+```
+
+## Setting up Repository
+
+### Clone the notification repository to /opt/backend directory
+
+```bash
+opt/backend$ git clone -b develop-2.5 --single-branch "https://github.com/ELEVATE-Project/notification.git"
+```
+
+### Install Npm packages from src directory
+
+```bash
+backend/notification/src$ sudo npm i
+```
+
+### Create .env file in src directory
+
+```bash
+notification/src$ sudo nano .env
+```
+
+Copy-paste the following env variables to the `.env` file:
+
+```env
+# Notification Service Config
+
+# Port on which service runs
+APPLICATION_PORT=3002
+
+# Application environment
+APPLICATION_ENV=development
-# Tech stack
+# Route after the base URL
+APPLICATION_BASE_URL=/notification/
-- Node - 16.0.0
-- Kafka - 3.1.0
-- Jest - 28.1.1
-- MongoDB - 4.1.4
+# Kafka endpoint
+KAFKA_HOST="localhost:9092"
+
+# Kafka topic name
+KAFKA_TOPIC="dev.notification"
+
+# Kafka consumer group id
+KAFKA_GROUP_ID="elevate-notification"
+
+# Sendgrid API key
+SENDGRID_API_KEY="SG.asd9f87a9s8d7f."
+
+# Sendgrid sender email address
+SENDGRID_FROM_MAIL="no-reply@some.org"
+
+# Api doc URL
+API_DOC_URL= "/notification/api-doc"
+
+INTERNAL_ACCESS_TOKEN="internal_access_token"
+ERROR_LOG_LEVEL='silly'
+DISABLE_LOG=false
+DEV_DATABASE_URL=postgres://shikshalokam:slpassword@localhost:9700/elevate_notification
+
+ZEST_ENV= "ZEST_ENV"
+created_time= "2023-12-29T17:04:19.017783534Z"
+custom_metadata= null
+destroyed=false
+version=8
+```
+
+Save and exit.
+
+## Setting up Databases
+
+**Log into the postgres user**
+
+```bash
+sudo su postgres
+```
+
+**Log into psql**
+
+```bash
+psql -p 9700
+```
+
+**Create a database user/role:**
+
+```sql
+CREATE USER shikshalokam WITH ENCRYPTED PASSWORD 'slpassword';
+```
+
+**Create the elevate_notification database**
+
+```sql
+CREATE DATABASE elevate_notification;
+GRANT ALL PRIVILEGES ON DATABASE elevate_notification TO shikshalokam;
+\c elevate_notification
+GRANT ALL ON SCHEMA public TO shikshalokam;
+```
+
+## Running Migrations To Create Tables
+
+**Exit the postgres user account and install sequelize-cli globally**
+
+```bash
+$ sudo npm i sequelize-cli -g
+```
+
+**Navigate to the src folder of notification service and run sequelize-cli migration command:**
+
+```bash
+notification/src$ npx sequelize-cli db:migrate
+```
+
+**Now all the tables must be available in the Citus databases**
+
+## Start the Service
+
+Navigate to the src folder of notification service and run pm2 start command:
+
+```bash
+notification/src$ pm2 start app.js -i 2 --name elevate-notification
+```
+
+#### Run pm2 ls command
+
+```bash
+$ pm2 ls
+```
+
+Output should look like this (Sample output, might slightly differ in your installation):
+
+```bash
+┌────┬─────────────────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
+│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
+├────┼─────────────────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
+│ 19 │ elevate-notification │ default │ 1.0.0 │ cluster │ 88026 │ 47h │ 0 │ online │ 0% │ 113.2mb │ jenkins │ disabled │
+│ 20 │ elevate-notification │ default │ 1.0.0 │ cluster │ 88036 │ 47h │ 0 │ online │ 0% │ 80.3mb │ jenkins │ disabled │
+└────┴─────────────────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
+```
+
+This concludes the service and dependency setup.
+
+
+
+
# Run tests
@@ -196,9 +415,9 @@ npm test
# Used in
-This project was built to be used with [Mentor Service](https://github.com/ELEVATE-Project/mentoring.git) and [User Service](https://github.com/ELEVATE-Project/user.git).
+This project was built to be used with [Mentoring Service](https://github.com/ELEVATE-Project/mentoring.git) and [User Service](https://github.com/ELEVATE-Project/user.git).
-The PWA [repo](https://github.com/ELEVATE-Project/mentoring-mobile-app).
+The frontend/mobile application [repo](https://github.com/ELEVATE-Project/mentoring-mobile-app).
You can learn more about the full implementation of MentorEd [here](https://elevate-docs.shikshalokam.org/.mentorEd/intro) .
From b0d92104e25dda34536b00717984126270b39e1e Mon Sep 17 00:00:00 2001
From: rakeshSgr
Date: Thu, 27 Jun 2024 15:04:53 +0530
Subject: [PATCH 03/12] build and jenkins file added
---
Jenkinsfile | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
build.sh | 15 +++++++++++++++
2 files changed, 63 insertions(+)
create mode 100644 Jenkinsfile
create mode 100644 build.sh
diff --git a/Jenkinsfile b/Jenkinsfile
new file mode 100644
index 00000000..c44eb8b5
--- /dev/null
+++ b/Jenkinsfile
@@ -0,0 +1,48 @@
+node('build-slave') {
+ try {
+
+ String ANSI_GREEN = "\u001B[32m"
+ String ANSI_NORMAL = "\u001B[0m"
+ String ANSI_BOLD = "\u001B[1m"
+ String ANSI_RED = "\u001B[31m"
+ String ANSI_YELLOW = "\u001B[33m"
+
+ ansiColor('xterm') {
+ stage('Checkout') {
+ if (!env.hub_org) {
+ println(ANSI_BOLD + ANSI_RED + "Uh Oh! Please set a Jenkins environment variable named hub_org with value as registery/sunbidrded" + ANSI_NORMAL)
+ error 'Please resolve the errors and rerun..'
+ } else
+ println(ANSI_BOLD + ANSI_GREEN + "Found environment variable named hub_org with value as: " + hub_org + ANSI_NORMAL)
+ }
+
+ cleanWs()
+ checkout scm
+ commit_hash = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
+ build_tag = sh(script: "echo " + params.github_release_tag.split('/')[-1] + "_" + commit_hash + "_" + env.BUILD_NUMBER, returnStdout: true).trim()
+ echo "build_tag: " + build_tag
+
+
+ stage('Build') {
+ env.NODE_ENV = "build"
+ print "Environment will be : ${env.NODE_ENV}"
+ sh('chmod 777 build.sh')
+ sh("bash -x build.sh ${build_tag} ${env.NODE_NAME} ${docker_server}")
+ }
+
+
+ stage('ArchiveArtifacts') {
+ sh ("echo ${build_tag} > build_tag.txt")
+ archiveArtifacts "metadata.json"
+ archiveArtifacts "build_tag.txt"
+ currentBuild.description = "${build_tag}"
+ }
+
+ }
+ }
+ catch (err) {
+ currentBuild.result = "FAILURE"
+ throw err
+ }
+
+}
\ No newline at end of file
diff --git a/build.sh b/build.sh
new file mode 100644
index 00000000..de962863
--- /dev/null
+++ b/build.sh
@@ -0,0 +1,15 @@
+#!/bin/bash
+
+echo "Docker build script"
+
+# Build script
+set -eo pipefail
+
+build_tag=$1
+name=mentoring-service
+node=$2
+org=$3
+
+docker build -f ./Dockerfile --label commitHash=$(git rev-parse --short HEAD) -t ${org}/${name}:${build_tag} .
+
+echo {\"image_name\" : \"${name}\", \"image_tag\" : \"${build_tag}\", \"node_name\" : \"$node\"} > metadata.json
\ No newline at end of file
From c96ba2d9bc1ab63d7a89b8c59fc65d258d273206 Mon Sep 17 00:00:00 2001
From: rakeshSgr
Date: Thu, 27 Jun 2024 15:09:56 +0530
Subject: [PATCH 04/12] service name change
---
build.sh | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/build.sh b/build.sh
index de962863..14a73d55 100644
--- a/build.sh
+++ b/build.sh
@@ -6,7 +6,7 @@ echo "Docker build script"
set -eo pipefail
build_tag=$1
-name=mentoring-service
+name=elevate-notification-service
node=$2
org=$3
From 47efb112b0a14fb6d61b8d830e27a2e8c14d005b Mon Sep 17 00:00:00 2001
From: rakeshSgr
Date: Thu, 27 Jun 2024 15:22:29 +0530
Subject: [PATCH 05/12] build changes
---
build.sh | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/build.sh b/build.sh
index 14a73d55..28c8e410 100644
--- a/build.sh
+++ b/build.sh
@@ -6,7 +6,7 @@ echo "Docker build script"
set -eo pipefail
build_tag=$1
-name=elevate-notification-service
+name=mentoring-notification-service
node=$2
org=$3
From 0f480145e23e721cca17c9d48219b86d30a7b997 Mon Sep 17 00:00:00 2001
From: Akash Shah
Date: Mon, 30 Sep 2024 18:07:11 +0530
Subject: [PATCH 06/12] Update README.md
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index c7529084..b72c227f 100644
--- a/README.md
+++ b/README.md
@@ -35,7 +35,7 @@
-->
-The Mentoring building block enables effective mentoring interactions between mentors and mentees. The capability aims to create a transparent eco-system to learn, connect, solve, and share within communities.MentorED is an open source mentoring application that facilitates peer learning and professional development by creating a community of mentors and mentees.
+The Notification building block enables sending emails via SendGrid service. It can receive informations - To, From, Subject, and Body via API as well as Kafka topic and sends the email using Sendgrid service, the credentials of which can be configured in the .env file. This service also maintains a log of all the emails sent in the Postgres.
From 635c036cc797cd7e22d8303f5e300af63d7c37fe Mon Sep 17 00:00:00 2001
From: Akash Shah
Date: Mon, 30 Sep 2024 18:09:02 +0530
Subject: [PATCH 07/12] Update README.md
---
README.md | 4 +---
1 file changed, 1 insertion(+), 3 deletions(-)
diff --git a/README.md b/README.md
index b72c227f..acd31c3e 100644
--- a/README.md
+++ b/README.md
@@ -417,9 +417,7 @@ npm test
This project was built to be used with [Mentoring Service](https://github.com/ELEVATE-Project/mentoring.git) and [User Service](https://github.com/ELEVATE-Project/user.git).
-The frontend/mobile application [repo](https://github.com/ELEVATE-Project/mentoring-mobile-app).
-
-You can learn more about the full implementation of MentorEd [here](https://elevate-docs.shikshalokam.org/.mentorEd/intro) .
+You can learn more about the full implementation of various capabilities of ELEVATE [here](https://elevate-docs.shikshalokam.org) .
# Team
From f005ae46c16f341301f50ddb8431dd933c6be9a7 Mon Sep 17 00:00:00 2001
From: Rocky <49852443+rakeshSgr@users.noreply.github.com>
Date: Thu, 5 Dec 2024 18:01:33 +0530
Subject: [PATCH 08/12] Update package.json
---
src/package.json | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/package.json b/src/package.json
index 4535f807..b26927fc 100644
--- a/src/package.json
+++ b/src/package.json
@@ -10,7 +10,7 @@
"stage": "node app.js",
"prepare": "cd .. && husky install src/.husky",
"test:integration": "jest --verbose ./integration-test --config=integrationJest.config.js --runInBand",
- "db:init": "sequelize-cli db:create && sequelize-cli db:migrate ",
+ "db:init": "sequelize-cli db:create || echo 'Database already exists or some issue while creating db, Please check' && sequelize-cli db:migrate ",
"db:seed:all": "sequelize-cli db:seed:all"
},
"author": "Aman Kumar Gupta ",
From fab0f89d2d99bd05ccb50092438192bb146cd994 Mon Sep 17 00:00:00 2001
From: Rocky <49852443+rakeshSgr@users.noreply.github.com>
Date: Wed, 11 Dec 2024 15:40:07 +0530
Subject: [PATCH 09/12] Update package.json
---
src/package.json | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/package.json b/src/package.json
index b26927fc..ea30d60f 100644
--- a/src/package.json
+++ b/src/package.json
@@ -11,7 +11,7 @@
"prepare": "cd .. && husky install src/.husky",
"test:integration": "jest --verbose ./integration-test --config=integrationJest.config.js --runInBand",
"db:init": "sequelize-cli db:create || echo 'Database already exists or some issue while creating db, Please check' && sequelize-cli db:migrate ",
- "db:seed:all": "sequelize-cli db:seed:all"
+ "db:seed:all": "sequelize-cli db:seed:all || echo 'Seeded data already exists or some issue while seeding the data, Please check' "
},
"author": "Aman Kumar Gupta ",
"license": "ISC",
From 5eb7d3f3752e7be1960f2fd3ada361ba00683ee5 Mon Sep 17 00:00:00 2001
From: priyanka-TL
Date: Tue, 1 Apr 2025 18:16:30 +0530
Subject: [PATCH 10/12] Smtp integration
---
README.md | 2 +-
dev-ops/integration_test.env | 5 +-
src/.env.sample | 24 ++++++++-
src/constants/common.js | 2 +
src/envVariables.js | 59 ++++++++++++++++++++-
src/generics/helpers/email-notifications.js | 41 +++++++++++---
src/package.json | 1 +
7 files changed, 119 insertions(+), 15 deletions(-)
diff --git a/README.md b/README.md
index acd31c3e..260f1b44 100644
--- a/README.md
+++ b/README.md
@@ -302,7 +302,7 @@ KAFKA_GROUP_ID="elevate-notification"
SENDGRID_API_KEY="SG.asd9f87a9s8d7f."
# Sendgrid sender email address
-SENDGRID_FROM_MAIL="no-reply@some.org"
+SENDGRID_OR_SMTP_FROM_MAIL="no-reply@some.org"
# Api doc URL
API_DOC_URL= "/notification/api-doc"
diff --git a/dev-ops/integration_test.env b/dev-ops/integration_test.env
index 15caaade..c9e5d0a1 100644
--- a/dev-ops/integration_test.env
+++ b/dev-ops/integration_test.env
@@ -4,10 +4,11 @@ APPLICATION_ENV = development
KAFKA_HOST = "localhost:9092"
KAFKA_TOPIC ="testTopic"
SENDGRID_API_KEY = "Yk*********"
-SENDGRID_FROM_MAIL = "****@gmail.com"
+SENDGRID_OR_SMTP_FROM_MAIL = "****@gmail.com"
APPLICATION_BASE_URL = '/notification/'
API_DOC_URL = 'xx'
INTERNAL_ACCESS_TOKEN = 'internal-access-token'
ACCESS_TOKEN_SECRET = 'ACCESS_TOKEN_SECRET'
ERROR_LOG_LEVEL='silly'
-DISABLE_LOG=false
\ No newline at end of file
+DISABLE_LOG=false
+EMAIL_SERVICE=sendgrid
\ No newline at end of file
diff --git a/src/.env.sample b/src/.env.sample
index 2c39afa2..eb7378ea 100644
--- a/src/.env.sample
+++ b/src/.env.sample
@@ -22,7 +22,7 @@ KAFKA_GROUP_ID = "notification"
SENDGRID_API_KEY = "SG.YkHql6d5TwqkKDd1vxpM5w.V7Vxn3ZKzc_EUPC4uuFUCUGqsw"
#sendgrid sender email address
-SENDGRID_FROM_MAIL = "example@test.com"
+SENDGRID_OR_SMTP_FROM_MAIL = "example@test.com"
# Api doc url
API_DOC_URL = '/api-doc'
@@ -31,4 +31,24 @@ API_DOC_URL = '/api-doc'
ERROR_LOG_LEVEL='silly'
#Disable all logs
-DISABLE_LOG=false
\ No newline at end of file
+DISABLE_LOG=false
+
+# Choose the email service: SMTP or SENDGRID
+EMAIL_SERVICE=smtp
+
+# SMTP Configuration
+# Replace with your actual SMTP server details
+# SMTP server host
+SMTP_HOST=sandbox.smtp.mailtrap.io
+
+# SMTP server port (587 for TLS, 465 for SSL)
+SMTP_PORT=25
+
+# Set to 'true' for secure connection (SSL/TLS)
+SMTP_SECURE=false
+
+# SMTP username
+SMTP_USER=1e5955****
+
+# SMTP password
+SMTP_PASS=21065dd******
\ No newline at end of file
diff --git a/src/constants/common.js b/src/constants/common.js
index d6e338ec..083777a9 100644
--- a/src/constants/common.js
+++ b/src/constants/common.js
@@ -13,4 +13,6 @@ module.exports = {
accessTokenSecret: 'hsghasghjab1273JHajnbabsjdj1273981273jhajksdh8y3123yhjkah812398yhjqwe7617237yuhdhhdqwu271',
refreshTokenSecret: '371hkjkjady2y3ihdkajshdkiq23iuekw71yekhaskdvkvegavy23t78veqwexqvxveit6ttxyeeytt62tx236vv',
guestUrls: [],
+ emailServiceSmtp: 'smtp',
+ emailServiceSendgrid: 'sendgrid',
}
diff --git a/src/envVariables.js b/src/envVariables.js
index ae42f9a6..0872e5cb 100644
--- a/src/envVariables.js
+++ b/src/envVariables.js
@@ -31,11 +31,20 @@ let enviromentVariables = {
message: 'Required kafka consumer group id',
optional: true,
},
+ EMAIL_SERVICE: {
+ message: 'Required the email service sendgrid/smtp',
+ optional: false,
+ },
SENDGRID_API_KEY: {
message: 'Required sendgrid api key',
- optional: false,
+ optional: true,
+ requiredIf: {
+ key: 'EMAIL_SERVICE',
+ operator: 'EQUALS',
+ value: 'sendgrid',
+ },
},
- SENDGRID_FROM_MAIL: {
+ SENDGRID_OR_SMTP_FROM_MAIL: {
message: 'Required sendgrid sender email address',
optional: false,
},
@@ -51,6 +60,51 @@ let enviromentVariables = {
message: 'Required disable log level',
optional: false,
},
+ SMTP_PASS: {
+ message: 'Required smtp password',
+ optional: true,
+ requiredIf: {
+ key: 'EMAIL_SERVICE',
+ operator: 'EQUALS',
+ value: 'smtp',
+ },
+ },
+ SMTP_HOST: {
+ message: 'Required smtp host',
+ optional: true,
+ requiredIf: {
+ key: 'EMAIL_SERVICE',
+ operator: 'EQUALS',
+ value: 'smtp',
+ },
+ },
+ SMTP_PORT: {
+ message: 'Required smtp port',
+ optional: true,
+ requiredIf: {
+ key: 'EMAIL_SERVICE',
+ operator: 'EQUALS',
+ value: 'smtp',
+ },
+ },
+ SMTP_USER: {
+ message: 'Required smtp username',
+ optional: true,
+ requiredIf: {
+ key: 'EMAIL_SERVICE',
+ operator: 'EQUALS',
+ value: 'smtp',
+ },
+ },
+ SMTP_SECURE: {
+ message: 'Required smtp secure',
+ optional: true,
+ requiredIf: {
+ key: 'EMAIL_SERVICE',
+ operator: 'EQUALS',
+ value: 'smtp',
+ },
+ },
}
let success = true
@@ -62,6 +116,7 @@ module.exports = function () {
}
let keyCheckPass = true
+ let validRequiredIfOperators = ['EQUALS', 'NOT_EQUALS']
if (
enviromentVariables[eachEnvironmentVariable].optional === true &&
diff --git a/src/generics/helpers/email-notifications.js b/src/generics/helpers/email-notifications.js
index 155b397a..297ce84a 100644
--- a/src/generics/helpers/email-notifications.js
+++ b/src/generics/helpers/email-notifications.js
@@ -7,9 +7,11 @@
//Dependencies
const sgMail = require('@sendgrid/mail')
-sgMail.setApiKey(process.env.SENDGRID_API_KEY)
const logQueries = require('../../database/queries/log')
const request = require('request')
+const nodemailer = require('nodemailer')
+const emailService = process.env.EMAIL_SERVICE || 'sendgrid'
+const common = require('../../constants/common')
/**
* Fetches a file from a given URL.
@@ -77,11 +79,13 @@ async function sendEmail(params) {
attachments: { message: error.message },
}
}
- let fromMail = process.env.SENDGRID_FROM_MAIL
+
+ let fromMail = process.env.SENDGRID_OR_SMTP_FROM_MAIL
if (params.from) {
fromMail = params.from
}
+
const to = params.to.split(',')
let message = {
@@ -98,13 +102,34 @@ async function sendEmail(params) {
message['replyTo'] = params.replyTo
}
try {
- const res = await sgMail.send(message)
- errorResponse = {
- email: to,
- response_code: Number(res[0].statusCode),
- meta: errorMeta,
+ if (emailService == common.emailServiceSmtp) {
+ // SMTP Configuration
+ const transporter = nodemailer.createTransport({
+ host: process.env.SMTP_HOST,
+ port: process.env.SMTP_PORT || 587,
+ secure: false,
+ auth: {
+ user: process.env.SMTP_USER,
+ pass: process.env.SMTP_PASS,
+ },
+ })
+
+ let response = await transporter.sendMail(message)
+ if (!response?.messageId) {
+ throw new Error('Failed to send message')
+ }
+ } else if (emailService == common.emailServiceSendgrid) {
+ sgMail.setApiKey(process.env.SENDGRID_API_KEY)
+ const res = await sgMail.send(message)
+ errorResponse = {
+ email: to,
+ response_code: Number(res[0].statusCode),
+ meta: errorMeta,
+ }
+ await logQueries.createLog(errorResponse)
+ } else {
+ throw new Error('emailService provided should be either smtp or sendgrid')
}
- await logQueries.createLog(errorResponse)
} catch (error) {
errorResponse = {
email: to,
diff --git a/src/package.json b/src/package.json
index ea30d60f..3622f7a7 100644
--- a/src/package.json
+++ b/src/package.json
@@ -31,6 +31,7 @@
"jsonwebtoken": "^8.5.1",
"kafkajs": "^2.2.2",
"module-alias": "^2.2.2",
+ "nodemailer": "^6.10.0",
"pg": "^8.11.3",
"pg-hstore": "^2.3.4",
"require-all": "^3.0.0",
From e682f8bb83cb2c430882f6d1d1589d6891db4716 Mon Sep 17 00:00:00 2001
From: priyanka-TL
Date: Tue, 1 Apr 2025 18:31:05 +0530
Subject: [PATCH 11/12] PR comment addressed
---
README.md | 2 +-
dev-ops/integration_test.env | 2 +-
src/.env.sample | 2 +-
src/envVariables.js | 4 ++--
src/generics/helpers/email-notifications.js | 22 ++++++++++++++++++---
5 files changed, 24 insertions(+), 8 deletions(-)
diff --git a/README.md b/README.md
index 260f1b44..90ec6d01 100644
--- a/README.md
+++ b/README.md
@@ -302,7 +302,7 @@ KAFKA_GROUP_ID="elevate-notification"
SENDGRID_API_KEY="SG.asd9f87a9s8d7f."
# Sendgrid sender email address
-SENDGRID_OR_SMTP_FROM_MAIL="no-reply@some.org"
+FROM_EMAIL="no-reply@some.org"
# Api doc URL
API_DOC_URL= "/notification/api-doc"
diff --git a/dev-ops/integration_test.env b/dev-ops/integration_test.env
index c9e5d0a1..74a19bec 100644
--- a/dev-ops/integration_test.env
+++ b/dev-ops/integration_test.env
@@ -4,7 +4,7 @@ APPLICATION_ENV = development
KAFKA_HOST = "localhost:9092"
KAFKA_TOPIC ="testTopic"
SENDGRID_API_KEY = "Yk*********"
-SENDGRID_OR_SMTP_FROM_MAIL = "****@gmail.com"
+FROM_EMAIL = "****@gmail.com"
APPLICATION_BASE_URL = '/notification/'
API_DOC_URL = 'xx'
INTERNAL_ACCESS_TOKEN = 'internal-access-token'
diff --git a/src/.env.sample b/src/.env.sample
index eb7378ea..731950b7 100644
--- a/src/.env.sample
+++ b/src/.env.sample
@@ -22,7 +22,7 @@ KAFKA_GROUP_ID = "notification"
SENDGRID_API_KEY = "SG.YkHql6d5TwqkKDd1vxpM5w.V7Vxn3ZKzc_EUPC4uuFUCUGqsw"
#sendgrid sender email address
-SENDGRID_OR_SMTP_FROM_MAIL = "example@test.com"
+FROM_EMAIL = "example@test.com"
# Api doc url
API_DOC_URL = '/api-doc'
diff --git a/src/envVariables.js b/src/envVariables.js
index 0872e5cb..fcb985fc 100644
--- a/src/envVariables.js
+++ b/src/envVariables.js
@@ -44,8 +44,8 @@ let enviromentVariables = {
value: 'sendgrid',
},
},
- SENDGRID_OR_SMTP_FROM_MAIL: {
- message: 'Required sendgrid sender email address',
+ FROM_EMAIL: {
+ message: 'Required sender email address',
optional: false,
},
API_DOC_URL: {
diff --git a/src/generics/helpers/email-notifications.js b/src/generics/helpers/email-notifications.js
index 297ce84a..3b5718e6 100644
--- a/src/generics/helpers/email-notifications.js
+++ b/src/generics/helpers/email-notifications.js
@@ -61,10 +61,26 @@ async function sendEmail(params) {
if (params.attachments && params.attachments.length > 0) {
const processAttachment = async (attachment) => {
const attachmentContent = await fetchFileByUrl(attachment)
+
+ // Common attachment properties
+ const baseAttachment = {
+ filename: attachment.filename,
+ content: attachmentContent.content,
+ }
+
+ if (emailService === common.emailServiceSmtp) {
+ // For SMTP, just return the content as is with contentType
+ return {
+ ...baseAttachment,
+ contentType: attachment.type, // Add content type for SMTP
+ }
+ }
+
+ // For SendGrid, encode the content in base64
return {
+ ...baseAttachment,
content: Buffer.from(attachmentContent.content).toString('base64'),
- filename: attachment.filename,
- type: attachment.type,
+ type: attachment.type, // Required for SendGrid
}
}
@@ -80,7 +96,7 @@ async function sendEmail(params) {
}
}
- let fromMail = process.env.SENDGRID_OR_SMTP_FROM_MAIL
+ let fromMail = process.env.FROM_EMAIL
if (params.from) {
fromMail = params.from
From a9a29647d7aef2f5f2f87d1bf6046d7cea4f66ad Mon Sep 17 00:00:00 2001
From: priyanka-TL
Date: Fri, 11 Apr 2025 12:54:58 +0530
Subject: [PATCH 12/12] secure take from env
---
src/generics/helpers/email-notifications.js | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/generics/helpers/email-notifications.js b/src/generics/helpers/email-notifications.js
index 3b5718e6..3cbf1b85 100644
--- a/src/generics/helpers/email-notifications.js
+++ b/src/generics/helpers/email-notifications.js
@@ -123,7 +123,7 @@ async function sendEmail(params) {
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT || 587,
- secure: false,
+ secure: process.env.SMTP_SECURE || false,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS,