-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #516 from haseebzaki-07/new_branch_%
Add complaints with email
- Loading branch information
Showing
10 changed files
with
232 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
|
||
import nodemailer from 'nodemailer' | ||
import Complaint from '../models/Complaint.js'; | ||
|
||
const transporter = nodemailer.createTransport({ | ||
service: 'gmail', | ||
auth: { | ||
user: process.env.EMAIL_USER, | ||
pass: process.env.EMAIL_PASS | ||
} | ||
}); | ||
|
||
|
||
const sendConfirmationEmail = (email, name) => { | ||
const mailOptions = { | ||
from: process.env.EMAIL_USER, | ||
to: email, | ||
subject: 'Complaint Received - Station Saarthi', | ||
text: `Dear ${name},\n\nThank you for submitting your complaint. We have received your complaint and will look into the matter soon.\n\nBest regards,\nStation Saarthi` | ||
}; | ||
|
||
return transporter.sendMail(mailOptions); | ||
}; | ||
|
||
const submitComplaint = async (req, res) => { | ||
const { name, phoneNumber, email, complain } = req.body; | ||
|
||
if (!name || !phoneNumber || !email || !complain) { | ||
return res.status(400).json({ message: 'All fields are required.' }); | ||
} | ||
|
||
try { | ||
// Create a new complaint record in the database | ||
const newComplaint = new Complaint({ name, phoneNumber, email, complain }); | ||
await newComplaint.save(); | ||
|
||
// Send a confirmation email to the user | ||
await sendConfirmationEmail(email, name); | ||
|
||
res.status(200).json({ message: 'Complaint submitted successfully! We will get back to you soon.' }); | ||
} catch (error) { | ||
console.error('Error submitting complaint:', error); | ||
res.status(500).json({ message: 'There was an error submitting your complaint. Please try again later.' }); | ||
} | ||
} | ||
|
||
export default submitComplaint; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import mongoose from "mongoose"; | ||
|
||
const complaintSchema = new mongoose.Schema({ | ||
name: { | ||
type: String, | ||
required: true | ||
}, | ||
phoneNumber: { | ||
type: String, | ||
required: true | ||
}, | ||
email: { | ||
type: String, | ||
required: true | ||
}, | ||
complain: { | ||
type: String, | ||
required: true | ||
}, | ||
createdAt: { | ||
type: Date, | ||
default: Date.now | ||
} | ||
}); | ||
|
||
const Complaint = mongoose.model('Complaint', complaintSchema); | ||
export default Complaint; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// routes/complaint.js | ||
import express from 'express' | ||
import submitComplaint from '../controllers/complaintController.js'; | ||
|
||
|
||
const router = express.Router(); | ||
|
||
// Handle complaint submission | ||
router.post('/complaint', submitComplaint); | ||
|
||
export default router |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.