diff --git a/app/Import/Routine/SpectreRoutine.php b/app/Import/Routine/SpectreRoutine.php index bfd6af5438..df5410bd60 100644 --- a/app/Import/Routine/SpectreRoutine.php +++ b/app/Import/Routine/SpectreRoutine.php @@ -24,6 +24,7 @@ namespace FireflyIII\Import\Routine; use FireflyIII\Models\ImportJob; use FireflyIII\Services\Spectre\Object\Customer; +use FireflyIII\Services\Spectre\Request\ListLoginsRequest; use FireflyIII\Services\Spectre\Request\NewCustomerRequest; use Illuminate\Support\Collection; use Log; @@ -91,9 +92,34 @@ class SpectreRoutine implements RoutineInterface // create customer if user does not have one: $customer = $this->getCustomer(); + // list all logins present at Spectre + $logins = $this->listLogins($customer); + + // use latest (depending on status, and if login exists for selected country + provider) + $country = $this->job->configuration['country']; + $providerId = $this->job->configuration['provider']; + $login = $this->filterLogins($logins, $country, $providerId); + + // create new login if list is empty or no login exists. + if (is_null($login)) { + $login = $this->createLogin($customer); + die('new login'); + } + + echo '
';
+        print_r($logins);
+        exit;
+
         return true;
     }
 
+    /**
+     * @param Customer $customer
+     */
+    protected function createLogin(Customer $customer) {
+
+    }
+
     /**
      * @param ImportJob $job
      */
@@ -104,18 +130,22 @@ class SpectreRoutine implements RoutineInterface
 
     /**
      * @return Customer
+     * @throws \FireflyIII\Exceptions\FireflyException
      */
     protected function createCustomer(): Customer
     {
         $newCustomerRequest = new NewCustomerRequest($this->job->user);
         $newCustomerRequest->call();
-        echo '
';
-        print_r($newCustomerRequest->getCustomer());
-        exit;
+        $customer = $newCustomerRequest->getCustomer();
+
+        // store customer. Not sure where. User preference? TODO
+        return $customer;
+
     }
 
     /**
      * @return Customer
+     * @throws \FireflyIII\Exceptions\FireflyException
      */
     protected function getCustomer(): Customer
     {
@@ -126,4 +156,40 @@ class SpectreRoutine implements RoutineInterface
         var_dump($preference->data);
         exit;
     }
+
+    /**
+     * Return login belonging to country and provider
+     * TODO must return Login object, not array
+     *
+     * @param array  $logins
+     * @param string $country
+     * @param int    $providerId
+     *
+     * @return array|null
+     */
+    private function filterLogins(array $logins, string $country, int $providerId): ?array
+    {
+        if (count($logins) === 0) {
+            return null;
+        }
+        foreach ($logins as $login) {
+            die('do some filter');
+        }
+
+        return null;
+    }
+
+    /**
+     * @return array
+     */
+    private function listLogins(Customer $customer): array
+    {
+        $listLoginRequest = new ListLoginsRequest($this->job->user);
+        $listLoginRequest->setCustomer($customer);
+        $listLoginRequest->call();
+
+        $logins = $listLoginRequest->getLogins();
+
+        return $logins;
+    }
 }
diff --git a/app/Services/Spectre/Object/Customer.php b/app/Services/Spectre/Object/Customer.php
index 218ad4d3e9..53d046a6d9 100644
--- a/app/Services/Spectre/Object/Customer.php
+++ b/app/Services/Spectre/Object/Customer.php
@@ -34,6 +34,18 @@ class Customer extends SpectreObject
     /** @var string */
     private $secret;
 
+    /**
+     * Customer constructor.
+     *
+     * @param array $data
+     */
+    public function __construct(array $data)
+    {
+        $this->id         = intval($data['id']);
+        $this->identifier = $data['identifier'];
+        $this->secret     = $data['secret'];
+    }
+
     /**
      * @return int
      */
