diff --git a/app/Api/V1/Controllers/PreferenceController.php b/app/Api/V1/Controllers/PreferenceController.php index 643c264a07..c3999d6f70 100644 --- a/app/Api/V1/Controllers/PreferenceController.php +++ b/app/Api/V1/Controllers/PreferenceController.php @@ -24,7 +24,9 @@ declare(strict_types=1); namespace FireflyIII\Api\V1\Controllers; use FireflyIII\Api\V1\Requests\PreferenceRequest; +use FireflyIII\Models\AccountType; use FireflyIII\Models\Preference; +use FireflyIII\Repositories\Account\AccountRepositoryInterface; use FireflyIII\Transformers\PreferenceTransformer; use FireflyIII\User; use Illuminate\Http\JsonResponse; @@ -41,6 +43,34 @@ use League\Fractal\Serializer\JsonApiSerializer; */ class PreferenceController extends Controller { + /** + * LinkTypeController constructor. + */ + public function __construct() + { + parent::__construct(); + $this->middleware( + function ($request, $next) { + /** @var User $user */ + $user = auth()->user(); + $repository = app(AccountRepositoryInterface::class); + $repository->setUser($user); + + // an important fallback is that the frontPageAccount array gets refilled automatically + // when it turns up empty. + $frontPageAccounts = app('preferences')->getForUser($user, 'frontPageAccounts', [])->data; + if (\count($frontPageAccounts) === 0) { + /** @var Collection $accounts */ + $accounts = $repository->getAccountsByType([AccountType::DEFAULT, AccountType::ASSET]); + $accountIds = $accounts->pluck('id')->toArray(); + app('preferences')->setForUser($user, 'frontPageAccounts', $accountIds); + } + + return $next($request); + } + ); + } + /** * List all of them. * @@ -51,12 +81,13 @@ class PreferenceController extends Controller public function index(Request $request): JsonResponse { /** @var User $user */ - $user = auth()->user(); - $available = [ + $user = auth()->user(); + $available = [ 'language', 'customFiscalYear', 'fiscalYearStart', 'currencyPreference', 'transaction_journal_optional_fields', 'frontPageAccounts', 'viewRange', 'listPageSize, twoFactorAuthEnabled', ]; + $preferences = new Collection; foreach ($available as $name) { $pref = app('preferences')->getForUser($user, $name); @@ -83,6 +114,32 @@ class PreferenceController extends Controller } + /** + * Return a single preference by name. + * + * @param Request $request + * @param Preference $preference + * + * @return JsonResponse + * @SuppressWarnings(PHPMD.CyclomaticComplexity) + */ + public function show(Request $request, Preference $preference): JsonResponse + { + // create some objects: + $manager = new Manager; + $baseUrl = $request->getSchemeAndHttpHost() . '/api/v1'; + + // present to user. + $manager->setSerializer(new JsonApiSerializer($baseUrl)); + /** @var PreferenceTransformer $transformer */ + $transformer = app(PreferenceTransformer::class); + $transformer->setParameters($this->parameters); + + $resource = new Item($preference, $transformer, 'preferences'); + + return response()->json($manager->createData($resource)->toArray())->header('Content-Type', 'application/vnd.api+json'); + } + /** * Update a preference. *