<commit_before>'use strict';

var Http = require('./Http');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var Promise = require('bluebird');
var Engagement = require('./Engagement');

/**
 * Ushur initializer and authenticator
 * @param       {Object} credentials An object containing email and password
 * @constructor
 */
function Ushur(credentials) {

  credentials = credentials || {};
  EventEmitter.call(this);

  /**
   * Email associated with Ushur account
   * @type {String}
   */
  this.email = credentials.email;

  /**
   * Password associated with ushur account
   * @type {String}
   */
  this.password = <PASSWORD>;

  /**
   * Authentication token to be sent along with every request
   * @type {String}
   */
  this.tokenId = undefined;

  //Authenticate in the start
  this.auth();

}

module.exports = Ushur;
util.inherits(Ushur, EventEmitter);


Ushur.prototype.auth = function () {
  var payload = {
    email: this.email,
    password: this.password
  };
  var _this = this;
  return new Promise(function (resolve, reject) {
    var http = new Http('POST', undefined, 'rest/login', undefined, payload);
    return http.send().then(function (userData) {
      _this.tokenId = userData.tokenId;
      _this.emit('loginSuccess', userData);
      resolve(userData);
    }).catch(function (error) {
      _this.emit('loginFalure', error);
      reject(error);
    });
  });

};

/**
 * To initiate an Ushur (also called as Campaign) to a
 * specific phone number if this is a single campaign but if a bulk
 * campaign the Ushur will be launched to all the users in the
 * enterprise and hence the userPhoneNo parameter will be ignored
 * if present.
 * @param  {Engagement} ushur An instance of Engagement
 * @return {Promise}       A promise that resolves to the response from API
 */
Ushur.prototype.initEngagement = function (engagement) {
  var _this = this;

  if(engagement instanceof Engagement && engagement.userPhoneNo && engagement.campaignId){
    engagement.cmd = 'initCampaign';
    var http = new Http('POST', _this.tokenId, 'initUshur', {}, engagement);
    return http.send();
  }else{
    throw new Error('Provide a valid engagement');
  }
};

/**
 * Ushur will be launched to all the users in the
 * enterprise and hence the userPhoneNo parameter will be ignored
 * if present.
 * @param  {Engagement} ushur An instance of Engagement
 * @return {Promise}       A promise that resolves to the response from API
 */
Ushur.prototype.initBulkEngagement = function (engagement) {
  var _this = this;

  if(engagement instanceof Engagement && engagement.userPhoneNo && engagement.campaignId){
    engagement.cmd = 'initBulkCampaign';
    var http = new Http('POST', _this.tokenId, 'initUshur', {}, engagement);
    return http.send();
  }else{
    throw new Error('Provide a valid engagement');
  }
};

Ushur.prototype.getActivity = function (activityId) {
  var _this = this;
  if(!activityId)
    throw new Error('Provide a valid activity ID');
  var payload = {activityId: activityId};
  var http = new Http('POST', _this.tokenId, 'infoQuery/getUshurActivity', {}, payload);
  return http.send();
};

/**
 * Instead of getting all the detailed information as part of the query,
 * here we get the responses for specific tags which implies
 * responses on specific micro-engagement.
 * @param  {Object} [params={ requestId:"", UeTag:[] }] An object with following optional keys
 * requestId, UeTag (Array of strings or a string with comma separated tags),
 * nRecords, lastRecordId, campaignId, filterCmd (array of strings or comma
 * separated keywords in a string), startDate (new Date), endDate (new Date)
 * @return {Promise}             Promise that resolves to the response from the API
 */
