. */ declare(strict_types=1); namespace FireflyIII\Api\V1\Controllers\Models\TransactionCurrency; use FireflyIII\Api\V1\Controllers\Controller; use FireflyIII\Exceptions\FireflyException; use FireflyIII\Models\TransactionCurrency; use FireflyIII\Repositories\Currency\CurrencyRepositoryInterface; use FireflyIII\User; use Illuminate\Http\JsonResponse; /** * Class DestroyController */ class DestroyController extends Controller { private CurrencyRepositoryInterface $repository; /** * CurrencyRepository constructor. * * @codeCoverageIgnore */ public function __construct() { parent::__construct(); $this->middleware( function ($request, $next) { $this->repository = app(CurrencyRepositoryInterface::class); $this->repository->setUser(auth()->user()); return $next($request); } ); } /** * This endpoint is documented at: * https://api-docs.firefly-iii.org/#/currencies/deleteCurrency * * Remove the specified resource from storage. * * @param TransactionCurrency $currency * * @return JsonResponse * @throws FireflyException * @codeCoverageIgnore */ public function destroy(TransactionCurrency $currency): JsonResponse { /** @var User $admin */ $admin = auth()->user(); if (!$this->userRepository->hasRole($admin, 'owner')) { // access denied: throw new FireflyException('200005: You need the "owner" role to do this.'); } if ($this->repository->currencyInUse($currency)) { throw new FireflyException('200006: Currency in use.'); } if ($this->repository->isFallbackCurrency($currency)) { throw new FireflyException('200026: Currency is fallback.'); } $this->repository->destroy($currency); app('preferences')->mark(); return response()->json([], 204); } }