Class: TimeOffRequests::GoogleCalendar

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

Constant Summary collapse

CALENDAR_ID =

Use 'primary' or a specific calendar ID

'primary'
MAX_RETRIES =

Prevent infinite loops

1
TIMEZONE =

All CRM events are in Chicago timezone

'America/Chicago'

Instance Method Summary collapse

Constructor Details

#initialize(time_off_request) ⇒ GoogleCalendar

Returns a new instance of GoogleCalendar.



6
7
8
9
10
11
# File 'app/services/time_off_requests/google_calendar.rb', line 6

def initialize(time_off_request)
  @time_off_request = time_off_request
  @employee = time_off_request.employee
  @retry_count = 0
  authorize_client
end

Instance Method Details

#create_eventObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/services/time_off_requests/google_calendar.rb', line 13

def create_event
  return unless @time_off_request.approved? && @client

  event = Google::Apis::CalendarV3::Event.new(
    summary: "HW Time Off: #{@time_off_request.time_off_type.name}",
    description: "Approved time off for #{@employee.full_name}",
    start: { date: @time_off_request.start_date.to_s, time_zone: TIMEZONE },
    end: { date: calendar_end_date, time_zone: TIMEZONE },
    attendees: [{ email: @employee.email }],
    transparency: 'opaque'  # Mark as busy (not free)
  )

  begin
    created_event = @service.insert_event(CALENDAR_ID, event)
    @time_off_request.update_column(:google_event_id, created_event.id) # Save event ID
  rescue Google::Apis::AuthorizationError
    handle_authorization_error(:create_event)
  rescue Google::Apis::ClientError => e
    Rails.logger.error("Failed to create event: #{e.message}")
  end
end

#delete_eventObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'app/services/time_off_requests/google_calendar.rb', line 54

def delete_event
  return unless @time_off_request.google_event_id.present? && @client

  begin
    @service.delete_event(CALENDAR_ID, @time_off_request.google_event_id)
    @time_off_request.update_column(:google_event_id, nil) # Remove event reference
  rescue Google::Apis::AuthorizationError
    handle_authorization_error(:delete_event)
  rescue Google::Apis::ClientError => e
    Rails.logger.error("Failed to delete event: #{e.message}")
  end
end

#fetch_eventObject

Fetch the current event details from Google Calendar
Returns a hash with event details or nil if not found/authorized



69
70
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
# File 'app/services/time_off_requests/google_calendar.rb', line 69

def fetch_event
  return { error: 'No Google Calendar event ID' } unless @time_off_request.google_event_id.present?
  return { error: 'Google Calendar not authorized' } unless @client

  begin
    event = @service.get_event(CALENDAR_ID, @time_off_request.google_event_id)
    {
      id: event.id,
      summary: event.summary,
      description: event.description,
      start_date: event.start&.date,
      end_date: event.end&.date,
      status: event.status,
      transparency: event.transparency || 'opaque',
      html_link: event.html_link,
      updated: event.updated
    }
  rescue Google::Apis::AuthorizationError
    if refresh_authorization
      fetch_event
    else
      { error: 'Authorization failed - please reconnect your Google account' }
    end
  rescue Google::Apis::ClientError => e
    if e.message.include?('notFound')
      # Event was deleted from Google Calendar
      @time_off_request.update_column(:google_event_id, nil)
      { error: 'Event not found in Google Calendar - it may have been deleted' }
    else
      Rails.logger.error("Failed to fetch event: #{e.message}")
      { error: "Failed to fetch event: #{e.message}" }
    end
  end
end

#sync_eventObject

Sync (create or update) the event to Google Calendar
Returns a hash with the result



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/services/time_off_requests/google_calendar.rb', line 106

def sync_event
  return { error: 'Request must be approved to sync with Google Calendar' } unless @time_off_request.approved?
  return { error: 'Google Calendar not authorized - please connect your Google account' } unless @client

  if @time_off_request.google_event_id.present?
    # Try to update existing event
    begin
      update_event
      { success: true, message: 'Event updated in Google Calendar' }
    rescue Google::Apis::ClientError => e
      if e.message.include?('notFound')
        # Event was deleted, create a new one
        @time_off_request.update_column(:google_event_id, nil)
        create_event
        { success: true, message: 'Event recreated in Google Calendar (previous event was deleted)' }
      else
        { error: "Failed to update event: #{e.message}" }
      end
    end
  else
    # Create new event
    create_event
    if @time_off_request.google_event_id.present?
      { success: true, message: 'Event created in Google Calendar' }
    else
      { error: 'Failed to create event in Google Calendar' }
    end
  end
end

#update_eventObject



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

def update_event
  return unless @time_off_request.approved? && @time_off_request.google_event_id.present? && @client

  event = @service.get_event(CALENDAR_ID, @time_off_request.google_event_id)
  event.summary = "HW Time Off: #{@time_off_request.time_off_type.name}"
  event.description = "Updated time off for #{@employee.full_name}"
  event.start = Google::Apis::CalendarV3::EventDateTime.new(date: @time_off_request.start_date.to_s, time_zone: TIMEZONE)
  event.end = Google::Apis::CalendarV3::EventDateTime.new(date: calendar_end_date, time_zone: TIMEZONE)
  event.transparency = 'opaque'  # Mark as busy (not free)

  begin
    @service.update_event(CALENDAR_ID, @time_off_request.google_event_id, event)
  rescue Google::Apis::AuthorizationError
    handle_authorization_error(:update_event)
  rescue Google::Apis::ClientError => e
    Rails.logger.error("Failed to update event: #{e.message}")
  end
end