<commit_before>const commands = require('./commands.config.js');

function msgService($http) {
  const services = this;
  var currentPromptIndex = 0;
  services.order;
  services.botResponses;
  services.userName;

  services.msgs = [
    {
      msg: 'Hey there! My name is Stella.',
      bot: true,
    },
    {
      msg: 'If you need any help, enter /help for a list of commands.',
      bot: true,
    }
];

  function addMessage(msg, botFlag) {
    services.msgs.push({
      msg: msg,
      bot: botFlag,
    })
  }

  services.submitMessage = function(msg) {
    addMessage(msg, false);
    if (msg === 'Start over.') {
      currentPromptIndex = 0;
      services.getNextBotMsg();
    } else if (commands[msg]) {
      services.retrieveData(services.userName, commands[msg]);
    } else {
      if (currentPromptIndex === 1) {
        services.userName = msg;
      }
      if (currentPromptIndex <= services.order.length) {
        const prevBotQuestion = services.order[currentPromptIndex - 1];
        services.postMsg(msg, prevBotQuestion);
        services.getNextBotMsg();
      }
    }
  };

  services.getNextBotMsg = function() {
    var response;
    if (currentPromptIndex < services.order.length) {
      nextResponse = services.order[currentPromptIndex++];
      response = services.botResponses[nextResponse];
    } else {
      response = 'Thanks! The onboarding is complete.';
    }
    addMessage(response, true);
  }

  services.postMsg = function(msg, key) {
    const data = {
      name: services.userName,
      field: {
        key: key,
        value: msg,
      },
    };

    $http.post('/users', data)
    .catch(function(err) {
      console.log('Error', err);
    })
  }

  services.retrieveData = function(userName, key) {
    $http.get('/users', {
      params: {
        userName: userName
      }
    })
    .then(function(data) {
      addMessage(data.data[key], true);
      services.getNextBotMsg();
    })
    .catch(function(err) {
      console.log(err);
    })
  }

  services.getBotResponses = function() {
     $http.get('/bot/responses')
      .then(function(data) {
        services.order = data.data.order;
        services.botResponses = data.data.responses;
        services.getNextBotMsg();
      });
  }<commit_msg>Fixes case where data is retrieved mid onboarding<commit_after>const commands = require('./commands.config.js');

function msgService($http) {
  const services = this;
  var currentPromptIndex = 0;
  services.order;
  services.botResponses;
  services.userName;

  services.msgs = [
    {
      msg: 'Hey there! My name is Stella.',
      bot: true,
    },
    {
      msg: 'If you need any help, enter /help for a list of commands.',
      bot: true,
    }
];

  function addMessage(msg, botFlag) {
    services.msgs.push({
      msg: msg,
      bot: botFlag,
    })
  }

  services.submitMessage = function(msg) {
    addMessage(msg, false);
    if (msg === 'Start over.') {
      currentPromptIndex = 0;
      services.getNextBotMsg();
    } else if (commands[msg]) {
      services.retrieveData(services.userName, commands[msg]);
    } else {
      if (currentPromptIndex === 1) {
        services.userName = msg;
      }
      console.log(currentPromptIndex)
      if (currentPromptIndex <= services.order.length) {
        const prevBotQuestion = services.order[currentPromptIndex - 1];
        services.postMsg(msg, prevBotQuestion);
        services.getNextBotMsg();
      }
    }
  };

  services.getSameBotMsg = function() {
    var response;
    if (currentPromptIndex < services.order.length) {
      mostRecentResponse = services.order[currentPromptIndex - 1];
      response = services.botResponses[mostRecentResponse];
    } else {
      response = 'Thanks! The onboarding is complete.';
    }
    addMessage(response, true);
  }

  services.getNextBotMsg = function() {
    var response;
    if (currentPromptIndex < services.order.length) {
      nextResponse = services.order[currentPromptIndex];
      response = services.botResponses[nextResponse];
    } else {
      response = 'Thanks! The onboarding is complete.';
    }
    currentPromptIndex++;
    addMessage(response, true);
  }

  services.postMsg = function(msg, key) {
    const data = {
      name: services.userName,
      field: {
        key: key,
        value: msg,
      },
    };

    $http.post('/users', data)
    .catch(function(err) {
      console.log('Error', err);
    })
  }

  services.retrieveData = function(userName, key) {
    $http.get('/users', {
      params: {
        userName: userName
      }
    })
    .then(function(data) {
      addMessage(data.data[key], true);
      services.getSameBotMsg();
    })
    .catch(function(err) {
      console.log(err);
    })
  }

  services.getBotResponses = function() {
     $http.get('/bot/responses')
      .then(function(data) {
        services.order = data.data.order;
        services.botResponses = data.data.responses;
        services.getNextBotMsg();
      });
  }
{'fraction_non_alphanumeric': 0.1334800880528317, 'fraction_numerical': 0.0018010806483890335, 'mean_word_length': 2.4756606397774688, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 3, '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': '5793812', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1552, 'n_words': 306}