Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions controllers/notificationController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const Notification = require("../models/NotificationModel");

exports.getNotifications = async (req, res) => {
try {
const { userId } = req.body;
//find all notification by user id

const newNoti = [
{
userId: userId,
message: "Welcome to the app",
type: "Info",
createdAt: Date.now(),
},
{
userId: userId,
message: "Your parcel is delivered",
type: "Success",
createdAt: Date.now(),
},
];

const notifications = await Notification.find({ userId });
if (!notifications) {
return res.json({ notifications: [] });
}
res.json({ notifications });
} catch (error) {
res.status(500).json({ error: error.message });
}
};
19 changes: 19 additions & 0 deletions models/NotificationModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const mongoose = require("mongoose");

const notificationSchema = new mongoose.Schema({
userId: {
type: mongoose.Types.ObjectId,
ref: "User",
required: true,
},
message: { type: String, required: true },
type: {
type: String,
required: true,
enum: ["Info", "Success", "Warning"],
},
createdAt: { type: Date, default: Date.now },
});

const Notification = mongoose.model("Notification", notificationSchema);
module.exports = Notification;
12 changes: 12 additions & 0 deletions routes/notificationRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const express = require('express');
const router = express.Router();
const authenticate = require('../middleware/authenticate');
const NotificationController = require('../controllers/notificationController');


//route to get all the notficiations of a user by id
router.get('/', authenticate, NotificationController.getNotifications);



module.exports = router;
2 changes: 2 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const helmet = require("helmet");
const userRoutes = require("./routes/userRoutes");
const lockersRoutes = require("./routes/LockerRoutes");
const parcelRoutes = require("./routes/parcelRoutes");
const notificationRoutes = require("./routes/notificationRoutes");
const { PORT } = require("./config/serverConfig");
const { dbUri } = require("./config/dbConfig");

Expand Down Expand Up @@ -43,6 +44,7 @@ mongoose
app.use("/api/users", userRoutes); // Changed base path to /api/users
app.use("/api/lockers", lockersRoutes);
app.use("/api/parcels", parcelRoutes);
app.use("/api/notifications", notificationRoutes);

// Health check endpoint
app.get("/health", (req, res) => res.status(200).send("OK"));
Expand Down