notification-bug-fix #112
| @ -63,10 +63,18 @@ export const checkExpiredMemberships = onSchedule( | ||||
|         expiringMemberships.map((m) => processExpiringMembership(m.id, m.data)) | ||||
|       ); | ||||
| 
 | ||||
|       const expiredSuccessful = expiredResults.filter((r) => r.status === "fulfilled").length; | ||||
|       const expiredFailed = expiredResults.filter((r) => r.status === "rejected").length; | ||||
|       const expiringSuccessful = expiringResults.filter((r) => r.status === "fulfilled").length; | ||||
|       const expiringFailed = expiringResults.filter((r) => r.status === "rejected").length; | ||||
|       const expiredSuccessful = expiredResults.filter( | ||||
|         (r) => r.status === "fulfilled" | ||||
|       ).length; | ||||
|       const expiredFailed = expiredResults.filter( | ||||
|         (r) => r.status === "rejected" | ||||
|       ).length; | ||||
|       const expiringSuccessful = expiringResults.filter( | ||||
|         (r) => r.status === "fulfilled" | ||||
|       ).length; | ||||
|       const expiringFailed = expiringResults.filter( | ||||
|         (r) => r.status === "rejected" | ||||
|       ).length; | ||||
| 
 | ||||
|       logger.info( | ||||
|         `Completed processing. Expired - Success: ${expiredSuccessful}, Failed: ${expiredFailed}. Expiring - Success: ${expiringSuccessful}, Failed: ${expiringFailed}` | ||||
| @ -139,7 +147,10 @@ async function findMembershipsExpiringIn10Days(): Promise< | ||||
|       const batchResults = await Promise.allSettled( | ||||
|         batch.map(async (doc) => { | ||||
|           const data = doc.data() as MembershipData; | ||||
|           const isExpiringIn10Days = await checkIfMembershipExpiringIn10Days(doc.id, data); | ||||
|           const isExpiringIn10Days = await checkIfMembershipExpiringIn10Days( | ||||
|             doc.id, | ||||
|             data | ||||
|           ); | ||||
|           if (isExpiringIn10Days) { | ||||
|             return { id: doc.id, data }; | ||||
|           } | ||||
| @ -241,11 +252,11 @@ async function checkIfMembershipExpiringIn10Days( | ||||
|       startDate, | ||||
|       data.subscription.frequency | ||||
|     ); | ||||
|      | ||||
| 
 | ||||
|     const now = new Date(); | ||||
|     const tenDaysFromNow = new Date(); | ||||
|     tenDaysFromNow.setDate(now.getDate() + 10); | ||||
|      | ||||
| 
 | ||||
|     const isExpiringIn10Days = expiryDate > now && expiryDate <= tenDaysFromNow; | ||||
| 
 | ||||
|     if (isExpiringIn10Days) { | ||||
| @ -381,7 +392,10 @@ async function processExpiringMembership( | ||||
|     logger.info(`Processing expiring membership ${membershipId}`); | ||||
|     await sendPlanExpiringNotification(membershipId, membershipData); | ||||
|   } catch (error) { | ||||
|     logger.error(`Error processing expiring membership ${membershipId}:`, error); | ||||
|     logger.error( | ||||
|       `Error processing expiring membership ${membershipId}:`, | ||||
|       error | ||||
|     ); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| @ -394,19 +408,6 @@ async function sendPlanExpiredNotification( | ||||
|     const gymOwnerId = await getGymOwnerId(membershipData.gymId); | ||||
|     const gymName = await getGymName(membershipData.gymId); | ||||
| 
 | ||||
|     const existing = await app | ||||
|       .firestore() | ||||
|       .collection("notifications") | ||||
|       .where("type", "==", "plan_expired") | ||||
|       .where("data.membershipId", "==", membershipId) | ||||
|       .limit(1) | ||||
|       .get(); | ||||
| 
 | ||||
|     if (!existing.empty) { | ||||
|       logger.info(`Notification already sent for ${membershipId}, skipping...`); | ||||
|       return; | ||||
|     } | ||||
| 
 | ||||
|     let expiryDate: Date | undefined; | ||||
|     let formattedDate = "Unknown Date"; | ||||
| 
 | ||||
| @ -422,7 +423,26 @@ async function sendPlanExpiredNotification( | ||||
|         month: "long", | ||||
|         day: "numeric", | ||||
|       }); | ||||
|        | ||||
|     } | ||||
| 
 | ||||
|     const existing = await app | ||||
|       .firestore() | ||||
|       .collection("notifications") | ||||
|       .where("type", "==", "plan_expired") | ||||
|       .where("data.membershipId", "==", membershipId) | ||||
|       .where( | ||||
|         "data.expiryDate", | ||||
|         "==", | ||||
|         expiryDate | ||||
|           ? admin.firestore.Timestamp.fromDate(expiryDate) | ||||
|           : admin.firestore.Timestamp.fromDate(new Date()) | ||||
|       ) | ||||
|       .limit(1) | ||||
|       .get(); | ||||
| 
 | ||||
|     if (!existing.empty) { | ||||
|       logger.info(`Notification already sent for ${membershipId}, skipping...`); | ||||
|       return; | ||||
|     } | ||||
| 
 | ||||
|     await app | ||||
| @ -435,13 +455,13 @@ async function sendPlanExpiredNotification( | ||||
|         notificationSent: false, | ||||
|         timestamp: admin.firestore.FieldValue.serverTimestamp(), | ||||
|         read: false, | ||||
|         readBy: [],  | ||||
|         readBy: [], | ||||
|         data: { | ||||
|           planName: membershipData.subscription?.name || "Unknown Plan", | ||||
|           clientName, | ||||
|           membershipId, | ||||
|           gymName, | ||||
|           ownerId: gymOwnerId,  | ||||
|           ownerId: gymOwnerId, | ||||
|           formattedExpiryDate: formattedDate, | ||||
|           expiryDate: expiryDate | ||||
|             ? admin.firestore.Timestamp.fromDate(expiryDate) | ||||
| @ -466,19 +486,6 @@ async function sendPlanExpiringNotification( | ||||
|     const gymOwnerId = await getGymOwnerId(membershipData.gymId); | ||||
|     const gymName = await getGymName(membershipData.gymId); | ||||
| 
 | ||||
|     const existing = await app | ||||
|       .firestore() | ||||
|       .collection("notifications") | ||||
|       .where("type", "==", "plan_expiring_soon") | ||||
|       .where("data.membershipId", "==", membershipId) | ||||
|       .limit(1) | ||||
|       .get(); | ||||
| 
 | ||||
|     if (!existing.empty) { | ||||
|       logger.info(`Expiring notification already sent for ${membershipId}, skipping...`); | ||||
|       return; | ||||
|     } | ||||
| 
 | ||||
|     let expiryDate: Date | undefined; | ||||
|     let formattedDate = "Unknown Date"; | ||||
|     let daysUntilExpiry = 10; | ||||
| @ -495,12 +502,34 @@ async function sendPlanExpiringNotification( | ||||
|         month: "long", | ||||
|         day: "numeric", | ||||
|       }); | ||||
|        | ||||
| 
 | ||||
|       const now = new Date(); | ||||
|       const timeDiff = expiryDate.getTime() - now.getTime(); | ||||
|       daysUntilExpiry = Math.ceil(timeDiff / (1000 * 3600 * 24)); | ||||
|     } | ||||
| 
 | ||||
|     const existing = await app | ||||
|       .firestore() | ||||
|       .collection("notifications") | ||||
|       .where("type", "==", "plan_expiring_soon") | ||||
|       .where("data.membershipId", "==", membershipId) | ||||
|       .where( | ||||
|         "data.expiryDate", | ||||
|         "==", | ||||
|         expiryDate | ||||
|           ? admin.firestore.Timestamp.fromDate(expiryDate) | ||||
|           : admin.firestore.Timestamp.fromDate(new Date()) | ||||
|       ) | ||||
|       .limit(1) | ||||
|       .get(); | ||||
| 
 | ||||
|     if (!existing.empty) { | ||||
|       logger.info( | ||||
|         `Expiring notification already sent for ${membershipId}, skipping...` | ||||
|       ); | ||||
|       return; | ||||
|     } | ||||
| 
 | ||||
|     await app | ||||
|       .firestore() | ||||
|       .collection("notifications") | ||||
| @ -511,13 +540,13 @@ async function sendPlanExpiringNotification( | ||||
|         notificationSent: false, | ||||
|         timestamp: admin.firestore.FieldValue.serverTimestamp(), | ||||
|         read: false, | ||||
|         readBy: [],  | ||||
|         readBy: [], | ||||
|         data: { | ||||
|           planName: membershipData.subscription?.name || "Unknown Plan", | ||||
|           clientName, | ||||
|           membershipId, | ||||
|           gymName, | ||||
|           ownerId: gymOwnerId,  | ||||
|           ownerId: gymOwnerId, | ||||
|           formattedExpiryDate: formattedDate, | ||||
|           expiryDate: expiryDate | ||||
|             ? admin.firestore.Timestamp.fromDate(expiryDate) | ||||
| @ -530,7 +559,10 @@ async function sendPlanExpiringNotification( | ||||
|       `Expiring notification sent for membership ${membershipId} (expires on ${formattedDate}, ${daysUntilExpiry} days remaining)` | ||||
|     ); | ||||
|   } catch (error) { | ||||
|     logger.error(`Error sending expiring notification for ${membershipId}:`, error); | ||||
|     logger.error( | ||||
|       `Error sending expiring notification for ${membershipId}:`, | ||||
|       error | ||||
|     ); | ||||
|   } | ||||
| } | ||||
| 
 | ||||
| @ -577,4 +609,4 @@ async function getGymName(gymId: string): Promise<string> { | ||||
|     logger.error(`Error getting gym name for gym ${gymId}:`, error); | ||||
|     return "Unknown Gym"; | ||||
|   } | ||||
| } | ||||
| } | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user