notification-bug-fix #114

Merged
dhanshas merged 81 commits from notification-bug-fix into dev 2025-09-11 08:47:47 +00:00
Showing only changes of commit d15c04fc91 - Show all commits

View File

@ -4,7 +4,7 @@ import * as admin from "firebase-admin";
const app = getAdmin(); const app = getAdmin();
const logger = getLogger(); const logger = getLogger();
const kTrainerRole = 'Trainer'; const kTrainerRole = "Trainer";
interface MembershipData { interface MembershipData {
id?: string; id?: string;
@ -439,15 +439,33 @@ async function processExpiredMembership(
membershipData: MembershipData membershipData: MembershipData
): Promise<void> { ): Promise<void> {
try { try {
await app.firestore().collection("memberships").doc(membershipId).update({ const payments = await getPaymentsForMembership(membershipId);
status: "EXPIRED", if (payments.length > 0) {
updatedAt: admin.firestore.FieldValue.serverTimestamp(), const latestPayment = payments[0];
}); const expiryDate = calculateExpiryDate(
latestPayment.dateTimestamp,
membershipData.subscription?.frequency || "monthly"
);
await app
.firestore()
.collection("memberships")
.doc(membershipId)
.update({
expirationDate: admin.firestore.Timestamp.fromDate(expiryDate),
status: "EXPIRED",
updatedAt: admin.firestore.FieldValue.serverTimestamp(),
});
} else {
await app.firestore().collection("memberships").doc(membershipId).update({
status: "EXPIRED",
updatedAt: admin.firestore.FieldValue.serverTimestamp(),
});
}
logger.info(`Marked membership ${membershipId} as EXPIRED`); logger.info(`Marked membership ${membershipId} as EXPIRED`);
await sendPlanExpiredNotification(membershipId, membershipData); await sendPlanExpiredNotification(membershipId, membershipData);
await sendTrainerNotifications(membershipId, membershipData, "expired"); await sendTrainerNotifications(membershipId, membershipData, "expired");
} catch (error) { } catch (error) {
logger.error(`Error processing membership ${membershipId}:`, error); logger.error(`Error processing membership ${membershipId}:`, error);
@ -461,8 +479,25 @@ async function processExpiringMembership(
try { try {
logger.info(`Processing expiring membership ${membershipId}`); logger.info(`Processing expiring membership ${membershipId}`);
await sendPlanExpiringNotification(membershipId, membershipData); const payments = await getPaymentsForMembership(membershipId);
if (payments.length > 0) {
const latestPayment = payments[0];
const expiryDate = calculateExpiryDate(
latestPayment.dateTimestamp,
membershipData.subscription?.frequency || "monthly"
);
await app
.firestore()
.collection("memberships")
.doc(membershipId)
.update({
expirationDate: admin.firestore.Timestamp.fromDate(expiryDate),
updatedAt: admin.firestore.FieldValue.serverTimestamp(),
});
}
await sendPlanExpiringNotification(membershipId, membershipData);
await sendTrainerNotifications(membershipId, membershipData, "expiring"); await sendTrainerNotifications(membershipId, membershipData, "expiring");
} catch (error) { } catch (error) {
logger.error( logger.error(
@ -546,7 +581,6 @@ async function sendTrainerNotifications(
try { try {
const trainerName = await getTrainerName(assignment.trainerId); const trainerName = await getTrainerName(assignment.trainerId);
const trainerUserId = await getTrainerUserId(assignment.trainerId); const trainerUserId = await getTrainerUserId(assignment.trainerId);
const notifType = const notifType =
notificationType === "expired" notificationType === "expired"