Skip to content

Commit

Permalink
📈 Completed Email
Browse files Browse the repository at this point in the history
  • Loading branch information
Ayush-Vish committed Oct 30, 2024
1 parent 2374bef commit b687d9b
Show file tree
Hide file tree
Showing 8 changed files with 3,270 additions and 263 deletions.
40 changes: 24 additions & 16 deletions api-gateway/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ http {
# upstream agent {
# server agent:5000;
# }

# upstream email-service {
# server email-service:5005
# }
server {
listen 80;
server_name api-gateway;
Expand All @@ -36,27 +38,33 @@ http {
# location /agent {
# proxy_pass http://agent;
# }
# location /email {
# proxy_pass http://email
# }
}

# server {
server {
# listen 443 ssl;
# server_name api-gateway;

# # Add your SSL certificates here
# ssl_certificate /etc/nginx/ssl/nginx.crt;
# ssl_certificate_key /etc/nginx/ssl/nginx.key;

# location /auth {
# proxy_pass http://auth;
# }
# location /client {
# proxy_pass http://client;
# }
# location /shopkeeper {
# proxy_pass http://shopkeeper;
# }
# location /agent {
# proxy_pass http://agent;
# }
# }
}
location /auth {
proxy_pass http://auth;
}
location /client {
proxy_pass http://client;
}
location /shopkeeper {
proxy_pass http://shopkeeper;
}
location /agent {
proxy_pass http://agent;
}
location /email {
proxy_pass http://email;
}
}
}
49 changes: 26 additions & 23 deletions customer/src/controllers/booking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,9 @@ export const createBooking = async (
// Validate booking time (e.g., "10:00 AM", "2:30 PM")
const timeRegex = /^(0?[1-9]|1[0-2]):([0-5]\d)\s?(AM|PM)$/i;
if (!timeRegex.test(bookingTime)) {
res
.status(400)
.json({
message: 'Invalid booking time format. Use format like "10:00 AM"',
});
res.status(400).json({
message: 'Invalid booking time format. Use format like "10:00 AM"',
});
return;
}

Expand Down Expand Up @@ -129,34 +127,39 @@ export const createBooking = async (
const populatedBooking = await Booking.findById(newBooking._id)
.populate('service', 'name description')
.populate('client', 'name email')
.populate('serviceProvider', 'name email');
.populate('serviceProvider', 'name email')
.exec();
console.log('PopulatedBooking ', populatedBooking);
await populatedBooking.save();

if (populatedBooking) {
// Extract necessary information
const clientEmail = (populatedBooking.client as any).email;
const clientName = (populatedBooking.client as any).name;
const serviceName = (populatedBooking.service as any).name;
const bookingDate = (populatedBooking.bookingDate as any).toDateString();
const bookingTime = populatedBooking.bookingTime as any;
const serviceProviderEmail = (populatedBooking.serviceProvider as any)
.email;
const serviceProviderName = (populatedBooking.serviceProvider as any)
.name;

// Send email to client
console.log("Client ema", clientEmail)
await addEmailJob({
to: (populatedBooking.client as any).email,
email: clientEmail,
subject: 'Booking Confirmation',
text: `Hello ${
(populatedBooking.client as any).name
},\n\nYour booking for ${(populatedBooking.service as any).name} on ${(
populatedBooking.bookingDate as any
).toDateString()} at ${
populatedBooking.bookingTime as any
} has been confirmed.\n\nThank you for choosing our service.\n\nBest,\nService Provider`,
content: `Hello ${clientName},\n\nYour booking for ${serviceName} on ${bookingDate} at ${bookingTime} has been confirmed.\n\nThank you for choosing our service.\n\nBest,\nService Provider`,
});

console.log("Client ema", serviceProviderEmail);
// Send email to service provider
await addEmailJob({
to: (populatedBooking.serviceProvider as any).email,
email: serviceProviderEmail,
subject: 'New Booking',
text: `Hello ${
(populatedBooking.serviceProvider as any).name
},\n\nYou have a new booking for ${
(populatedBooking.service as any).name
} on ${(populatedBooking.bookingDate as any).toDateString()} at ${
populatedBooking.bookingTime as any
}.\n\nBest,\nService`,
content: `Hello ${serviceProviderName},\n\nYou have a new booking for ${serviceName} on ${bookingDate} at ${bookingTime}.\n\nBest,\nService`,
});
} else {
res.status(404).json({ message: 'Failed to Send email' });
}

res.status(201).json(newBooking);
Expand Down
13 changes: 13 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ services:
# - auth
# - client
# - shopkeeper
- email-service
- redis


Expand Down Expand Up @@ -71,4 +72,16 @@ services:
environment:
- ALLOW_EMPTY_PASSWORD=yes
command: ["redis-server", "--appendonly", "yes"]
email-service:
build:
context: .
dockerfile: Dockerfile
args:
PROJECT: email-service
env_file:
- .env
environment:
- NODE_ENV=production
ports:
- "5005:5005"

37 changes: 28 additions & 9 deletions email-service/src/routes/routes.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,52 @@
import express from 'express';
import { Resend } from 'resend';
import nodemailer from 'nodemailer';
import dotenv from 'dotenv';

dotenv.config();

const router = express.Router();
const resend = new Resend(process.env.RESEND_API_KEY);

const transporter = nodemailer.createTransport({
service: 'Gmail',
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
user: process.env.GMAIL_USER,
pass: process.env.GMAIL_APP_PASSWORD,
},
});

router.use(express.json()); // Middleware to parse JSON request bodies

router.get('/', (req, res) => {
res.send({ message: 'Welcome to email-service!' });
});

router.post('/send', async (req, res) => {
console.log('Request body:', req.body);
const { email, subject, content } = req.body;

if (!email || !subject || !content) {
return res.status(400).json({ error: 'Email, subject, and content are required' });
}
console.log('Sending email to:', email);

try {
const response = await resend.emails.send({
from: "Acme <[email protected]>",
// Send email using Nodemailer
const info = await transporter.sendMail({
from: `Excited User <${process.env.GMAIL_USER}>`, // Sender email
to: email,
subject,
html: content,
})
subject: subject,
html: content, // HTML content (or use 'text' property for plain text)
});

return res.status(200).json({ message: 'Email sent successfully', data: response.data });
console.log('Email sent:', info);
return res.status(200).json({ message: 'Email sent successfully', data: info });
} catch (error) {
console.error('Error sending email:', error);
res.status(500).json({ error: 'Failed to send email', details: error.message });
}
});

export default router;
export default router;
Loading

0 comments on commit b687d9b

Please sign in to comment.