mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-30 02:26:58 +00:00
Code for #1607
This commit is contained in:
@@ -61,6 +61,7 @@ class UserEventHandler
|
|||||||
|
|
||||||
// first user ever?
|
// first user ever?
|
||||||
if (1 === $repository->count()) {
|
if (1 === $repository->count()) {
|
||||||
|
Log::debug('User count is one, attach role.');
|
||||||
$repository->attachRole($event->user, 'owner');
|
$repository->attachRole($event->user, 'owner');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -54,6 +54,7 @@ class UserRepository implements UserRepositoryInterface
|
|||||||
{
|
{
|
||||||
$roleObject = Role::where('name', $role)->first();
|
$roleObject = Role::where('name', $role)->first();
|
||||||
if (null === $roleObject) {
|
if (null === $roleObject) {
|
||||||
|
Log::error(sprintf('Could not find role "%s" in attachRole()', $role));
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,7 +62,7 @@ class UserRepository implements UserRepositoryInterface
|
|||||||
$user->roles()->attach($roleObject);
|
$user->roles()->attach($roleObject);
|
||||||
} catch (QueryException $e) {
|
} catch (QueryException $e) {
|
||||||
// don't care
|
// don't care
|
||||||
Log::info(sprintf('Query exception when giving user a role: %s', $e->getMessage()));
|
Log::error(sprintf('Query exception when giving user a role: %s', $e->getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@@ -77,6 +78,7 @@ class UserRepository implements UserRepositoryInterface
|
|||||||
* @see updateEmail
|
* @see updateEmail
|
||||||
*
|
*
|
||||||
* @return bool
|
* @return bool
|
||||||
|
* @throws \Exception
|
||||||
*/
|
*/
|
||||||
public function changeEmail(User $user, string $newEmail): bool
|
public function changeEmail(User $user, string $newEmail): bool
|
||||||
{
|
{
|
||||||
|
@@ -116,7 +116,7 @@ class StageImportDataHandler
|
|||||||
* @return array
|
* @return array
|
||||||
* @throws FireflyException
|
* @throws FireflyException
|
||||||
*/
|
*/
|
||||||
private function convertPayment(BunqPayment $payment, LocalAccount $source): array
|
private function convertPayment(BunqPayment $payment, int $bunqAccountId, LocalAccount $source): array
|
||||||
{
|
{
|
||||||
Log::debug(sprintf('Now at payment with ID #%d', $payment->getId()));
|
Log::debug(sprintf('Now at payment with ID #%d', $payment->getId()));
|
||||||
$type = TransactionType::WITHDRAWAL;
|
$type = TransactionType::WITHDRAWAL;
|
||||||
@@ -300,15 +300,158 @@ class StageImportDataHandler
|
|||||||
*/
|
*/
|
||||||
private function getTransactionsFromBunq(int $bunqAccountId, LocalAccount $localAccount): array
|
private function getTransactionsFromBunq(int $bunqAccountId, LocalAccount $localAccount): array
|
||||||
{
|
{
|
||||||
|
Log::debug('Now in getTransactionsFromBunq(%d).');
|
||||||
|
|
||||||
|
// what was the last transaction we grabbed from bunq?
|
||||||
$return = [];
|
$return = [];
|
||||||
// make request:
|
$preferenceName = sprintf('bunq-last-transaction-%d', $bunqAccountId);
|
||||||
|
$transactionPref = \Preferences::getForUser($this->importJob->user, $preferenceName, 0);
|
||||||
|
$transactionId = (int)$transactionPref->data;
|
||||||
|
|
||||||
|
Log::debug(sprintf('ID of latest transaction is #%d', $transactionId));
|
||||||
|
|
||||||
|
if (0 === $transactionId) {
|
||||||
|
Log::debug('Its zero so we go back in time.');
|
||||||
|
// we go back into the past, way until the system says there is no more.
|
||||||
|
$return = $this->goBackInTime($bunqAccountId, $localAccount);
|
||||||
|
}
|
||||||
|
if (0 !== $transactionId) {
|
||||||
|
$return = $this->goForwardInTime($bunqAccountId, $localAccount);
|
||||||
|
// work my way forward.
|
||||||
|
}
|
||||||
|
sleep(1);
|
||||||
|
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $bunqAccountId
|
||||||
|
* @param LocalAccount $localAccount
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
* @throws FireflyException
|
||||||
|
*/
|
||||||
|
private function goBackInTime(int $bunqAccountId, LocalAccount $localAccount): array
|
||||||
|
{
|
||||||
|
Log::debug('Now in goBackInTime().');
|
||||||
|
$hasMoreTransactions = true;
|
||||||
|
$olderId = null;
|
||||||
|
$count = 0;
|
||||||
|
$return = [];
|
||||||
|
$veryFirstTransaction = null;
|
||||||
|
|
||||||
|
// loop die loop!
|
||||||
|
sleep(1);
|
||||||
|
while ($hasMoreTransactions && $count < 50) {
|
||||||
|
Log::debug(sprintf('Now in loop #%d', $count));
|
||||||
/** @var Payment $paymentRequest */
|
/** @var Payment $paymentRequest */
|
||||||
$paymentRequest = app(Payment::class);
|
$paymentRequest = app(Payment::class);
|
||||||
$result = $paymentRequest->listing($bunqAccountId, ['count' => 100]);
|
$response = $paymentRequest->listing($bunqAccountId, ['count' => 20, 'older_id' => $olderId]);
|
||||||
// loop result:
|
$pagination = $response->getPagination();
|
||||||
|
/*
|
||||||
|
* If pagination is not null, we can go back even further.
|
||||||
|
*/
|
||||||
|
if (null !== $pagination) {
|
||||||
|
$olderId = $pagination->getOlderId();
|
||||||
|
Log::debug(sprintf('Pagination object is not null, olderID is "%s"', $olderId));
|
||||||
|
}
|
||||||
|
Log::debug('Now looping results...');
|
||||||
/** @var BunqPayment $payment */
|
/** @var BunqPayment $payment */
|
||||||
foreach ($result->getValue() as $payment) {
|
foreach ($response->getValue() as $payment) {
|
||||||
$return[] = $this->convertPayment($payment, $localAccount);
|
$return[] = $this->convertPayment($payment, $bunqAccountId, $localAccount);
|
||||||
|
|
||||||
|
// store the very first transaction ID for this particular account.
|
||||||
|
if (null === $veryFirstTransaction) {
|
||||||
|
$veryFirstTransaction = $payment->getId();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Log::debug(sprintf('Count of result is now %d', \count($return)));
|
||||||
|
$count++;
|
||||||
|
if (null === $olderId) {
|
||||||
|
Log::debug('Older ID is NULL, so stop looping cause we are done!');
|
||||||
|
$hasMoreTransactions = false;
|
||||||
|
}
|
||||||
|
if (null === $pagination) {
|
||||||
|
Log::debug('No pagination object, stop looping.');
|
||||||
|
$hasMoreTransactions = false;
|
||||||
|
}
|
||||||
|
sleep(1);
|
||||||
|
}
|
||||||
|
Log::debug(sprintf('Done with looping. Final loop count is %d, first transaction is %d', $count, $veryFirstTransaction));
|
||||||
|
if (null !== $veryFirstTransaction) {
|
||||||
|
Log::debug('Very first transaction is not null, so set the preference!');
|
||||||
|
$preferenceName = sprintf('bunq-last-transaction-%d', $bunqAccountId);
|
||||||
|
$pref = \Preferences::setForUser($this->importJob->user, $preferenceName, $veryFirstTransaction);
|
||||||
|
Log::debug(sprintf('Preference set to: %s', $pref->data));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $return;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $bunqAccountId
|
||||||
|
* @param LocalAccount $localAccount
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
* @throws FireflyException
|
||||||
|
*/
|
||||||
|
private function goForwardInTime(int $bunqAccountId, LocalAccount $localAccount): array
|
||||||
|
{
|
||||||
|
Log::debug('Now in goForwardInTime().');
|
||||||
|
$hasMoreTransactions = true;
|
||||||
|
$count = 0;
|
||||||
|
$return = [];
|
||||||
|
$latestTransaction = null;
|
||||||
|
|
||||||
|
// newer ID comes from pref:
|
||||||
|
$preferenceName = sprintf('bunq-last-transaction-%d', $bunqAccountId);
|
||||||
|
$transactionPref = \Preferences::getForUser($this->importJob->user, $preferenceName, 0);
|
||||||
|
$newerId = (int)$transactionPref->data;
|
||||||
|
|
||||||
|
// loop die loop!
|
||||||
|
sleep(1);
|
||||||
|
while ($hasMoreTransactions && $count < 50) {
|
||||||
|
Log::debug(sprintf('Now in loop #%d', $count));
|
||||||
|
/** @var Payment $paymentRequest */
|
||||||
|
$paymentRequest = app(Payment::class);
|
||||||
|
$params = ['count' => 20, 'newer_id' => $newerId];
|
||||||
|
$response = $paymentRequest->listing($bunqAccountId, $params);
|
||||||
|
$pagination = $response->getPagination();
|
||||||
|
Log::debug('Submit payment request with params', $params);
|
||||||
|
/*
|
||||||
|
* If pagination is not null, we can go forward further.
|
||||||
|
*/
|
||||||
|
if (null !== $pagination) {
|
||||||
|
$newerId = $pagination->getNewerId();
|
||||||
|
Log::debug(sprintf('Pagination object is not null, newerID is "%s"', $newerId));
|
||||||
|
}
|
||||||
|
Log::debug('Now looping results...');
|
||||||
|
/** @var BunqPayment $payment */
|
||||||
|
foreach ($response->getValue() as $payment) {
|
||||||
|
$return[] = $this->convertPayment($payment, $bunqAccountId, $localAccount);
|
||||||
|
|
||||||
|
// store the very last transaction ID for this particular account.
|
||||||
|
$latestTransaction = $payment->getId() > $latestTransaction ? $payment->getId() : $latestTransaction;
|
||||||
|
}
|
||||||
|
Log::debug(sprintf('Count of result is now %d', \count($return)));
|
||||||
|
$count++;
|
||||||
|
if (null === $newerId) {
|
||||||
|
Log::debug('Newer ID is NULL, so stop looping cause we are done!');
|
||||||
|
$hasMoreTransactions = false;
|
||||||
|
}
|
||||||
|
if (null === $pagination) {
|
||||||
|
Log::debug('No pagination object, stop looping.');
|
||||||
|
$hasMoreTransactions = false;
|
||||||
|
}
|
||||||
|
sleep(1);
|
||||||
|
}
|
||||||
|
Log::debug(sprintf('Done with looping. Final loop count is %d, latest transaction is %d', $count, $latestTransaction));
|
||||||
|
if (null !== $latestTransaction) {
|
||||||
|
Log::debug('Latest transaction is not null, so set the preference!');
|
||||||
|
$preferenceName = sprintf('bunq-last-transaction-%d', $bunqAccountId);
|
||||||
|
$pref = \Preferences::setForUser($this->importJob->user, $preferenceName, $latestTransaction);
|
||||||
|
Log::debug(sprintf('Preference set to: %s', $pref->data));
|
||||||
}
|
}
|
||||||
|
|
||||||
return $return;
|
return $return;
|
||||||
|
@@ -62,7 +62,7 @@ class Preferences
|
|||||||
try {
|
try {
|
||||||
Preference::where('user_id', auth()->user()->id)->where('name', $name)->delete();
|
Preference::where('user_id', auth()->user()->id)->where('name', $name)->delete();
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Log::debug(sprintf('Not interesting: %s', $e->getMessage()));
|
Log::debug(sprintf('Could not delete preference: %s', $e->getMessage()));
|
||||||
// don't care.
|
// don't care.
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -101,7 +101,7 @@ class Modifier
|
|||||||
try {
|
try {
|
||||||
$compareDate = new Carbon($compare);
|
$compareDate = new Carbon($compare);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Log::debug(sprintf('Not interesting: %s', $e->getMessage()));
|
Log::debug(sprintf('Not interesting in Modifier:dateAfter(): %s', $e->getMessage()));
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -120,7 +120,7 @@ class Modifier
|
|||||||
try {
|
try {
|
||||||
$compareDate = new Carbon($compare);
|
$compareDate = new Carbon($compare);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Log::debug(sprintf('Not interesting: %s', $e->getMessage()));
|
Log::debug(sprintf('Not interesting in modifier:dateBefore(): %s', $e->getMessage()));
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -139,7 +139,7 @@ class Modifier
|
|||||||
try {
|
try {
|
||||||
$compareDate = new Carbon($compare);
|
$compareDate = new Carbon($compare);
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Log::debug(sprintf('Not interesting: %s', $e->getMessage()));
|
Log::debug(sprintf('Not interesting in Modifier:sameDate(): %s', $e->getMessage()));
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
@@ -439,7 +439,6 @@ class Steam
|
|||||||
$value = Crypt::decrypt($value);
|
$value = Crypt::decrypt($value);
|
||||||
} catch (DecryptException $e) {
|
} catch (DecryptException $e) {
|
||||||
// do not care.
|
// do not care.
|
||||||
Log::debug(sprintf('Not interesting: %s', $e->getMessage()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $value;
|
return $value;
|
||||||
|
Reference in New Issue
Block a user