'use strict';

const graphql = require('graphql');
const graphqlService = require('../index');
const paymentProcessorType = require('../types/payment-processor');
const paymentProcessorEnum = require('../types/payment-processor-enum');

const addPaymentMethod = {
  type: paymentProcessorType,
  args: {
    name: {
      type: paymentProcessorEnum
    },
    data: {
      type: graphql.GraphQLString
    }
  },
  resolve: function(_, args) {
    return graphqlService.defaultPaymentProcessor
      .then((paymentProcessor) => {
        const data = JSON.parse(args.data);

        return graphqlService.currentUser.then((user) => {
          console.log('graphql service current user: ', user);
          if (!paymentProcessor) {
            return graphqlService.addPaymentProcessor(args.name, data);
          }

          if (paymentProcessor.paymentMethods > 1) {
            throw new Error('Multiple card support not available at this time.');
          }

          return new Promise((resolve, reject) => {
            paymentProcessor
              .addPaymentMethod(data)
              .then((paymentProcessor) => {
                if (user.isFreeTier) {
                  user.isFreeTier = false;
                  console.log('user isFreeTier should be false: %j', user.isFreeTier);
                  return user.save()
                    .then((paymentProcessor) => resolve(paymentProcessor))
                    .catch((err) => reject(err));
                }
                return resolve(paymentProcessor)
              })
              .catch((err) => reject(err));
          });
        })
      }).catch((err) => {
        console.error(err);
        return { error: new Error(err) };
    });
  }
};

module.exports = addPaymentMethod;

@@ -228,16 +228,53 @@
 -enum');
+%0Aconst Promise = require('bluebird');
 %0A%0Aconst 
@@ -477,32 +477,95 @@
 s) %7B%0A    return 
+new Promise((resolve, reject) =%3E %7B%0A      Promise.join(%0A        
 graphqlService.d
@@ -590,23 +590,71 @@
 ssor
+,
 %0A      
-.then((p
+  graphqlService.currentUser,%0A        function(defaultP
 ayme
@@ -656,39 +656,44 @@
 PaymentProcessor
-) =%3E
+, user)
  %7B%0A
+  
         const da
@@ -733,156 +733,110 @@
     
-return graphqlService.currentUser.then((user) =%3E %7B%0A          console.log('graphql service current user: ', user);%0A          if (!paymentProcessor) %7B
+  if (!defaultPaymentProcessor) %7B%0A          // NB: addPaymentProcessor also adds in the payment method
 %0A   
@@ -865,16 +865,31 @@
 lService
+%0A              
 .addPaym
@@ -917,16 +917,153 @@
 e, data)
+%0A              .then(() =%3E _convertUser(user))%0A              .then(() =%3E resolve())%0A              .catch((err) =%3E reject(%7B error: err %7D))
 ;%0A      
@@ -1083,17 +1083,24 @@
     if (
-p
+defaultP
 aymentPr
@@ -1121,16 +1121,23 @@
 tMethods
+.length
  %3E 1) %7B%0A
@@ -1152,13 +1152,36 @@
     
-throw
+return reject(%0A             
  new
@@ -1238,18 +1238,16 @@
 s time.'
-);
 %0A       
@@ -1249,29 +1249,30 @@
         
-%7D%0A%0A  
+  ));%0A
         
 return n
@@ -1267,49 +1267,12 @@
     
-return new Promise((resolve, reject) =%3E %7B
+  %7D%0A
 %0A   
@@ -1282,11 +1282,16 @@
     
-  p
+defaultP
 ayme
@@ -1302,18 +1302,16 @@
 ocessor%0A
-  
         
@@ -1354,251 +1354,40 @@
     
-  
 .then((
-paymentProcessor) =%3E %7B%0A                if (user.isFreeTier) %7B%0A                  user.isFreeTier = false;%0A                  console.log('user isFreeTier should be false: %25j', user.isFreeTier);%0A                  return user.save()%0A     
+) =%3E _convertUser(user))%0A
     
@@ -1398,63 +1398,28 @@
     
-   
 .then((
-paymentProcessor) =%3E resolve(paymentProcessor
+) =%3E resolve(
 ))%0A 
@@ -1425,32 +1425,24 @@
             
-        
 .catch((err)
@@ -1444,35 +1444,46 @@
 (err) =%3E reject(
+%7B error: 
 err
+ %7D
 ));%0A            
@@ -1482,24 +1482,16 @@
     
-       
- 
 %7D%0A      
     
@@ -1490,143 +1490,195 @@
     
+);%0A
     
-      return resolve(paymentProcessor)%0A              %7D)%0A              .catch((err) =%3E reject(err));%0A          %7D);
+%7D);%0A  %7D%0A%7D;%0A%0Amodule.exports = addPaymentMethod;%0A%0Afunction _convertUser(user) %7B%0A  return new Promise((resolve, reject) =%3E %7B%0A    user.isFreeTier = false;%0A    user
 %0A      
-  %7D
+.save(
 )%0A      
 %7D).c
@@ -1677,145 +1677,79 @@
     
-%7D).catch((
+.then((us
 er
-r
 ) =%3E 
-%7B%0A        console.error(err);%0A        return %7B error: new Error
+resolve(user))%0A      .catch((err) =%3E reject
 (err)
- %7D
+)
 ;%0A
-  
   %7D);%0A
-  %7D%0A%7D;%0A%0Amodule.exports = addPaymentMethod;
+%7D
 %0A

Make sure isFreeTier gets set; refactor code