/// <summary> /// Syncs an eSpace event into Rock. /// </summary> /// <param name="eSpaceClient">The eSpace client</param> /// <param name="eSpaceEvent">The eSpace event</param> /// <param name="occurrencesFilter">The filter to use when syncing occurrences</param> /// <returns>The synced Rock event</returns> public static async Task SyncEvent( Client eSpaceClient, ESpace.Event eSpaceEvent, GetEventOccurrencesOptions occurrencesFilter, EventCalendarCache globalCalendar, EventCalendarCache publicCalendar, EventCalendarCache privateCalendar, string occurrenceApprovedAttributeKey ) { using (var rockContext = new RockContext()) { // Create our services var eventItemService = new EventItemService(rockContext); var eventItemOccurrenceService = new EventItemOccurrenceService(rockContext); var eventItemAudienceService = new EventItemAudienceService(rockContext); var scheduleService = new ScheduleService(rockContext); var personService = new PersonService(rockContext); // Get or create the linked Rock event var rockEvent = eventItemService.GetOrCreateByForeignId( "EventCalendarItems,EventItemOccurrences", ForeignKey_eSpaceEventId, eSpaceEvent.EventId.Value, out var _ ); // Track if we needed to update anything var changed = false; // Update the Name if (rockEvent.Name != eSpaceEvent.EventName) { changed = true; rockEvent.Name = eSpaceEvent.EventName; } // Update the Active State var eSpaceEventIsActive = eSpaceEvent.Status != ESpaceStatus_Draft; if (rockEvent.IsActive != eSpaceEventIsActive) { changed = true; rockEvent.IsActive = eSpaceEventIsActive; } // Update the Approval State var eSpaceEventIsApproved = eSpaceEvent.Status == ESpaceStatus_Approved; if (rockEvent.IsApproved != eSpaceEventIsApproved) { changed = true; rockEvent.IsApproved = eSpaceEventIsApproved; if (eSpaceEventIsApproved) { rockEvent.ApprovedOnDateTime = DateTime.Now; } else { rockEvent.ApprovedOnDateTime = null; } } // Update the Summary if (rockEvent.Summary != eSpaceEvent.Description) { changed = true; rockEvent.Summary = eSpaceEvent.Description; } // Update the details Url var eSpaceEventPublicLink = eSpaceEvent.PublicLink?.ToString(); if (rockEvent.DetailsUrl != eSpaceEventPublicLink) { changed = true; rockEvent.DetailsUrl = eSpaceEventPublicLink; } // Update the audiences var eSpaceCategories = MatchCategories(eSpaceEvent.Categories); // Check global calendar if (globalCalendar != null) { rockEvent.AddToCalendar(globalCalendar, out var addedToCalendar); changed = changed || addedToCalendar; } var eventCalendarItemService = new EventCalendarItemService(rockContext); // Check public calendar if (publicCalendar != null) { if (eSpaceEvent.IsPublic ?? false) { rockEvent.AddToCalendar(publicCalendar, out var addedToCalendar); changed = changed || addedToCalendar; } else { rockEvent.RemoveFromCalendar(eventCalendarItemService, publicCalendar, out var removedFromCalendar); changed = changed || removedFromCalendar; } } // Check private calendar if (privateCalendar != null) { if (!(eSpaceEvent.IsPublic ?? false)) { rockEvent.AddToCalendar(privateCalendar, out var addedToCalendar); changed = changed || addedToCalendar; } else { rockEvent.RemoveFromCalendar(eventCalendarItemService, privateCalendar, out var removedFromCalendar); changed = changed || removedFromCalendar; } } // Fetch the occurrences for the event if (occurrencesFilter == null) { occurrencesFilter = new GetEventOccurrencesOptions { StartDate = DateTime.Now } } ; occurrencesFilter.EventId = eSpaceEvent.EventId; var eSpaceEventOccurrences = await eSpaceClient.GetEventOccurrences(occurrencesFilter); // Calculate some stuff for the occurrences var campusLocations = MatchLocations(eSpaceEvent.IsOffSite ?? false ? eSpaceEvent.PublicLocations : eSpaceEvent.Locations); var contactPerson = personService.FindPerson(eSpaceEvent.Contacts.FirstOrDefault()); var firstESpaceOccurrence = eSpaceEventOccurrences.FirstOrDefault(); if (firstESpaceOccurrence != null) { // Update the Description if (rockEvent.Description != firstESpaceOccurrence.PublicHtmlNotes) { rockEvent.Description = firstESpaceOccurrence.PublicHtmlNotes; changed = true; } } var syncedRockOccurrences = new List <EventItemOccurrence>();; var rockOccurrencesWithAttributeChanges = new List <EventItemOccurrence>(); // Update each occurrence foreach (var eSpaceOccurrence in eSpaceEventOccurrences) { foreach (var campusLocation in campusLocations) { var rockOccurrence = SyncOccurrence(eSpaceEvent, eSpaceOccurrence, rockEvent, campusLocation, contactPerson, occurrenceApprovedAttributeKey, out var occurrenceChanged, out var occurrenceAttributeChanged); changed = changed || occurrenceChanged; syncedRockOccurrences.Add(rockOccurrence); if (occurrenceAttributeChanged) { rockOccurrencesWithAttributeChanges.Add(rockOccurrence); } } } // Remove any desynced occurrences var removedOccurrences = rockEvent.EventItemOccurrences.Except(syncedRockOccurrences).ToList(); foreach (var occurrence in removedOccurrences) { rockEvent.EventItemOccurrences.Remove(occurrence); if (occurrence.Schedule != null) { scheduleService.Delete(occurrence.Schedule); occurrence.Schedule = null; } eventItemOccurrenceService.Delete(occurrence); changed = true; } // If anything was updated, save it if (changed) { rockContext.SaveChanges(); } // If any occurrences had attributes modified, save them foreach (var rockOccurrence in rockOccurrencesWithAttributeChanges) { rockOccurrence.SaveAttributeValues(); } } }
public static EventItemOccurrence SyncOccurrence( ESpace.Event eSpaceEvent, ESpace.Occurrence eSpaceOccurrence, EventItem rockEvent, CampusCache campus, Person contactPerson, string occurrenceApprovedAttributeKey, out bool changed, out bool attributeChanged ) { attributeChanged = false; changed = false; // Get or create the linked Rock occurrence var rockOccurrence = rockEvent.EventItemOccurrences.FirstOrDefault(e => e.ForeignKey == ForeignKey_eSpaceOccurrenceId && e.ForeignId == eSpaceOccurrence.OccurrenceId.Value && e.CampusId == campus.Id ); if (rockOccurrence == null) { rockOccurrence = new EventItemOccurrence { ForeignKey = ForeignKey_eSpaceOccurrenceId, ForeignId = eSpaceOccurrence.OccurrenceId.Value, CampusId = campus.Id }; rockEvent.EventItemOccurrences.Add(rockOccurrence); changed = true; } // Update the linked Contact Person if (rockOccurrence.ContactPersonAliasId != contactPerson?.PrimaryAliasId) { rockOccurrence.ContactPersonAliasId = contactPerson?.PrimaryAliasId; changed = true; } // Get the contact data var eventContact = eSpaceEvent.Contacts.FirstOrDefault(); // Update the Contact Email var eventContactEmail = eventContact?.Email ?? ""; if (rockOccurrence.ContactEmail != eventContactEmail) { rockOccurrence.ContactEmail = eventContactEmail; changed = true; } // Update the Contact Phone var eventContactPhone = PhoneNumber.FormattedNumber(null, eventContact?.Phone, false); if (rockOccurrence.ContactPhone != eventContactPhone) { rockOccurrence.ContactPhone = eventContactPhone; changed = true; } // Update the event location var eventLocation = eSpaceEvent.OffsiteLocation ?? ""; if (rockOccurrence.Location != eventLocation) { rockOccurrence.Location = eventLocation; changed = true; } // Sync the schedule SyncSchedule(eSpaceOccurrence, rockOccurrence, out var scheduleChanged); changed = changed || scheduleChanged; // Check the approved attribute if (!string.IsNullOrEmpty(occurrenceApprovedAttributeKey)) { rockOccurrence.LoadAttributes(); var eSpaceApprovedValue = eSpaceOccurrence.OccurrenceStatus == ESpaceStatus_Approved ? "Approved" : ""; var rockApprovedValue = rockOccurrence.GetAttributeValue(occurrenceApprovedAttributeKey); if (rockApprovedValue != eSpaceApprovedValue) { rockOccurrence.SetAttributeValue(occurrenceApprovedAttributeKey, eSpaceApprovedValue); attributeChanged = true; } } return(rockOccurrence); }