Last active
February 19, 2023 16:00
-
-
Save alagu/fe35ac0538a7a2438129a66632e52564 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
e = Employee.find(id) | |
ooo_cal_ids = OneOnOne.where('reportee_id = ? OR manager_id = ? OR creator_id = ?', e.id, e.id, e.id).where('start_time > ?',Time.now.beginning_of_day).where('start_time < ?', 40.days.from_now).where('cal_id is not null').order('start_time asc').map(&:cal_id) | |
calendar_event_cal_ids = CalendarEvent.where('start_time > ?',Time.now).where(manager: e).map(&:cal_event_id) | |
@service = Google::Apis::CalendarV3::CalendarService.new | |
@service.authorization = e.user.token.google_secret.to_authorization | |
token = e.user.google_token | |
new_access_token = @service.authorization.refresh! | |
token.access_token = new_access_token['access_token'] | |
token.expires_at = Time.now.to_i + new_access_token['expires_in'].to_i | |
token.save | |
# Fetch the events list | |
@events_list = @service.list_events('primary', time_min: Time.now.beginning_of_day.rfc3339, time_max: 2.months.from_now.beginning_of_day.rfc3339, single_events: true, max_results: 1000) | |
accepted_events = @events_list.items.select { |item| item.attendees && item.attendees.length.positive? && item.attendees.select { |x| x.self == true }.first.response_status != 'declined' } | |
live_cal_ids = accepted_events.map(&:id) | |
# Existing events | |
removed_ooo_cal_ids = ooo_cal_ids - live_cal_ids | |
removed_calendar_cal_ids = calendar_event_cal_ids - live_cal_ids | |
# Removed events | |
removed_ooo = OneOnOne.joins(:one_on_one_notes).where(cal_id: removed_ooo_cal_ids, one_on_one_notes: {edited: false, private: true}) | |
removed_cal_events = CalendarEvent.where(cal_event_id: removed_calendar_cal_ids) | |
# Remove all events | |
removed_ooo.destroy_all | |
removed_cal_events.destroy_all |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
CalendarEvent.where('one_on_one_id is null and start_time < ?', 30.days.ago).destroy_all |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment