Skip to content

Commit

Permalink
Merge pull request #131 from haseebzaki-07/new_branch_2
Browse files Browse the repository at this point in the history
Added notification for bookings
  • Loading branch information
dhairyagothi authored Oct 8, 2024
2 parents 7a3d44b + 934a08a commit 1193942
Show file tree
Hide file tree
Showing 7 changed files with 265 additions and 37 deletions.
27 changes: 13 additions & 14 deletions backend/controllers/WheelchairController.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import WheelchairBooking from "../models/WheelChairBooking.js";
import { io } from "../index.js";

// Controller to handle booking creation
export const createWheelchairBooking = async (req, res) => {
const { station, bookingDate, bookingTime, wheelchairType } = req.body;

if (!station || !bookingDate || !bookingTime) {
return res
.status(400)
.json({
message:
"Please provide all required fields: station, bookingDate, bookingTime",
});
}

try {
const newBooking = new WheelchairBooking({
station,
Expand All @@ -22,11 +15,17 @@ export const createWheelchairBooking = async (req, res) => {

const savedBooking = await newBooking.save();

res.status(201).json(savedBooking);
} catch (error) {
console.error("Error creating wheelchair booking:", error);
// Emit real-time booking confirmation to the client
io.emit("wheelchairBookingConfirmation", {
message: `Your booking at ${station} for ${bookingDate} has been confirmed.`,
booking: savedBooking,
});

// Send back HTTP response as well
res
.status(500)
.json({ message: "Server error. Could not create wheelchair booking." });
.status(201)
.json({ message: "Booking created successfully", booking: savedBooking });
} catch (error) {
res.status(500).json({ message: "Server error." });
}
};
6 changes: 6 additions & 0 deletions backend/controllers/cloakroomController.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { io } from "../index.js";
import CloakroomBooking from "../models/CloakroomBooking.js";

export const createCloakroomBooking = async (req, res) => {
Expand All @@ -21,6 +22,11 @@ export const createCloakroomBooking = async (req, res) => {

const savedBooking = await newBooking.save();

io.emit("cloakroomBookingConfirmation", {
message: `Your booking at ${station} for ${items} has been confirmed.`,
booking: savedBooking,
});

res.status(201).json(savedBooking);
} catch (error) {
console.error("Error creating cloakroom booking:", error);
Expand Down
6 changes: 6 additions & 0 deletions backend/controllers/coolieController.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { io } from "../index.js";
import CoolieBooking from "../models/CoolieBooking.js";

export const createCoolieBooking = async (req, res) => {
Expand Down Expand Up @@ -35,6 +36,11 @@ export const createCoolieBooking = async (req, res) => {

const savedBooking = await coolieBooking.save();

io.emit("coolieBookingConfirmation", {
message: `Your booking at ${station} for coolie has been confirmed.`,
booking: savedBooking,
});

res.status(201).json({
message: "Coolie booking created successfully.",
data: savedBooking,
Expand Down
73 changes: 50 additions & 23 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,63 @@
import express from 'express';
import cors from 'cors';
import connectDB from './config/dbConnection.js';
import cookieParser from 'cookie-parser';
import express from "express";
import cors from "cors";
import connectDB from "./config/dbConnection.js";
import cookieParser from "cookie-parser";
import { createServer } from "http";
import { Server } from "socket.io";

const app = express();
const server = createServer(app);

const port = process.env.PORT || 3000;

app.use(cors({
origin: 'http://localhost:5173',
methods: ['GET', 'POST'],
allowedHeaders: ['Content-Type'],
credentials: true
})); // Enable CORS
app.use(
cors({
origin: ["http://localhost:5173", "http://127.0.0.1:5500"],
methods: ["GET", "POST"],
allowedHeaders: ["Content-Type"],
credentials: true,
})
);

app.use(cookieParser()); // Parse cookies
app.use(express.json()); // Parse incoming request bodies in a middleware before your handlers
app.use(express.urlencoded({ extended: true })); // Parse URL-encoded bodies
app.use(cookieParser());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// Connect to MongoDB
connectDB();

// Routes
import authRoutes from './routes/authRoutes.js';
import authRoutes from "./routes/authRoutes.js";

app.use('/auth', authRoutes);
app.use('/api', authRoutes);
app.use("/auth", authRoutes);
app.use("/api", authRoutes);

app.get('/', (req, res) => {
res.send('Working...');
app.get("/", (req, res) => {
res.send("Working...");
});
// Start the server
app.listen(port, () => {



const io = new Server(server, {
cors: {
origin: ["http://localhost:5173", "http://127.0.0.1:5500"],
methods: ["GET", "POST"],
credentials: true,
},
});

io.on("connection", (socket) => {
console.log("A user connected:", socket.id);

socket.on("disconnect", () => {
console.log("User disconnected:", socket.id);
});

socket.on("error", (err) => {
console.error("Socket error:", err);
});
});

server.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
});

export { io };
134 changes: 134 additions & 0 deletions backend/package-lock.json

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

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"side-channel": "^1.0.6",
"sift": "^17.1.3",
"simple-update-notifier": "^2.0.0",
"socket.io": "^4.8.0",
"sparse-bitfield": "^3.0.3",
"statuses": "^2.0.1",
"supports-color": "^5.5.0",
Expand Down
Loading

0 comments on commit 1193942

Please sign in to comment.