From d89480cc970bd9662c35642db82e86db6123ee0f Mon Sep 17 00:00:00 2001 From: Mst Aktar Date: Wed, 22 Nov 2023 22:01:36 +0200 Subject: [PATCH] notification --- controllers/notificationController.js | 31 +++++++++++++++++++++++++++ models/NotificationModel.js | 19 ++++++++++++++++ routes/notificationRoutes.js | 12 +++++++++++ server.js | 2 ++ 4 files changed, 64 insertions(+) create mode 100644 controllers/notificationController.js create mode 100644 models/NotificationModel.js create mode 100644 routes/notificationRoutes.js diff --git a/controllers/notificationController.js b/controllers/notificationController.js new file mode 100644 index 0000000..ed87116 --- /dev/null +++ b/controllers/notificationController.js @@ -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 }); + } +}; \ No newline at end of file diff --git a/models/NotificationModel.js b/models/NotificationModel.js new file mode 100644 index 0000000..79ba0ec --- /dev/null +++ b/models/NotificationModel.js @@ -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; \ No newline at end of file diff --git a/routes/notificationRoutes.js b/routes/notificationRoutes.js new file mode 100644 index 0000000..f39e710 --- /dev/null +++ b/routes/notificationRoutes.js @@ -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; \ No newline at end of file diff --git a/server.js b/server.js index a341c97..714dd9b 100644 --- a/server.js +++ b/server.js @@ -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"); @@ -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"));