Skip to content

Commit

Permalink
Merge pull request #141 from Hassan950/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
Hassan950 authored Jun 9, 2020
2 parents 80c2eef + 6a7bc24 commit 75cfd92
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 1 deletion.
49 changes: 48 additions & 1 deletion src/controllers/chat.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ const { chatService } = require('../services');
const AppError = require('../utils/AppError');
const httpStatus = require('http-status');

/**
* A middleware that gets the messages' threads of the user in paging object sorted from most frequent modified to least
*
* @function
* @author Hassan Mohamed
* @summary Get messages' threads
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {Function} next - Express next middleware function
*/
exports.getChat = async (req, res, next) => {
const result = await chatService.getChat(req.user._id, req.query);
if (result instanceof AppError) return next(result);
Expand All @@ -13,14 +23,41 @@ exports.getChat = async (req, res, next) => {
});
};


/**
* A middleware that gets a specific thread of the user with its messages
* wrapped in paging object sorted from most frequent one to least.
*
* @function
* @author Hassan Mohamed
* @summary Get messages of a thread
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {Function} next - Express next middleware function
*/
exports.getThread = async (req, res, next) => {
const result = await chatService.getThread(req.user._id, req.params.id, req.query);
const result = await chatService.getThread(
req.user._id,
req.params.id,
req.query
);
if (result instanceof AppError) return next(result);
res.status(httpStatus.OK).json({
result
});
};


/**
* A middleware that sends a message to a specific thread of the user.
*
* @function
* @author Hassan Mohamed
* @summary Send Message to a Thread
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {Function} next - Express next middleware function
*/
exports.sendMessage = async (req, res, next) => {
const result = await chatService.sendMessage(
req.user._id,
Expand All @@ -31,6 +68,16 @@ exports.sendMessage = async (req, res, next) => {
res.sendStatus(httpStatus.CREATED);
};

/**
* A middleware that deletes a specific message from a thread
*
* @function
* @author Hassan Mohamed
* @summary Delete Message From a Thread
* @param {Object} req - Express request object
* @param {Object} res - Express response object
* @param {Function} next - Express next middleware function
*/
exports.deleteMessage = async (req, res, next) => {
const result = await chatService.deleteMessage(
req.user._id,
Expand Down
50 changes: 50 additions & 0 deletions src/services/chat.serivce.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ const mongoose = require('mongoose');
const AppError = require('../utils/AppError');
const httpStatus = require('http-status');


/**
* A method that gets the messages' threads of the user sorted from most frequent modified to least and total number of the threads
*
* @function
* @author Hassan Mohamed
* @summary Get messages' threads
* @param {String} userId - The id of the user
* @param {Object} query - Query object has the limit and offset
* @returns {Object} object has the thread with total number of the threads
*/
exports.getChat = async (userId, query) => {
let data = Thread.find({ $or: [{ from: userId }, { to: userId }] })
.slice('messages', 1)
Expand All @@ -27,6 +38,19 @@ exports.getChat = async (userId, query) => {
return { data, total };
};


/**
* A method that gets a specific thread of the user with its messages
* wrapped in paging object sorted from most frequent one to least.
*
* @function
* @author Hassan Mohamed
* @summary Get messages of a thread
* @param {String} userId - The id of the user
* @param {String} threadId - The id of the thread
* @param {Object} query - Query object has the limit and offset
* @returns {Object} object has the thread with messages populated wrapped in paging object
*/
exports.getThread = async (userId, threadId, query) => {
let data = Thread.findOne({
$and: [{ _id: threadId }, { $or: [{ from: userId }, { to: userId }] }]
Expand Down Expand Up @@ -87,6 +111,19 @@ exports.getThread = async (userId, threadId, query) => {
return data;
};


/**
* A method that sends a message to a specific thread of the user.
*
* @function
* @author Hassan Mohamed
* @summary Send Message to a Thread
* @param {String} userId - The id of the user
* @param {String} recId - The id of the recipient
* @param {String} message - The content of the message
* @returns {true} if done successfully
* @returns {AppError} if there is no user with the given recId
*/
exports.sendMessage = async (userId, recId, message) => {
message = {
message: message,
Expand Down Expand Up @@ -151,6 +188,19 @@ exports.sendMessage = async (userId, recId, message) => {
return true;
};


/**
* A method that deletes a specific message from a thread
*
* @function
* @author Hassan Mohamed
* @summary Delete Message From a Thread
* @param {String} userId - The id of the user
* @param {String} recId - The id of the recipient
* @param {String} messageId - The id of the message
* @returns {true} if done successfully
* @returns {AppError} if there is no thread or no message with the given ids
*/
exports.deleteMessage = async (userId, threadId, messageId) => {
const thread = await Thread.findOneAndUpdate(
{ _id: threadId, 'messages._id': messageId, 'messages.author': userId },
Expand Down

0 comments on commit 75cfd92

Please sign in to comment.