{"size": 1942, "ext": "js", "max_stars_repo_path": "controllers/routers/chatRoutes/sendMessage.js", "max_stars_repo_name": "PeterAyad/Backend-API", "max_stars_repo_stars_event_min_datetime": "2022-01-06T17:20:54.000Z", "max_stars_repo_stars_event_max_datetime": "2022-03-15T16:37:06.000Z", "max_issues_repo_path": "controllers/routers/chatRoutes/sendMessage.js", "max_issues_repo_name": "TumblrX/Backend-API", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "controllers/routers/chatRoutes/sendMessage.js", "max_forks_repo_name": "TumblrX/Backend-API", "max_forks_repo_forks_event_min_datetime": "2022-01-06T14:20:20.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-06T18:07:15.000Z"}
{"max_stars_count": 7, "max_issues_count": null, "max_forks_count": 2, "avg_line_length": 39.6326530612, "max_line_length": 102, "alphanum_fraction": 0.5906282183}
const router = require('express').Router();
const verifyToken = require('../../verifyToken');
const {catchAsync} = require('../../errorHandler');
const {ChatModel} = require('../../../models/Chat');
const mongoose = require('mongoose');
const AppError = require('../../utils/appError');
const User = require('../../../models/User');

const sendMessageLogic = catchAsync(async (req, res, next) => {
    if (req.user._id.equals(mongoose.Types.ObjectId(req.body.user2Id))) {
        return next(new AppError('you can\'t text yourself', 400));
    }
    const receiverUser = await User.findOne({_id: mongoose.Types.ObjectId(req.body.user2Id)});
    if (!receiverUser) return next(new AppError('no such user was found', 400));
    let chat = await ChatModel.findOne({
        $or: [{$and: [{user1: req.user._id, user2: mongoose.Types.ObjectId(req.body.user2Id)}]},
            {$and: [{user1: mongoose.Types.ObjectId(req.body.user2Id)}, {user2: req.user._id}]},
        ],
    });
    if (chat) {
        // if there is a chat already between these two users
        await ChatModel.updateOne({
            $or: [{$and: [{user1: req.user._id}, {user2: mongoose.Types.ObjectId(req.body.user2Id)}]},
                {$and: [{user1: mongoose.Types.ObjectId(req.body.user2Id)}, {user2: req.user._id}]},
            ],
        }, {
            $push: {messages: {text: req.body.textMessage, senderId: req.user._id}},
        });
    } else {
    // assumtion is the user1Id is the senderId
        chat = new ChatModel({
            messages: [{
                text: req.body.textMessage,
                senderId: req.user._id,
            }],
            user1: req.user._id,
            user2: mongoose.Types.ObjectId(req.body.user2Id),
        });
        chat = await chat.save();
    }
    res.status(200).json({
        chatId: chat._id,
    });
});

router.route('/send-message').post(verifyToken, sendMessageLogic);

module.exports = router;
