From 3d0a9441f438c599d6c1ddeccf192a7affb66cb2 Mon Sep 17 00:00:00 2001
From: Dani Palou <dani@moodle.com>
Date: Thu, 20 Apr 2023 13:27:45 +0200
Subject: [PATCH 1/2] MOBILE-4317 calendar: Fix updateAllSitesEventReminders

---
 src/addons/calendar/services/calendar.ts | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/addons/calendar/services/calendar.ts b/src/addons/calendar/services/calendar.ts
index 2fa158b92..e2074f3e1 100644
--- a/src/addons/calendar/services/calendar.ts
+++ b/src/addons/calendar/services/calendar.ts
@@ -1392,7 +1392,7 @@ export class AddonCalendarProvider {
 
         const siteIds = await CoreSites.getSitesIds();
 
-        await Promise.all(siteIds.map((siteId: string) => async () => {
+        await Promise.all(siteIds.map(async (siteId) => {
 
             // Check if calendar is disabled for the site.
             const disabled = await this.isDisabled(siteId);

From d1f28f43abff16b64b46bb9db9503a66d30623b2 Mon Sep 17 00:00:00 2001
From: Dani Palou <dani@moodle.com>
Date: Thu, 20 Apr 2023 13:49:51 +0200
Subject: [PATCH 2/2] MOBILE-4317 calendar: Schedule new site reminders

---
 src/addons/calendar/services/calendar.ts | 41 ++++++++++++++++--------
 1 file changed, 27 insertions(+), 14 deletions(-)

diff --git a/src/addons/calendar/services/calendar.ts b/src/addons/calendar/services/calendar.ts
index e2074f3e1..b776f44d8 100644
--- a/src/addons/calendar/services/calendar.ts
+++ b/src/addons/calendar/services/calendar.ts
@@ -49,6 +49,7 @@ import {
     CoreReminderValueAndUnit,
 } from '@features/reminders/services/reminders';
 import { CoreReminderDBRecord } from '@features/reminders/services/database/reminders';
+import { CoreEvents } from '@singletons/events';
 
 const ROOT_CACHE_KEY = 'mmaCalendar:';
 
@@ -297,6 +298,14 @@ export class AddonCalendarProvider {
                 this.notificationClicked(notification);
             },
         );
+
+        CoreEvents.on(CoreEvents.SITE_ADDED, (data) => {
+            if (!data.siteId) {
+                return;
+            }
+
+            this.updateSiteEventReminders(data.siteId);
+        });
     }
 
     /**
@@ -1381,27 +1390,31 @@ export class AddonCalendarProvider {
     }
 
     /**
-     * Get the next events for all the sites and schedules their notifications.
-     * If an event notification time is 0, cancel its scheduled notification (if any).
-     * If local notification plugin is not enabled, resolve the promise.
-     *
-     * @returns Promise resolved when all the notifications have been scheduled.
+     * Get the next events for all the sites and updates their reminders.
      */
     async updateAllSitesEventReminders(): Promise<void> {
         await CorePlatform.ready();
 
         const siteIds = await CoreSites.getSitesIds();
 
-        await Promise.all(siteIds.map(async (siteId) => {
+        await Promise.all(siteIds.map(siteId => this.updateSiteEventReminders(siteId)));
+    }
 
-            // Check if calendar is disabled for the site.
-            const disabled = await this.isDisabled(siteId);
-            if (!disabled) {
-                // Get first events.
-                const events = await this.getEventsList(undefined, undefined, undefined, siteId);
-                await this.updateEventsReminders(events, siteId);
-            }
-        }));
+    /**
+     * Get the next events for a site and updates their reminders.
+     *
+     * @param siteId Site ID.
+     */
+    async updateSiteEventReminders(siteId: string): Promise<void> {
+        // Check if calendar is disabled for the site.
+        const disabled = await this.isDisabled(siteId);
+        if (disabled) {
+            return;
+        }
+
+        // Get first events.
+        const events = await this.getEventsList(undefined, undefined, undefined, siteId);
+        await this.updateEventsReminders(events, siteId);
     }
 
     /**