mirror of
				https://github.com/firefly-iii/firefly-iii.git
				synced 2025-10-31 10:47:00 +00:00 
			
		
		
		
	First attempt at "create transaction"-form for v2 users.
This commit is contained in:
		| @@ -32,7 +32,7 @@ use FireflyIII\Models\AccountType; | ||||
| use FireflyIII\Repositories\Account\AccountRepositoryInterface; | ||||
| use FireflyIII\Repositories\UserGroups\Account\AccountRepositoryInterface as AdminAccountRepositoryInterface; | ||||
| use FireflyIII\Support\Http\Api\AccountFilter; | ||||
| use FireflyIII\Support\Http\Api\ValidatesUserGroupTrait; | ||||
| use http\Env\Response; | ||||
| use Illuminate\Http\JsonResponse; | ||||
| 
 | ||||
| /** | ||||
| @@ -41,7 +41,6 @@ use Illuminate\Http\JsonResponse; | ||||
| class AccountController extends Controller | ||||
| { | ||||
|     use AccountFilter; | ||||
|     use ValidatesUserGroupTrait; | ||||
| 
 | ||||
|     private AdminAccountRepositoryInterface $adminRepository; | ||||
|     private array                           $balanceTypes; | ||||
| @@ -86,15 +85,14 @@ class AccountController extends Controller | ||||
|      */ | ||||
|     public function accounts(AutocompleteRequest $request): JsonResponse | ||||
|     { | ||||
|         $data  = $request->getData(); | ||||
|         $types = $data['types']; | ||||
|         $query = $data['query']; | ||||
|         $date  = $this->parameters->get('date') ?? today(config('app.timezone')); | ||||
| 
 | ||||
|         $return          = []; | ||||
|         $data            = $request->getData(); | ||||
|         $types           = $data['types']; | ||||
|         $query           = $data['query']; | ||||
|         $date            = $this->parameters->get('date') ?? today(config('app.timezone')); | ||||
|         $result          = $this->adminRepository->searchAccount((string)$query, $types, $data['limit']); | ||||
|         $defaultCurrency = app('amount')->getDefaultCurrency(); | ||||
| 
 | ||||
|         $allItems = []; | ||||
|         /** @var Account $account */ | ||||
|         foreach ($result as $account) { | ||||
|             $nameWithBalance = $account->name; | ||||
| @@ -104,11 +102,17 @@ class AccountController extends Controller | ||||
|                 $balance         = app('steam')->balance($account, $date); | ||||
|                 $nameWithBalance = sprintf('%s (%s)', $account->name, app('amount')->formatAnything($currency, $balance, false)); | ||||
|             } | ||||
| 
 | ||||
|             $return[] = [ | ||||
|             $type                 = (string)trans(sprintf('firefly.%s', $account->accountType->type)); | ||||
|             $groupedResult[$type] = $groupedResult[$type] ?? [ | ||||
|                 'group ' => $type, | ||||
|                 'items'  => [], | ||||
|             ]; | ||||
|             $allItems[]           = [ | ||||
|                 'id'                      => (string)$account->id, | ||||
|                 'value'                   => (string)$account->id, | ||||
|                 'name'                    => $account->name, | ||||
|                 'name_with_balance'       => $nameWithBalance, | ||||
|                 'label'                   => $nameWithBalance, | ||||
|                 'type'                    => $account->accountType->type, | ||||
|                 'currency_id'             => (string)$currency->id, | ||||
|                 'currency_name'           => $currency->name, | ||||
| @@ -118,10 +122,9 @@ class AccountController extends Controller | ||||
|             ]; | ||||
|         } | ||||
| 
 | ||||
|         // custom order.
 | ||||
|         usort( | ||||
|             $return, | ||||
|             function ($a, $b) { | ||||
|             $allItems, | ||||
|             function (array $a, array $b): int { | ||||
|                 $order = [AccountType::ASSET, AccountType::REVENUE, AccountType::EXPENSE]; | ||||
|                 $pos_a = array_search($a['type'], $order, true); | ||||
|                 $pos_b = array_search($b['type'], $order, true); | ||||
| @@ -129,7 +132,6 @@ class AccountController extends Controller | ||||
|                 return $pos_a - $pos_b; | ||||
|             } | ||||
|         ); | ||||
| 
 | ||||
|         return response()->json($return); | ||||
|         return response()->json($allItems); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -0,0 +1,94 @@ | ||||
| <?php | ||||
| /* | ||||
|  * TransactionController.php | ||||
|  * Copyright (c) 2023 james@firefly-iii.org | ||||
|  * | ||||
|  * This file is part of Firefly III (https://github.com/firefly-iii). | ||||
|  * | ||||
|  * This program is free software: you can redistribute it and/or modify | ||||
|  * it under the terms of the GNU Affero General Public License as | ||||
|  * published by the Free Software Foundation, either version 3 of the | ||||
|  * License, or (at your option) any later version. | ||||
|  * | ||||
|  * This program is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||
|  * GNU Affero General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU Affero General Public License | ||||
|  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | ||||
|  */ | ||||
| 
 | ||||
| declare(strict_types=1); | ||||
| 
 | ||||
| namespace FireflyIII\Api\V2\Controllers\Autocomplete; | ||||
| 
 | ||||
| use FireflyIII\Api\V2\Controllers\Controller; | ||||
| use FireflyIII\Api\V2\Request\Autocomplete\AutocompleteRequest; | ||||
| use FireflyIII\Models\TransactionJournal; | ||||
| use FireflyIII\Repositories\UserGroups\Journal\JournalRepositoryInterface; | ||||
| use Illuminate\Http\JsonResponse; | ||||
| 
 | ||||
| /** | ||||
|  * Class TransactionController | ||||
|  */ | ||||
| class TransactionController extends Controller | ||||
| { | ||||
|     private JournalRepositoryInterface $repository; | ||||
| 
 | ||||
|     /** | ||||
|      * AccountController constructor. | ||||
|      */ | ||||
|     public function __construct() | ||||
|     { | ||||
|         parent::__construct(); | ||||
|         $this->middleware( | ||||
|             function ($request, $next) { | ||||
|                 $this->repository = app(JournalRepositoryInterface::class); | ||||
| 
 | ||||
|                 $userGroup = $this->validateUserGroup($request); | ||||
|                 if (null !== $userGroup) { | ||||
|                     $this->repository->setUserGroup($userGroup); | ||||
|                 } | ||||
| 
 | ||||
|                 return $next($request); | ||||
|             } | ||||
|         ); | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      *  Documentation for this endpoint: | ||||
|      *  TODO list of checks | ||||
|      *  1. use dates from ParameterBag | ||||
|      *  2. Request validates dates | ||||
|      *  3. Request includes user_group_id | ||||
|      *  4. Endpoint is documented. | ||||
|      *  5. Collector uses user_group_id | ||||
|      * | ||||
|      * | ||||
|      * @return JsonResponse | ||||
|      */ | ||||
|     public function transactionDescriptions(AutocompleteRequest $request): JsonResponse | ||||
|     { | ||||
|         $data   = $request->getData(); | ||||
|         $result = $this->repository->searchJournalDescriptions($data['query'], $data['limit']); | ||||
| 
 | ||||
|         // limit and unique
 | ||||
|         $filtered = $result->unique('description'); | ||||
|         $array    = []; | ||||
| 
 | ||||
|         /** @var TransactionJournal $journal */ | ||||
|         foreach ($filtered as $journal) { | ||||
|             $array[] = [ | ||||
|                 'id'                   => (string)$journal->id, | ||||
|                 'transaction_group_id' => (string)$journal->transaction_group_id, | ||||
|                 'name'                 => $journal->description, | ||||
|                 'description'          => $journal->description, | ||||
|             ]; | ||||
|         } | ||||
| 
 | ||||
|         return response()->json($array); | ||||
| 
 | ||||
|     } | ||||
| 
 | ||||
| } | ||||
| @@ -27,6 +27,7 @@ namespace FireflyIII\Api\V2\Controllers; | ||||
| use Carbon\Carbon; | ||||
| use Carbon\Exceptions\InvalidDateException; | ||||
| use Carbon\Exceptions\InvalidFormatException; | ||||
| use FireflyIII\Support\Http\Api\ValidatesUserGroupTrait; | ||||
| use FireflyIII\Transformers\V2\AbstractTransformer; | ||||
| use Illuminate\Database\Eloquent\Model; | ||||
| use Illuminate\Pagination\LengthAwarePaginator; | ||||
| @@ -48,6 +49,8 @@ use Symfony\Component\HttpFoundation\ParameterBag; | ||||
|  */ | ||||
| class Controller extends BaseController | ||||
| { | ||||
|     use ValidatesUserGroupTrait; | ||||
| 
 | ||||
|     protected const CONTENT_TYPE = 'application/vnd.api+json'; | ||||
|     protected ParameterBag $parameters; | ||||
| 
 | ||||
|   | ||||
| @@ -28,6 +28,7 @@ use FireflyIII\Models\AccountType; | ||||
| use FireflyIII\Support\Request\ChecksLogin; | ||||
| use FireflyIII\Support\Request\ConvertsDataTypes; | ||||
| use FireflyIII\User; | ||||
| use FireflyIII\Validation\Administration\ValidatesAdministrationAccess; | ||||
| use Illuminate\Foundation\Http\FormRequest; | ||||
| use Illuminate\Validation\Validator; | ||||
| 
 | ||||
| @@ -38,6 +39,7 @@ class AutocompleteRequest extends FormRequest | ||||
| { | ||||
|     use ConvertsDataTypes; | ||||
|     use ChecksLogin; | ||||
| 
 | ||||
|     protected array $acceptedRoles = [UserRoleEnum::MANAGE_TRANSACTIONS]; | ||||
| 
 | ||||
|     /** | ||||
| @@ -75,21 +77,4 @@ class AutocompleteRequest extends FormRequest | ||||
|             'limit' => 'min:0|max:1337', | ||||
|         ]; | ||||
|     } | ||||
| 
 | ||||
|     /** | ||||
|      * Configure the validator instance with special rules for after the basic validation rules. | ||||
|      * | ||||
|      * @param Validator $validator | ||||
|      * | ||||
|      * @return void | ||||
|      */ | ||||
|     public function withValidator(Validator $validator): void | ||||
|     { | ||||
|         $validator->after( | ||||
|             function (Validator $validator) { | ||||
|                 // validate if the account can access this administration
 | ||||
|                 $this->validateAdministration($validator, [UserRoleEnum::MANAGE_TRANSACTIONS]); | ||||
|             } | ||||
|         ); | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -31,6 +31,8 @@ use FireflyIII\Repositories\Journal\JournalCLIRepository; | ||||
| use FireflyIII\Repositories\Journal\JournalCLIRepositoryInterface; | ||||
| use FireflyIII\Repositories\Journal\JournalRepository; | ||||
| use FireflyIII\Repositories\Journal\JournalRepositoryInterface; | ||||
| use FireflyIII\Repositories\UserGroups\Journal\JournalRepository as GroupJournalRepository; | ||||
| use FireflyIII\Repositories\UserGroups\Journal\JournalRepositoryInterface as GroupJournalRepositoryInterface; | ||||
| use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepository; | ||||
| use FireflyIII\Repositories\TransactionGroup\TransactionGroupRepositoryInterface; | ||||
| use Illuminate\Foundation\Application; | ||||
| @@ -44,9 +46,7 @@ class JournalServiceProvider extends ServiceProvider | ||||
|     /** | ||||
|      * Bootstrap the application services. | ||||
|      */ | ||||
|     public function boot(): void | ||||
|     { | ||||
|     } | ||||
|     public function boot(): void {} | ||||
| 
 | ||||
|     /** | ||||
|      * Register the application services. | ||||
| @@ -76,6 +76,19 @@ class JournalServiceProvider extends ServiceProvider | ||||
|             } | ||||
|         ); | ||||
| 
 | ||||
|         $this->app->bind( | ||||
|             GroupJournalRepositoryInterface::class, | ||||
|             static function (Application $app) { | ||||
|                 /** @var GroupJournalRepositoryInterface $repository */ | ||||
|                 $repository = app(GroupJournalRepository::class); | ||||
|                 if ($app->auth->check()) { // @phpstan-ignore-line (phpstan does not understand the reference to auth)
 | ||||
|                     $repository->setUser(auth()->user()); | ||||
|                 } | ||||
| 
 | ||||
|                 return $repository; | ||||
|             } | ||||
|         ); | ||||
| 
 | ||||
|         // also bind new API repository
 | ||||
|         $this->app->bind( | ||||
|             JournalAPIRepositoryInterface::class, | ||||
|   | ||||
| @@ -245,7 +245,7 @@ class JournalRepository implements JournalRepositoryInterface | ||||
|     { | ||||
|         $query = $this->user->transactionJournals() | ||||
|                             ->orderBy('date', 'DESC'); | ||||
|         if ('' !== $query) { | ||||
|         if ('' !== $search) { | ||||
|             $query->where('description', 'LIKE', sprintf('%%%s%%', $search)); | ||||
|         } | ||||
| 
 | ||||
|   | ||||
							
								
								
									
										49
									
								
								app/Repositories/UserGroups/Journal/JournalRepository.php
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								app/Repositories/UserGroups/Journal/JournalRepository.php
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,49 @@ | ||||
| <?php | ||||
| /* | ||||
|  * JournalRepository.php | ||||
|  * Copyright (c) 2023 james@firefly-iii.org | ||||
|  * | ||||
|  * This file is part of Firefly III (https://github.com/firefly-iii). | ||||
|  * | ||||
|  * This program is free software: you can redistribute it and/or modify | ||||
|  * it under the terms of the GNU Affero General Public License as | ||||
|  * published by the Free Software Foundation, either version 3 of the | ||||
|  * License, or (at your option) any later version. | ||||
|  * | ||||
|  * This program is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||
|  * GNU Affero General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU Affero General Public License | ||||
|  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | ||||
|  */ | ||||
| 
 | ||||
| declare(strict_types=1); | ||||
| 
 | ||||
| namespace FireflyIII\Repositories\UserGroups\Journal; | ||||
| 
 | ||||
| use FireflyIII\Support\Repositories\UserGroup\UserGroupTrait; | ||||
| use Illuminate\Support\Collection; | ||||
| 
 | ||||
| /** | ||||
|  * Class JournalRepository | ||||
|  */ | ||||
| class JournalRepository implements JournalRepositoryInterface | ||||
| { | ||||
|     use UserGroupTrait; | ||||
| 
 | ||||
|     /** | ||||
|      * @inheritDoc | ||||
|      */ | ||||
|     public function searchJournalDescriptions(string $search, int $limit): Collection | ||||
|     { | ||||
|         $query = $this->userGroup->transactionJournals() | ||||
|                                  ->orderBy('date', 'DESC'); | ||||
|         if ('' !== $search) { | ||||
|             $query->where('description', 'LIKE', sprintf('%%%s%%', $search)); | ||||
|         } | ||||
| 
 | ||||
|         return $query->take($limit)->get(); | ||||
|     } | ||||
| } | ||||
| @@ -0,0 +1,42 @@ | ||||
| <?php | ||||
| /* | ||||
|  * JournalRepositoryInterface.php | ||||
|  * Copyright (c) 2023 james@firefly-iii.org | ||||
|  * | ||||
|  * This file is part of Firefly III (https://github.com/firefly-iii). | ||||
|  * | ||||
|  * This program is free software: you can redistribute it and/or modify | ||||
|  * it under the terms of the GNU Affero General Public License as | ||||
|  * published by the Free Software Foundation, either version 3 of the | ||||
|  * License, or (at your option) any later version. | ||||
|  * | ||||
|  * This program is distributed in the hope that it will be useful, | ||||
|  * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
|  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||
|  * GNU Affero General Public License for more details. | ||||
|  * | ||||
|  * You should have received a copy of the GNU Affero General Public License | ||||
|  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | ||||
|  */ | ||||
| 
 | ||||
| declare(strict_types=1); | ||||
| 
 | ||||
| namespace FireflyIII\Repositories\UserGroups\Journal; | ||||
| 
 | ||||
| use Illuminate\Support\Collection; | ||||
| 
 | ||||
| /** | ||||
|  * Interface JournalRepositoryInterface | ||||
|  */ | ||||
| interface JournalRepositoryInterface | ||||
| { | ||||
|     /** | ||||
|      * Search in journal descriptions. | ||||
|      * | ||||
|      * @param string $search | ||||
|      * @param int    $limit | ||||
|      * | ||||
|      * @return Collection | ||||
|      */ | ||||
|     public function searchJournalDescriptions(string $search, int $limit): Collection; | ||||
| } | ||||
| @@ -29,6 +29,9 @@ use FireflyIII\Models\UserGroup; | ||||
| use FireflyIII\User; | ||||
| use Illuminate\Http\Request; | ||||
| 
 | ||||
| /** | ||||
|  * Trait ValidatesUserGroupTrait | ||||
|  */ | ||||
| trait ValidatesUserGroupTrait | ||||
| { | ||||
|     /** | ||||
|   | ||||
		Reference in New Issue
	
	Block a user