Class: GcalWorker

Inherits:
ApplicationWorker show all
Includes:
Workers::Helpers
Defined in:
app/workers/gcal_worker.rb

Overview

TODO delete this worker

Instance Method Summary collapse

Methods included from Workers::Helpers

#parse_json

Methods inherited from ApplicationWorker

unlimited_retry

Instance Method Details

#perform(calendar_id, method, id = nil) ⇒ Object

if method is :insert_event, then id parameter is reservation, else id = event_id



13
14
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'app/workers/gcal_worker.rb', line 13

def perform(calendar_id, method, id = nil)
  return unless Figaro.bool_env! 'SIDEKIQ_CREATE_GOOGLE_CALENDAR_EVENT'

  gcal = GoogleCalendar::Base.new
  case method.to_sym
  when :insert_event
    reservation = begin
                    Reservation.fetch id
                  rescue ActiveRecord::RecordNotFound
                    sleep 2
                    Reservation.find_by id: id
                  end

    return if reservation.blank?
    return if reservation.email.blank?
    return unless EmailValidator.valid?(reservation.email)

    event_parameters = gcal.build_add_event_payload(reservation, reservation.restaurant)
    event = GCalendar::Event.new event_parameters.to_h
    res = gcal.client.insert_event(calendar_id, event)
    reservation.update_column :gcal_id, res.id
  when :delete_event
    event_id = id
    gcal.client.delete_event(calendar_id, event_id) if event_id.present?
  when :update_event
    reservation = begin
                    Reservation.fetch id
                  rescue ActiveRecord::RecordNotFound
                    sleep 2
                    Reservation.find_by id: id
                  end
    return if reservation.blank?

    event_parameters = gcal.build_update_event_payload(reservation, reservation.restaurant)
    event = GCalendar::Event.new event_parameters.to_h
    res = gcal.client.update_event(calendar_id, reservation.gcal_id, event)
    reservation.update_column :gcal_id, res.id
  else
    raise 'Unsupported method'
  end
rescue Google::Apis::ClientError, StandardError => e
  return true if e.message == 'Invalid attendee email'
  return true if e.message.include?('Resource has been deleted')

  if e.message.include? 'email'
    # do nothing
    # it is an error because the email is not valid
    # example: ": katebbxxx@icloud.com"
    return
  else
    APMErrorHandler.report e
  end

  raise e
end