Changes Updated

This commit is contained in:
Sharon Dcruz 2025-08-18 19:07:45 +05:30
parent f3fdd248c5
commit d15c04fc91

View File

@ -4,7 +4,7 @@ import * as admin from "firebase-admin";
const app = getAdmin();
const logger = getLogger();
const kTrainerRole = 'Trainer';
const kTrainerRole = "Trainer";
interface MembershipData {
id?: string;
@ -439,15 +439,33 @@ async function processExpiredMembership(
membershipData: MembershipData
): Promise<void> {
try {
await app.firestore().collection("memberships").doc(membershipId).update({
status: "EXPIRED",
updatedAt: admin.firestore.FieldValue.serverTimestamp(),
});
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),
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`);
await sendPlanExpiredNotification(membershipId, membershipData);
await sendTrainerNotifications(membershipId, membershipData, "expired");
} catch (error) {
logger.error(`Error processing membership ${membershipId}:`, error);
@ -461,8 +479,25 @@ async function processExpiringMembership(
try {
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");
} catch (error) {
logger.error(
@ -546,7 +581,6 @@ async function sendTrainerNotifications(
try {
const trainerName = await getTrainerName(assignment.trainerId);
const trainerUserId = await getTrainerUserId(assignment.trainerId);
const notifType =
notificationType === "expired"