const ld = require('lodash');
const moment = require('moment');
const setMetadata = require('../utils/updateMetadata.js');

/**
 * Adds metadata from billing into usermix
 * @param  {String} username
 * @return {Promise}
 */
module.exports = function mixPlan(username, audience) {
  const { amqp, config } = this;
  const { payments } = config;
  const route = [payments.prefix, payments.routes.planGet].join('.');
  const id = 'free';

  return amqp
    .publishAndWait(route, id, { timeout: 5000 })
    .bind(this)
    .then(function mix(plan) {
      const subscription = ld.findWhere(plan.subs, { name: id });
      const nextCycle = moment().add(1, 'month').format();
      const update = {
        username,
        audience,
        metadata: {
          '$set': {
            plan: id,
            nextCycle,
            models: subscription.models,
            modelPrice: subscription.price,
          },
        },
      };

      return setMetadata.call(this, update);
    });
};

@@ -1,12 +1,49 @@
+const Promise = require('bluebird');%0A
 const ld = r
@@ -467,16 +467,124 @@
 free';%0A%0A
+  // make sure that ms-payments is launched before%0A  return Promise%0A    .delay(10000)%0A    .then(() =%3E %7B%0A    
   return
@@ -589,16 +589,18 @@
 rn amqp%0A
+  
     .pub
@@ -641,16 +641,18 @@
 5000 %7D)%0A
+  
     .bin
@@ -659,24 +659,26 @@
 d(this)%0A    
+  
 .then(functi
@@ -692,16 +692,18 @@
 plan) %7B%0A
+  
       co
@@ -762,24 +762,26 @@
 d %7D);%0A      
+  
 const nextCy
@@ -823,24 +823,26 @@
 at();%0A      
+  
 const update
@@ -854,16 +854,18 @@
         
+ 
+ 
 username
@@ -866,16 +866,18 @@
 ername,%0A
+  
         
@@ -882,24 +882,26 @@
   audience,%0A
+  
         meta
@@ -918,16 +918,18 @@
         
+  
 '$set': 
@@ -926,24 +926,26 @@
   '$set': %7B%0A
+  
             
@@ -966,16 +966,18 @@
         
+ 
+ 
 nextCycl
@@ -979,16 +979,18 @@
 tCycle,%0A
+  
         
@@ -1030,24 +1030,26 @@
             
+ 
+ 
 modelPrice: 
@@ -1074,27 +1074,31 @@
 ,%0A          
+  
 %7D,%0A
+  
         %7D,%0A 
@@ -1106,12 +1106,16 @@
     
+  
 %7D;%0A%0A
+  
     
@@ -1155,16 +1155,26 @@
 pdate);%0A
+      %7D);%0A
     %7D);%0A

Add sleep in custom action