3838use App \Services \Nordigen \Services \AccountInformationCollector ;
3939use App \Services \Nordigen \TokenManager ;
4040use App \Services \Session \Constants ;
41+ use App \Services \Spectre \Model \Account as SpectreAccount ;
42+ use App \Services \Spectre \Request \GetAccountsRequest as SpectreGetAccountsRequest ;
43+ use App \Services \Spectre \Response \GetAccountsResponse as SpectreGetAccountsResponse ;
4144use App \Services \Storage \StorageService ;
4245use App \Support \Token ;
4346use Cache ;
@@ -75,7 +78,7 @@ public function index(Request $request)
7578 {
7679 Log::debug (sprintf ('Now at %s ' , __METHOD__ ));
7780 $ mainTitle = 'Configuration ' ;
78- $ subTitle = 'Configure your TODO file import ' ;
81+ $ subTitle = 'Configure your import ' ;
7982 $ accounts = [];
8083 $ flow = $ request ->cookie (Constants::FLOW_COOKIE );
8184
@@ -123,20 +126,28 @@ public function index(Request $request)
123126 }
124127
125128 // also get the nordigen / spectre accounts
126- $ nordigenAccounts = [];
129+ $ importerAccounts = [];
127130 if ('nordigen ' === $ flow ) {
128131 // list all accounts in Nordigen:
129132 $ reference = $ configuration ->getRequisition (session ()->get (Constants::REQUISITION_REFERENCE ));
130- $ nordigenAccounts = $ this ->getNordigenAccounts ($ reference );
131- $ nordigenAccounts = $ this ->mergeAccountLists ( $ nordigenAccounts , $ accounts );
133+ $ importerAccounts = $ this ->getNordigenAccounts ($ reference );
134+ $ importerAccounts = $ this ->mergeNordigenAccountLists ( $ importerAccounts , $ accounts );
132135 }
136+
133137 if ('spectre ' === $ flow ) {
134- die ('grab config for spectre. ' );
138+ // get the accounts over at Spectre.
139+ $ url = config ('spectre.url ' );
140+ $ appId = config ('spectre.app_id ' );
141+ $ secret = config ('spectre.secret ' );
142+ $ spectreList = new SpectreGetAccountsRequest ($ url , $ appId , $ secret );
143+ $ spectreList ->connection = $ configuration ->getConnection ();
144+ $ spectreAccounts = $ spectreList ->get ();
145+ $ importerAccounts = $ this ->mergeSpectreAccountLists ($ spectreAccounts , $ accounts );
135146 }
136147
137148 return view (
138149 'import.004-configure.index ' ,
139- compact ('mainTitle ' , 'subTitle ' , 'accounts ' , 'configuration ' , 'flow ' , 'nordigenAccounts ' )
150+ compact ('mainTitle ' , 'subTitle ' , 'accounts ' , 'configuration ' , 'flow ' , 'importerAccounts ' )
140151 );
141152 }
142153
@@ -171,6 +182,7 @@ public function postIndex(ConfigurationPostRequest $request): RedirectResponse
171182 $ configuration ->setFlow ($ request ->cookie (Constants::FLOW_COOKIE ));
172183
173184 // TODO are all fields actually in the config?
185+
174186 // loop accounts:
175187 $ accounts = [];
176188 foreach (array_keys ($ fromRequest ['do_import ' ]) as $ identifier ) {
@@ -193,9 +205,14 @@ public function postIndex(ConfigurationPostRequest $request): RedirectResponse
193205
194206 session ()->put (Constants::CONFIGURATION , $ configuration ->toSessionArray ());
195207
208+
209+ Log::debug (sprintf ('Configuration debug: Connection ID is "%s" ' , $ configuration ->getConnection ()));
196210 // set config as complete.
197211 session ()->put (Constants::CONFIG_COMPLETE_INDICATOR , true );
198-
212+ if ('nordigen ' === $ configuration ->getFlow () || 'spectre ' === $ configuration ->getFlow ()) {
213+ // at this point, nordigen is ready for data conversion.
214+ session ()->put (Constants::READY_FOR_CONVERSION , true );
215+ }
199216 // always redirect to roles, even if this isn't the step yet
200217 // for nordigen and spectre, roles will be skipped right away.
201218 return redirect (route ('005-roles.index ' ));
@@ -245,25 +262,75 @@ private function getNordigenAccounts(string $identifier): array
245262 return $ return ;
246263 }
247264
265+
266+ /**
267+ * @param SpectreGetAccountsResponse $spectre
268+ * @param array $firefly
269+ *
270+ * TODO should be a helper
271+ */
272+ private function mergeSpectreAccountLists (SpectreGetAccountsResponse $ spectre , array $ firefly ): array
273+ {
274+ $ return = [];
275+ Log::debug ('Now creating Spectre account lists. ' );
276+
277+ /** @var SpectreAccount $spectreAccount */
278+ foreach ($ spectre as $ spectreAccount ) {
279+ Log::debug (sprintf ('Now working on Spectre account "%s": "%s" ' , $ spectreAccount ->name , $ spectreAccount ->id ));
280+ $ iban = $ spectreAccount ->iban ;
281+ $ currency = $ spectreAccount ->currencyCode ;
282+ $ entry = [
283+ 'import_service ' => $ spectreAccount ,
284+ 'firefly ' => [],
285+ ];
286+
287+ // only iban?
288+ $ filteredByIban = $ this ->filterByIban ($ firefly , $ iban );
289+
290+ if (1 === count ($ filteredByIban )) {
291+ Log::debug (sprintf ('This account (%s) has a single Firefly III counter part (#%d, "%s", same IBAN), so will use that one. ' , $ iban , $ filteredByIban [0 ]->id , $ filteredByIban [0 ]->name ));
292+ $ entry ['firefly ' ] = $ filteredByIban ;
293+ $ return [] = $ entry ;
294+ continue ;
295+ }
296+ Log::debug (sprintf ('Found %d accounts with the same IBAN ("%s") ' , count ($ filteredByIban ), $ iban ));
297+
298+ // only currency?
299+ $ filteredByCurrency = $ this ->filterByCurrency ($ firefly , $ currency );
300+
301+ if (count ($ filteredByCurrency ) > 0 ) {
302+ Log::debug (sprintf ('This account (%s) has some Firefly III counter parts with the same currency so will only use those. ' , $ currency ));
303+ $ entry ['firefly ' ] = $ filteredByCurrency ;
304+ $ return [] = $ entry ;
305+ continue ;
306+ }
307+ Log::debug ('No special filtering on the Firefly III account list. ' );
308+ $ entry ['firefly ' ] = $ firefly ;
309+ $ return [] = $ entry ;
310+ }
311+ return $ return ;
312+ }
313+
314+
248315 /**
249316 * @param array $nordigen
250317 * @param array $firefly
251318 * @return array
252319 *
253320 * TODO move to some helper.
254321 */
255- private function mergeAccountLists (array $ nordigen , array $ firefly ): array
322+ private function mergeNordigenAccountLists (array $ nordigen , array $ firefly ): array
256323 {
257- Log::debug ('Now creating account lists. ' );
324+ Log::debug ('Now creating Nordigen account lists. ' );
258325 $ return = [];
259326 /** @var NordigenAccount $nordigenAccount */
260327 foreach ($ nordigen as $ nordigenAccount ) {
261328 Log::debug (sprintf ('Now working on account "%s": "%s" ' , $ nordigenAccount ->getName (), $ nordigenAccount ->getIdentifier ()));
262329 $ iban = $ nordigenAccount ->getIban ();
263330 $ currency = $ nordigenAccount ->getCurrency ();
264331 $ entry = [
265- 'nordigen ' => $ nordigenAccount ,
266- 'firefly ' => [],
332+ 'import_service ' => $ nordigenAccount ,
333+ 'firefly ' => [],
267334 ];
268335
269336 // only iban?
0 commit comments