diff --git a/app/Services/Spectre/Request/ListLoginsRequest.php b/app/Services/Spectre/Request/ListLoginsRequest.php
new file mode 100644
index 0000000000..d69af2a026
--- /dev/null
+++ b/app/Services/Spectre/Request/ListLoginsRequest.php
@@ -0,0 +1,101 @@
+.
+ */
+declare(strict_types=1);
+
+namespace FireflyIII\Services\Spectre\Request;
+
+use FireflyIII\Services\Spectre\Object\Customer;
+use Log;
+
+/**
+ * Class ListLoginsRequest
+ */
+class ListLoginsRequest extends SpectreRequest
+{
+    /** @var Customer */
+    protected $customer;
+
+    /** @var array */
+    protected $logins = [];
+
+    /**
+     *
+     * @throws \FireflyIII\Exceptions\FireflyException
+     */
+    public function call(): void
+    {
+        $hasNextPage = true;
+        $nextId      = 0;
+        while ($hasNextPage) {
+            Log::debug(sprintf('Now calling list-logins for next_id %d', $nextId));
+            $parameters = ['customer_id' => $this->customer->getId(), 'from_id' => $nextId];
+            $uri        = '/api/v3/logins?' . http_build_query($parameters);
+            $response   = $this->sendSignedSpectreGet($uri, []);
+
+            // count entries:
+            Log::debug(sprintf('Found %d entries in data-array', count($response['data'])));
+
+            // extract next ID
+            $hasNextPage = false;
+            if (isset($response['meta']['next_id']) && intval($response['meta']['next_id']) > $nextId) {
+                $hasNextPage = true;
+                $nextId      = $response['meta']['next_id'];
+                Log::debug(sprintf('Next ID is now %d.', $nextId));
+            } else {
+                Log::debug('No next page.');
+            }
+
+            // store providers:
+            foreach ($response['data'] as $loginArray) {
+                var_dump($loginArray);
+                exit;
+            }
+        }
+
+        return;
+    }
+
+    /**
+     * @return Customer
+     */
+    public function getCustomer(): Customer
+    {
+        return $this->customer;
+    }
+
+    /**
+     * @param Customer $customer
+     */
+    public function setCustomer(Customer $customer): void
+    {
+        $this->customer = $customer;
+    }
+
+    /**
+     * @return array
+     */
+    public function getLogins(): array
+    {
+        return $this->logins;
+    }
+
+
+}
diff --git a/app/Services/Spectre/Request/ListProvidersRequest.php b/app/Services/Spectre/Request/ListProvidersRequest.php
index e523ea38f8..966023904c 100644
--- a/app/Services/Spectre/Request/ListProvidersRequest.php
+++ b/app/Services/Spectre/Request/ListProvidersRequest.php
@@ -25,7 +25,7 @@ namespace FireflyIII\Services\Spectre\Request;
 use Log;
 
 /**
- * Class ListUserRequest.
+ * Class ListProvidersRequest
  */
 class ListProvidersRequest extends SpectreRequest
 {
diff --git a/app/Services/Spectre/Request/NewCustomerRequest.php b/app/Services/Spectre/Request/NewCustomerRequest.php
index e7a96330ec..ff36919d70 100644
--- a/app/Services/Spectre/Request/NewCustomerRequest.php
+++ b/app/Services/Spectre/Request/NewCustomerRequest.php
@@ -22,37 +22,44 @@ declare(strict_types=1);
 
 namespace FireflyIII\Services\Spectre\Request;
 
+use FireflyIII\Services\Spectre\Object\Customer;
+
 /**
  * Class NewCustomerRequest
  */
 class NewCustomerRequest extends SpectreRequest
 {
-    /** @var array */
-    protected $customer = [];
+    /** @var Customer */
+    protected $customer;
 
     /**
      * @throws \FireflyIII\Exceptions\FireflyException
      */
     public function call(): void
     {
-        $data     = [
+        $data = [
             'data' => [
                 'identifier' => 'default_ff3_customer',
             ],
         ];
-        $uri      = '/api/v3/customers/';
-        $response = $this->sendSignedSpectrePost($uri, $data);
-
+        $uri  = '/api/v3/customers/';
+        //$response = $this->sendSignedSpectrePost($uri, $data);
+        $response = ['data' => [
+            'id'         => 527858,
+            'identifier' => 'default_ff3_customer',
+            'secret'     => 'qpZjRPJRTb6mMcQgwDkssZ3fQVVDPIH04zBlkKC6MvI',
+        ],
+        ];
         // create customer:
-        $this->customer = $response['data'];
+        $this->customer = new Customer($response['data']);
 
         return;
     }
 
     /**
-     * @return array
+     * @return Customer
      */
-    public function getCustomer(): array
+    public function getCustomer(): Customer
     {
         return $this->customer;
     }
diff --git a/app/Services/Spectre/Request/SpectreRequest.php b/app/Services/Spectre/Request/SpectreRequest.php
index 824bd8aca8..8d87297829 100644
--- a/app/Services/Spectre/Request/SpectreRequest.php
+++ b/app/Services/Spectre/Request/SpectreRequest.php
@@ -29,8 +29,6 @@ use Requests;
 use Requests_Exception;
 use Requests_Response;
 
-//use FireflyIII\Services\Bunq\Object\ServerPublicKey;
-
 /**
  * Class BunqRequest.
  */
@@ -267,7 +265,7 @@ abstract class SpectreRequest
 
         Log::debug('Final headers for spectre signed POST request:', $headers);
         try {
-            $response = Requests::get($fullUri, $headers);
+            $response = Requests::post($fullUri, $headers, $body);
         } catch (Requests_Exception $e) {
             throw new FireflyException(sprintf('Request Exception: %s', $e->getMessage()));
         }
diff --git a/app/Support/Import/Information/SpectreInformation.php b/app/Support/Import/Information/SpectreInformation.php
index 4d59d6e855..0cc93f8992 100644
--- a/app/Support/Import/Information/SpectreInformation.php
+++ b/app/Support/Import/Information/SpectreInformation.php
@@ -63,6 +63,7 @@ class SpectreInformation implements InformationInterface
      * @return array
      *
      * @throws FireflyException
+     * @throws \Exception
      */
     public function getAccounts(): array
     {
diff --git a/app/User.php b/app/User.php
index 32325cf6cf..d0620307ec 100644
--- a/app/User.php
+++ b/app/User.php
@@ -61,6 +61,7 @@ class User extends Authenticatable
     protected $table = 'users';
 
     /**
+     * @codeCoverageIgnore
      * Link to accounts.
      *
      * @return HasMany
@@ -95,6 +96,7 @@ class User extends Authenticatable
     }
 
     /**
+     * @codeCoverageIgnore
      * Link to attachments
      *
      * @return HasMany
@@ -105,6 +107,7 @@ class User extends Authenticatable
     }
 
     /**
+     * @codeCoverageIgnore
      * Link to available budgets
      *
      * @return HasMany
@@ -115,6 +118,7 @@ class User extends Authenticatable
     }
 
     /**
+     * @codeCoverageIgnore
      * Link to bills.
      *
      * @return HasMany
@@ -125,6 +129,7 @@ class User extends Authenticatable
     }
 
     /**
+     * @codeCoverageIgnore
      * Link to budgets.
      *
      * @return HasMany
@@ -135,6 +140,7 @@ class User extends Authenticatable
     }
 
     /**
+     * @codeCoverageIgnore
      * Link to categories
      *
      * @return HasMany
@@ -145,6 +151,7 @@ class User extends Authenticatable
     }
 
     /**
+     * @codeCoverageIgnore
      * Link to currency exchange rates
      *
      * @return HasMany
@@ -155,6 +162,7 @@ class User extends Authenticatable
     }
 
     /**
+     * @codeCoverageIgnore
      * Link to export jobs
      *
      * @return HasMany
@@ -165,6 +173,7 @@ class User extends Authenticatable
     }
 
     /**
+     * @codeCoverageIgnore
      * Generates access token.
      *
      * @return string
@@ -177,6 +186,7 @@ class User extends Authenticatable
     }
 
     /**
+     * @codeCoverageIgnore
      * Checks if the user has a role by its name.
      *
      * Full credit goes to: https://github.com/Zizaco/entrust
@@ -197,6 +207,7 @@ class User extends Authenticatable
     }
 
     /**
+     * @codeCoverageIgnore
      * Link to import jobs.
      *
      * @return HasMany
@@ -207,6 +218,7 @@ class User extends Authenticatable
     }
 
     /**
+     * @codeCoverageIgnore
      * Link to piggy banks.
      *
      * @return HasManyThrough
@@ -217,6 +229,7 @@ class User extends Authenticatable
     }
 
     /**
+     * @codeCoverageIgnore
      * Link to preferences.
      *
      * @return HasMany
@@ -227,6 +240,7 @@ class User extends Authenticatable
     }
 
     /**
+     * @codeCoverageIgnore
      * Link to roles.
      *
      * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
@@ -237,6 +251,7 @@ class User extends Authenticatable
     }
 
     /**
+     * @codeCoverageIgnore
      * Link to rule groups.
      *
      * @return HasMany
@@ -247,6 +262,7 @@ class User extends Authenticatable
     }
 
     /**
+     * @codeCoverageIgnore
      * Link to rules.
      *
      * @return HasMany
@@ -257,6 +273,7 @@ class User extends Authenticatable
     }
 
     /**
+     * @codeCoverageIgnore
      * Send the password reset notification.
      *
      * @param string $token
@@ -269,6 +286,7 @@ class User extends Authenticatable
     }
 
     /**
+     * @codeCoverageIgnore
      * Link to tags.
      *
      * @return HasMany
@@ -279,6 +297,7 @@ class User extends Authenticatable
     }
 
     /**
+     * @codeCoverageIgnore
      * Link to transaction journals.
      *
      * @return HasMany
@@ -289,6 +308,7 @@ class User extends Authenticatable
     }
 
     /**
+     * @codeCoverageIgnore
      * Link to transactions.
      *
      * @return HasManyThrough
diff --git a/resources/lang/en_US/import.php b/resources/lang/en_US/import.php
index 5958cf5f42..c38b44a9ac 100644
--- a/resources/lang/en_US/import.php
+++ b/resources/lang/en_US/import.php
@@ -141,6 +141,9 @@ return [
     'column_opposing-number'          => 'Opposing account (account number)',
     'column_note'                     => 'Note(s)',
 
+    // prerequisites
+    'prerequisites'                   => 'Prerequisites',
+
     // bunq
     'bunq_prerequisites_title'        => 'Prerequisites for an import from bunq',
     'bunq_prerequisites_text'         => 'In order to import from bunq, you need to obtain an API key. You can do this through the app.',
diff --git a/routes/breadcrumbs.php b/routes/breadcrumbs.php
index 7c2835dd3c..757c2a548b 100644
--- a/routes/breadcrumbs.php
+++ b/routes/breadcrumbs.php
@@ -573,6 +573,16 @@ Breadcrumbs::register(
         $breadcrumbs->push(trans('import.config_sub_title', ['key' => $job->key]), route('import.configure', [$job->key]));
     }
 );
+
+Breadcrumbs::register(
+    'import.prerequisites',
+    function (BreadCrumbsGenerator $breadcrumbs, string $bank) {
+        $breadcrumbs->parent('import.index');
+        $breadcrumbs->push(trans('import.prerequisites'), route('import.prerequisites', [$bank]));
+    }
+);
+
+
 Breadcrumbs::register(
     'import.status',
     function (BreadCrumbsGenerator $breadcrumbs, ImportJob $job) {
@@ -581,6 +591,8 @@ Breadcrumbs::register(
     }
 );
 
+
+
 // PREFERENCES
 Breadcrumbs::register(
     'preferences.index',
diff --git a/routes/web.php b/routes/web.php
index f4773ece42..061c95f038 100755
--- a/routes/web.php
+++ b/routes/web.php
@@ -445,7 +445,7 @@ Route::group(
     Route::get('json/{importJob}', ['uses' => 'Import\StatusController@json', 'as' => 'status.json']);
 
     // start a job
-    Route::post('start/{importJob}', ['uses' => 'Import\IndexController@start', 'as' => 'start']);
+    Route::any('start/{importJob}', ['uses' => 'Import\IndexController@start', 'as' => 'start']);
 
     // download config
     Route::get('download/{importJob}', ['uses' => 'Import\IndexController@download', 'as' => 'download']);