Expand tests and API code.

This commit is contained in:
James Cole
2018-02-18 10:31:15 +01:00
parent 6cda9f2900
commit eb0da038fb
28 changed files with 1171 additions and 172 deletions

View File

@@ -98,7 +98,7 @@ class BillController extends Controller
$resource = new FractalCollection($bills, new BillTransformer($this->parameters), 'bills');
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
return response()->json($manager->createData($resource)->toArray());
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
@@ -120,7 +120,7 @@ class BillController extends Controller
$resource = new Item($bill, new BillTransformer($this->parameters), 'bills');
return response()->json($manager->createData($resource)->toArray());
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
/**
@@ -137,7 +137,7 @@ class BillController extends Controller
$resource = new Item($bill, new BillTransformer($this->parameters), 'bills');
return response()->json($manager->createData($resource)->toArray());
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
@@ -158,7 +158,7 @@ class BillController extends Controller
$resource = new Item($bill, new BillTransformer($this->parameters), 'bills');
return response()->json($manager->createData($resource)->toArray());
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
}

View File

@@ -34,6 +34,7 @@ use Symfony\Component\HttpFoundation\ParameterBag;
/**
* Class Controller.
* @codeCoverageIgnore
*/
class Controller extends BaseController
{

View File

@@ -26,6 +26,7 @@ namespace FireflyIII\Api\V1\Controllers;
use FireflyIII\Api\V1\Requests\TransactionRequest;
use FireflyIII\Factory\TransactionJournalFactory;
use FireflyIII\Helpers\Collector\JournalCollector;
use FireflyIII\Helpers\Collector\JournalCollectorInterface;
use FireflyIII\Helpers\Filter\InternalTransferFilter;
use FireflyIII\Helpers\Filter\NegativeAmountFilter;
use FireflyIII\Helpers\Filter\PositiveAmountFilter;
@@ -107,7 +108,7 @@ class TransactionController extends Controller
$manager->setSerializer(new JsonApiSerializer($baseUrl));
// collect transactions using the journal collector
$collector = new JournalCollector;
$collector = app(JournalCollectorInterface::class);
$collector->setUser(auth()->user());
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
$collector->setAllAssetAccounts();
@@ -130,7 +131,7 @@ class TransactionController extends Controller
$resource = new FractalCollection($transactions, new TransactionTransformer($this->parameters), 'transactions');
$resource->setPaginator(new IlluminatePaginatorAdapter($paginator));
return response()->json($manager->createData($resource)->toArray());
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
@@ -152,7 +153,7 @@ class TransactionController extends Controller
// needs a lot of extra data to match the journal collector. Or just expand that one.
// collect transactions using the journal collector
$collector = new JournalCollector;
$collector = app(JournalCollectorInterface::class);
$collector->setUser(auth()->user());
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
// filter on specific journals.
@@ -167,11 +168,10 @@ class TransactionController extends Controller
$collector->addFilter(NegativeAmountFilter::class);
}
$transaction = $collector->getJournals();
$transactions = $collector->getJournals();
$resource = new Item($transactions->first(), new TransactionTransformer($this->parameters), 'transactions');
$resource = new FractalCollection($transaction, new TransactionTransformer($this->parameters), 'transactions');
return response()->json($manager->createData($resource)->toArray());
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
/**
@@ -199,7 +199,7 @@ class TransactionController extends Controller
// needs a lot of extra data to match the journal collector. Or just expand that one.
// collect transactions using the journal collector
$collector = new JournalCollector;
$collector = app(JournalCollectorInterface::class);
$collector->setUser(auth()->user());
$collector->withOpposingAccount()->withCategoryInformation()->withBudgetInformation();
// filter on specific journals.
@@ -214,10 +214,10 @@ class TransactionController extends Controller
$collector->addFilter(NegativeAmountFilter::class);
}
$transaction = $collector->getJournals();
$resource = new FractalCollection($transaction, new TransactionTransformer($this->parameters), 'transactions');
$transactions = $collector->getJournals();
$resource = new Item($transactions->first(), new TransactionTransformer($this->parameters), 'transactions');
return response()->json($manager->createData($resource)->toArray());
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
@@ -238,7 +238,7 @@ class TransactionController extends Controller
$resource = new Item($bill, new BillTransformer($this->parameters), 'bills');
return response()->json($manager->createData($resource)->toArray());
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');
}
@@ -308,7 +308,7 @@ class TransactionController extends Controller
return $types[$type];
}
return $types['default'];
return $types['default']; // @codeCoverageIgnore
}
}

View File

@@ -23,6 +23,8 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use Illuminate\Validation\Validator;
/**
* Class BillRequest
*/
@@ -93,4 +95,25 @@ class BillRequest extends Request
return $rules;
}
/**
* Configure the validator instance.
*
* @param Validator $validator
*
* @return void
*/
public function withValidator(Validator $validator): void
{
$validator->after(
function (Validator $validator) {
$data = $validator->getData();
$min = floatval($data['amount_min']);
$max = floatval($data['amount_max']);
if ($min > $max) {
$validator->errors()->add('amount_min', trans('validation.amount_min_over_max'));
}
}
);
}
}

