-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
152 lines (126 loc) · 4.83 KB
/
index.js
File metadata and controls
152 lines (126 loc) · 4.83 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const express = require("express");
const mongoose = require("mongoose");
const socketIo = require("socket.io");
const webpush = require("web-push");
const bodyParser = require("body-parser");
const dotenv = require("dotenv");
const cors = require("cors");
const Notification = require("./models/notification");
const User = require("./models/user");
const MongoStore = require("connect-mongo");
const createHttpError = require("http-errors");
const ConnectFlash = require("connect-flash");
dotenv.config(); // Load environment variables
// Ensure VAPID keys are set correctly
const VAPID_PUBLIC_KEY = process.env.VAPID_PUBLIC_KEY;
const VAPID_PRIVATE_KEY = process.env.VAPID_PRIVATE_KEY;
if (!VAPID_PUBLIC_KEY || !VAPID_PRIVATE_KEY) {
throw new Error("VAPID keys are not set. Please ensure your .env file contains VAPID_PUBLIC_KEY and VAPID_PRIVATE_KEY.");
}
webpush.setVapidDetails(
"mailto:project542005@gmail.com", // Replace with your email
VAPID_PUBLIC_KEY, // Your public key from .env
VAPID_PRIVATE_KEY // Your private key from .env
);
const app = express();
const server = app.listen(5000, () => {
console.log("Server is running on http://localhost:5000");
});
// Enable CORS for your frontend
const corsOptions = {
origin: process.env.API_URL, // Allow your frontend to make requests (dynamically from .env)
methods: ["GET", "POST"], // Allow GET and POST requests
allowedHeaders: ["Content-Type"], // Allow content-type header
};
app.use(cors(corsOptions)); // Apply CORS middleware
// Setup socket.io with CORS configuration
const io = socketIo(server, {
cors: {
origin: process.env.API_URL, // Allow socket.io connection from your frontend (dynamically from .env)
methods: ["GET", "POST"],
},
});
// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(ConnectFlash());
// Database connection
require("./config/db");
// Mock of user subscriptions (You should store this in the DB)
let userSubscriptions = {};
// Fetch all unseen notifications for the user
app.get("/api/notifications", async (req, res) => {
const userId = req.query.userId;
const notifications = await User.findById(userId).populate({
path: 'notifications', // Assuming 'notifications' is the field with references to the Notification model
match: { seen: false }, // Filter only unseen notifications
select: '' });
res.json(notifications);
});
// Store subscription in the backend when the user grants notification permission
app.post("/api/subscribe", (req, res) => {
const { subscription, userId } = req.body;
// Save the subscription in the userSubscriptions object or DB
userSubscriptions[userId] = subscription;
res.status(201).json({ message: "Subscribed for notifications." });
});
// Send notifications to the client
const sendNotification = async (subscription, notification) => {
try {
await webpush.sendNotification(subscription, JSON.stringify(notification));
} catch (error) {
console.error("Error sending notification:", error);
}
};
// Watch for new notifications and send them to subscribers
Notification.watch().on("change", async (change) => {
if (change.operationType === "insert") {
const newNotification = change.fullDocument;
// Notify all users who are subscribed
for (const userId in userSubscriptions) {
const subscription = userSubscriptions[userId];
// Create a notification payload
const notification = {
title: "New Notification",
message: newNotification.message,
};
sendNotification(subscription, notification);
}
// Emit to the frontend using Socket.io for real-time updates
io.emit("new-notification", newNotification);
}
});
// Socket.io connection for real-time updates
io.on("connection", (socket) => {
console.log("User connected");
// Listen for user disconnection
socket.on("disconnect", () => {
console.log("User disconnected");
});
});
// Routes
app.use("/api/auth", require("./routes/auth.route"));
app.use("/api/user", require("./routes/admin.route"));
app.use("/api/user", require("./routes/apply.route"));
app.use("/api/user", require("./routes/notify.route"));
app.use("/api", require("./routes/doctor.route"));
app.use("/api", require("./routes/appointment.route"));
app.post("/api/subscribe", async (req, res) => {
const { subscription, userId } = req.body;
// Store the subscription in the database for the user
try {
await User.findByIdAndUpdate(userId, { pushSubscription: subscription });
res.status(201).json({ message: "Subscription saved!" });
} catch (error) {
res.status(500).json({ error: "Error saving subscription." });
}
});
// Example of a basic GET route
app.get("/", (req, res) => {
res.send("Server is running");
});
// Error handling middleware
app.use((req, res, next) => {
next(createHttpError(404, "Not Found"));
});