| 
									
										
										
										
											2021-02-03 14:57:37 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2024-11-25 04:18:55 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-03 14:57:37 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * AccountController.php | 
					
						
							|  |  |  |  * Copyright (c) 2019 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\V1\Controllers\Models\Account; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use FireflyIII\Api\V1\Controllers\Controller; | 
					
						
							| 
									
										
										
										
											2021-03-06 07:20:49 +01:00
										 |  |  | use FireflyIII\Api\V1\Requests\Models\Account\UpdateRequest; | 
					
						
							| 
									
										
										
										
											2021-02-03 14:57:37 +01:00
										 |  |  | use FireflyIII\Models\Account; | 
					
						
							|  |  |  | use FireflyIII\Repositories\Account\AccountRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2025-02-15 16:51:13 +01:00
										 |  |  | use FireflyIII\Support\JsonApi\Enrichments\AccountEnrichment; | 
					
						
							| 
									
										
										
										
											2021-02-03 14:57:37 +01:00
										 |  |  | use FireflyIII\Transformers\AccountTransformer; | 
					
						
							| 
									
										
										
										
											2025-02-15 16:51:13 +01:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2021-02-03 14:57:37 +01:00
										 |  |  | use Illuminate\Http\JsonResponse; | 
					
						
							| 
									
										
										
										
											2025-06-08 15:20:22 +02:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2023-05-29 13:56:55 +02:00
										 |  |  | use League\Fractal\Resource\Item; | 
					
						
							| 
									
										
										
										
											2021-02-03 14:57:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class UpdateController | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class UpdateController extends Controller | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-12-02 12:56:48 +01:00
										 |  |  |     public const string RESOURCE_KEY = 'accounts'; | 
					
						
							| 
									
										
										
										
											2021-02-03 14:57:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     private AccountRepositoryInterface $repository; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * AccountController constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         parent::__construct(); | 
					
						
							|  |  |  |         $this->middleware( | 
					
						
							|  |  |  |             function ($request, $next) { | 
					
						
							|  |  |  |                 $this->repository = app(AccountRepositoryInterface::class); | 
					
						
							|  |  |  |                 $this->repository->setUser(auth()->user()); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $next($request); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-09-18 10:07:45 +02:00
										 |  |  |      * This endpoint is documented at: | 
					
						
							| 
									
										
										
										
											2023-02-12 06:53:36 +01:00
										 |  |  |      * https://api-docs.firefly-iii.org/?urls.primaryName=2.0.0%20(v1)#/accounts/updateAccount
 | 
					
						
							| 
									
										
										
										
											2021-09-18 10:07:45 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-02-03 14:57:37 +01:00
										 |  |  |      * Update account. | 
					
						
							|  |  |  |      */ | 
					
						
							| 
									
										
										
										
											2021-03-06 07:20:49 +01:00
										 |  |  |     public function update(UpdateRequest $request, Account $account): JsonResponse | 
					
						
							| 
									
										
										
										
											2021-02-03 14:57:37 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2025-06-08 15:20:22 +02:00
										 |  |  |         Log::debug(sprintf('Now in %s', __METHOD__)); | 
					
						
							| 
									
										
										
										
											2022-03-29 14:56:27 +02:00
										 |  |  |         $data         = $request->getUpdateData(); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |         $data['type'] = config('firefly.shortNamesByFullName.'.$account->accountType->type); | 
					
						
							| 
									
										
										
										
											2022-03-29 14:56:27 +02:00
										 |  |  |         $account      = $this->repository->update($account, $data); | 
					
						
							|  |  |  |         $manager      = $this->getManager(); | 
					
						
							| 
									
										
										
										
											2021-03-08 17:56:21 +01:00
										 |  |  |         $account->refresh(); | 
					
						
							| 
									
										
										
										
											2023-10-29 05:54:01 +01:00
										 |  |  |         app('preferences')->mark(); | 
					
						
							| 
									
										
										
										
											2021-02-03 14:57:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-02-15 16:51:13 +01:00
										 |  |  |         // enrich
 | 
					
						
							|  |  |  |         /** @var User $admin */ | 
					
						
							| 
									
										
										
										
											2025-02-17 04:11:50 +01:00
										 |  |  |         $admin        = auth()->user(); | 
					
						
							|  |  |  |         $enrichment   = new AccountEnrichment(); | 
					
						
							| 
									
										
										
										
											2025-08-03 10:22:12 +02:00
										 |  |  |         $enrichment->setDate(null); | 
					
						
							| 
									
										
										
										
											2025-02-15 16:51:13 +01:00
										 |  |  |         $enrichment->setUser($admin); | 
					
						
							| 
									
										
										
										
											2025-02-17 04:11:50 +01:00
										 |  |  |         $account      = $enrichment->enrichSingle($account); | 
					
						
							| 
									
										
										
										
											2025-02-15 16:51:13 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-03 14:57:37 +01:00
										 |  |  |         /** @var AccountTransformer $transformer */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |         $transformer  = app(AccountTransformer::class); | 
					
						
							| 
									
										
										
										
											2021-02-03 14:57:37 +01:00
										 |  |  |         $transformer->setParameters($this->parameters); | 
					
						
							| 
									
										
										
										
											2024-01-01 14:41:31 +01:00
										 |  |  |         $resource     = new Item($account, $transformer, self::RESOURCE_KEY); | 
					
						
							| 
									
										
										
										
											2021-02-03 14:57:37 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return response()->json($manager->createData($resource)->toArray())->header('Content-Type', self::CONTENT_TYPE); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |