src/chartmogul/chartmogul-feeder.js
/**
   * The middleware in chargin of fetch the leads for today.
   *
   * @param {Object} config - The config require for the fetch
   * @return {Promise} - The promisified geckoFormatted result of the fetch.
   *
   */
The middleware in chargin of fetch the leads for today.
@param {Object} config - The config require for the fetch
@return {Promise} - The promisified geckoFormatted result of the fetch.
The middleware in chargin of fetch the leads for today . @param { Object } config - The config require for the fetch @return { Promise } - The promisified geckoFormatted result of the fetch .
async fetchNbLeadsToday(config) {
    const {today, last30Days} = Util.getAllDates();
    today.setHours(0, 0, 0, 0);
    last30Days.setHours(0, 0, 0, 0);
    const item = [{
        'value': 0,
        'absolute': true,
      },
      {
        'value': 0,
      },
    ];
    const [leads, avgLeadsLastMonth] = await Promise.all([
      this.fetchAndFilterCustomers(1, {
        onlyLead: true,
      }),
      this.getCached(KEY_AVG_LEADS_LAST_MONTH),
    ]);
    leads.forEach((lead) => {
      const leadDateInMs = new Date(lead['lead_created_at']).getTime();
      if (leadDateInMs >= today.getTime()) {
        item[0].value += 1;
      }
      if (avgLeadsLastMonth === null
        &&
        leadDateInMs >= last30Days.getTime() &&
        leadDateInMs < today.getTime()) {
        item[1].value += 1;
      }
    });
    if (avgLeadsLastMonth === null) {
      item[1].value = Math.round(item[1].value / 30);
      super.setInCache(KEY_AVG_LEADS_LAST_MONTH, item[1].value);
    } else {
      item[1].value = avgLeadsLastMonth;
    }
    return GeckoFormatter.toNumberAndSecondStat(item[0], item[1]);
  }
async fetchNbLeadsToday ( config ) { const { today , last30Days } = Util . getAllDates ( ) ; today . setHours ( 0 , 0 , 0 , 0 ) ; last30Days . setHours ( 0 , 0 , 0 , 0 ) ; const item = [ { 'value' : 0 , 'absolute' : true , } , { 'value' : 0 , } , ] ; const [ leads , avgLeadsLastMonth ] = await Promise . all ( [ this . fetchAndFilterCustomers ( 1 , { onlyLead : true , } ) , this . getCached ( KEY_AVG_LEADS_LAST_MONTH ) , ] ) ; leads . forEach ( ( lead ) => { const leadDateInMs = new Date ( lead [ 'lead_created_at' ] ) . getTime ( ) ; if ( leadDateInMs >= today . getTime ( ) ) { item [ 0 ] . value += 1 ; } if ( avgLeadsLastMonth === null && leadDateInMs >= last30Days . getTime ( ) && leadDateInMs < today . getTime ( ) ) { item [ 1 ] . value += 1 ; } } ) ; if ( avgLeadsLastMonth === null ) { item [ 1 ] . value = Math . round ( item [ 1 ] . value / 30 ) ; super . setInCache ( KEY_AVG_LEADS_LAST_MONTH , item [ 1 ] . value ) ; } else { item [ 1 ] . value = avgLeadsLastMonth ; } return GeckoFormatter . toNumberAndSecondStat ( item [ 0 ] , item [ 1 ] ) ; }
The middleware in chargin of fetch the leads for today.
The middleware in chargin of fetch the leads for today .
// console.log('Nb Filtered : ' + leads.length, avgLeadsLastMonth); // const leadDate = Util.convertDate(lead['lead_created_at']); // console.log('Lead : '+leadDate); // Only the Leads made within the previous month // Calc the avg on 30 days