Class: NotificationService::Email

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

Overview

typed: ignore frozen_string_literal: true

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(receiver:, user: nil, reservation: nil, restaurant: nil, owner: nil) ⇒ Email

Returns a new instance of Email.

Parameters:

  • receiver, (Symbol)

    it should be a symbol, e.g. :user, :owner

  • user (User) (defaults to: nil)
  • reservation (Reservation) (defaults to: nil)
  • restaurant (Restaurant) (defaults to: nil)
  • owner (Owner) (defaults to: nil)


12
13
14
15
16
17
18
# File 'app/services/notification_service/email.rb', line 12

def initialize(receiver:, user: nil, reservation: nil, restaurant: nil, owner: nil)
  @receiver = receiver
  @user = user
  @reservation = reservation
  @restaurant = restaurant
  @owner = owner
end

Instance Attribute Details

#ownerObject

Returns the value of attribute owner.



5
6
7
# File 'app/services/notification_service/email.rb', line 5

def owner
  @owner
end

#receiverObject

Returns the value of attribute receiver.



5
6
7
# File 'app/services/notification_service/email.rb', line 5

def receiver
  @receiver
end

#reservationObject

Returns the value of attribute reservation.



5
6
7
# File 'app/services/notification_service/email.rb', line 5

def reservation
  @reservation
end

#restaurantObject

Returns the value of attribute restaurant.



5
6
7
# File 'app/services/notification_service/email.rb', line 5

def restaurant
  @restaurant
end

#userObject

Returns the value of attribute user.



5
6
7
# File 'app/services/notification_service/email.rb', line 5

def user
  @user
end

Instance Method Details

#points_expiration_reminderObject



117
118
119
# File 'app/services/notification_service/email.rb', line 117

def points_expiration_reminder
  UserMailer.notify_points_expiration(user).deliver_later!
end

#send_cancel_emailObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/services/notification_service/email.rb', line 44

def send_cancel_email
  return if reservation.dummy?

  user_id = user.present? ? user.id : nil
  case receiver
  when :owner
    OwnerMailer.booking_cancel(reservation.id).deliver_later!
  when :user
    UserMailer.booking_cancel(reservation.id, user_id).deliver_later!
  when :staff
    StaffMailer.booking_cancel(reservation.id).deliver_later!
  else
    APMErrorHandler.report('receiver is undefined', receiver: receiver.to_s)
  end
end

#send_confirmation_emailObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/services/notification_service/email.rb', line 28

def send_confirmation_email
  user_id = user.present? ? user.id : nil
  case receiver
  when :owner
    # there is a case where id is nil, so we use `deliver!` instead of `deliver_later!`
    # to track the error
    OwnerMailer.booking_confirmation(reservation.id).deliver!
  when :user
    UserMailer.booking_confirmation(reservation.id, user_id).deliver_later!
  when :staff
    StaffMailer.booking_confirm(reservation.id).deliver_later!
  else
    APMErrorHandler.report('receiver is undefined', receiver: receiver.to_s)
  end
end

#send_modification_emailObject



71
72
73
74
75
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
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/services/notification_service/email.rb', line 71

def send_modification_email
  # Add comprehensive tracking for booking modification email
  ElasticAPM.with_span('NotificationService::Email#send_modification_email', 'service') do
    # Validate reservation exists
    if reservation.nil?
      error_context = {
        receiver: receiver,
        user_id: user&.id,
        caller_stack: capture_stack_trace,
      }

      APMErrorHandler.report(
        'NotificationService::Email#send_modification_email called with nil reservation',
        context: error_context,
        handled: true,
      )
      return false
    end

    # Validate reservation has an ID
    if reservation.id.nil?
      error_context = {
        receiver: receiver,
        reservation_present: reservation.present?,
        reservation_class: reservation.class.name,
        reservation_attributes: reservation.attributes.slice('id', 'created_at', 'updated_at'),
        user_id: user&.id,
        caller_stack: capture_stack_trace,
      }

      APMErrorHandler.report(
        'NotificationService::Email#send_modification_email called with reservation without ID',
        context: error_context,
        handled: true,
      )

      return false
    end

    user_id = user.present? ? user.id : nil
    if receiver == :user
      UserMailer.booking_modification(reservation.id, user_id).deliver_later!
    end
  end
end

#send_pending_emailObject



60
61
62
63
64
65
66
67
68
69
# File 'app/services/notification_service/email.rb', line 60

def send_pending_email
  case receiver
  when :user
    UserMailer.booking_pending(reservation.id).deliver_later!
  when :staff
    StaffMailer.booking_pending(reservation.id).deliver_later!
  else
    APMErrorHandler.report('receiver is undefined', receiver: receiver.to_s)
  end
end