From 165cd74a17c5773f744c918ddd297b93e645635a Mon Sep 17 00:00:00 2001 From: Sharon Dcruz Date: Tue, 19 Aug 2025 11:18:53 +0000 Subject: [PATCH] notification-bug-fix (#102) Co-authored-by: Dhansh A S Reviewed-on: https://git.cosqnet.com/cosqnet/fitlien-services/pulls/102 Reviewed-by: Dhansh A S Co-authored-by: Sharon Dcruz Co-committed-by: Sharon Dcruz --- .../membershipStatusNotifications.ts | 34 +++++++++++++------ 1 file changed, 23 insertions(+), 11 deletions(-) diff --git a/functions/src/notifications/membershipStatusNotifications.ts b/functions/src/notifications/membershipStatusNotifications.ts index ba73340..7e1b88e 100644 --- a/functions/src/notifications/membershipStatusNotifications.ts +++ b/functions/src/notifications/membershipStatusNotifications.ts @@ -53,7 +53,6 @@ export const checkExpiredMemberships = onSchedule( logger.info("Starting scheduled membership expiry check..."); try { - await updateDaysUntilExpiryForAllMemberships(); const expiredMemberships = await findExpiredMemberships(); const expiringMemberships = await findMembershipsExpiringIn10Days(); @@ -381,8 +380,10 @@ function calculateRenewalDateFromPayment( async function updateDaysUntilExpiryForAllMemberships(): Promise { try { - logger.info("Starting to update daysUntilExpiry for all active memberships..."); - + logger.info( + "Starting to update daysUntilExpiry for all active memberships..." + ); + const snapshot = await app .firestore() .collection("memberships") @@ -399,16 +400,22 @@ async function updateDaysUntilExpiryForAllMemberships(): Promise { batch.map(async (doc) => { const data = doc.data() as MembershipData; const daysUntilExpiry = await calculateDaysUntilExpiry(doc.id, data); - + if (daysUntilExpiry !== null) { + const updateData: any = { + daysUntilExpiry: daysUntilExpiry, + updatedAt: admin.firestore.FieldValue.serverTimestamp(), + }; + await app .firestore() .collection("memberships") .doc(doc.id) - .update({ - daysUntilExpiry: daysUntilExpiry, - updatedAt: admin.firestore.FieldValue.serverTimestamp(), - }); + .update(updateData); + + logger.info( + `Updated membership ${doc.id} with daysUntilExpiry: ${daysUntilExpiry}` + ); return doc.id; } return null; @@ -429,7 +436,6 @@ async function updateDaysUntilExpiryForAllMemberships(): Promise { } } - async function calculateDaysUntilExpiry( membershipId: string, data: MembershipData @@ -459,7 +465,14 @@ async function calculateDaysUntilExpiry( ); const now = new Date(); - const timeDiff = expiryDate.getTime() - now.getTime(); + const today = new Date(now.getFullYear(), now.getMonth(), now.getDate()); + const expiryDateOnly = new Date( + expiryDate.getFullYear(), + expiryDate.getMonth(), + expiryDate.getDate() + ); + + const timeDiff = expiryDateOnly.getTime() - today.getTime(); const daysUntilExpiry = Math.ceil(timeDiff / (1000 * 3600 * 24)); return daysUntilExpiry; @@ -472,7 +485,6 @@ async function calculateDaysUntilExpiry( } } - async function getTrainerAssignmentsForMembership( membershipId: string ): Promise {