Skip to content

Commit

Permalink
Merge pull request #213 from a0uda/UnitTests
Browse files Browse the repository at this point in the history
Unit Tests & Functional Documentation Batch #2
  • Loading branch information
FatemaKotb authored May 11, 2024
2 parents 10898fb + 005eaee commit 10a80d1
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 5 additions & 0 deletions src/routers/chatRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
13 changes: 13 additions & 0 deletions src/routers/communityRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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);
Expand Down
69 changes: 60 additions & 9 deletions src/services/chatService.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
/**
* @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";

// 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<Object>} - 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 {
Expand Down Expand Up @@ -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(<socket_id>).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<Object>} - 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;
Expand Down Expand Up @@ -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<Object>} - 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 {
Expand Down Expand Up @@ -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<Object>} - 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 {
Expand Down Expand Up @@ -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<Object>} - 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" } };
Expand Down
2 changes: 1 addition & 1 deletion src/services/communityQueueService.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @module community/queue/service
* @module community/service/queue
*/

import { Post } from '../db/models/Post.js';
Expand Down
2 changes: 1 addition & 1 deletion src/services/communityScheduledPostsService.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @module community/scheduled-posts/service
* @module community/service/scheduled-posts
*/

// TODO: Scheduled posts appear in the unmoderated queue.
Expand Down
2 changes: 1 addition & 1 deletion src/services/communitySettingsService.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* @module community/settings/service
* @module community/service/settings
*/

// Mod Tools --> Settings --> General Settings
Expand Down

0 comments on commit 10a80d1

Please sign in to comment.