From 559997a16e9bd26cbc7972992f767e027ec3f40a Mon Sep 17 00:00:00 2001 From: Sharon Dcruz Date: Tue, 19 Aug 2025 09:53:01 +0530 Subject: [PATCH] Changes Updated --- firestore.indexes.json | 14 --- .../membershipStatusNotifications.ts | 96 +++++++++++++++++++ 2 files changed, 96 insertions(+), 14 deletions(-) diff --git a/firestore.indexes.json b/firestore.indexes.json index c1c2245..2d8dc7b 100644 --- a/firestore.indexes.json +++ b/firestore.indexes.json @@ -57,20 +57,6 @@ ] }, - { - "collectionGroup": "gyms", - "queryScope": "COLLECTION", - "fields": [ - { - "fieldPath": "userId", - "order": "ASCENDING" - }, - { - "fieldPath": "name", - "order": "ASCENDING" - } - ] - }, { "collectionGroup": "memberships", "queryScope": "COLLECTION", diff --git a/functions/src/notifications/membershipStatusNotifications.ts b/functions/src/notifications/membershipStatusNotifications.ts index a26f9eb..ba73340 100644 --- a/functions/src/notifications/membershipStatusNotifications.ts +++ b/functions/src/notifications/membershipStatusNotifications.ts @@ -53,6 +53,8 @@ export const checkExpiredMemberships = onSchedule( logger.info("Starting scheduled membership expiry check..."); try { + + await updateDaysUntilExpiryForAllMemberships(); const expiredMemberships = await findExpiredMemberships(); const expiringMemberships = await findMembershipsExpiringIn10Days(); @@ -377,6 +379,100 @@ function calculateRenewalDateFromPayment( return renewalDate; } +async function updateDaysUntilExpiryForAllMemberships(): Promise { + try { + logger.info("Starting to update daysUntilExpiry for all active memberships..."); + + const snapshot = await app + .firestore() + .collection("memberships") + .where("status", "==", "ACTIVE") + .get(); + + const batchSize = 10; + const docs = snapshot.docs; + let updatedCount = 0; + + for (let i = 0; i < docs.length; i += batchSize) { + const batch = docs.slice(i, i + batchSize); + const batchResults = await Promise.allSettled( + batch.map(async (doc) => { + const data = doc.data() as MembershipData; + const daysUntilExpiry = await calculateDaysUntilExpiry(doc.id, data); + + if (daysUntilExpiry !== null) { + await app + .firestore() + .collection("memberships") + .doc(doc.id) + .update({ + daysUntilExpiry: daysUntilExpiry, + updatedAt: admin.firestore.FieldValue.serverTimestamp(), + }); + return doc.id; + } + return null; + }) + ); + + batchResults.forEach((result) => { + if (result.status === "fulfilled" && result.value) { + updatedCount++; + } + }); + } + + logger.info(`Updated daysUntilExpiry for ${updatedCount} memberships`); + } catch (error) { + logger.error("Error updating daysUntilExpiry for memberships:", error); + throw error; + } +} + + +async function calculateDaysUntilExpiry( + membershipId: string, + data: MembershipData +): Promise { + try { + if (!data.subscription || !data.subscription.frequency) { + logger.warn( + `Skipping expiry calculation for membership ${membershipId} with missing subscription data.` + ); + return null; + } + + const payments = await getPaymentsForMembership(membershipId); + if (payments.length === 0) { + logger.warn( + `No payments found for membership ${membershipId}, cannot determine expiry` + ); + return null; + } + + const latestPayment = payments[0]; + const startDate = latestPayment.dateTimestamp; + + const expiryDate = calculateExpiryDate( + startDate, + data.subscription.frequency + ); + + const now = new Date(); + const timeDiff = expiryDate.getTime() - now.getTime(); + const daysUntilExpiry = Math.ceil(timeDiff / (1000 * 3600 * 24)); + + return daysUntilExpiry; + } catch (error) { + logger.error( + `Error calculating daysUntilExpiry for membership ${membershipId}:`, + error + ); + return null; + } +} + + async function getTrainerAssignmentsForMembership( membershipId: string ): Promise {