-
Notifications
You must be signed in to change notification settings - Fork 3
/
socket.ts
116 lines (106 loc) · 3.71 KB
/
socket.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { Server as HttpServer } from "http";
import { Server, Socket } from "socket.io";
import { OrderModel } from "./models/orderModel";
export const userSocketMap = new Map<string, Set<string>>(); // Map of userId -> Set of socketIds
// Declare a variable to hold the Socket.IO instance
let io: Server;
export const initSocketServer = (httpServer: HttpServer) => {
// Initialize Socket.IO server
io = new Server(httpServer, {
cors: {
origin: "http://localhost:5173/", // Replace with your client URL
methods: ["GET", "POST"],
},
});
// Handle socket connection
io.on("connection", (socket: Socket) => {
console.log(`New client connected: ${socket.id}`);
// For saving the socket ID of the user
socket.on("user_connected", (userId: string) => {
if (!userSocketMap.has(userId)) {
userSocketMap.set(userId, new Set());
}
userSocketMap.get(userId)?.add(socket.id);
console.log(`User ${userId} is connected with socket ID: ${socket.id}`);
});
// Handle socket disconnection
socket.on("disconnect", () => {
console.log(`Client disconnected: ${socket.id}`);
for (const [userId, socketIds] of userSocketMap.entries()) {
if (socketIds.has(socket.id)) {
socketIds.delete(socket.id);
console.log(`Socket ID ${socket.id} removed for user ${userId}`);
if (socketIds.size === 0) {
userSocketMap.delete(userId); // Clean up the map if no sockets are left for the user
}
break;
}
}
});
//
socket.on("accept_order", async (orderId: string) => {
//Update the order status in the database
console.log(orderId);
const updateOrder = await OrderModel.findByIdAndUpdate(
orderId,
{ status: "payment-awaited" },
{ new: true }
);
if (updateOrder) {
console.log("Order Updated");
//Emit the event to the user and vendor
const userSocketIds = userSocketMap.get(updateOrder.user.toString());
if (userSocketIds) {
userSocketIds.forEach((socketId) => {
io.to(socketId).emit("initiate_payment", {
orderId: updateOrder._id,
amount: updateOrder.total,
currency: "INR",
});
});
}
const vendorSocketIds = userSocketMap.get(
updateOrder.vendor.toString()
);
if (vendorSocketIds) {
vendorSocketIds.forEach((socketId) => {
io.to(socketId).emit("order_updated", updateOrder);
});
}
}
});
socket.on("payment_confirmed", async (data: { orderId: string }) => {
const { orderId } = data;
const updateOrder = await OrderModel.findByIdAndUpdate(
orderId,
{ status: "processing" },
{ new: true }
);
if (updateOrder) {
console.log("Payment for Order Confirmed", orderId);
//Notify the user about payment confirmation
const userSocketIds = userSocketMap.get(updateOrder.user.toString());
if (userSocketIds) {
userSocketIds.forEach((socketId) => {
io.to(socketId).emit("payment_status_updated", {
orderId: updateOrder._id,
status: "Payment Confirmed",
});
});
}
//Notify the vendor about payment confirmation
const vendorSocketIds = userSocketMap.get(
updateOrder.vendor.toString()
);
if (vendorSocketIds) {
vendorSocketIds.forEach((socketId) => {
io.to(socketId).emit("order_confirmed", updateOrder);
});
}
}
});
});
return io;
};
// Export the io instance for use in other parts of the application
export { io };