View File

@@ -77,27 +77,26 @@ class TransactionRequest extends Request
];
foreach ($this->get('transactions') as $transaction) {
$array = [
'description' => $transaction['description'] ?? null,
'amount' => $transaction['amount'],
'currency_id' => isset($transaction['currency_id']) ? intval($transaction['currency_id']) : null,
'currency_code' => isset($transaction['currency_code']) ? $transaction['currency_code'] : null,
'foreign_amount' => $transaction['foreign_amount'] ?? null,
'foreign_currency_id' => isset($transaction['foreign_currency_id']) ? intval($transaction['foreign_currency_id']) : null,
'foreign_currency_code' => $transaction['foreign_currency_code'] ?? null,
'budget_id' => isset($transaction['budget_id']) ? intval($transaction['budget_id']) : null,
'budget_name' => $transaction['budget_name'] ?? null,
'category_id' => isset($transaction['category_id']) ? intval($transaction['category_id']) : null,
'category_name' => $transaction['category_name'] ?? null,
'source_account_id' => isset($transaction['source_account_id']) ? intval($transaction['source_account_id']) : null,
'source_account_name' => $transaction['source_account_name'] ?? null,
'destination_account_id' => isset($transaction['destination_account_id']) ? intval($transaction['destination_account_id']) : null,
'destination_account_name' => $transaction['destination_account_name'] ?? null,
'reconciled' => intval($transaction['reconciled'] ?? 0) === 1 ? true : false,
'identifier' => isset($transaction['identifier']) ? intval($transaction['identifier']) : 0,
'description' => $transaction['description'] ?? null,
'amount' => $transaction['amount'],
'currency_id' => isset($transaction['currency_id']) ? intval($transaction['currency_id']) : null,
'currency_code' => isset($transaction['currency_code']) ? $transaction['currency_code'] : null,
'foreign_amount' => $transaction['foreign_amount'] ?? null,
'foreign_currency_id' => isset($transaction['foreign_currency_id']) ? intval($transaction['foreign_currency_id']) : null,
'foreign_currency_code' => $transaction['foreign_currency_code'] ?? null,
'budget_id' => isset($transaction['budget_id']) ? intval($transaction['budget_id']) : null,
'budget_name' => $transaction['budget_name'] ?? null,
'category_id' => isset($transaction['category_id']) ? intval($transaction['category_id']) : null,
'category_name' => $transaction['category_name'] ?? null,
'source_id' => isset($transaction['source_id']) ? intval($transaction['source_id']) : null,
'source_name' => $transaction['source_name'] ?? null,
'destination_id' => isset($transaction['destination_id']) ? intval($transaction['destination_id']) : null,
'destination_name' => $transaction['destination_name'] ?? null,
'reconciled' => intval($transaction['reconciled'] ?? 0) === 1 ? true : false,
'identifier' => isset($transaction['identifier']) ? intval($transaction['identifier']) : 0,
];
$data['transactions'][] = $array;
}
return $data;
}
@@ -108,44 +107,44 @@ class TransactionRequest extends Request
{
return [
// basic fields for journal:
'type' => 'required|in:withdrawal,deposit,transfer',
'date' => 'required|date',
'description' => 'between:1,255',
'piggy_bank_id' => ['numeric', 'nullable', 'mustExist:piggy_banks,id', new BelongsUser],
'piggy_bank_name' => ['between:1,255', 'nullable', new BelongsUser],
'bill_id' => ['numeric', 'nullable', 'mustExist:bills,id', new BelongsUser],
'bill_name' => ['between:1,255', 'nullable', new BelongsUser],
'tags' => 'between:1,255',
'type' => 'required|in:withdrawal,deposit,transfer',
'date' => 'required|date',
'description' => 'between:1,255',
'piggy_bank_id' => ['numeric', 'nullable', 'mustExist:piggy_banks,id', new BelongsUser],
'piggy_bank_name' => ['between:1,255', 'nullable', new BelongsUser],
'bill_id' => ['numeric', 'nullable', 'mustExist:bills,id', new BelongsUser],
'bill_name' => ['between:1,255', 'nullable', new BelongsUser],
'tags' => 'between:1,255',
// then, custom fields for journal
'interest_date' => 'date|nullable',
'book_date' => 'date|nullable',
'process_date' => 'date|nullable',
'due_date' => 'date|nullable',
'payment_date' => 'date|nullable',
'invoice_date' => 'date|nullable',
'internal_reference' => 'min:1,max:255|nullable',
'notes' => 'min:1,max:50000|nullable',
'interest_date' => 'date|nullable',
'book_date' => 'date|nullable',
'process_date' => 'date|nullable',
'due_date' => 'date|nullable',
'payment_date' => 'date|nullable',
'invoice_date' => 'date|nullable',
'internal_reference' => 'min:1,max:255|nullable',
'notes' => 'min:1,max:50000|nullable',
// transaction rules (in array for splits):
'transactions.*.description' => 'nullable|between:1,255',
'transactions.*.amount' => 'required|numeric|more:0',
'transactions.*.currency_id' => 'numeric|exists:transaction_currencies,id|required_without:transactions.*.currency_code',
'transactions.*.currency_code' => 'min:3|max:3|exists:transaction_currencies,code|required_without:transactions.*.currency_id',
'transactions.*.foreign_amount' => 'numeric|more:0',
'transactions.*.foreign_currency_id' => 'numeric|exists:transaction_currencies,id',
'transactions.*.foreign_currency_code' => 'min:3|max:3|exists:transaction_currencies,code',
'transactions.*.budget_id' => ['mustExist:budgets,id', new BelongsUser],
'transactions.*.budget_name' => ['between:1,255', 'nullable', new BelongsUser],
'transactions.*.category_id' => ['mustExist:categories,id', new BelongsUser],
'transactions.*.category_name' => 'between:1,255|nullable',
'transactions.*.reconciled' => 'boolean|nullable',
'transactions.*.identifier' => 'numeric|nullable',
'transactions.*.description' => 'nullable|between:1,255',
'transactions.*.amount' => 'required|numeric|more:0',
'transactions.*.currency_id' => 'numeric|exists:transaction_currencies,id|required_without:transactions.*.currency_code',
'transactions.*.currency_code' => 'min:3|max:3|exists:transaction_currencies,code|required_without:transactions.*.currency_id',
'transactions.*.foreign_amount' => 'numeric|more:0',
'transactions.*.foreign_currency_id' => 'numeric|exists:transaction_currencies,id',
'transactions.*.foreign_currency_code' => 'min:3|max:3|exists:transaction_currencies,code',
'transactions.*.budget_id' => ['mustExist:budgets,id', new BelongsUser],
'transactions.*.budget_name' => ['between:1,255', 'nullable', new BelongsUser],
'transactions.*.category_id' => ['mustExist:categories,id', new BelongsUser],
'transactions.*.category_name' => 'between:1,255|nullable',
'transactions.*.reconciled' => 'boolean|nullable',
'transactions.*.identifier' => 'numeric|nullable',
// basic rules will be expanded later.
'transactions.*.source_account_id' => ['numeric', 'nullable', new BelongsUser],
'transactions.*.source_account_name' => 'between:1,255|nullable',
'transactions.*.destination_account_id' => ['numeric', 'nullable', new BelongsUser],
'transactions.*.destination_account_name' => 'between:1,255|nullable',
'transactions.*.source_id' => ['numeric', 'nullable', new BelongsUser],
'transactions.*.source_name' => 'between:1,255|nullable',
'transactions.*.destination_id' => ['numeric', 'nullable', new BelongsUser],
'transactions.*.destination_name' => 'between:1,255|nullable',
// todo tags
@@ -377,37 +376,37 @@ class TransactionRequest extends Request
$transactions = $data['transactions'] ?? [];
foreach ($transactions as $index => $transaction) {
$sourceId = isset($transaction['source_account_id']) ? intval($transaction['source_account_id']) : null;
$sourceName = $transaction['source_account_name'] ?? null;
$destinationId = isset($transaction['destination_account_id']) ? intval($transaction['destination_account_id']) : null;
$destinationName = $transaction['destination_account_name'] ?? null;
$sourceId = isset($transaction['source_id']) ? intval($transaction['source_id']) : null;
$sourceName = $transaction['source_name'] ?? null;
$destinationId = isset($transaction['destination_id']) ? intval($transaction['destination_id']) : null;
$destinationName = $transaction['destination_name'] ?? null;
switch ($data['type']) {
case 'withdrawal':
$idField = 'transactions.' . $index . '.source_account_id';
$nameField = 'transactions.' . $index . '.source_account_name';
$idField = 'transactions.' . $index . '.source_id';
$nameField = 'transactions.' . $index . '.source_name';
$this->assetAccountExists($validator, $sourceId, $sourceName, $idField, $nameField);
$idField = 'transactions.' . $index . '.destination_account_id';
$nameField = 'transactions.' . $index . '.destination_account_name';
$idField = 'transactions.' . $index . '.destination_id';
$nameField = 'transactions.' . $index . '.destination_name';
$this->opposingAccountExists($validator, AccountType::EXPENSE, $destinationId, $destinationName, $idField, $nameField);
break;
case 'deposit':
$idField = 'transactions.' . $index . '.source_account_id';
$nameField = 'transactions.' . $index . '.source_account_name';
$idField = 'transactions.' . $index . '.source_id';
$nameField = 'transactions.' . $index . '.source_name';
$this->opposingAccountExists($validator, AccountType::REVENUE, $sourceId, $sourceName, $idField, $nameField);
$idField = 'transactions.' . $index . '.destination_account_id';
$nameField = 'transactions.' . $index . '.destination_account_name';
$idField = 'transactions.' . $index . '.destination_id';
$nameField = 'transactions.' . $index . '.destination_name';
$this->assetAccountExists($validator, $destinationId, $destinationName, $idField, $nameField);
break;
case 'transfer':
$idField = 'transactions.' . $index . '.source_account_id';
$nameField = 'transactions.' . $index . '.source_account_name';
$idField = 'transactions.' . $index . '.source_id';
$nameField = 'transactions.' . $index . '.source_name';
$this->assetAccountExists($validator, $sourceId, $sourceName, $idField, $nameField);
$idField = 'transactions.' . $index . '.destination_account_id';
$nameField = 'transactions.' . $index . '.destination_account_name';
$idField = 'transactions.' . $index . '.destination_id';
$nameField = 'transactions.' . $index . '.destination_name';
$this->assetAccountExists($validator, $destinationId, $destinationName, $idField, $nameField);
break;
default: