From 95ce72fce7c6cb72687c7f78468919e0a4ffda0f Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 11 Aug 2018 18:27:45 +0200 Subject: [PATCH] Expand API options for available budgets and journal links --- .../Controllers/AvailableBudgetController.php | 3 + .../V1/Controllers/JournalLinkController.php | 3 +- .../V1/Requests/AvailableBudgetRequest.php | 18 ++--- app/Api/V1/Requests/JournalLinkRequest.php | 72 ++++++++++++++++--- .../LinkType/LinkTypeRepository.php | 5 ++ 5 files changed, 83 insertions(+), 18 deletions(-) diff --git a/app/Api/V1/Controllers/AvailableBudgetController.php b/app/Api/V1/Controllers/AvailableBudgetController.php index fa2697dbeb..9ea09b38e0 100644 --- a/app/Api/V1/Controllers/AvailableBudgetController.php +++ b/app/Api/V1/Controllers/AvailableBudgetController.php @@ -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.'); } diff --git a/app/Api/V1/Controllers/JournalLinkController.php b/app/Api/V1/Controllers/JournalLinkController.php index 7ae6b12792..c428fc0146 100644 --- a/app/Api/V1/Controllers/JournalLinkController.php +++ b/app/Api/V1/Controllers/JournalLinkController.php @@ -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'); diff --git a/app/Api/V1/Requests/AvailableBudgetRequest.php b/app/Api/V1/Requests/AvailableBudgetRequest.php index dd6daf4e65..2ad7fe1367 100644 --- a/app/Api/V1/Requests/AvailableBudgetRequest.php +++ b/app/Api/V1/Requests/AvailableBudgetRequest.php @@ -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; diff --git a/app/Api/V1/Requests/JournalLinkRequest.php b/app/Api/V1/Requests/JournalLinkRequest.php index 19e5f8f430..2c31e0d889 100644 --- a/app/Api/V1/Requests/JournalLinkRequest.php +++ b/app/Api/V1/Requests/JournalLinkRequest.php @@ -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.'); + } + } } diff --git a/app/Repositories/LinkType/LinkTypeRepository.php b/app/Repositories/LinkType/LinkTypeRepository.php index 10d53f37af..9006f4c459 100644 --- a/app/Repositories/LinkType/LinkTypeRepository.php +++ b/app/Repositories/LinkType/LinkTypeRepository.php @@ -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; }