diff --git a/package.json b/package.json index dcd74e4..8defc49 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "scripts": { "start": "node src/index.js", "dev": "nodemon src/index.js", - "test": "jest ./__test__/community/communityQueue.test.js", + "test": "jest ./__test__/community/communityScheduledPosts.test.js", "docs": "jsdoc -c src/config/jsdocsconfig.json", "seed": "node ./seeds/seed.js", "coverage": "jest --coverage" diff --git a/src/routers/chatRouter.js b/src/routers/chatRouter.js index 8158ccd..a6b6206 100644 --- a/src/routers/chatRouter.js +++ b/src/routers/chatRouter.js @@ -11,6 +11,11 @@ import { const chatRouter = express.Router(); +// Unit Tests Coverage ==> - +// Functional Documentation ==> Done +// API Documentation ==> - + + // The receiver's username is passed in the URL. chatRouter.post("/chats/send/:username", protectRoute, sendMessageController); // The other user's username is passed in the URL. diff --git a/src/routers/communityRouter.js b/src/routers/communityRouter.js index 301f431..16d66b7 100644 --- a/src/routers/communityRouter.js +++ b/src/routers/communityRouter.js @@ -117,6 +117,10 @@ communityRouter.get("/communities/get-community-names-by-popularity", getCommuni communityRouter.get("/communities/get-visible-posts/:community_name", protectRoute, getVisiblePostsController); //////////////////////////////////////////////////////////////////////// Get & Change Settings ////////////////////////////////////////////////////////////// +// Unit Tests Coverage ==> 100% +// Functional Documentation ==> Done +// API Documentation ==> - + communityRouter.get("/communities/get-general-settings/:community_name", getCommunityGeneralSettingsController); communityRouter.get("/communities/get-content-controls/:community_name", getCommunityContentControlsController); communityRouter.get("/communities/get-posts-and-comments/:community_name", getCommunityPostsAndCommentsController); @@ -126,6 +130,10 @@ communityRouter.post("/communities/change-content-controls/:community_name", cha communityRouter.post("/communities/change-posts-and-comments/:community_name", changeCommunityPostsAndCommentsController); //////////////////////////////////////////////////////////////////////// Mod Queue //////////////////////////////////////////////////////////////////// +// Unit Tests Coverage ==> 95.86% +// Functional Documentation ==> Done +// API Documentation ==> - + communityRouter.post("/communities/object-item/:community_name", protectRoute, protectModeratorRoute, objectItemConroller); communityRouter.post("/communities/edit-item/:community_name", protectRoute, editItemController); @@ -134,7 +142,12 @@ communityRouter.post("/communities/handle-edit/:community_name", protectRoute, p communityRouter.post("/communities/handle-unmoderated-item/:community_name", protectRoute, protectModeratorRoute, handleUnmoderatedItemController); communityRouter.get("/communities/get-items-from-queue/:community_name", protectRoute, protectModeratorRoute, getItemsFromQueueController); + //////////////////////////////////////////////////////////////////////// Schedule Posts ////////////////////////////////////////////////////////////// +// Unit Tests Coverage ==> - +// Functional Documentation ==> Done +// API Documentation ==> - + communityRouter.post("/communities/schedule-post/:community_name", protectRoute, protectModeratorRoute, schedulePostController); communityRouter.get("/communities/get-scheduled-posts/:community_name", protectRoute, protectModeratorRoute, getScheduledPostsController); communityRouter.post("/communities/edit-scheduled-post/:community_name", protectRoute, protectModeratorRoute, editScheduledPostController); diff --git a/src/services/chatService.js b/src/services/chatService.js index 843bd8c..c76afab 100644 --- a/src/services/chatService.js +++ b/src/services/chatService.js @@ -1,3 +1,7 @@ +/** + * @module community/service/chat + */ + import ChatModel from "../db/models/ChatModel.js"; import MessageModel from "../db/models/MessageModel.js"; import { User } from "../db/models/User.js"; @@ -5,8 +9,19 @@ import { User } from "../db/models/User.js"; // TODO: Uncomment. import { getReceiverSocketId, io } from "../socket/socket.js"; +/** + * Sends a message from a sender to a receiver. If a chat between the sender and receiver does not exist, it creates a new chat. + * + * @param {Object} sender - The sender of the message. This is an object that represents a User. + * @param {string} receiverUsername - The username of the receiver of the message. + * @param {string} message - The message to be sent. + * + * @returns {Promise} - A promise that resolves to an object. If the function is successful, the object contains a 'newMessage' property with the new message. If an error occurs, the object contains an 'err' property with the status code and error message. + * + * @throws {Object} - If an error occurs, an object is thrown with an 'err' property containing the status code and error message. + */ + const sendMessage = async (sender, receiverUsername, message) => { - console.log("ANA HENAAA FOOOOOOOOOOOOOOOOOOOOOOOOOO ", message); // Validating the sender and receiver. let receiver; try { @@ -117,22 +132,25 @@ const sendMessage = async (sender, receiverUsername, message) => { } const receiverSocketId = getReceiverSocketId(receiver._id); - console.log("receiverSocketId", receiverSocketId); - console.log("savedMessage", savedMessage); - console.log( - "AOUDA BARAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" - ); if (receiverSocketId) { // io.to().emit() used to send events to specific client - console.log("receiverSocketId", receiverSocketId); - console.log("savedMessage", savedMessage); - console.log("AOUDA"); io.to(receiverSocketId).emit("newMessage", savedMessage); } return { newMessage }; }; +/** + * Retrieves all messages between a sender and a receiver. + * + * @param {Object} sender - The sender of the messages. This is an object that represents a User. + * @param {string} receiverUsername - The username of the receiver of the messages. + * + * @returns {Promise} - A promise that resolves to an object. If the function is successful, the object contains a 'messages' property with all the messages between the sender and receiver. If an error occurs, the object contains an 'err' property with the status code and error message. + * + * @throws {Object} - If an error occurs, an object is thrown with an 'err' property containing the status code and error message. + */ + const getMessages = async (sender, receiverUsername) => { // Validate sender and receiver let receiver; @@ -202,6 +220,16 @@ const getMessages = async (sender, receiverUsername) => { return { messages }; }; +/** + * Retrieves all chats for a logged-in user, formatted for display in a sidebar. + * + * @param {string} loggedInUserId - The ID of the logged-in user. + * + * @returns {Promise} - A promise that resolves to an object. If the function is successful, the object contains a 'sideBarChats' property with all the chats for the logged-in user, formatted for display in a sidebar. Each chat includes the chat ID, the username and profile picture of the other participant, and the sender, text, and timestamp of the last message. If an error occurs, the object contains an 'err' property with the status code and error message. + * + * @throws {Object} - If an error occurs, an object is thrown with an 'err' property containing the status code and error message. + */ + const getSideBarChats = async (loggedInUserId) => { let chats; try { @@ -271,6 +299,18 @@ const getSideBarChats = async (loggedInUserId) => { return { sideBarChats }; }; +/** + * Reports a message for a given reason by a user. + * + * @param {string} messageId - The ID of the message to be reported. + * @param {string} reason - The reason for reporting the message. + * @param {string} reportingUserId - The ID of the user reporting the message. + * + * @returns {Promise} - A promise that resolves to an object. If the function is successful, the object contains a 'successMessage' property with the success message. If an error occurs, the object contains an 'err' property with the status code and error message. + * + * @throws {Object} - If an error occurs, an object is thrown with an 'err' property containing the status code and error message. + */ + const reportMessage = async (messageId, reason, reportingUserId) => { if (!messageId || !reason || !reportingUserId) { return { @@ -346,6 +386,17 @@ const reportMessage = async (messageId, reason, reportingUserId) => { return { successMessage: "Message reported successfully" }; }; +/** + * Removes a message by a user. + * + * @param {string} messageId - The ID of the message to be removed. + * @param {string} removingUserId - The ID of the user removing the message. + * + * @returns {Promise} - A promise that resolves to an object. If the function is successful, the object contains a 'successMessage' property with the success message. If an error occurs, the object contains an 'err' property with the status code and error message. + * + * @throws {Object} - If an error occurs, an object is thrown with an 'err' property containing the status code and error message. + */ + const removeMessage = async (messageId, removingUserId) => { if (!messageId) { return { err: { status: 400, message: "Message ID is missing" } }; diff --git a/src/services/communityQueueService.js b/src/services/communityQueueService.js index 20e9067..3b6a593 100644 --- a/src/services/communityQueueService.js +++ b/src/services/communityQueueService.js @@ -1,5 +1,5 @@ /** - * @module community/queue/service + * @module community/service/queue */ import { Post } from '../db/models/Post.js'; diff --git a/src/services/communityScheduledPostsService.js b/src/services/communityScheduledPostsService.js index 9e3f033..20c0124 100644 --- a/src/services/communityScheduledPostsService.js +++ b/src/services/communityScheduledPostsService.js @@ -1,5 +1,5 @@ /** - * @module community/scheduled-posts/service + * @module community/service/scheduled-posts */ // TODO: Scheduled posts appear in the unmoderated queue. diff --git a/src/services/communitySettingsService.js b/src/services/communitySettingsService.js index 945aeee..3a4bf6d 100644 --- a/src/services/communitySettingsService.js +++ b/src/services/communitySettingsService.js @@ -1,5 +1,5 @@ /** - * @module community/settings/service + * @module community/service/settings */ // Mod Tools --> Settings --> General Settings