Class: ExportHhPointsExpiryService

Inherits:
Object
  • Object
show all
Defined in:
app/services/export_hh_points_expiry_service.rb

Overview

typed: ignore frozen_string_literal: true

Constant Summary collapse

FILENAME_FORMAT =
"HH-Account-Points-Expiry-#{Time.current_time.to_i}.xlsx"
COLUMNS =
%w[user_id email start_date expiry_date total_points current_loyalty_level].freeze

Class Method Summary collapse

Class Method Details

.add_worksheet(report) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'app/services/export_hh_points_expiry_service.rb', line 23

def self.add_worksheet(report)
  report.workbook.add_worksheet(name: 'HH Account Points Expiry') do |sheet|
    sheet.add_row(COLUMNS)

    users_query.find_each do |user|
      sheet.add_row(generate_row_data(user))
    end
  end
end

.downloadObject



8
9
10
11
12
13
14
15
16
# File 'app/services/export_hh_points_expiry_service.rb', line 8

def self.download
  filepath = generate_filepath

  report = Axlsx::Package.new
  add_worksheet(report)

  report.use_shared_strings = true
  report.serialize(filepath)
end

.generate_filepathObject



18
19
20
21
# File 'app/services/export_hh_points_expiry_service.rb', line 18

def self.generate_filepath
  filename = format(FILENAME_FORMAT, timestamp: Time.current.to_i)
  Rails.root.join('tmp', filename).to_s
end

.generate_row_data(user) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/services/export_hh_points_expiry_service.rb', line 37

def self.generate_row_data(user)
  points_total = Reward.count_credits(user.id)
  last_reservation = self.last_booking(user)
  start_date = last_reservation[:start_date] if last_reservation.present?
  points_expiry_date = self.points_expiry_date(user)

  [
    user.id,
    user.email,
    start_date.to_s,
    points_expiry_date.to_s,
    points_total,
    user.user_loyalty&.loyalty_level&.name.to_s,
  ]
end

.last_booking(user) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/services/export_hh_points_expiry_service.rb', line 76

def self.last_booking(user)
  user_reservations = user.reservations.reached_goal_scope
  last_reservation_no_payment = user_reservations.past&.last
  last_reservation_with_payment = user_reservations.joins(:charges)&.last

  last_reservation = if last_reservation_no_payment.present? && last_reservation_with_payment.present?
                       if last_reservation_no_payment.date < last_reservation_with_payment.date
                         last_reservation_with_payment
                       else
                         last_reservation_no_payment
                       end
                     else
                       last_reservation_no_payment || last_reservation_with_payment
                     end

  return nil if last_reservation.blank?

  start_date = if last_reservation.payment_type_provider.present?
                 (last_reservation.reservation_time + 24.hours).past? ? last_reservation.date : last_reservation.created_at.to_date
               else
                 last_reservation.date
               end

  {
    start_date: start_date,
    time_zone: last_reservation.restaurant&.time_zone.presence || 'Asia/Bangkok',
  }
end

.points_expiry_date(user) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/services/export_hh_points_expiry_service.rb', line 53

def self.points_expiry_date(user)
  last_reservation = self.last_booking(user)

  return nil if last_reservation.blank?

  time_zone = last_reservation[:time_zone]
  start_date = last_reservation[:start_date]

  # Set the minimum expiry date
  # Since this rules will be announced at 1st september 2024,
  # all of the expiration dates in august will be extended until 30th september 2024
  # https://app.clickup.com/9003122396/v/dc/8ca1fpw-7922/8ca1fpw-40216
  min_expiry_date = Date.new(2024, 10, 31)

  return user.get_credits.positive? ? min_expiry_date : nil if start_date.blank?

  # Calculate expiry date as end of the month 4 months after the last booking date
  expiry_date = (start_date + 4.months).in_time_zone(time_zone).end_of_month

  # Return the greater of the two expiry dates
  expiry_date < min_expiry_date ? min_expiry_date.to_date : expiry_date.to_date
end

.users_queryObject



33
34
35
# File 'app/services/export_hh_points_expiry_service.rb', line 33

def self.users_query
  User.where('email LIKE ?', '%@hungryhub.com%')
end