Class: Bscf::Core::Voucher
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Bscf::Core::Voucher
- Defined in:
- app/models/bscf/core/voucher.rb
Instance Method Summary collapse
- #can_cancel? ⇒ Boolean
- #can_return? ⇒ Boolean
- #cancel! ⇒ Object
- #redeem!(to_account) ⇒ Object
- #return! ⇒ Object
Instance Method Details
#can_cancel? ⇒ Boolean
83 84 85 |
# File 'app/models/bscf/core/voucher.rb', line 83 def can_cancel? (pending? || active?) && !expired? && !returned? && !redeemed? end |
#can_return? ⇒ Boolean
79 80 81 |
# File 'app/models/bscf/core/voucher.rb', line 79 def can_return? !redeemed? && !returned? && !cancelled? && !expired? end |
#cancel! ⇒ Object
71 72 73 74 75 76 77 |
# File 'app/models/bscf/core/voucher.rb', line 71 def cancel! return false unless can_cancel? update!(status: :cancelled, returned_at: Time.current) true rescue ActiveRecord::RecordInvalid false end |
#redeem!(to_account) ⇒ Object
26 27 28 29 30 31 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 |
# File 'app/models/bscf/core/voucher.rb', line 26 def redeem!(to_account) return false unless active? return false if expired? || returned? || cancelled? from_account = issued_by.virtual_account success = false ActiveRecord::Base.transaction do transaction = VirtualAccountTransaction.new( from_account: from_account, to_account: to_account, amount: amount, transaction_type: :transfer ) unless transaction.process! errors.add(:base, "Voucher redemption failed due to transaction processing error.") raise ActiveRecord::Rollback end unless from_account.unlock_amount!(amount) errors.add(:base, "Failed to unlock amount from issuer after successful transfer.") raise ActiveRecord::Rollback end update!(status: :redeemed, redeemed_at: Time.current) success = true end success rescue ActiveRecord::RecordInvalid => e errors.add(:base, "Voucher redemption failed: #{e.}") false rescue ActiveRecord::Rollback false end |
#return! ⇒ Object
63 64 65 66 67 68 69 |
# File 'app/models/bscf/core/voucher.rb', line 63 def return! return false unless can_return? update!(status: :returned, returned_at: Time.current) true rescue ActiveRecord::RecordInvalid false end |