<commit_before>/**************************************************************************/
/* WEBSERVER-MAINSERVER COMMUNICATION */
/**************************************************************************/

var constants = require('./constants.js');
var net = require('net');
var debug = require('debug')('communicate');

module.exports = {

  /**
   * Sends a request to the main server.
   * @param  type the kind of request that we're sending
   * @param  req  the HTTP request from the browser
   * @param  res  the HTTP response to the browser
   * @return nothing
   */
  request : function(type, req, res) {

    var json_request = {};
    json_request.message_type = type;

    switch(type) {
      case constants.SEARCH_REQ:  
        json_request.options = {'price' : 'none', 'num' : '-1'};
        json_request.items = [req.query.item];
        // json_request.name 
        break;
                      
      case constants.CREATE_ACC_REQ:
        json_request.name = req.body.name;
        json_request.email = req.body.email;
        json_request.password = <PASSWORD>;
        break;

      case constants.LOGIN_REQ:
        json_request.email = req.body.email;
        json_request.password = <PASSWORD>;
        break;

      case constants.ADD_LIST_REQ:
      case constants.DEL_LIST_REQ:
      case constants.GET_LIST_REQ:
        json_request.list_name = req.body.list_name;
        json_request.list = req.body.list;
        json_request.email = req.session.email;
        break;

      case constants.ACC_UPDATE_REQ:
        json_request.email = req.session.email;
        json_request.old_password = <PASSWORD>;
        json_request.password = <PASSWORD>;
        break;

      // case constants.
    }
    socket(json_request, req, res);
  }

};

/**
 * Handles responses from the main server.
 * @param  res_from_server the response from the server
 * @param  req the HTTP request from the browser
 * @param  res the HTTP response to the browser
 * @return nothing
 */
response = function (res_from_server, req, res) {
  
  var message = JSON.parse(res_from_server.toString());
  var type = message.message_type;
  debug('message is of type: ' + type);
  switch(type) {
    case constants.READ_RSP:
      for (var i = 0; i < message.items.length; i++) {
        // replace all instances of '&amp;' in item names with '&'
        message.items[i].data.name = message.items[i].data.name.replace('&amp;', '&');
      }
      
      res.render('item_searched', {'title': 'Search Results', 'list_items': message.items});
      break;
    
    // check if acc_created is true or false
    case constants.CREATE_ACC_RSP:
      if (message.status == constants.SUCCESS) {
        // save name and email, then redirect to home page
        req.session.name = req.body.name;
        req.session.email = req.body.email;
        res.redirect('/');
      }
      else {
        // failure, which means email is already in use
        res.send('<p>That email is already in use.</p>');
      }
      break;

    case constants.LOGIN_RSP:
      // check if either acc_exists is false or correct_password is false;
      if (message.status == constants.SUCCESS) {
        // login successful
        // need to get name from message
        // req.session.name = message.name;
        debug('login: successful');
        req.session.email = req.body.email;
        res.redirect('/');
      } 
      else if (message.status == constants.FAILURE) {
        // password is incorrect
        debug('login: failure');
        res.send('<p>Incorrect password.</p>');
        
      }
      else if (message.status == constants.DOES_NOT_EXIST) {
        // email does not exist
        debug('login: does not exist');
        res.send('<p>Account does not exist.</p>');
      }
      else {
        debug(message.status);
      }
      break;

    case constants.ACC_UPDATE_RSP:

      if (message.status == constants.SUCCESS) {
        res.send('<p>Password successfully updated.</p>');
      }<commit_msg>successful login now displays name<commit_after>/**************************************************************************/
/* WEBSERVER-MAINSERVER COMMUNICATION */
/**************************************************************************/

var constants = require('./constants.js');
var net = require('net');
var debug = require('debug')('communicate');

module.exports = {

  /**
   * Sends a request to the main server.
   * @param  type the kind of request that we're sending
   * @param  req  the HTTP request from the browser
   * @param  res  the HTTP response to the browser
   * @return nothing
   */
  request : function(type, req, res) {

    var json_request = {};
    json_request.message_type = type;

    switch(type) {
      case constants.SEARCH_REQ:  
        json_request.options = {'price' : 'none', 'num' : '-1'};
        json_request.items = [req.query.item];
        // json_request.name 
        break;
                      
      case constants.CREATE_ACC_REQ:
        json_request.name = req.body.name;
        json_request.email = req.body.email;
        json_request.password = <PASSWORD>;
        break;

      case constants.LOGIN_REQ:
        json_request.email = req.body.email;
        json_request.password = <PASSWORD>;
        break;

      case constants.ADD_LIST_REQ:
      case constants.DEL_LIST_REQ:
      case constants.GET_LIST_REQ:
        json_request.list_name = req.body.list_name;
        json_request.list = req.body.list;
        json_request.email = req.session.email;
        break;

      case constants.ACC_UPDATE_REQ:
        json_request.email = req.session.email;
        json_request.old_password = <PASSWORD>;
        json_request.password = <PASSWORD>;
        break;

      // case constants.
    }
    socket(json_request, req, res);
  }

};

/**
 * Handles responses from the main server.
 * @param  res_from_server the response from the server
 * @param  req the HTTP request from the browser
 * @param  res the HTTP response to the browser
 * @return nothing
 */
response = function (res_from_server, req, res) {
  
  var message = JSON.parse(res_from_server.toString());
  var type = message.message_type;
  debug('message is of type: ' + type);
  switch(type) {
    case constants.READ_RSP:
      for (var i = 0; i < message.items.length; i++) {
        // replace all instances of '&amp;' in item names with '&'
        message.items[i].data.name = message.items[i].data.name.replace('&amp;', '&');
      }
      
      res.render('item_searched', {'title': 'Search Results', 'list_items': message.items});
      break;
    
    // check if acc_created is true or false
    case constants.CREATE_ACC_RSP:
      if (message.status == constants.SUCCESS) {
        // save name and email, then redirect to home page
        req.session.name = req.body.name;
        req.session.email = req.body.email;
        res.redirect('/');
      }
      else {
        // failure, which means email is already in use
        res.send('<p>That email is already in use.</p>');
      }
      break;

    case constants.LOGIN_RSP:
      // check if either acc_exists is false or correct_password is false;
      if (message.status == constants.SUCCESS) {
        // login successful
        // need to get name from message
        debug('login: successful');
        req.session.name = message.name;
        req.session.email = req.body.email;
        res.redirect('/');
      } 
      else if (message.status == constants.FAILURE) {
        // password is incorrect
        debug('login: failure');
        res.send('<p>Incorrect password.</p>');
        
      }
      else if (message.status == constants.DOES_NOT_EXIST) {
        // email does not exist
        debug('login: does not exist');
        res.send('<p>Account does not exist.</p>');
      }
      else {
        debug(message.status);
      }
      break;

    case constants.ACC_UPDATE_RSP:

      if (message.status == constants.SUCCESS) {
        res.send('<p>Password successfully updated.</p>');
      }
{'fraction_non_alphanumeric': 0.1541904998756528, 'fraction_numerical': 0.0004973887092762995, 'mean_word_length': 2.3965371621621623, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3801101', 'n_tokens_mistral': 2324, 'n_tokens_neox': 2277, 'n_words': 594}