src/chartmogul/chartmogul-feeder.js
/**
   * The middleware in charge of fetching the Latest Leads or Customers.
   *
   * @param {Object} config The context of the request and response.
   * @return {Promise} the next middleware()
   *
   */
The middleware in charge of fetching the Latest Leads or Customers.
@param {Object} config The context of the request and response.
@return {Promise} the next middleware()
The middleware in charge of fetching the Latest Leads or Customers . @param { Object } config The context of the request and response . @return { Promise } the next middleware ()
async fetchLatestCustomers(config) {
    const requestForCustomerPlan = async(uuid) => {
      let subscription = {};
      const dest = `/customers/${uuid}/subscriptions`;
      const {
        entries: subscriptions,
      } = await this.requestChartMogulFor(dest);
      if (!Util.isEmptyOrNull(subscriptions)) {
        subscription = subscriptions.pop();
      }
      return subscription;
    };
    const onlyLead = Boolean(config.onlyLead);
    let startingPage = (onlyLead) ?
      this.leads_.startPage : this.customers_.startPage;
    const leads = await this.fetchAndFilterCustomers(startingPage, {
      onlyLead: onlyLead,
      status: (onlyLead) ? 'Lead' : 'Active',
    });
    let latestLeads = leads
      .sort((cust1, cust2) => {
        const cust1DateTime = (onlyLead) ?
          cust1['lead_created_at'] :
          cust1['customer-since'];
        const cust2DateTime = (onlyLead) ?
          cust2['lead_created_at'] :
          cust2['customer-since'];
        const cmp = new Date(cust2DateTime).getTime() -
          new Date(cust1DateTime).getTime();
        return (cmp !== 0) ? cmp : cust2.mrr - cust1.mrr;
      })
      .slice(0, 5);
    if (!onlyLead) {
      for (let cust of latestLeads) {
        cust.plan = await requestForCustomerPlan(cust.uuid);
      }
    }
    latestLeads = latestLeads.map((cust, i) => {
      const when = new Date(
        (onlyLead) ?
        cust['lead_created_at'] :
        cust['customer-since']
      );
      if (config.out === 'json') {
        return {
          'country': cust.country,
          'country_full': Util.getCountryFromISOCode(cust.country),
          'name': cust.name,
          'company': cust.company,
          'date': when.toISOString(),
          'mrr': (!onlyLead) ? cust.mrr : undefined,
          'plan': cust.plan,
        };
      }
      return {
        'type': ((i === 0) ? 1 : 0),
        'text': HTMLFormatter.toListCustomer({
          'who': cust.company || cust.name,
          'when': when,
          'where': cust.country,
          'city': cust.city,
          'mrr': (!onlyLead) ? cust.mrr : undefined,
          'plan': cust.plan,
        }),
      };
    });
    if (config.out === 'json') {
      return latestLeads;
    }
    return GeckoFormatter.toText(latestLeads);
  }
async fetchLatestCustomers ( config ) { const requestForCustomerPlan = async ( uuid ) => { let subscription = { } ; const dest = ` ${ uuid } ` ; const { entries : subscriptions , } = await this . requestChartMogulFor ( dest ) ; if ( ! Util . isEmptyOrNull ( subscriptions ) ) { subscription = subscriptions . pop ( ) ; } return subscription ; } ; const onlyLead = Boolean ( config . onlyLead ) ; let startingPage = ( onlyLead ) ? this . leads_ . startPage : this . customers_ . startPage ; const leads = await this . fetchAndFilterCustomers ( startingPage , { onlyLead : onlyLead , status : ( onlyLead ) ? 'Lead' : 'Active' , } ) ; let latestLeads = leads . sort ( ( cust1 , cust2 ) => { const cust1DateTime = ( onlyLead ) ? cust1 [ 'lead_created_at' ] : cust1 [ 'customer-since' ] ; const cust2DateTime = ( onlyLead ) ? cust2 [ 'lead_created_at' ] : cust2 [ 'customer-since' ] ; const cmp = new Date ( cust2DateTime ) . getTime ( ) - new Date ( cust1DateTime ) . getTime ( ) ; return ( cmp !== 0 ) ? cmp : cust2 . mrr - cust1 . mrr ; } ) . slice ( 0 , 5 ) ; if ( ! onlyLead ) { for ( let cust of latestLeads ) { cust . plan = await requestForCustomerPlan ( cust . uuid ) ; } } latestLeads = latestLeads . map ( ( cust , i ) => { const when = new Date ( ( onlyLead ) ? cust [ 'lead_created_at' ] : cust [ 'customer-since' ] ) ; if ( config . out === 'json' ) { return { 'country' : cust . country , 'country_full' : Util . getCountryFromISOCode ( cust . country ) , 'name' : cust . name , 'company' : cust . company , 'date' : when . toISOString ( ) , 'mrr' : ( ! onlyLead ) ? cust . mrr : undefined , 'plan' : cust . plan , } ; } return { 'type' : ( ( i === 0 ) ? 1 : 0 ) , 'text' : HTMLFormatter . toListCustomer ( { 'who' : cust . company || cust . name , 'when' : when , 'where' : cust . country , 'city' : cust . city , 'mrr' : ( ! onlyLead ) ? cust . mrr : undefined , 'plan' : cust . plan , } ) , } ; } ) ; if ( config . out === 'json' ) { return latestLeads ; } return GeckoFormatter . toText ( latestLeads ) ; }
The middleware in charge of fetching the Latest Leads or Customers.
The middleware in charge of fetching the Latest Leads or Customers .
// Go fetch the subscriptions plans of those cust // Convert to bool if other type then Bool // Go fetch the subscriptions plans of those cust // 'text': (company || name)+' at '+ //   new Date(cust.lead_created_at).toLocaleString('fr-FR') //   +' incomming MRR : '+Util.toMoneyFormat(mrr/100),