1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-18 07:52:23 +01:00

icalendar source sync: create/update/delete ical events

This commit is contained in:
Sylvain 2019-12-03 12:16:07 +01:00
parent 538b5cef78
commit 64fe68b2b0
3 changed files with 21 additions and 12 deletions

View File

@ -36,8 +36,7 @@ class API::ICalendarController < API::ApiController
end
def sync
worker = ICalendarImportWorker.new
worker.perform([params[:id]])
ICalendarImportWorker.perform_async([params[:id]])
render json: { processing: [params[:id]] }, status: :created
end

View File

@ -3,4 +3,10 @@
# iCalendar (RFC 5545) event, belonging to an ICalendar object (its source)
class ICalendarEvent < ActiveRecord::Base
belongs_to :i_calendar
def self.update_or_create_by(args, attributes)
obj = find_or_create_by(args)
obj.update(attributes)
obj
end
end

View File

@ -7,25 +7,29 @@ class ICalendarImportService
require 'uri'
require 'icalendar'
events = []
uids = []
i_cal = ICalendar.find(i_calendar_id)
ics = Net::HTTP.get(URI.parse(i_cal.url))
cals = Icalendar::Calendar.parse(ics)
# create new events and update existings
cals.each do |cal|
cal.events.each do |evt|
events.push(
uid: evt.uid,
uids.push(evt.uid.to_s)
ICalendarEvent.update_or_create_by(
{ uid: evt.uid.to_s },
{
dtstart: evt.dtstart,
dtend: evt.dtend,
summary: evt.summary,
description: evt.description,
i_calendar_id: i_calendar_id
}
)
end
end
ICalendarEvent.create!(events)
# remove deleted events
ICalendarEvent.where(i_calendar_id: i_calendar_id).where.not(uid: uids).destroy_all
end
end