'use strict';
/**
 * @file Define initial handlers,
 *
 * Trade grant_tokens for access_tokens.
 *
 */
var Anyfetch = require('anyfetch');
var async = require('async');
var restify = require('restify');
var rarity = require('rarity');
var url = require('url');

var CancelError = require('../../index.js').CancelError;
var TempToken = require('../../models/temp-token.js');
var Token = require('../../models/token.js');

/**
 * The user is redirected to this handler after getting access to his provider.
 *
 * @param {Object} req Request object from the client
 * @param {Object} res Response we want to return
 * @param {Function} next Callback to call once res has been populated.
 */
module.exports = function(retrieveTokens, config) {
  return function(req, res, next) {
    if(!req.ANYFETCH_SESSION.code) {
      console.warn("ERR: Missing code cookie.");
      return next(new restify.MissingParameterError("Missing code cookie."));
    }

    async.waterfall([
      function retrieveTempToken(cb) {
        TempToken.findOne({anyfetchCode: req.ANYFETCH_SESSION.code}, cb);
      },
      function retrieveDataFromProvider(tempToken, cb) {
        if(!tempToken) {
          return cb(new Error("Unable to retrieve provider preData."));
        }

        // Retrieve data from provider
        retrieveTokens(req.params, tempToken.data, rarity.carry([tempToken], cb));
      },
      function createAccessToken(tempToken, accountName, providerData, cb) {
        if(typeof accountName !== "string") {
          console.warn("Error: String attempt as accountName");
        }

        // Create a new anyfetch access-token with our grant
        Anyfetch.getAccessToken(config.appId, config.appSecret, tempToken.anyfetchCode, rarity.carry([accountName, providerData, tempToken], cb));
      },
      function setAccountName(accountName, providerData, tempToken, anyfetchAccessToken, cb) {
        var anyfetchClient = new Anyfetch(anyfetchAccessToken);
        anyfetchClient.postAccountName(accountName, rarity.carryAndSlice([accountName, providerData, tempToken, anyfetchAccessToken], 5, cb));
      },
      function createNewToken(accountName, providerData, tempToken, anyfetchAccessToken, cb) {
        // Create new token
        var token = new Token({
          anyfetchToken: anyfetchAccessToken,
          data: providerData,
          accountName: accountName
        });

        token.save(rarity.carryAndSlice([anyfetchAccessToken, tempToken, tempToken.returnTo], 4, cb));
      },
      function removeTempToken(anyfetchAccessToken, tempToken, returnTo, cb) {
        tempToken.remove(rarity.carryAndSlice([anyfetchAccessToken, returnTo], 3, cb));
      },
      function updateCompany(anyfetchAccessToken, returnTo, cb) {
        var anyfetchClient = new Anyfetch(anyfetchAccessToken);
        anyfetchClient.postCompanyUpdate(rarity.carry([returnTo], cb));
      }
    ], function sendResponse(err, returnTo) {
      if(err && !(err instanceof CancelError)) {
        console.warn("ERR:", err);
        res.send(500);
        return next(err);
      }

      var urlObj = url.parse(returnTo);
      urlObj.query.state = (err instanceof CancelError) ? 'canceled' : 'success';
      returnTo = url.format(urlObj);

      res.send(302, null, {
        location: returnTo
      });

      next();
    });
  };
};

@@ -936,24 +936,102 @@
 %22));%0A    %7D%0A%0A
+    var returnTo = process.env.MANAGER_URL %7C%7C %22https://manager.anyfetch.com%22;%0A
     async.wa
@@ -1319,32 +1319,72 @@
 %22));%0A        %7D%0A%0A
+        returnTo = tempToken.returnTo;%0A%0A
         // Retri
@@ -2578,28 +2578,8 @@
 oken
-, tempToken.returnTo
 %5D, 4
@@ -2653,34 +2653,24 @@
 , tempToken,
- returnTo,
  cb) %7B%0A     
@@ -2730,26 +2730,16 @@
 essToken
-, returnTo
 %5D, 3, cb
@@ -2804,18 +2804,8 @@
 ken,
- returnTo,
  cb)
@@ -2916,37 +2916,11 @@
 ate(
-rarity.carry(%5BreturnTo%5D, 
 cb)
-)
 ;%0A  
@@ -2961,18 +2961,8 @@
 (err
-, returnTo
 ) %7B%0A

Fix a bug with returnTo