notification-bug-fix #111
| @ -63,10 +63,18 @@ export const checkExpiredMemberships = onSchedule( | |||||||
|         expiringMemberships.map((m) => processExpiringMembership(m.id, m.data)) |         expiringMemberships.map((m) => processExpiringMembership(m.id, m.data)) | ||||||
|       ); |       ); | ||||||
| 
 | 
 | ||||||
|       const expiredSuccessful = expiredResults.filter((r) => r.status === "fulfilled").length; |       const expiredSuccessful = expiredResults.filter( | ||||||
|       const expiredFailed = expiredResults.filter((r) => r.status === "rejected").length; |         (r) => r.status === "fulfilled" | ||||||
|       const expiringSuccessful = expiringResults.filter((r) => r.status === "fulfilled").length; |       ).length; | ||||||
|       const expiringFailed = expiringResults.filter((r) => r.status === "rejected").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( |       logger.info( | ||||||
|         `Completed processing. Expired - Success: ${expiredSuccessful}, Failed: ${expiredFailed}. Expiring - Success: ${expiringSuccessful}, Failed: ${expiringFailed}` |         `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( |       const batchResults = await Promise.allSettled( | ||||||
|         batch.map(async (doc) => { |         batch.map(async (doc) => { | ||||||
|           const data = doc.data() as MembershipData; |           const data = doc.data() as MembershipData; | ||||||
|           const isExpiringIn10Days = await checkIfMembershipExpiringIn10Days(doc.id, data); |           const isExpiringIn10Days = await checkIfMembershipExpiringIn10Days( | ||||||
|  |             doc.id, | ||||||
|  |             data | ||||||
|  |           ); | ||||||
|           if (isExpiringIn10Days) { |           if (isExpiringIn10Days) { | ||||||
|             return { id: doc.id, data }; |             return { id: doc.id, data }; | ||||||
|           } |           } | ||||||
| @ -381,7 +392,10 @@ async function processExpiringMembership( | |||||||
|     logger.info(`Processing expiring membership ${membershipId}`); |     logger.info(`Processing expiring membership ${membershipId}`); | ||||||
|     await sendPlanExpiringNotification(membershipId, membershipData); |     await sendPlanExpiringNotification(membershipId, membershipData); | ||||||
|   } catch (error) { |   } 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 gymOwnerId = await getGymOwnerId(membershipData.gymId); | ||||||
|     const gymName = await getGymName(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 expiryDate: Date | undefined; | ||||||
|     let formattedDate = "Unknown Date"; |     let formattedDate = "Unknown Date"; | ||||||
| 
 | 
 | ||||||
| @ -422,7 +423,26 @@ async function sendPlanExpiredNotification( | |||||||
|         month: "long", |         month: "long", | ||||||
|         day: "numeric", |         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 |     await app | ||||||
| @ -466,19 +486,6 @@ async function sendPlanExpiringNotification( | |||||||
|     const gymOwnerId = await getGymOwnerId(membershipData.gymId); |     const gymOwnerId = await getGymOwnerId(membershipData.gymId); | ||||||
|     const gymName = await getGymName(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 expiryDate: Date | undefined; | ||||||
|     let formattedDate = "Unknown Date"; |     let formattedDate = "Unknown Date"; | ||||||
|     let daysUntilExpiry = 10; |     let daysUntilExpiry = 10; | ||||||
| @ -501,6 +508,28 @@ async function sendPlanExpiringNotification( | |||||||
|       daysUntilExpiry = Math.ceil(timeDiff / (1000 * 3600 * 24)); |       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 |     await app | ||||||
|       .firestore() |       .firestore() | ||||||
|       .collection("notifications") |       .collection("notifications") | ||||||
| @ -530,7 +559,10 @@ async function sendPlanExpiringNotification( | |||||||
|       `Expiring notification sent for membership ${membershipId} (expires on ${formattedDate}, ${daysUntilExpiry} days remaining)` |       `Expiring notification sent for membership ${membershipId} (expires on ${formattedDate}, ${daysUntilExpiry} days remaining)` | ||||||
|     ); |     ); | ||||||
|   } catch (error) { |   } catch (error) { | ||||||
|     logger.error(`Error sending expiring notification for ${membershipId}:`, error); |     logger.error( | ||||||
|  |       `Error sending expiring notification for ${membershipId}:`, | ||||||
|  |       error | ||||||
|  |     ); | ||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user