mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-04 03:43:07 +00:00
Catch errors in transaction API.
This commit is contained in:
@@ -81,7 +81,7 @@ class GroupUpdateService
|
|||||||
$updated = $this->updateTransactions($transactionGroup, $transactions);
|
$updated = $this->updateTransactions($transactionGroup, $transactions);
|
||||||
Log::debug('Array of updated IDs: ', $updated);
|
Log::debug('Array of updated IDs: ', $updated);
|
||||||
|
|
||||||
if(0 === count($updated)) {
|
if (0 === count($updated)) {
|
||||||
Log::error('There were no transactions updated or created. Will not delete anything.');
|
Log::error('There were no transactions updated or created. Will not delete anything.');
|
||||||
$transactionGroup->refresh();
|
$transactionGroup->refresh();
|
||||||
app('preferences')->mark();
|
app('preferences')->mark();
|
||||||
@@ -94,7 +94,7 @@ class GroupUpdateService
|
|||||||
/** @var string $deletedId */
|
/** @var string $deletedId */
|
||||||
foreach ($result as $deletedId) {
|
foreach ($result as $deletedId) {
|
||||||
/** @var TransactionJournal $journal */
|
/** @var TransactionJournal $journal */
|
||||||
$journal = $transactionGroup->transactionJournals()->find((int)$deletedId);
|
$journal = $transactionGroup->transactionJournals()->find((int) $deletedId);
|
||||||
/** @var JournalDestroyService $service */
|
/** @var JournalDestroyService $service */
|
||||||
$service = app(JournalDestroyService::class);
|
$service = app(JournalDestroyService::class);
|
||||||
$service->destroy($journal);
|
$service->destroy($journal);
|
||||||
@@ -119,6 +119,9 @@ class GroupUpdateService
|
|||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (1 === count($data) && array_key_exists('transaction_journal_id', $data)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
/** @var JournalUpdateService $updateService */
|
/** @var JournalUpdateService $updateService */
|
||||||
$updateService = app(JournalUpdateService::class);
|
$updateService = app(JournalUpdateService::class);
|
||||||
$updateService->setTransactionGroup($transactionGroup);
|
$updateService->setTransactionGroup($transactionGroup);
|
||||||
@@ -145,7 +148,7 @@ class GroupUpdateService
|
|||||||
*/
|
*/
|
||||||
foreach ($transactions as $index => $transaction) {
|
foreach ($transactions as $index => $transaction) {
|
||||||
Log::debug(sprintf('Now at #%d of %d', ($index + 1), count($transactions)), $transaction);
|
Log::debug(sprintf('Now at #%d of %d', ($index + 1), count($transactions)), $transaction);
|
||||||
$journalId = (int)($transaction['transaction_journal_id'] ?? 0);
|
$journalId = (int) ($transaction['transaction_journal_id'] ?? 0);
|
||||||
/** @var TransactionJournal|null $journal */
|
/** @var TransactionJournal|null $journal */
|
||||||
$journal = $transactionGroup->transactionJournals()->find($journalId);
|
$journal = $transactionGroup->transactionJournals()->find($journalId);
|
||||||
if (null === $journal) {
|
if (null === $journal) {
|
||||||
|
@@ -55,7 +55,7 @@ trait GroupValidation
|
|||||||
$transactions = $this->getTransactionsArray($validator);
|
$transactions = $this->getTransactionsArray($validator);
|
||||||
$validDescriptions = 0;
|
$validDescriptions = 0;
|
||||||
foreach ($transactions as $transaction) {
|
foreach ($transactions as $transaction) {
|
||||||
if ('' !== (string)($transaction['description'] ?? null)) {
|
if ('' !== (string) ($transaction['description'] ?? null)) {
|
||||||
$validDescriptions++;
|
$validDescriptions++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -63,7 +63,7 @@ trait GroupValidation
|
|||||||
// no valid descriptions?
|
// no valid descriptions?
|
||||||
if (0 === $validDescriptions) {
|
if (0 === $validDescriptions) {
|
||||||
$validator->errors()->add(
|
$validator->errors()->add(
|
||||||
'transactions.0.description', (string)trans('validation.filled', ['attribute' => (string)trans('validation.attributes.description')])
|
'transactions.0.description', (string) trans('validation.filled', ['attribute' => (string) trans('validation.attributes.description')])
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -79,7 +79,7 @@ trait GroupValidation
|
|||||||
|
|
||||||
$groupTitle = $data['group_title'] ?? '';
|
$groupTitle = $data['group_title'] ?? '';
|
||||||
if ('' === $groupTitle && count($transactions) > 1) {
|
if ('' === $groupTitle && count($transactions) > 1) {
|
||||||
$validator->errors()->add('group_title', (string)trans('validation.group_title_mandatory'));
|
$validator->errors()->add('group_title', (string) trans('validation.group_title_mandatory'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,26 +88,29 @@ trait GroupValidation
|
|||||||
*/
|
*/
|
||||||
protected function preventNoAccountInfo(Validator $validator): void
|
protected function preventNoAccountInfo(Validator $validator): void
|
||||||
{
|
{
|
||||||
$transactions = $this->getTransactionsArray($validator);
|
$transactions = $this->getTransactionsArray($validator);
|
||||||
$hasAccountInfo = false;
|
$keys = ['source_id', 'destination_id', 'source_name', 'destination_name', 'source_iban', 'destination_iban', 'source_number', 'destination_number'];
|
||||||
$keys = ['source_id', 'destination_id', 'source_name', 'destination_name', 'source_iban', 'destination_iban', 'source_number',
|
|
||||||
'destination_number'];
|
|
||||||
/** @var array $transaction */
|
/** @var array $transaction */
|
||||||
foreach ($transactions as $transaction) {
|
foreach ($transactions as $index => $transaction) {
|
||||||
foreach($keys as $key) {
|
$hasAccountInfo = false;
|
||||||
if(array_key_exists($key, $transaction) && '' !== (string) $transaction[$key]) {
|
$hasJournalId = array_key_exists('transaction_journal_id', $transaction);
|
||||||
|
foreach ($keys as $key) {
|
||||||
|
if (array_key_exists($key, $transaction) && '' !== (string) $transaction[$key]) {
|
||||||
$hasAccountInfo = true;
|
$hasAccountInfo = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// set errors:
|
||||||
|
if (false === $hasAccountInfo && !$hasJournalId) {
|
||||||
|
$validator->errors()->add(
|
||||||
|
sprintf('transactions.%d.source_id', $index), (string) trans('validation.generic_no_source')
|
||||||
|
);
|
||||||
|
$validator->errors()->add(
|
||||||
|
sprintf('transactions.%d.destination_id', $index), (string) trans('validation.generic_no_destination')
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if(false === $hasAccountInfo) {
|
|
||||||
$validator->errors()->add(
|
// only an issue if there is no transaction_journal_id
|
||||||
'transactions.0.source_id', (string)trans('validation.generic_no_source')
|
|
||||||
);
|
|
||||||
$validator->errors()->add(
|
|
||||||
'transactions.0.destination_id', (string)trans('validation.generic_no_destination')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -164,7 +167,7 @@ trait GroupValidation
|
|||||||
if (null === $journalId || 0 === $count) {
|
if (null === $journalId || 0 === $count) {
|
||||||
Log::warning(sprintf('Transaction group #%d has %d journals with ID %d', $transactionGroup->id, $count, $journalId));
|
Log::warning(sprintf('Transaction group #%d has %d journals with ID %d', $transactionGroup->id, $count, $journalId));
|
||||||
Log::warning('Invalid submission: Each split must have transaction_journal_id (either valid ID or 0).');
|
Log::warning('Invalid submission: Each split must have transaction_journal_id (either valid ID or 0).');
|
||||||
$validator->errors()->add(sprintf('transactions.%d.source_name', $index), (string)trans('validation.need_id_in_edit'));
|
$validator->errors()->add(sprintf('transactions.%d.source_name', $index), (string) trans('validation.need_id_in_edit'));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user