Class: PushNotification

Inherits:
Object
  • Object
show all
Includes:
Netcore::Payload
Defined in:
app/my_lib/push_notification.rb

Instance Method Summary collapse

Methods included from Netcore::Payload

#fav_payload, #reservation_data_payload, #reward_data_payload, #user_loyalty_payload, #user_properties, #user_properties_for_client

Methods included from CountryIdMemoization

#malaysia_id, #singapore_id, #thailand_id

Constructor Details

#initialize(type) ⇒ PushNotification

Returns a new instance of PushNotification.



10
11
12
13
# File 'app/my_lib/push_notification.rb', line 10

def initialize(type)
  @firebase_for = type
  configure_fcmpush
end

Instance Method Details

#publish(**args) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'app/my_lib/push_notification.rb', line 15

def publish(**args)
  payload = create_payload(**args)
  Retriable.retriable(on: [Fcmpush::BadGateway,
                           Fcmpush::NotFound,
                           SocketError,
                           Curl::Err::HostResolutionError,
                           Fcmpush::InternalServerError],
                      tries: 10,
                      base_interval: 5,
                      on_retry: APMErrorHandler.report_retriable_event('notification.push_notification')) do
    client = for_user ? user_firebase : staff_firebase
    if client.respond_to?(:push)
      client.push(payload)
    else
      # normally each app only has one firebase client.
      # #user_firebase and #staff_firebase is a hack to support multiple firebase clients
      # so sometimes this error will be raised and we need to fix it
      APMErrorHandler.report('failed to create Firebase client', payload: payload,
                             for_user: for_user, for_staff: for_staff,
                             staff_profile: Figaro.env.FIREBASE_PROJECT_ID_PARTNER!,
                             user_profile: Figaro.env.FIREBASE_PROJECT_ID_USER!,
                             fcmpush: Fcmpush.configuration.inspect)
    end
  rescue Fcmpush::Forbidden, Fcmpush::BadRequest, Fcmpush::NotFound, Fcmpush::Unauthorized
    PushNotificationAndPush.new(firebase_for).publish(**args)
  end
end

#publish_event(event_name, options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/my_lib/push_notification.rb', line 59

def publish_event(event_name, options = {})
  case event_name.to_sym
  when :reservation_created
    push_reservation_created(options)
  when :reservation_cancelled
    push_reservation_cancelled(options)
  when :reservation_modified
    push_reservation_modified(options)
  when :redemption_completed
    push_redemption_completed(options)
  when :cooking_reminder
    push_cooking_reminder(options)
  when :points_expiry_reminder
    push_points_expiry_reminder(options)
  else
    raise NotImplementedError
  end
end

#publish_promotion(promotion_id) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'app/my_lib/push_notification.rb', line 43

def publish_promotion(promotion_id)
  promotion = Promotion.find(promotion_id)

  MyLocaleManager.available_locales.each do |locale|
    topic = "/topics/promotions-#{locale}"
    publish(
      to: topic,
      title: promotion.send("title_#{locale}".to_sym),
      body: promotion.send("content_#{locale}".to_sym),
      deep_link: "app://promotions/#{promotion_id}",
      id: promotion_id,
      type: 'promotions',
    )
  end
end

#push_cooking_reminder(options) ⇒ Object

Parameters:

  • options (Hash)
    • object_id [Integer] reservation id



141
142
143
144
145
146
147
148
149
# File 'app/my_lib/push_notification.rb', line 141

def push_cooking_reminder(options)
  reservation = fetch_reservation(options)
  deep_link = "app://reservations/#{reservation.id}"
  type = 'reservations'

  if for_staff
    publish_to_staff(:cooking_reminder, deep_link, reservation, type)
  end
end

#push_points_expiry_reminder(options) ⇒ Object

Parameters:

  • options (Hash)
    • object_id [Integer] user id



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'app/my_lib/push_notification.rb', line 153

