From 99a2d47e3050db0e9ff375a56e53d2015fa9dad9 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Mon, 24 Apr 2023 09:11:16 +0200 Subject: [PATCH] MOBILE-4317 calendar: Store events from getEventsList in DB This schedules their default reminder if it's a new event --- src/addons/calendar/services/calendar.ts | 39 ++++++++++++++++-------- 1 file changed, 26 insertions(+), 13 deletions(-) diff --git a/src/addons/calendar/services/calendar.ts b/src/addons/calendar/services/calendar.ts index b776f44d8..c7b4067ee 100644 --- a/src/addons/calendar/services/calendar.ts +++ b/src/addons/calendar/services/calendar.ts @@ -658,8 +658,7 @@ export class AddonCalendarProvider { const response: AddonCalendarGetCalendarEventByIdWSResponse = await site.read('core_calendar_get_calendar_event_by_id', params, preSets); - this.storeEventInLocalDb(response.event, { siteId }); - this.updateEventsReminders([response.event], site.getId()); + this.updateLocalEvents([response.event], { siteId }); return response.event; } catch (error) { @@ -835,8 +834,7 @@ export class AddonCalendarProvider { preSets.emergencyCache = false; } const response: AddonCalendarCalendarDay = await site.read('core_calendar_get_calendar_day_view', params, preSets); - this.storeEventsInLocalDB(response.events, { siteId }); - this.updateEventsReminders(response.events, site.getId()); + this.updateLocalEvents(response.events, { siteId }); return response; } @@ -958,8 +956,10 @@ export class AddonCalendarProvider { uniqueCacheKey: true, updateFrequency: CoreSite.FREQUENCY_SOMETIMES, }; - const response: AddonCalendarGetCalendarEventsWSResponse = - await site.read('core_calendar_get_calendar_events', params, preSets); + const response = + await site.read('core_calendar_get_calendar_events', params, preSets); + + this.updateLocalEvents(response.events, { siteId }); return response.events; } @@ -1042,8 +1042,7 @@ export class AddonCalendarProvider { const response = await site.read('core_calendar_get_calendar_monthly_view', params, preSets); response.weeks.forEach((week) => { week.days.forEach((day) => { - this.storeEventsInLocalDB(day.events, { siteId }); - this.updateEventsReminders(day.events, site.getId()); + this.updateLocalEvents(day.events, { siteId }); }); }); @@ -1140,8 +1139,7 @@ export class AddonCalendarProvider { } const response = await site.read('core_calendar_get_calendar_upcoming_view', params, preSets); - this.storeEventsInLocalDB(response.events, { siteId }); - this.updateEventsReminders(response.events, site.getId()); + this.updateLocalEvents(response.events, { siteId }); return response; } @@ -1412,9 +1410,8 @@ export class AddonCalendarProvider { return; } - // Get first events. - const events = await this.getEventsList(undefined, undefined, undefined, siteId); - await this.updateEventsReminders(events, siteId); + // Get first events to store/update them in local database and update their reminders. + await this.getEventsList(undefined, undefined, undefined, siteId); } /** @@ -1600,6 +1597,22 @@ export class AddonCalendarProvider { await Promise.all(events.map((event) => this.storeEventInLocalDb(event, options))); } + /** + * Update local events, storing them in DB and also updating their reminders. + * + * @param events Events to store or update. + * @param options Options. + */ + protected async updateLocalEvents( + events: (AddonCalendarGetEventsEvent | AddonCalendarCalendarEvent | AddonCalendarEvent)[], + options: AddonCalendarStoreEventsOptions = {}, + ): Promise { + options.siteId = options.siteId || CoreSites.getCurrentSiteId(); + + await this.storeEventsInLocalDB(events, options); + await this.updateEventsReminders(events, options.siteId); + } + /** * Submit a calendar event. *