notification-bug-fix (#88)
Some checks failed
Deploy FitLien services to Dev / Deploy to Dev (push) Failing after 1m44s

Reviewed-on: #88
Reviewed-by: Allen T J <allentj@cosq.net>
Co-authored-by: Sharon Dcruz <sharondcruz@cosq.net>
Co-committed-by: Sharon Dcruz <sharondcruz@cosq.net>
This commit is contained in:
Sharon Dcruz 2025-08-19 04:30:40 +00:00 committed by Allen T J
parent 7259e67833
commit 5ff7b8bb84
2 changed files with 97 additions and 28 deletions

View File

@ -56,34 +56,7 @@
}
]
},
{
"collectionGroup": "gyms",
"queryScope": "COLLECTION_GROUP",
"fields": [
{
"fieldPath": "userId",
"order": "ASCENDING"
},
{
"fieldPath": "createdAt",
"order": "ASCENDING"
}
]
},
{
"collectionGroup": "gyms",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "userId",
"order": "ASCENDING"
},
{
"fieldPath": "name",
"order": "ASCENDING"
}
]
},
{
"collectionGroup": "memberships",
"queryScope": "COLLECTION",

View File

@ -53,6 +53,8 @@ export const checkExpiredMemberships = onSchedule(
logger.info("Starting scheduled membership expiry check...");
try {
await updateDaysUntilExpiryForAllMemberships();
const expiredMemberships = await findExpiredMemberships();
const expiringMemberships = await findMembershipsExpiringIn10Days();
@ -377,6 +379,100 @@ function calculateRenewalDateFromPayment(
return renewalDate;
}
async function updateDaysUntilExpiryForAllMemberships(): Promise<void> {
try {
logger.info("Starting to update daysUntilExpiry for all active memberships...");
const snapshot = await app
.firestore()
.collection("memberships")
.where("status", "==", "ACTIVE")
.get();
const batchSize = 10;
const docs = snapshot.docs;
let updatedCount = 0;
for (let i = 0; i < docs.length; i += batchSize) {
const batch = docs.slice(i, i + batchSize);
const batchResults = await Promise.allSettled(
batch.map(async (doc) => {
const data = doc.data() as MembershipData;
const daysUntilExpiry = await calculateDaysUntilExpiry(doc.id, data);
if (daysUntilExpiry !== null) {
await app
.firestore()
.collection("memberships")
.doc(doc.id)
.update({
daysUntilExpiry: daysUntilExpiry,
updatedAt: admin.firestore.FieldValue.serverTimestamp(),
});
return doc.id;
}
return null;
})
);
batchResults.forEach((result) => {
if (result.status === "fulfilled" && result.value) {
updatedCount++;
}
});
}
logger.info(`Updated daysUntilExpiry for ${updatedCount} memberships`);
} catch (error) {
logger.error("Error updating daysUntilExpiry for memberships:", error);
throw error;
}
}
async function calculateDaysUntilExpiry(
membershipId: string,
data: MembershipData
): Promise<number | null> {
try {
if (!data.subscription || !data.subscription.frequency) {
logger.warn(
`Skipping expiry calculation for membership ${membershipId} with missing subscription data.`
);
return null;
}
const payments = await getPaymentsForMembership(membershipId);
if (payments.length === 0) {
logger.warn(
`No payments found for membership ${membershipId}, cannot determine expiry`
);
return null;
}
const latestPayment = payments[0];
const startDate = latestPayment.dateTimestamp;
const expiryDate = calculateExpiryDate(
startDate,
data.subscription.frequency
);
const now = new Date();
const timeDiff = expiryDate.getTime() - now.getTime();
const daysUntilExpiry = Math.ceil(timeDiff / (1000 * 3600 * 24));
return daysUntilExpiry;
} catch (error) {
logger.error(
`Error calculating daysUntilExpiry for membership ${membershipId}:`,
error
);
return null;
}
}
async function getTrainerAssignmentsForMembership(
membershipId: string
): Promise<PersonalTrainerAssign[]> {