src/chartmogul/chartmogul-feeder.js
/**
   * The middleware in charge of fetching the customers' countries.
   *
   * @param {Object} config The context of the request and response.
   * @return {Promise} the next middleware()
   *
   */
The middleware in charge of fetching the customers' countries.
@param {Object} config The context of the request and response.
@return {Promise} the next middleware()
The middleware in charge of fetching the customers ' countries . @param { Object } config The context of the request and response . @return { Promise } the next middleware ()
async fetchCountriesByCustomers(config) {
    const customers = await this.fetchAndFilterCustomers(1, {
      status: 'Active',
    });
    const firstInMonth = new Date();
    if (firstInMonth.getDate() === 1) {
      firstInMonth.setDate(0); 
    }
    firstInMonth.setDate(1);
    const tmpCountryCount = await customers.filter((cust) => {
        return new Date(cust['customer-since']).getTime() >=
          firstInMonth.getTime();
      })
      .reduce(
        (countryCount, {
          country,
        }) => {
          let count = countryCount[country];
          countryCount[country] = (!count) ? 1 : ++count;
          return countryCount;
        }, {}
      );
    if (config.out === 'json') {
      return tmpCountryCount;
    }
    const countryCount = Object.keys(tmpCountryCount)
      .sort((c1, c2) => tmpCountryCount[c2] - tmpCountryCount[c1])
      .slice(0, 4)
      .map((iso) => [iso, tmpCountryCount[iso]]);
    const html = HTMLFormatter.toTextMrrCountryCount(countryCount);
    return GeckoFormatter.toText(html);
  }
async fetchCountriesByCustomers ( config ) { const customers = await this . fetchAndFilterCustomers ( 1 , { status : 'Active' , } ) ; const firstInMonth = new Date ( ) ; if ( firstInMonth . getDate ( ) === 1 ) { firstInMonth . setDate ( 0 ) ; } firstInMonth . setDate ( 1 ) ; const tmpCountryCount = await customers . filter ( ( cust ) => { return new Date ( cust [ 'customer-since' ] ) . getTime ( ) >= firstInMonth . getTime ( ) ; } ) . reduce ( ( countryCount , { country , } ) => { let count = countryCount [ country ] ; countryCount [ country ] = ( ! count ) ? 1 : ++ count ; return countryCount ; } , { } ) ; if ( config . out === 'json' ) { return tmpCountryCount ; } const countryCount = Object . keys ( tmpCountryCount ) . sort ( ( c1 , c2 ) => tmpCountryCount [ c2 ] - tmpCountryCount [ c1 ] ) . slice ( 0 , 4 ) . map ( ( iso ) => [ iso , tmpCountryCount [ iso ] ] ) ; const html = HTMLFormatter . toTextMrrCountryCount ( countryCount ) ; return GeckoFormatter . toText ( html ) ; }
The middleware in charge of fetching the customers' countries.
The middleware in charge of fetching the customers ' countries .
// Point it to end last month