Skip to content

Commit c8a8683

Browse files
Implemented backend for notification log
1 parent ac35123 commit c8a8683

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

server/controllers/message.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import MessageModel from "../models/Message.js";
1616
import UserModel from "../models/User.js";
17+
import NotificationModel from "../models/Notification.js";
1718
import {
1819
handleNotFound,
1920
handleServerError,
@@ -75,12 +76,61 @@ export const postMessage = async (req, res) => {
7576
user.messages.push(saved_message._id);
7677
await user.save();
7778

79+
// Add to the user's friends' notification log
80+
if (user.notifyFriends) {
81+
const notification = new NotificationModel({
82+
user_id: user._id,
83+
message_text: req.body.text,
84+
location: req.body.location,
85+
notification_text: `${user.userName} has posted "${req.body.text}"`
86+
});
87+
const saved_log = await notification.save();
88+
for (let [key, value] of user.friends.entries()) {
89+
const internalReq = {
90+
user_id: key,
91+
notif: saved_log
92+
};
93+
const notifRes = await addNotification(internalReq);
94+
}
95+
}
96+
7897
handleSuccess(res, saved_message);
7998
} catch (err) {
8099
handleServerError(res, err);
81100
}
82101
};
83102

103+
/**
104+
* Add a notification log to a user
105+
*
106+
* Creates and stores a new message in the database.
107+
* Adds the new message as a notification log to the user's array of friends' notification
108+
* @param {Object} req - The request object containing the message and user details.
109+
* @param {Object} res - The response object used to reply to the client.
110+
*/
111+
export const addNotification = async (internalReq, res) => {
112+
try {
113+
// Check if the user exists
114+
const user = await UserModel.findById(internalReq.body.user_id);
115+
if (!user) {
116+
return handleNotFound(
117+
res,
118+
"User not found"
119+
);
120+
}
121+
// Notification log
122+
const notification = internalReq.notif;
123+
124+
// Save the notif log to the user log array
125+
user.notificationLog.push(notification._id);
126+
await user.save();
127+
128+
handleSuccess(res, notification);
129+
} catch (err) {
130+
handleServerError(res, err);
131+
}
132+
};
133+
84134
/**
85135
* Deletes a specific message.
86136
*

server/models/Notification.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Notification Model Schema
3+
*
4+
* Defines the Mongoose schema for user message notification in the application. It includes fields for user ID
5+
* (who posted the original message),
6+
* message text (of the original message),
7+
* notification text,
8+
* and the location of the original message
9+
*/
10+
11+
import mongoose from "mongoose";
12+
const { Schema } = mongoose;
13+
14+
export const NotificationSchema = new Schema({
15+
user_id: {
16+
type: Schema.Types.ObjectId,
17+
required: true,
18+
},
19+
message_text: {
20+
type: String,
21+
required: [true, "Please enter the content of the message"],
22+
},
23+
location: {
24+
type: {
25+
type: String,
26+
enum: ["Point"],
27+
required: true,
28+
},
29+
coordinates: {
30+
type: [Number],
31+
required: true,
32+
},
33+
},
34+
notification_text: {
35+
type: String,
36+
required: [true, "Please enter the content of the notification"],
37+
}
38+
});
39+
40+
const NotificationModel = mongoose.model("Notification", NotificationSchema);
41+
export default NotificationModel;

server/models/User.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@ export const UserSchema = new Schema({
3030
default: [],
3131
},
3232
],
33+
// Notification Log for Message Notification,
34+
// which is an array of notifications that serve as notification logs
35+
notificationLog: [
36+
{
37+
type: mongoose.Schema.Types.ObjectId,
38+
ref: "Notification",
39+
default: [],
40+
},
41+
],
3342
friends: {
3443
type: Map,
3544
of: String,

0 commit comments

Comments
 (0)