Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Email service #35

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions config/database.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
const { Sequelize } = require('sequelize');
require('dotenv').config();
const Sequelize = require('sequelize');
const pg = require('pg');

const sequelize = new Sequelize(process.env.DB_NAME, process.env.DB_USER, process.env.DB_PASSWORD, {
host: process.env.DB_HOST,
dialect: 'postgres',
});
const sequelize = new Sequelize(
process.env.DB_NAME,
process.env.DB_USER,
process.env.DB_PASSWORD,
{
host: "localhost",
dialect: "postgres",
dialectModule: pg,
}
)

module.exports = sequelize;
module.exports = sequelize;
12 changes: 12 additions & 0 deletions config/emailConfig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const nodemailer= require('nodemailer');


const transporter = nodemailer.createTransport({
service: 'Gmail',
auth: {
user: process.env.EMAIL_ID,
pass: process.env.EMAIL_PASS
}
});

module.exports = transporter;
1 change: 1 addition & 0 deletions controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const registerUser = async (req, res) => {
const hashedPassword = await bcrypt.hash(req.body.password, 10);
const user = await User.create({
username: req.body.username,
email: req.body.email,
password: hashedPassword,
role: req.body.role
});
Expand Down
18 changes: 17 additions & 1 deletion controllers/bookingController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const sequelize = require('../config/database');
const Booking = require('../models/booking');
const Train = require('../models/train');
const User = require("../models/user")
const sendBasicEmail = require('../utils/emailService');

const bookSeat = async (req, res) => {
const { train_id } = req.body;
Expand All @@ -16,10 +17,13 @@ const bookSeat = async (req, res) => {
try {
const train = await Train.findByPk(train_id, { transaction: t });
if (!train) {
console.log('Could not find train');
await t.rollback();
return res.status(404).json({ message: 'Train not found' });
}

if (train.available_seats > 0) {

train.available_seats -= 1;
await train.save({ transaction: t });

Expand All @@ -28,7 +32,19 @@ const bookSeat = async (req, res) => {
TrainId: train_id
}, { transaction: t });

await t.commit();
await t.commit();

// Send email to the user
try{
const user = await User.findByPk(userId);
const mailSubject = 'Booking Confirmation';
const mailBody = `Dear ${user.username},\n\nYour booking has been confirmed. Details:\nBooking ID: ${booking.id}\nBooking Time: ${booking.booking_time}\nTrain Name: ${train.train_name}\nSource: ${train.source}\nDestination: ${train.destination}\n\nThank you for choosing our service.`;
await sendBasicEmail(user.email, mailSubject, mailBody);
console.log('Email sent');
}
catch(error){
console.log('Error sending email',error);
}
res.status(201).json(booking);
} else {
await t.rollback();
Expand Down
5 changes: 5 additions & 0 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ const User = sequelize.define('User', {
autoIncrement: true,
primaryKey: true
},
email:{
type: DataTypes.STRING,
unique: true,
allowNull: false
},
username: {
type: DataTypes.STRING,
unique: true,
Expand Down
136 changes: 129 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.19.2",
"i": "^0.3.7",
"iconv-lite": "^0.6.3",
"jsonwebtoken": "^9.0.2",
"nodemailer": "^6.9.13",
"nodemon": "^3.1.0",
"pg": "^8.11.5",
"pg-hstore": "^2.3.4",
"pg-types": "^4.0.2",
"sequelize": "^6.37.3"
}
}
20 changes: 20 additions & 0 deletions utils/emailService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const transporter = require('../config/emailConfig');


const sendBasicEmail = async ( mailTo, mailSubject, mailBody) => {
try {
const response = await transporter.sendMail({
from: {
name: 'IRCTC Backend Service',
address: "[email protected]"
},
to: mailTo,
subject: mailSubject,
text: mailBody
});
} catch (error) {
console.log(error);
}
}

module.exports = sendBasicEmail;