{"size": 1003, "ext": "js", "max_stars_repo_path": "middleware/match/finish.js", "max_stars_repo_name": "sashahonchar/minglr", "max_stars_repo_stars_event_min_datetime": "2020-08-03T19:06:27.000Z", "max_stars_repo_stars_event_max_datetime": "2021-11-24T05:16:04.000Z", "max_issues_repo_path": "middleware/match/finish.js", "max_issues_repo_name": "sashahonchar/minglr", "max_issues_repo_issues_event_min_datetime": "2020-08-03T19:15:52.000Z", "max_issues_repo_issues_event_max_datetime": "2020-11-19T16:12:58.000Z", "max_forks_repo_path": "middleware/match/finish.js", "max_forks_repo_name": "sashahonchar/minglr", "max_forks_repo_forks_event_min_datetime": "2020-08-25T22:03:38.000Z", "max_forks_repo_forks_event_max_datetime": "2021-07-11T00:16:13.000Z"}
{"max_stars_count": 26, "max_issues_count": 7, "max_forks_count": 5, "avg_line_length": 22.2888888889, "max_line_length": 80, "alphanum_fraction": 0.5892323031}

const { log } = require("../../libs/log");
const { putBackMatchedUser, putBackUser } = require("../../libs/socketMethods");

const finish = (req, res, next) => {
    const currentUser = res.locals.user;
    const current_id = currentUser._id.toString();
    const io = req.app.get("io"); 
            
    currentUser.calling = false;

    /*
    * already finished
    */
    if (!currentUser.matched) {
        currentUser.save();

        // put back the currentUser to others' lists
        putBackUser(currentUser, io);

        return res.status(200).json({
            message: "Already finished",
        })
    }

    /*
    * finish
    */
    const matched_id = currentUser.matched.toString();
    
    // delete my match
    currentUser.matched = undefined;
    currentUser.save();

    res.status(200).json({
        success: true,
    })

    // send message
    putBackMatchedUser(currentUser, matched_id, io);

    log("FINISHED", current_id, matched_id);
}

module.exports = { finish }