From a69ed7078aea51608decf274046c0f74447778a1 Mon Sep 17 00:00:00 2001 From: Sharon Dcruz Date: Tue, 23 Sep 2025 07:37:15 +0000 Subject: [PATCH] feature/fitlien-828 (#124) Reviewed-on: https://git.cosqnet.com/cosqnet/fitlien-services/pulls/124 Co-authored-by: Sharon Dcruz Co-committed-by: Sharon Dcruz --- .../membershipStatusNotifications.ts | 44 ++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/functions/src/notifications/membershipStatusNotifications.ts b/functions/src/notifications/membershipStatusNotifications.ts index cc4f5ae..5a3958c 100644 --- a/functions/src/notifications/membershipStatusNotifications.ts +++ b/functions/src/notifications/membershipStatusNotifications.ts @@ -6,6 +6,40 @@ const app = getAdmin(); const logger = getLogger(); const kTrainerRole = "Trainer"; +async function updateCacheForMembership( + gymId: string, + membershipId: string +): Promise { + try { + const response = await fetch( + `https://updatemembercache-2k7djjvd3q-el.a.run.app/updateMemberCache`, + { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + gymId: gymId, + incrementalUpdate: true, + membershipIds: [membershipId], + }), + } + ); + + if (response.ok) { + logger.info( + `Cache updated successfully for membership ${membershipId} in gym ${gymId}` + ); + } else { + logger.warn( + `Cache update failed for membership ${membershipId}: ${response.status}` + ); + } + } catch (error) { + logger.error(`Error updating cache for membership ${membershipId}:`, error); + } +} + interface MembershipData { id?: string; userId: string; @@ -150,6 +184,7 @@ async function findExpiredMembershipsWithoutExpiryDate(): Promise< throw error; } } + async function updateExpiryDateForExpiredMembership( membershipId: string, membershipData: MembershipData @@ -184,6 +219,8 @@ async function updateExpiryDateForExpiredMembership( updatedAt: admin.firestore.FieldValue.serverTimestamp(), }); + await updateCacheForMembership(membershipData.gymId, membershipId); + logger.info( `Updated expiry date for expired membership ${membershipId}: ${expiryDate.toISOString()}` ); @@ -513,6 +550,8 @@ async function updateDaysUntilExpiryForAllMemberships(): Promise { .doc(doc.id) .update(updateData); + await updateCacheForMembership(data.gymId, doc.id); + logger.info( `Updated membership ${doc.id} with daysUntilExpiry: ${daysUntilExpiry}` ); @@ -535,7 +574,6 @@ async function updateDaysUntilExpiryForAllMemberships(): Promise { throw error; } } - async function calculateDaysUntilExpiry( membershipId: string, data: MembershipData @@ -665,6 +703,8 @@ async function processExpiredMembership( }); } + await updateCacheForMembership(membershipData.gymId, membershipId); + logger.info(`Marked membership ${membershipId} as EXPIRED`); await sendPlanExpiredNotification(membershipId, membershipData); @@ -697,6 +737,8 @@ async function processExpiringMembership( expirationDate: admin.firestore.Timestamp.fromDate(expiryDate), updatedAt: admin.firestore.FieldValue.serverTimestamp(), }); + + await updateCacheForMembership(membershipData.gymId, membershipId); } await sendPlanExpiringNotification(membershipId, membershipData);