Expand API options for available budgets and journal links

This commit is contained in:
James Cole 2018-08-11 18:27:45 +02:00
parent a803dfc7fa
commit 95ce72fce7
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
5 changed files with 83 additions and 18 deletions

View File

@ -153,6 +153,9 @@ class AvailableBudgetController extends Controller
{
$data = $request->getAll();
$currency = $this->currencyRepository->findNull($data['transaction_currency_id']);
if (null === $currency) {
$this->currencyRepository->findByCodeNull($data['transaction_currency_code']);
}
if (null === $currency) {
throw new FireflyException('Could not find the indicated currency.');
}

View File

@ -175,8 +175,7 @@ class JournalLinkController extends Controller
$data['direction'] = 'inward';
$journalLink = $this->repository->storeLink($data, $inward, $outward);
$resource = new Item($journalLink, new JournalLinkTransformer($this->parameters), 'journal_links');
$resource = new Item($journalLink, new JournalLinkTransformer($this->parameters), 'journal_links');
return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json');

View File

@ -47,10 +47,11 @@ class AvailableBudgetRequest extends Request
public function getAll(): array
{
return [
'transaction_currency_id' => $this->integer('transaction_currency_id'),
'amount' => $this->string('amount'),
'start_date' => $this->date('start_date'),
'end_date' => $this->date('end_date'),
'transaction_currency_id' => $this->integer('currency_id'),
'transaction_currency_code' => $this->string('currency_code'),
'amount' => $this->string('amount'),
'start_date' => $this->date('start_date'),
'end_date' => $this->date('end_date'),
];
}
@ -62,10 +63,11 @@ class AvailableBudgetRequest extends Request
public function rules(): array
{
$rules = [
'transaction_currency_id' => 'required|numeric|exists:transaction_currencies,id',
'amount' => 'required|numeric|more:0',
'start_date' => 'required|date|before:end_date',
'end_date' => 'required|date|after:start_date',
'currency_id' => 'numeric|exists:transaction_currencies,id|required_without:currency_code',
'currency_code' => 'min:3|max:3|exists:transaction_currencies,code|required_without:currency_id',
'amount' => 'required|numeric|more:0',
'start_date' => 'required|date|before:end_date',
'end_date' => 'required|date|after:start_date',
];
return $rules;

View File

@ -23,6 +23,9 @@ declare(strict_types=1);
namespace FireflyIII\Api\V1\Requests;
use FireflyIII\Repositories\Journal\JournalRepositoryInterface;
use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface;
use Illuminate\Validation\Validator;
/**
*
@ -49,10 +52,11 @@ class JournalLinkRequest extends Request
public function getAll(): array
{
return [
'link_type_id' => $this->integer('link_type_id'),
'inward_id' => $this->integer('inward_id'),
'outward_id' => $this->integer('outward_id'),
'notes' => $this->string('notes'),
'link_type_id' => $this->integer('link_type_id'),
'link_type_name' => $this->string('link_type_name'),
'inward_id' => $this->integer('inward_id'),
'outward_id' => $this->integer('outward_id'),
'notes' => $this->string('notes'),
];
}
@ -65,11 +69,63 @@ class JournalLinkRequest extends Request
public function rules(): array
{
return [
'link_type_id' => 'required|exists:link_types,id',
'inward_id' => 'required|belongsToUser:transaction_journals,id',
'outward_id' => 'required|belongsToUser:transaction_journals,id',
'notes' => 'between:0,65000',
'link_type_id' => 'exists:link_types,id|required_without:link_type_name',
'link_type_name' => 'exists:link_types,name|required_without:link_type_id',
'inward_id' => 'required|belongsToUser:transaction_journals,id',
'outward_id' => 'required|belongsToUser:transaction_journals,id',
'notes' => 'between:0,65000',
];
}
/**
* Configure the validator instance.
*
* @param Validator $validator
*
* @return void
*/
public function withValidator(Validator $validator): void
{
$validator->after(
function (Validator $validator) {
$this->validateExistingLink($validator);
}
);
}
/**
* @param Validator $validator
*/
private function validateExistingLink(Validator $validator): void
{
/** @var LinkTypeRepositoryInterface $repository */
$repository = app(LinkTypeRepositoryInterface::class);
$repository->setUser(auth()->user());
/** @var JournalRepositoryInterface $journalRepos */
$journalRepos = app(JournalRepositoryInterface::class);
$journalRepos->setUser(auth()->user());
$data = $validator->getData();
$inwardId = (int)($data['inward_id'] ?? 0);
$outwardId = (int)($data['outward_id'] ?? 0);
$inward = $journalRepos->findNull($inwardId);
$outward = $journalRepos->findNull($outwardId);
if (null === $inward) {
$validator->errors()->add('inward_id', 'Invalid inward ID.');
return;
}
if (null === $outward) {
$validator->errors()->add('outward_id', 'Invalid outward ID.');
return;
}
if ($repository->findLink($inward, $outward)) {
$validator->errors()->add('outward_id', 'Already have a link between inward and outward.');
$validator->errors()->add('inward_id', 'Already have a link between inward and outward.');
}
}
}

View File

@ -230,6 +230,11 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface
public function storeLink(array $information, TransactionJournal $inward, TransactionJournal $outward): ?TransactionJournalLink
{
$linkType = $this->findNull((int)($information['link_type_id'] ?? 0));
if (null === $linkType) {
$linkType = $this->findByName($information['link_type_name']);
}
if (null === $linkType) {
return null;
}