diff --git a/functions/src/memberCache/memberCache.ts b/functions/src/memberCache/memberCache.ts index e0322fb..27ac98e 100644 --- a/functions/src/memberCache/memberCache.ts +++ b/functions/src/memberCache/memberCache.ts @@ -18,6 +18,8 @@ interface MembershipData { }; isPartialPayment?: boolean; remainingAmount?: number; + daysUntilExpiry?: number; + expirationDate?: admin.firestore.Timestamp; createdAt?: admin.firestore.Timestamp; updatedAt?: admin.firestore.Timestamp; [key: string]: any; @@ -42,6 +44,7 @@ interface MinimalCacheEntry { phoneNumber?: string | null; alternateContact?: string | null; renewalDate?: string | null; + expirationDate?: string | null; createdAt?: string | null; daysUntilExpiry?: number | null; hasPersonalTraining: boolean; @@ -389,44 +392,10 @@ async function generateMinimalCacheEntry( logger.error(`Error getting fields for user ${userId}:`, error); } - let renewalDate: Date | null = null; - let daysUntilExpiry: number | null = null; + const daysUntilExpiry = membershipData.daysUntilExpiry || null; - if (membershipData.subscription) { - try { - const paymentsSnapshot = await app - .firestore() - .collection("membership_payments") - .where("membershipId", "==", membershipId) - .orderBy("dateTimestamp", "desc") - .limit(1) - .get(); - - if (!paymentsSnapshot.empty) { - const latestPayment = paymentsSnapshot.docs[0].data() as PaymentData; - if (latestPayment.dateTimestamp) { - const paymentDate = latestPayment.dateTimestamp.toDate(); - renewalDate = calculateRenewalDateTimeFromPayment( - membershipData.subscription, - paymentDate - ); - - if (renewalDate && membershipData.status === "ACTIVE") { - const now = new Date(); - const difference = Math.floor( - (renewalDate.getTime() - now.getTime()) / (1000 * 60 * 60 * 24) - ); - daysUntilExpiry = difference > 0 ? difference : null; - } - } - } - } catch (error) { - logger.error( - `Error getting renewal date for membership ${membershipId}:`, - error - ); - } - } + const displayName = firstName.length === 0 ? 'Unknown' : + (lastName.length === 0 ? firstName : `${firstName} ${lastName}`); const displayName = firstName.length === 0 ? 'Unknown' : (lastName.length === 0 ? firstName : `${firstName} ${lastName}`); @@ -443,7 +412,10 @@ async function generateMinimalCacheEntry( email: email || null, phoneNumber: phoneNumber || null, alternateContact: alternateContact || null, - renewalDate: renewalDate ? renewalDate.toISOString() : null, + renewalDate: null, + expirationDate: membershipData.expirationDate + ? membershipData.expirationDate.toDate().toISOString() + : null, createdAt: membershipData.createdAt ? membershipData.createdAt.toDate().toISOString() : null, @@ -461,38 +433,3 @@ async function generateMinimalCacheEntry( } } -function calculateRenewalDateTimeFromPayment( - subscription: { frequency?: string }, - paymentDate: Date -): Date { - const frequency = subscription.frequency || "Monthly"; - - switch (frequency) { - case "Monthly": - return new Date( - paymentDate.getFullYear(), - paymentDate.getMonth() + 1, - paymentDate.getDate() - ); - case "Quarterly": - return new Date( - paymentDate.getFullYear(), - paymentDate.getMonth() + 3, - paymentDate.getDate() - ); - case "Half-yearly": - return new Date( - paymentDate.getFullYear(), - paymentDate.getMonth() + 6, - paymentDate.getDate() - ); - case "Yearly": - return new Date( - paymentDate.getFullYear() + 1, - paymentDate.getMonth(), - paymentDate.getDate() - ); - default: - return new Date(Date.now() + 30 * 24 * 60 * 60 * 1000); - } -} \ No newline at end of file