def push_points_expiry_reminder(options)
  user = User.fetch(options[:object_id])

  user_loyalty = user.user_loyalty
  tier_name = user_loyalty.loyalty_level&.name.to_s.downcase
  total_points = user.get_credits
  points_expiry_date = Reward.points_expiry_date(user)

  # skip if no expiry date
  return if points_expiry_date.blank?

  points_expiry_format = points_expiry_date.strftime("#{points_expiry_date.day.ordinalize} %b")

  message = if total_points > 0
              if tier_name == 'red'
                "Your #{total_points} points will expire on #{points_expiry_format}"
              else
                "Your #{total_points} points and #{tier_name.titleize} tier will expire on #{points_expiry_format}"
              end
            elsif total_points == 0
              "Your #{tier_name.titleize} tier will expire on #{points_expiry_format}"
            end

  c = content_for(:points_expiry_reminder, message)
  tokens_by_user_id(user.id).each do |data|
    begin
      publish(
        to: data[:token],
        title: c[:title],
        body: c[:body],
        deep_link: 'app://user/rewards',
        id: user_loyalty.id,
        type: 'user_loyalties',
      )
    rescue Fcmpush::Forbidden, Fcmpush::BadRequest, Fcmpush::NotFound, Fcmpush::Unauthorized
      UserDevice.find(data[:id]).update(enable: false)
    end
  end
  create_message(user, user_loyalty, c[:title], c[:body])
end

#push_redemption_completed(options) ⇒ Object

Parameters:

  • options (Hash)
    • object_id [Integer] user id



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/my_lib/push_notification.rb', line 122

def push_redemption_completed(options)
  user = User.fetch(options[:object_id])
  reward = user.rewards.cancellable_rewards.last
  return if reward.blank?

  points = reward.redeemed_amount
  c = content_for(:redemption_completed, points)
  tokens_by_user_id(user.id).each do |data|
    begin
      publish(to: data[:token], title: c[:title], body: c[:body], deep_link: 'app://user/rewards')
    rescue Fcmpush::Forbidden, Fcmpush::BadRequest, Fcmpush::NotFound, Fcmpush::Unauthorized
      UserDevice.find(data[:id]).update(enable: false)
    end
  end
  create_message(user, reward, c[:title], c[:body])
end

#push_reservation_cancelled(options) ⇒ Object

Parameters:

  • options (Hash)
    • object_id [Integer] reservation id



94
95
96
97
98
99
100
101
102
103
104
# File 'app/my_lib/push_notification.rb', line 94

def push_reservation_cancelled(options)
  reservation = fetch_reservation(options)
  deep_link = "app://reservations/#{reservation.id}"
  type = 'reservations'

  if for_user
    publish_to_user(:reservation_cancelled, deep_link, reservation, type)
  elsif for_staff
    publish_to_staff(:reservation_cancelled, deep_link, reservation, type)
  end
end

#push_reservation_created(options) ⇒ Object

Parameters:

  • options (Hash)
    • object_id [Integer] reservation id



80
81
82
83
84
85
86
87
88
89
90
# File 'app/my_lib/push_notification.rb', line 80

def push_reservation_created(options)
  reservation = fetch_reservation(options)
  deep_link = "app://reservations/#{reservation.id}"
  type = 'reservations'

  if for_user
    publish_to_user(:reservation_created, deep_link, reservation, type)
  elsif for_staff
    publish_to_staff(:reservation_created, deep_link, reservation, type)
  end
end

#push_reservation_modified(options) ⇒ Object

Parameters:

  • options (Hash)
    • object_id [Integer] reservation id



108
109
110
111
112
113
114
115
116
117
118
# File 'app/my_lib/push_notification.rb', line 108

def push_reservation_modified(options)
  reservation = fetch_reservation(options)
  deep_link = "app://reservations/#{reservation.id}"
  type = 'reservations'

  if for_user
    publish_to_user(:reservation_modified, deep_link, reservation, type)
  elsif for_staff
    publish_to_staff(:reservation_modified, deep_link, reservation, type)
  end
end