require('dotenv').config(); const express = require('express'); const nodemailer = require("nodemailer"); const { Pool } = require('pg'); const app = express(); const PORT = 3000; const transporter = nodemailer.createTransport({ host: "smtp.gmail.com", port: 587, secure: false, auth: { user: process.env.ETHEREAL_USER, pass: process.env.ETHEREAL_PASS, }, }); // Serve static files app.use(express.static('public')); app.use(express.json()); // PostgreSQL connection const pool = new Pool({ user: process.env.DB_USER, host: process.env.DB_HOST, database: process.env.DB_DATABASE, password: process.env.DB_PASSWORD, port: process.env.DB_PORT }); // Create booking app.post('/api/book', async (req, res) => { const { name, email, phone, date, time } = req.body; try { const result = await pool.query( `INSERT INTO bookings (name, email, phone, booking_date, booking_time) VALUES ($1, $2, $3, $4, $5) RETURNING *`, [name, email, phone, date, time] ); const booking = result.rows[0]; // Send confirmation email const customerEmail = await transporter.sendMail({ from: '"Booking System" ', to: email, // send to the customer subject: "Booking Request", text: `Hi ${name}, your booking request for ${date} at ${time} has been received and is being processed. We will get back to you as soon as possible.`, html: `

Hi ${name},

your booking request for ${date} at ${time} has been received and is being processed. We will get back to you as soon as possible.


Note: This is a test email


`, }); const ownerEmail = await transporter.sendMail({ from: '"Booking System" ', to: email, // send to the admin subject: "Booking Request", text: `${name} wants to book in for ${date} at ${time}.`, html: `

${name} wants to book in for ${date} at ${time}.


Please give the customer a response under:
booking.4l3ks.com


Note: This is a test email


`, }); res.json({ reply: "Booking successful!", booking }); } catch (err) { console.error("Error inserting booking:", err); res.status(500).json({ reply: "Database error" }); } }); // Get all bookings app.get('/api/book', async (req, res) => { try { const result = await pool.query('SELECT * FROM bookings'); res.json(result.rows); } catch (err) { console.error("Error fetching bookings:", err); res.status(500).send('Database error'); } }); // Update booking status app.put('/api/book', async (req, res) => { const { accepted, id } = req.body; try { const result = await pool.query( `UPDATE bookings SET accepted = $1 WHERE id = $2 RETURNING *`, [accepted, id] ); if (result.rows.length === 0) { return res.status(404).json({ reply: "Booking not found" }); } const booking = result.rows[0]; // updated booking row const { name, email, booking_date, booking_time } = booking; // Send confirmation email only if booking is accepted if (accepted) { const customerConf = await transporter.sendMail({ from: '"Booking System" ', to: email, // customer's email from DB subject: "Booking Confirmation", text: `Hi ${name}, your booking request for ${booking_date} at ${booking_time} has been accepted.`, html: `

Hi ${name},

Your booking request for ${booking_date} at ${booking_time} has been accepted.


Note: This is a test email


`, }); console.log("Email sent:", customerConf.messageId); console.log("Preview URL:", nodemailer.getTestMessageUrl(customerConf)); } res.json({ reply: "Booking updated", booking }); } catch (err) { console.error("Error updating booking:", err); res.status(500).send("Database error"); } }); // Delete booking app.delete('/api/book/:id', async (req, res) => { const { id } = req.params; try { const result = await pool.query( `DELETE FROM bookings WHERE id = $1 RETURNING *`, [id] ); if (result.rows.length === 0) { return res.status(404).json({ reply: "Booking not found" }); } const booking = result.rows[0]; // deleted booking data const { name, email, booking_date, booking_time } = booking; // Send cancellation email const cancelEmail = await transporter.sendMail({ from: '"Booking System" ', to: email, subject: "Booking Cancelled", text: `Hi ${name}, your booking for ${booking_date} at ${booking_time} has been cancelled.`, html: `

Hi ${name},

We’re letting you know that your booking for ${booking_date} at ${booking_time} has been cancelled.


Note: This is a test email


`, }); console.log("Cancellation email sent:", cancelEmail.messageId); console.log("Preview URL:", nodemailer.getTestMessageUrl(cancelEmail)); res.json({ reply: "Booking deleted", booking }); } catch (err) { console.error("Error deleting booking:", err); res.status(500).send("Database error"); } }); app.listen(PORT, () => { console.log(`Server running at http://localhost:${PORT}`); });