Skip to content

Commit

Permalink
Implemented backend for notification log
Browse files Browse the repository at this point in the history
  • Loading branch information
annazhang2001 committed Dec 2, 2023
1 parent ac35123 commit c8a8683
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
50 changes: 50 additions & 0 deletions server/controllers/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import MessageModel from "../models/Message.js";
import UserModel from "../models/User.js";
import NotificationModel from "../models/Notification.js";
import {
handleNotFound,
handleServerError,
Expand Down Expand Up @@ -75,12 +76,61 @@ export const postMessage = async (req, res) => {
user.messages.push(saved_message._id);
await user.save();

// Add to the user's friends' notification log
if (user.notifyFriends) {
const notification = new NotificationModel({
user_id: user._id,
message_text: req.body.text,
location: req.body.location,
notification_text: `${user.userName} has posted "${req.body.text}"`
});
const saved_log = await notification.save();
for (let [key, value] of user.friends.entries()) {
const internalReq = {
user_id: key,
notif: saved_log
};
const notifRes = await addNotification(internalReq);
}
}

handleSuccess(res, saved_message);
} catch (err) {
handleServerError(res, err);
}
};

/**
* Add a notification log to a user
*
* Creates and stores a new message in the database.
* Adds the new message as a notification log to the user's array of friends' notification
* @param {Object} req - The request object containing the message and user details.
* @param {Object} res - The response object used to reply to the client.
*/
export const addNotification = async (internalReq, res) => {
try {
// Check if the user exists
const user = await UserModel.findById(internalReq.body.user_id);
if (!user) {
return handleNotFound(
res,
"User not found"
);
}
// Notification log
const notification = internalReq.notif;

// Save the notif log to the user log array
user.notificationLog.push(notification._id);
await user.save();

handleSuccess(res, notification);
} catch (err) {
handleServerError(res, err);
}
};

/**
* Deletes a specific message.
*
Expand Down
41 changes: 41 additions & 0 deletions server/models/Notification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Notification Model Schema
*
* Defines the Mongoose schema for user message notification in the application. It includes fields for user ID
* (who posted the original message),
* message text (of the original message),
* notification text,
* and the location of the original message
*/

import mongoose from "mongoose";
const { Schema } = mongoose;

export const NotificationSchema = new Schema({
user_id: {
type: Schema.Types.ObjectId,
required: true,
},
message_text: {
type: String,
required: [true, "Please enter the content of the message"],
},
location: {
type: {
type: String,
enum: ["Point"],
required: true,
},
coordinates: {
type: [Number],
required: true,
},
},
notification_text: {
type: String,
required: [true, "Please enter the content of the notification"],
}
});

const NotificationModel = mongoose.model("Notification", NotificationSchema);
export default NotificationModel;
9 changes: 9 additions & 0 deletions server/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ export const UserSchema = new Schema({
default: [],
},
],
// Notification Log for Message Notification,
// which is an array of notifications that serve as notification logs
notificationLog: [
{
type: mongoose.Schema.Types.ObjectId,
ref: "Notification",
default: [],
},
],
friends: {
type: Map,
of: String,
Expand Down

0 comments on commit c8a8683

Please sign in to comment.