2019-01-16 16:28:25 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# API Controller for resources of type Wallet
|
2016-07-18 18:16:54 +02:00
|
|
|
class API::WalletController < API::ApiController
|
|
|
|
before_action :authenticate_user!
|
|
|
|
|
|
|
|
def by_user
|
2019-06-03 16:00:09 +02:00
|
|
|
invoicing_profile = InvoicingProfile.find_by(user_id: params[:user_id])
|
|
|
|
@wallet = Wallet.find_by(invoicing_profile_id: invoicing_profile.id)
|
2016-07-07 15:57:06 +02:00
|
|
|
authorize @wallet
|
2016-07-18 18:16:54 +02:00
|
|
|
render :show
|
|
|
|
end
|
2016-07-05 13:20:25 +02:00
|
|
|
|
|
|
|
def transactions
|
|
|
|
@wallet = Wallet.find(params[:id])
|
|
|
|
authorize @wallet
|
2019-06-03 16:00:09 +02:00
|
|
|
@wallet_transactions = @wallet.wallet_transactions.includes(:invoice, :invoicing_profile).order(created_at: :desc)
|
2016-07-05 13:20:25 +02:00
|
|
|
end
|
2016-07-05 19:07:50 +02:00
|
|
|
|
|
|
|
def credit
|
2020-02-12 18:15:44 +01:00
|
|
|
return head 422 if Rails.application.secrets.fablab_without_wallet == 'true'
|
|
|
|
|
2016-12-12 14:20:26 +01:00
|
|
|
@wallet = Wallet.find(credit_params[:id])
|
2016-07-05 19:07:50 +02:00
|
|
|
authorize @wallet
|
|
|
|
service = WalletService.new(user: current_user, wallet: @wallet)
|
2016-12-12 14:20:26 +01:00
|
|
|
transaction = service.credit(credit_params[:amount].to_f)
|
|
|
|
if transaction
|
2019-01-16 16:28:25 +01:00
|
|
|
service.create_avoir(transaction, credit_params[:avoir_date], credit_params[:avoir_description]) if credit_params[:avoir]
|
2016-07-05 19:07:50 +02:00
|
|
|
render :show
|
|
|
|
else
|
|
|
|
head 422
|
|
|
|
end
|
|
|
|
end
|
2016-12-12 14:20:26 +01:00
|
|
|
|
|
|
|
private
|
2019-01-07 12:48:22 +01:00
|
|
|
|
2016-12-12 14:20:26 +01:00
|
|
|
def credit_params
|
|
|
|
params.permit(:id, :amount, :avoir, :avoir_date, :avoir_description)
|
|
|
|
end
|
2016-07-18 18:16:54 +02:00
|
|
|
end
|