Class: ReservationRefundGuarantee

Inherits:
ApplicationRecord show all
Extended by:
Enumerize
Defined in:
app/models/reservation_refund_guarantee.rb

Overview

Schema Information

Table name: reservation_refund_guarantees

id                      :bigint           not null, primary key
refund_currency         :string(191)      default("THB")
refund_fee_cents        :integer          default(0), not null
refundable_amount_cents :integer          default(0), not null
refundable_until        :datetime
status                  :string(191)
created_at              :datetime         not null
updated_at              :datetime         not null
reservation_id          :integer

Indexes

index_reservation_refund_guarantees_on_refundable_until  (refundable_until)
index_reservation_refund_guarantees_on_reservation_id    (reservation_id)
index_reservation_refund_guarantees_on_status            (status)

Foreign Keys

fk_rails_...  (reservation_id => reservations.id)

Constant Summary collapse

STATUS_PENDING =

Constants

:pending
STATUS_CLAIMED =
:claimed
STATUS_EXPIRED =
:expired
CANCEL_REASON =
'refund guarantee'.freeze

Instance Method Summary collapse

Methods inherited from ApplicationRecord

sync_carrierwave_url

Instance Method Details

#can_claim_refund?Boolean

Check if refund guarantee can be claimed Refund can be claimed if:

  1. Status is pending

  2. Current time is before refundable_until deadline

Note: refundable_until is stored in UTC, using Time.current.utc for explicit UTC comparison

Returns:

  • (Boolean)

    true if refund can be claimed, false otherwise



69
70
71
72
73
74
# File 'app/models/reservation_refund_guarantee.rb', line 69

def can_claim_refund?
  return false if status_as_symbol != STATUS_PENDING
  return false if refundable_until.blank?

  Time.current.utc < refundable_until
end

#formatted_refundable_untilString

Get formatted refundable_until in restaurant's local time zone

Examples:

“Monday, 5 July 2021 at 14:30”

Returns:

  • (String)

    formatted refundable_until



55
56
57
58
59
60
# File 'app/models/reservation_refund_guarantee.rb', line 55

def formatted_refundable_until
  restaurant_tz = reservation.restaurant.time_zone || HungryHub::Time::DEFAULT_ZONE
  local_time = refundable_until.in_time_zone(restaurant_tz)

  local_time.strftime('%A, %-d %B %Y at %H:%M')
end

#mark_as_claimed!Boolean

Mark refund guarantee as claimed This should be called when user successfully claims the refund

Returns:

  • (Boolean)

    true if status update successful



90
91
92
# File 'app/models/reservation_refund_guarantee.rb', line 90

def mark_as_claimed!
  update!(status: STATUS_CLAIMED)
end

#mark_as_expired!Boolean

Mark refund guarantee as expired This can be called by a background job to update expired refund guarantees

Returns:

  • (Boolean)

    true if status update successful



98
99
100
# File 'app/models/reservation_refund_guarantee.rb', line 98

def mark_as_expired!
  update!(status: STATUS_EXPIRED)
end

#refund_period_expired?Boolean

Check if refund period has expired

Note: refundable_until is stored in UTC, using Time.current.utc for explicit UTC comparison

Returns:

  • (Boolean)

    true if current time is past refundable_until deadline



80
81
82
83
84
# File 'app/models/reservation_refund_guarantee.rb', line 80

def refund_period_expired?
  return false if refundable_until.blank?

  Time.current.utc >= refundable_until
end

#status_as_symbolSymbol

Get refund guarantee status as symbol

Returns:

  • (Symbol)

    status as symbol



48
49
50
# File 'app/models/reservation_refund_guarantee.rb', line 48

def status_as_symbol
  status.to_sym
end