Class: Dscf::Banking::VouchersController

Inherits:
ApplicationController show all
Includes:
DemoPermissionBypass, Core::Common
Defined in:
app/controllers/dscf/banking/vouchers_controller.rb

Instance Method Summary collapse

Methods included from DemoPermissionBypass

#authorize_review_action!, #bypass_permissions_for_demo?, #pundit_user

Methods inherited from ApplicationController

#bypass_permissions_for_demo?, #pundit_user

Instance Method Details

#account_vouchersObject



90
91
92
93
94
95
96
97
# File 'app/controllers/dscf/banking/vouchers_controller.rb', line 90

def 
   = Dscf::Banking::Account.find(params[:account_id])
  vouchers = .vouchers.order(created_at: :desc)

  render_success(data: vouchers, serializer_options: { include: default_serializer_includes[:default] })
rescue ActiveRecord::RecordNotFound
  render_error(errors: "Account not found", status: :not_found)
end

#cancelObject



79
80
81
82
83
84
85
86
87
88
# File 'app/controllers/dscf/banking/vouchers_controller.rb', line 79

def cancel
  service = Dscf::Banking::VoucherService.new
  service.cancel_voucher(code: params[:code])

  if service.success?
    render_success(data: service.voucher, serializer_options: { include: default_serializer_includes[:default] })
  else
    render_error(errors: service.errors, status: :unprocessable_entity)
  end
end

#createObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/dscf/banking/vouchers_controller.rb', line 8

def create
   = Dscf::Banking::Account.find_by(account_number: model_params[:parent_account_number])

  unless 
    return render_error(errors: "Parent account not found", status: :not_found)
  end

  service = Dscf::Banking::VoucherService.new
  service.generate_voucher(
    parent_account: ,
    amount: model_params[:amount],
    recipient_mobile: model_params[:recipient_mobile],
    recipient_name: model_params[:recipient_name],
    message: model_params[:message],
    expires_at: model_params[:expires_at]
  )

  if service.success?
    render_success(data: service.voucher, serializer_options: { include: default_serializer_includes[:default] }, status: :created)
  else
    render_error(errors: service.errors, status: :unprocessable_entity)
  end
end

#redeemObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/controllers/dscf/banking/vouchers_controller.rb', line 32

def redeem
  service = Dscf::Banking::VoucherService.new
   = nil

  if redeem_params[:destination_account_number].present?
     = Dscf::Banking::Account.find_by(account_number: redeem_params[:destination_account_number])
    unless 
      return render_error(errors: "Destination account not found", status: :not_found)
    end
  end

  service.redeem_voucher(
    code: redeem_params[:code] || params[:code],
    amount: redeem_params[:amount],
    recipient_mobile: redeem_params[:recipient_mobile],
    destination_account: 
  )

  if service.success?
    render_success(
      data: {
        voucher: service.voucher,
        redemption: service.redemption
      },
      serializer_options: {
        voucher: { include: default_serializer_includes[:default] },
        redemption: { include: [ :destination_account ] }
      }
    )
  else
    render_error(errors: service.errors, status: :unprocessable_entity)
  end
end

#showObject



66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/controllers/dscf/banking/vouchers_controller.rb', line 66

def show
  code = params[:code].presence || params[:id].presence
  voucher = Dscf::Banking::Voucher.find_by(code: code.to_s.strip.upcase)
  return render_error(errors: "Voucher not found", status: :not_found) unless voucher

  if voucher.active? && voucher.expired?
    Dscf::Banking::VoucherService.new.expire_voucher(voucher)
    voucher.reload
  end

  render_success(data: voucher, serializer_options: { include: default_serializer_includes[:default] })
end