Refactor temporary account storage and fix a bug in the bunq import. #1607

This commit is contained in:
James Cole 2018-10-25 20:03:48 +02:00
parent e1402d5d8a
commit 3a427dd0f4
5 changed files with 85 additions and 20 deletions

View File

@ -85,7 +85,7 @@ class JobStatusController extends Controller
*/
public function json(ImportJob $importJob): JsonResponse
{
$count = \count($importJob->transactions);
$count = $this->repository->countTransactions($importJob);
$json = [
'status' => $importJob->status,
'errors' => $importJob->errors,

View File

@ -72,12 +72,12 @@ class ImportArrayStorage
*/
public function setImportJob(ImportJob $importJob): void
{
$this->importJob = $importJob;
$this->countTransfers();
$this->importJob = $importJob;
$this->repository = app(ImportJobRepositoryInterface::class);
$this->repository->setUser($importJob->user);
$this->countTransfers();
$this->journalRepos = app(JournalRepositoryInterface::class);
$this->journalRepos->setUser($importJob->user);
@ -157,7 +157,9 @@ class ImportArrayStorage
{
Log::debug('Now in countTransfers()');
/** @var array $array */
$array = $this->importJob->transactions;
$array = $this->repository->getTransactions($this->importJob);
$count = 0;
foreach ($array as $index => $transaction) {
if (strtolower(TransactionType::TRANSFER) === strtolower($transaction['type'])) {
@ -418,7 +420,7 @@ class ImportArrayStorage
private function storeArray(): Collection
{
/** @var array $array */
$array = $this->importJob->transactions;
$array = $this->repository->getTransactions($this->importJob);
$count = \count($array);
$toStore = [];
@ -541,8 +543,9 @@ class ImportArrayStorage
Log::debug(sprintf('Comparison is a hit! (%s)', $hits));
// compare description:
Log::debug(sprintf('Comparing "%s" to "%s"', $description, $transfer->description));
if ($description !== $transfer->description) {
$comparison = '(empty description)' === $transfer->description ? '' : $transfer->description;
Log::debug(sprintf('Comparing "%s" to "%s" (original: "%s")', $description, $transfer->description, $comparison));
if ($description !== $comparison) {
continue; // @codeCoverageIgnore
}
++$hits;

View File

@ -22,6 +22,7 @@ declare(strict_types=1);
namespace FireflyIII\Models;
use Carbon\Carbon;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
@ -45,6 +46,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @property array $errors
* @property array extended_status
* @property int id
* @property Carbon $created_at
*/
class ImportJob extends Model
{

View File

@ -84,26 +84,36 @@ class ImportJobRepository implements ImportJobRepositoryInterface
* @param array $transactions
*
* @return ImportJob
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function appendTransactions(ImportJob $job, array $transactions): ImportJob
{
Log::debug(sprintf('Now in appendTransactions(%s)', $job->key));
$existingTransactions = $job->transactions;
if (!\is_array($existingTransactions)) {
$existingTransactions = [];
}
$new = array_merge($existingTransactions, $transactions);
$existingTransactions = $this->getTransactions($job);
$new = array_merge($existingTransactions, $transactions);
Log::debug(sprintf('Old transaction count: %d', \count($existingTransactions)));
Log::debug(sprintf('To be added transaction count: %d', \count($transactions)));
Log::debug(sprintf('New count: %d', \count($new)));
$job->transactions = $new;
$job->save();
$this->setTransactions($job, $new);
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
*
@ -201,6 +211,31 @@ class ImportJobRepository implements ImportJobRepositoryInterface
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 array $configuration
@ -272,8 +307,17 @@ class ImportJobRepository implements ImportJobRepositoryInterface
*/
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();
// store file.
$disk->put($filename, $json);
return $job;
}
@ -377,7 +421,7 @@ class ImportJobRepository implements ImportJobRepositoryInterface
$fileObject->rewind();
if(0 === $file->getSize()) {
if (0 === $file->getSize()) {
throw new FireflyException('Cannot upload empty or non-existent file.');
}
@ -390,7 +434,6 @@ class ImportJobRepository implements ImportJobRepositoryInterface
return new MessageBag;
}
/**
* @codeCoverageIgnore
*

View File

@ -35,6 +35,7 @@ use Symfony\Component\HttpFoundation\File\UploadedFile;
*/
interface ImportJobRepositoryInterface
{
/**
* Add message to job.
*
@ -55,6 +56,13 @@ interface ImportJobRepositoryInterface
*/
public function appendTransactions(ImportJob $job, array $transactions): ImportJob;
/**
* @param ImportJob $job
*
* @return int
*/
public function countTransactions(ImportJob $job): int;
/**
* @param string $importProvider
*
@ -96,6 +104,15 @@ interface ImportJobRepositoryInterface
*/
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 array $configuration