mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-02-25 18:45:27 -06:00
Refactor temporary account storage and fix a bug in the bunq import. #1607
This commit is contained in:
parent
e1402d5d8a
commit
3a427dd0f4
@ -85,7 +85,7 @@ class JobStatusController extends Controller
|
|||||||
*/
|
*/
|
||||||
public function json(ImportJob $importJob): JsonResponse
|
public function json(ImportJob $importJob): JsonResponse
|
||||||
{
|
{
|
||||||
$count = \count($importJob->transactions);
|
$count = $this->repository->countTransactions($importJob);
|
||||||
$json = [
|
$json = [
|
||||||
'status' => $importJob->status,
|
'status' => $importJob->status,
|
||||||
'errors' => $importJob->errors,
|
'errors' => $importJob->errors,
|
||||||
|
@ -73,11 +73,11 @@ class ImportArrayStorage
|
|||||||
public function setImportJob(ImportJob $importJob): void
|
public function setImportJob(ImportJob $importJob): void
|
||||||
{
|
{
|
||||||
$this->importJob = $importJob;
|
$this->importJob = $importJob;
|
||||||
$this->countTransfers();
|
|
||||||
|
|
||||||
$this->repository = app(ImportJobRepositoryInterface::class);
|
$this->repository = app(ImportJobRepositoryInterface::class);
|
||||||
$this->repository->setUser($importJob->user);
|
$this->repository->setUser($importJob->user);
|
||||||
|
|
||||||
|
$this->countTransfers();
|
||||||
|
|
||||||
$this->journalRepos = app(JournalRepositoryInterface::class);
|
$this->journalRepos = app(JournalRepositoryInterface::class);
|
||||||
$this->journalRepos->setUser($importJob->user);
|
$this->journalRepos->setUser($importJob->user);
|
||||||
|
|
||||||
@ -157,7 +157,9 @@ class ImportArrayStorage
|
|||||||
{
|
{
|
||||||
Log::debug('Now in countTransfers()');
|
Log::debug('Now in countTransfers()');
|
||||||
/** @var array $array */
|
/** @var array $array */
|
||||||
$array = $this->importJob->transactions;
|
$array = $this->repository->getTransactions($this->importJob);
|
||||||
|
|
||||||
|
|
||||||
$count = 0;
|
$count = 0;
|
||||||
foreach ($array as $index => $transaction) {
|
foreach ($array as $index => $transaction) {
|
||||||
if (strtolower(TransactionType::TRANSFER) === strtolower($transaction['type'])) {
|
if (strtolower(TransactionType::TRANSFER) === strtolower($transaction['type'])) {
|
||||||
@ -418,7 +420,7 @@ class ImportArrayStorage
|
|||||||
private function storeArray(): Collection
|
private function storeArray(): Collection
|
||||||
{
|
{
|
||||||
/** @var array $array */
|
/** @var array $array */
|
||||||
$array = $this->importJob->transactions;
|
$array = $this->repository->getTransactions($this->importJob);
|
||||||
$count = \count($array);
|
$count = \count($array);
|
||||||
$toStore = [];
|
$toStore = [];
|
||||||
|
|
||||||
@ -541,8 +543,9 @@ class ImportArrayStorage
|
|||||||
Log::debug(sprintf('Comparison is a hit! (%s)', $hits));
|
Log::debug(sprintf('Comparison is a hit! (%s)', $hits));
|
||||||
|
|
||||||
// compare description:
|
// compare description:
|
||||||
Log::debug(sprintf('Comparing "%s" to "%s"', $description, $transfer->description));
|
$comparison = '(empty description)' === $transfer->description ? '' : $transfer->description;
|
||||||
if ($description !== $transfer->description) {
|
Log::debug(sprintf('Comparing "%s" to "%s" (original: "%s")', $description, $transfer->description, $comparison));
|
||||||
|
if ($description !== $comparison) {
|
||||||
continue; // @codeCoverageIgnore
|
continue; // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
++$hits;
|
++$hits;
|
||||||
|
@ -22,6 +22,7 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace FireflyIII\Models;
|
namespace FireflyIII\Models;
|
||||||
|
|
||||||
|
use Carbon\Carbon;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Illuminate\Database\Eloquent\Model;
|
use Illuminate\Database\Eloquent\Model;
|
||||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||||
@ -45,6 +46,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
|||||||
* @property array $errors
|
* @property array $errors
|
||||||
* @property array extended_status
|
* @property array extended_status
|
||||||
* @property int id
|
* @property int id
|
||||||
|
* @property Carbon $created_at
|
||||||
*/
|
*/
|
||||||
class ImportJob extends Model
|
class ImportJob extends Model
|
||||||
{
|
{
|
||||||
|
@ -84,26 +84,36 @@ class ImportJobRepository implements ImportJobRepositoryInterface
|
|||||||
* @param array $transactions
|
* @param array $transactions
|
||||||
*
|
*
|
||||||
* @return ImportJob
|
* @return ImportJob
|
||||||
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
||||||
*/
|
*/
|
||||||
public function appendTransactions(ImportJob $job, array $transactions): ImportJob
|
public function appendTransactions(ImportJob $job, array $transactions): ImportJob
|
||||||
{
|
{
|
||||||
Log::debug(sprintf('Now in appendTransactions(%s)', $job->key));
|
Log::debug(sprintf('Now in appendTransactions(%s)', $job->key));
|
||||||
$existingTransactions = $job->transactions;
|
$existingTransactions = $this->getTransactions($job);
|
||||||
if (!\is_array($existingTransactions)) {
|
|
||||||
$existingTransactions = [];
|
|
||||||
}
|
|
||||||
$new = array_merge($existingTransactions, $transactions);
|
$new = array_merge($existingTransactions, $transactions);
|
||||||
Log::debug(sprintf('Old transaction count: %d', \count($existingTransactions)));
|
Log::debug(sprintf('Old transaction count: %d', \count($existingTransactions)));
|
||||||
Log::debug(sprintf('To be added transaction count: %d', \count($transactions)));
|
Log::debug(sprintf('To be added transaction count: %d', \count($transactions)));
|
||||||
Log::debug(sprintf('New count: %d', \count($new)));
|
Log::debug(sprintf('New count: %d', \count($new)));
|
||||||
$job->transactions = $new;
|
$this->setTransactions($job, $new);
|
||||||
|
|
||||||
|
|
||||||
$job->save();
|
|
||||||
|
|
||||||
return $job;
|
return $job;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ImportJob $job
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function countTransactions(ImportJob $job): int
|
||||||
|
{
|
||||||
|
$info = $job->transactions ?? [];
|
||||||
|
if (isset($info['count'])) {
|
||||||
|
return (int)$info['count'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $importProvider
|
* @param string $importProvider
|
||||||
*
|
*
|
||||||
@ -201,6 +211,31 @@ class ImportJobRepository implements ImportJobRepositoryInterface
|
|||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return transactions from attachment.
|
||||||
|
*
|
||||||
|
* @param ImportJob $job
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
|
||||||
|
*/
|
||||||
|
public function getTransactions(ImportJob $job): array
|
||||||
|
{
|
||||||
|
// this will overwrite all transactions currently in the job.
|
||||||
|
$disk = Storage::disk('upload');
|
||||||
|
$filename = sprintf('%s-%s.crypt.json', $job->created_at->format('Ymd'), $job->key);
|
||||||
|
$array = [];
|
||||||
|
if ($disk->exists($filename)) {
|
||||||
|
$json = Crypt::decrypt($disk->get($filename));
|
||||||
|
$array = json_decode($json, true);
|
||||||
|
}
|
||||||
|
if (false === $array) {
|
||||||
|
$array = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ImportJob $job
|
* @param ImportJob $job
|
||||||
* @param array $configuration
|
* @param array $configuration
|
||||||
@ -272,8 +307,17 @@ class ImportJobRepository implements ImportJobRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function setTransactions(ImportJob $job, array $transactions): ImportJob
|
public function setTransactions(ImportJob $job, array $transactions): ImportJob
|
||||||
{
|
{
|
||||||
$job->transactions = $transactions;
|
// this will overwrite all transactions currently in the job.
|
||||||
|
$disk = Storage::disk('upload');
|
||||||
|
$filename = sprintf('%s-%s.crypt.json', $job->created_at->format('Ymd'), $job->key);
|
||||||
|
$json = Crypt::encrypt(json_encode($transactions));
|
||||||
|
|
||||||
|
// set count for easy access
|
||||||
|
$array = ['count' => \count($transactions)];
|
||||||
|
$job->transactions = $array;
|
||||||
$job->save();
|
$job->save();
|
||||||
|
// store file.
|
||||||
|
$disk->put($filename, $json);
|
||||||
|
|
||||||
return $job;
|
return $job;
|
||||||
}
|
}
|
||||||
@ -377,7 +421,7 @@ class ImportJobRepository implements ImportJobRepositoryInterface
|
|||||||
$fileObject->rewind();
|
$fileObject->rewind();
|
||||||
|
|
||||||
|
|
||||||
if(0 === $file->getSize()) {
|
if (0 === $file->getSize()) {
|
||||||
throw new FireflyException('Cannot upload empty or non-existent file.');
|
throw new FireflyException('Cannot upload empty or non-existent file.');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -390,7 +434,6 @@ class ImportJobRepository implements ImportJobRepositoryInterface
|
|||||||
return new MessageBag;
|
return new MessageBag;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @codeCoverageIgnore
|
* @codeCoverageIgnore
|
||||||
*
|
*
|
||||||
|
@ -35,6 +35,7 @@ use Symfony\Component\HttpFoundation\File\UploadedFile;
|
|||||||
*/
|
*/
|
||||||
interface ImportJobRepositoryInterface
|
interface ImportJobRepositoryInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add message to job.
|
* Add message to job.
|
||||||
*
|
*
|
||||||
@ -55,6 +56,13 @@ interface ImportJobRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function appendTransactions(ImportJob $job, array $transactions): ImportJob;
|
public function appendTransactions(ImportJob $job, array $transactions): ImportJob;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ImportJob $job
|
||||||
|
*
|
||||||
|
* @return int
|
||||||
|
*/
|
||||||
|
public function countTransactions(ImportJob $job): int;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $importProvider
|
* @param string $importProvider
|
||||||
*
|
*
|
||||||
@ -96,6 +104,15 @@ interface ImportJobRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function getExtendedStatus(ImportJob $job): array;
|
public function getExtendedStatus(ImportJob $job): array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return transactions from attachment.
|
||||||
|
*
|
||||||
|
* @param ImportJob $job
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getTransactions(ImportJob $job): array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ImportJob $job
|
* @param ImportJob $job
|
||||||
* @param array $configuration
|
* @param array $configuration
|
||||||
|
Loading…
Reference in New Issue
Block a user