-
Notifications
You must be signed in to change notification settings - Fork 98
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added subscribe fitflex feature (#457)
- Loading branch information
1 parent
fbe2a3c
commit c7df98b
Showing
10 changed files
with
562 additions
and
155 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
MONGO_URI=mongodb://localhost:27017/fitflexDB | ||
JWT_SECRET=scijyasfy7dsvegdffvbfbfgg435tgrsnbgfgn | ||
PORT=5000 | ||
EMAIL_ID= | ||
PASS_KEY= | ||
ADMIN_EMAIL_ID= |
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,25 @@ | ||
import express from "express"; | ||
import dotenv from "dotenv"; | ||
import connectDB from "./utils/db.js"; | ||
import cors from "cors"; | ||
import newsletterRoutes from "./routes/newsletterRoute.js"; | ||
|
||
|
||
|
||
dotenv.config(); | ||
const app = express(); | ||
connectDB(); | ||
|
||
app.use(express.json()); | ||
|
||
// To avoid cross-origin error | ||
app.use(cors()); | ||
|
||
// Use the imported routes | ||
app.use("/api/newsletter", newsletterRoutes); | ||
|
||
|
||
const PORT = process.env.PORT || 5000; | ||
app.listen(PORT, () => { | ||
console.log(`Server running on port ${PORT}`); | ||
}); |
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 NewsLetter from "../models/Newsletter.js"; | ||
import { sendMailToSubscriber } from "../sendSuscribedMail.js"; | ||
|
||
export async function saveNewsletter(req, res) { | ||
try { | ||
const { name, email } = req.body; | ||
|
||
if (!name || !email) { | ||
return res.status(400).json({ message: "All fields are required." }); | ||
} | ||
|
||
// Create new contact document | ||
const newNewsLetter = new NewsLetter({ name, email }); | ||
sendMailToSubscriber(newNewsLetter) | ||
await newNewsLetter.save(); | ||
res | ||
.status(201) | ||
.json({ message: "Contact form submitted successfully!", newNewsLetter }); | ||
} catch (error) { | ||
console.error("Error saving contact form:", error); | ||
res.status(500).json({ message: "Failed to submit contact form.", error }); | ||
} | ||
} | ||
|
||
export async function getNewsletter(req, res) { | ||
res.send('hello newsletter') | ||
} |
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,23 @@ | ||
import mongoose from "mongoose"; | ||
|
||
const newsletterSchema = new mongoose.Schema({ | ||
name: { | ||
type: String, | ||
required: true, | ||
trim: true | ||
}, | ||
email: { | ||
type: String, | ||
required: true, | ||
trim: true, | ||
match: [/^\S+@\S+\.\S+$/, "Please enter a valid email address"] | ||
}, | ||
subscribedAt: { | ||
type: Date, | ||
default: Date.now | ||
} | ||
}); | ||
|
||
const Newsletter = mongoose.model("Newsletter", newsletterSchema); | ||
|
||
export default Newsletter; |
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,23 @@ | ||
{ | ||
"name": "server", | ||
"version": "1.0.0", | ||
"main": "app.js", | ||
"type": "module", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"keywords": [ | ||
"megablog" | ||
], | ||
"author": "", | ||
"license": "ISC", | ||
"description": "", | ||
"dependencies": { | ||
"cors": "^2.8.5", | ||
"dotenv": "^16.4.5", | ||
"express": "^4.21.1", | ||
"mongoose": "^8.8.0", | ||
"nodemailer": "^6.9.16", | ||
"nodemon": "^3.1.7" | ||
} | ||
} |
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,8 @@ | ||
import express from "express"; | ||
const router = express.Router(); | ||
import { getNewsletter, saveNewsletter } from "../controller/newsletterController.js"; | ||
|
||
router.post("/suscribe", saveNewsletter); | ||
router.get("/getNewsletter", getNewsletter); | ||
|
||
export default router; |
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,55 @@ | ||
import nodemailer from 'nodemailer'; | ||
import dotenv from 'dotenv'; | ||
dotenv.config(); | ||
|
||
const sendMailToSubscriber = (userdata) => { | ||
const transporter = nodemailer.createTransport({ | ||
service: "gmail", | ||
host: "smtp.gmail.com", | ||
port: 587, | ||
secure: false, | ||
auth: { | ||
user: process.env.EMAIL_ID, | ||
pass: process.env.PASS_KEY, | ||
}, | ||
}); | ||
|
||
async function main() { | ||
await transporter.sendMail({ | ||
from: { | ||
name: "FitFlex", | ||
address: process.env.EMAIL_ID, | ||
}, | ||
to: userdata.email, | ||
subject: "Welcome to FitFlex! 💪 You've successfully subscribed", | ||
text: "Thank you for subscribing to FitFlex! Get ready to transform your fitness journey!", | ||
html: ` | ||
<div style="background-color: #fff5e0; color: #333; padding: 20px; font-family: Arial, sans-serif;"> | ||
<div style="max-width: 600px; margin: 0 auto; background: white; padding: 20px; border-radius: 10px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);"> | ||
<h2 style="text-align: center; color: #ff6600;">Welcome to FitFlex, ${userdata.name}!</h2> | ||
<p style="font-size: 16px; line-height: 1.6; color: #555;"> | ||
Hi ${userdata.name}, | ||
</p> | ||
<p style="font-size: 16px; line-height: 1.6; color: #555;"> | ||
Congratulations on subscribing to FitFlex! You’re now part of a community dedicated to helping you achieve your fitness goals—whether it's weight loss, muscle gain, or overall fitness. Our daily workout plans will guide you through each day, step by step! | ||
</p> | ||
<p style="font-size: 16px; line-height: 1.6; color: #555;"> | ||
Your fitness journey starts here, and we're excited to support you every step of the way. Track your progress, stay consistent, and transform your life with FitFlex. | ||
</p> | ||
<p style="font-size: 16px; line-height: 1.6; color: #555;"> | ||
Stay motivated, and don’t forget to check in daily for your new workout plan! Let's get stronger together. | ||
</p> | ||
<p style="font-size: 16px; line-height: 1.6; color: #555;"> | ||
Best Regards, <br/> | ||
The FitFlex Team | ||
</p> | ||
</div> | ||
</div> | ||
`, | ||
}); | ||
} | ||
|
||
main().catch(console.error); | ||
}; | ||
|
||
export { sendMailToSubscriber }; |
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,15 @@ | ||
import mongoose from "mongoose"; | ||
import dotenv from "dotenv"; | ||
dotenv.config(); | ||
|
||
const connectDB = async () => { | ||
try { | ||
await mongoose.connect(process.env.MONGO_URI); | ||
console.log("MongoDB Connected"); | ||
} catch (error) { | ||
console.error("Database connection error:", error); | ||
process.exit(1); | ||
} | ||
}; | ||
|
||
export default connectDB; |
Oops, something went wrong.