Ushur.prototype.getResponses = function (params = {}) {
  var _this = this;
  var payload = {
    requestId: params.requestId,
    cmd: params.cmd ? params.cmd : "responseForReqId",//support for cmd: "getCampaignList"
    onlyParent: params.onlyParent,
    onlyPrimary: params.onlyPrimary,
    UeTag: params.UeTag ? (typeof params.UeTag === "string"? params.UeTag : params.UeTag.join()): undefined,
    nRecords: params.nRecords || "50",
    lastRecordId: params.lastRecordId,
    campaignId: params.campaignId,
    entId: params.entId ? params.entId : null,
    apiSecToken: params.apiSecToken ? params.apiSecToken : null,
    startDate: params.startDate ? params.startDate.toISOString(): undefined,
    endDate: params.endDate ? params.endDate.toISOString(): undefined,
    userPhoneNo: params.userPhoneNo,
    filterCmd: params.filterCmd ? (typeof params.filterCmd === "string"? params.filterCmd : params.filterCmd.join()): undefined,
  };

  var http = new Http('POST', _this.tokenId, 'infoQuery', {}, payload);
  return http.send();<commit_msg>fixed promified login<commit_after>'use strict';

var Http = require('./Http');
var EventEmitter = require('events').EventEmitter;
var util = require('util');
var Promise = require('bluebird');
var Engagement = require('./Engagement');

/**
 * Ushur initializer and authenticator
 * @param       {Object} credentials An object containing email and password
 * @constructor
 */
function Ushur(credentials) {

  credentials = credentials || {};
  EventEmitter.call(this);

  /**
   * Email associated with Ushur account
   * @type {String}
   */
  this.email = credentials.email;

  /**
   * Password associated with ushur account
   * @type {String}
   */
  this.password = credentials.password;

  /**
   * Authentication token to be sent along with every request
   * @type {String}
   */
  this.tokenId = undefined;

  //Authenticate in the start
  if(credentials.mode && credentials.mode.toLowerCase() == "promise")
      return this.auth();
  else
      this.auth();

}

module.exports = Ushur;
util.inherits(Ushur, EventEmitter);


Ushur.prototype.auth = function () {
  var payload = {
    email: this.email,
    password: <PASSWORD>
  };
  var _this = this;
  return new Promise(function (resolve, reject) {
    var http = new Http('POST', undefined, 'rest/login', undefined, payload);
    return http.send().then(function (userData) {
      _this.tokenId = userData.tokenId;
      _this.emit('loginSuccess', userData);
      resolve(userData);
    }).catch(function (error) {
      _this.emit('loginFalure', error);
      reject(error);
    });
  });

};

/**
 * To initiate an Ushur (also called as Campaign) to a
 * specific phone number if this is a single campaign but if a bulk
 * campaign the Ushur will be launched to all the users in the
 * enterprise and hence the userPhoneNo parameter will be ignored
 * if present.
 * @param  {Engagement} ushur An instance of Engagement
 * @return {Promise}       A promise that resolves to the response from API
 */
Ushur.prototype.initEngagement = function (engagement) {
  var _this = this;

  if(engagement instanceof Engagement && engagement.userPhoneNo && engagement.campaignId){
    engagement.cmd = 'initCampaign';
    var http = new Http('POST', _this.tokenId, 'initUshur', {}, engagement);
    return http.send();
  }else{
    throw new Error('Provide a valid engagement');
  }
};

/**
 * Ushur will be launched to all the users in the
 * enterprise and hence the userPhoneNo parameter will be ignored
 * if present.
 * @param  {Engagement} ushur An instance of Engagement
 * @return {Promise}       A promise that resolves to the response from API
 */
Ushur.prototype.initBulkEngagement = function (engagement) {
  var _this = this;

  if(engagement instanceof Engagement && engagement.userPhoneNo && engagement.campaignId){
    engagement.cmd = 'initBulkCampaign';
    var http = new Http('POST', _this.tokenId, 'initUshur', {}, engagement);
    return http.send();
  }else{
    throw new Error('Provide a valid engagement');
  }
};

Ushur.prototype.getActivity = function (activityId) {
  var _this = this;
  if(!activityId)
    throw new Error('Provide a valid activity ID');
  var payload = {activityId: activityId};
  var http = new Http('POST', _this.tokenId, 'infoQuery/getUshurActivity', {}, payload);
  return http.send();
};

/**
 * Instead of getting all the detailed information as part of the query,
 * here we get the responses for specific tags which implies
 * responses on specific micro-engagement.
 * @param  {Object} [params={ requestId:"", UeTag:[] }] An object with following optional keys
 * requestId, UeTag (Array of strings or a string with comma separated tags),
 * nRecords, lastRecordId, campaignId, filterCmd (array of strings or comma
 * separated keywords in a string), startDate (new Date), endDate (new Date)
 * @return {Promise}             Promise that resolves to the response from the API
 */
Ushur.prototype.getResponses = function (params = {}) {
  var _this = this;
  var payload = {
    requestId: params.requestId,
    cmd: params.requestId ? "responseForReqId" : params.cmd,
    onlyParent: params.onlyParent,
    onlyPrimary: params.onlyPrimary,
    UeTag: params.UeTag ? (typeof params.UeTag === "string"? params.UeTag : params.UeTag.join()): undefined,
    nRecords: params.nRecords || "50",
    lastRecordId: params.lastRecordId,
    campaignId: params.campaignId,
    entId: params.entId ? params.entId : null,
    apiSecToken: params.apiSecToken ? params.apiSecToken : null,
    startDate: params.startDate ? params.startDate.toISOString(): undefined,
    endDate: params.endDate ? params.endDate.toISOString(): undefined,
    userPhoneNo: params.userPhoneNo,
    filterCmd: params.filterCmd ? (typeof params.filterCmd === "string"? params.filterCmd : params.filterCmd.join()): undefined,
  };

  var http = new Http('POST', _this.tokenId, 'infoQuery', {}, payload);
  return http.send();
{'fraction_non_alphanumeric': 0.12044471896232242, 'fraction_numerical': 0.00041177681696520483, 'mean_word_length': 3.8333333333333335, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '1114755', 'n_tokens_mistral': 2864, 'n_tokens_neox': 2841, 'n_words': 908}