{"size": 2486, "ext": "js", "max_stars_repo_path": "server/v1/controllers/SessionController.js", "max_stars_repo_name": "descholar-ceo/freementor", "max_stars_repo_stars_event_min_datetime": "2020-07-16T06:17:15.000Z", "max_stars_repo_stars_event_max_datetime": "2021-08-06T16:15:01.000Z", "max_issues_repo_path": "server/v1/controllers/SessionController.js", "max_issues_repo_name": "descholar-ceo/freementor", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/v1/controllers/SessionController.js", "max_forks_repo_name": "descholar-ceo/freementor", "max_forks_repo_forks_event_min_datetime": "2020-05-05T21:22:48.000Z", "max_forks_repo_forks_event_max_datetime": "2020-05-05T21:22:48.000Z"}
{"max_stars_count": 4, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 27.0217391304, "max_line_length": 90, "alphanum_fraction": 0.6242960579}
import users from '../models/User';
import Session from '../models/Session';
import GeneralHelper from '../helpers/general';

const { response } = GeneralHelper;

class SessionController {
  static view_sessions(req, res) {
    const { auth_user } = req;

    let { type: user_type } = auth_user;

    if (user_type === 'user') {
      user_type = 'mentee';
    }
    const all_sessions = Session.findFor(auth_user.id, user_type);

    return response(res, 200, 'OK', all_sessions);
  }

  static create(req, res) {
    const { body } = req;
    const { id, email } = req.auth_user;

    body.menteeId = id;
    body.mentorId = parseInt(body.mentorId);
    body.status = 'pending';

    const fetch_mentor = users.findMentor(body.mentorId);

    if (fetch_mentor) {
      const session = Session.create(body);
      const session_review = Session.review(session.id);

      session.review = session_review;
      session.menteeEmail = email;

      return response(res, 200, 'OK', session);
    }

    return response(res, 400, 'Mentor not found');
  }


  static acceptSession(req, res) {
    const { sessionId } = req.params;
    const { auth_user } = req;
    const fetch_session = Session.find(sessionId);
    let msg = 'Session not found,create sessions';

    if (fetch_session) {
      if (auth_user.id === fetch_session.mentorId) {
        if (fetch_session.status === 'pending') {
          const update_session = Session.update(sessionId, { status: 'accepted' });

          return response(res, 200, 'OK', update_session);
        }

        msg = `You can not do this operation : session status is ${fetch_session.status}`;
      } else {
        msg = 'Session does not concern you';
      }
    }

    return response(res, 400, msg);
  }

  static rejectSession(req, res) {
    const { sessionId } = req.params;
    const { auth_user } = req;
    const fetch_session = Session.find(sessionId);
    let msg = 'Session not found,create sessions';

    if (fetch_session) {
      if (auth_user.id === fetch_session.mentorId) {
        if (fetch_session.status === 'pending') {
          const update_session = Session.update(sessionId, { status: 'rejected' });

          return response(res, 200, 'OK', update_session);
        }

        msg = `You can not do this operation : session status is ${fetch_session.status}`;
      } else {
        msg = 'Session does not concern you';
      }
    }
    return response(res, 400, msg);
  }
}

export default SessionController;
