Module: LoyaltyProgram::Benefits::DineInPoints

Includes:
SpecialBenefits::Campaign
Included in:
Modules::Reservations::RewardPoints, Modules::TicketTransactions::RewardPoints
Defined in:
app/my_lib/loyalty_program/benefits/dine_in_points.rb

Instance Method Summary collapse

Methods included from SpecialBenefits::Campaign

#eligible_to_double_point?, #eligible_to_triple_point?

Instance Method Details

#calc_with_loyalty_level(points, transaction) ⇒ Integer

Calculates the number of dine-in points a user will receive based on their loyalty level and the transaction they are making.

Examples:

Calculate dine-in points for a user with a reservation

calc_with_loyalty_level(100, reservation)

Calculate dine-in points for a user with a voucher transaction

calc_with_loyalty_level(50, ticket_transaction)

Parameters:

  • points (Integer)

    The number of points to calculate.

  • transaction (Reservation, TicketTransaction)

    The transaction for which to calculate the points.

Returns:

  • (Integer)

    The final calculated points value.

Raises:

  • (NoMethodError)

    If a method or attribute is called that is not present in the Reservation or VoucherTransaction model.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/my_lib/loyalty_program/benefits/dine_in_points.rb', line 16

def calc_with_loyalty_level(points, transaction)
  user = transaction.user
  return points if user.blank?

  user_loyalty = user.user_loyalty
  return 0 if user_loyalty.blank?

  restaurant_country = transaction&.restaurant&.country
  dine_in_point = user_loyalty.dine_in_point_key_by_country(restaurant_country)
  benefit = user_loyalty.benefit_value(dine_in_point)
  points = (points.to_f * benefit).floor

  if eligible_to_triple_point?(transaction)
    points *= 3
  elsif eligible_to_double_point?(transaction)
    points *= 2
  end

  points
end