Compare commits

..

No commits in common. "f6b1545cf690737b317021826c73dac0d7c3e20b" and "195262a6de429d97891099c7eb5af8b75b72cee9" have entirely different histories.

3 changed files with 46 additions and 328 deletions

View File

@ -166,20 +166,6 @@
}
]
},
{
"collectionGroup": "notifications",
"queryScope": "COLLECTION",
"fields": [
{
"fieldPath": "recipientId",
"order": "ASCENDING"
},
{
"fieldPath": "timestamp",
"order": "DESCENDING"
}
]
},
{
"collectionGroup": "workout_logs",
"queryScope": "COLLECTION",

View File

@ -4,7 +4,6 @@ import * as admin from "firebase-admin";
const app = getAdmin();
const logger = getLogger();
const kTrainerRole = 'Trainer';
interface MembershipData {
id?: string;
@ -34,15 +33,6 @@ interface PaymentData {
discount?: number;
}
interface PersonalTrainerAssign {
id: string;
ownerId: string;
trainerId?: string;
clientId: string;
membershipId: string;
gymId: string;
}
export const checkExpiredMemberships = onSchedule(
{
schedule: "*/5 * * * *",
@ -73,18 +63,10 @@ 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}`
@ -157,10 +139,7 @@ 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 };
}
@ -262,11 +241,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) {
@ -377,63 +356,6 @@ function calculateRenewalDateFromPayment(
return renewalDate;
}
async function getTrainerAssignmentsForMembership(
membershipId: string
): Promise<PersonalTrainerAssign[]> {
try {
const querySnapshot = await app
.firestore()
.collection("personal_trainer_assignments")
.where("membershipId", "==", membershipId)
.get();
return querySnapshot.docs.map((doc) => ({
id: doc.id,
...doc.data(),
})) as PersonalTrainerAssign[];
} catch (error) {
logger.error(
`Error getting trainer assignments for membership ${membershipId}:`,
error
);
return [];
}
}
async function getTrainerName(trainerId: string): Promise<string> {
try {
const doc = await app
.firestore()
.collection("trainer_profiles")
.doc(trainerId)
.get();
if (!doc.exists) {
const userDoc = await app
.firestore()
.collection("users")
.doc(trainerId)
.get();
if (userDoc.exists) {
const userData = userDoc.data();
return userData?.name || userData?.displayName || "Unknown Trainer";
}
return "Unknown Trainer";
}
const data = doc.data();
const fields = data?.fields;
if (fields) {
const firstName = fields["first-name"] || "";
const lastName = fields["last-name"] || "";
return `${firstName} ${lastName}`.trim() || "Unknown Trainer";
}
return data?.name || data?.displayName || "Unknown Trainer";
} catch (error) {
logger.error(`Error getting trainer name for ${trainerId}:`, error);
return "Unknown Trainer";
}
}
async function processExpiredMembership(
membershipId: string,
membershipData: MembershipData
@ -445,10 +367,7 @@ async function processExpiredMembership(
});
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);
}
@ -460,164 +379,9 @@ async function processExpiringMembership(
): Promise<void> {
try {
logger.info(`Processing expiring membership ${membershipId}`);
await sendPlanExpiringNotification(membershipId, membershipData);
await sendTrainerNotifications(membershipId, membershipData, "expiring");
} catch (error) {
logger.error(
`Error processing expiring membership ${membershipId}:`,
error
);
}
}
async function getTrainerUserId(trainerId: string): Promise<string> {
try {
const trainerDoc = await app
.firestore()
.collection("trainer_profiles")
.doc(trainerId)
.get();
if (!trainerDoc.exists) {
throw new Error(`Trainer profile not found for ID: ${trainerId}`);
}
const trainerData = trainerDoc.data();
if (!trainerData?.userId) {
throw new Error(`userId not found in trainer profile: ${trainerId}`);
}
return trainerData.userId;
} catch (error) {
logger.error(`Error getting userId for trainer ${trainerId}:`, error);
return trainerId;
}
}
async function sendTrainerNotifications(
membershipId: string,
membershipData: MembershipData,
notificationType: "expired" | "expiring"
): Promise<void> {
try {
const trainerAssignments = await getTrainerAssignmentsForMembership(
membershipId
);
if (trainerAssignments.length === 0) {
logger.info(
`No trainer assignments found for membership ${membershipId}`
);
return;
}
const clientName = await getClientName(membershipId, membershipData.userId);
const gymName = await getGymName(membershipData.gymId);
let expiryDate: Date | undefined;
let formattedDate = "Unknown Date";
let daysUntilExpiry = 0;
const payments = await getPaymentsForMembership(membershipId);
if (payments.length > 0) {
const latestPayment = payments[0];
expiryDate = calculateRenewalDateFromPayment(
membershipData.subscription,
latestPayment.dateTimestamp
);
formattedDate = expiryDate.toLocaleDateString("en-US", {
year: "numeric",
month: "long",
day: "numeric",
});
if (notificationType === "expiring") {
const now = new Date();
const timeDiff = expiryDate.getTime() - now.getTime();
daysUntilExpiry = Math.ceil(timeDiff / (1000 * 3600 * 24));
}
}
for (const assignment of trainerAssignments) {
if (!assignment.trainerId) continue;
try {
const trainerName = await getTrainerName(assignment.trainerId);
const trainerUserId = await getTrainerUserId(assignment.trainerId);
const notifType =
notificationType === "expired"
? "trainer_client_plan_expired"
: "trainer_client_plan_expiring";
const existing = await app
.firestore()
.collection("notifications")
.where("type", "==", notifType)
.where("recipientId", "==", trainerUserId)
.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(
`${notificationType} notification already sent to trainer ${assignment.trainerId} for membership ${membershipId}, skipping...`
);
continue;
}
const notificationData: any = {
senderId: "system",
recipientId: trainerUserId,
type: notifType,
notificationSent: false,
timestamp: admin.firestore.FieldValue.serverTimestamp(),
read: false,
readBy: [],
data: {
planName: membershipData.subscription?.name || "Unknown Plan",
clientName,
membershipId,
gymName,
assignmentId: assignment.id,
formattedExpiryDate: formattedDate,
role: kTrainerRole,
expiryDate: expiryDate
? admin.firestore.Timestamp.fromDate(expiryDate)
: admin.firestore.Timestamp.fromDate(new Date()),
},
};
if (notificationType === "expiring") {
notificationData.data.daysUntilExpiry = daysUntilExpiry;
}
await app.firestore().collection("notifications").add(notificationData);
logger.info(
`${notificationType} notification sent to trainer ${assignment.trainerId} (${trainerName}) for client ${clientName}'s membership ${membershipId}`
);
} catch (trainerError) {
logger.error(
`Error sending notification to trainer ${assignment.trainerId} for membership ${membershipId}:`,
trainerError
);
}
}
} catch (error) {
logger.error(
`Error sending trainer notifications for membership ${membershipId}:`,
error
);
logger.error(`Error processing expiring membership ${membershipId}:`, error);
}
}
@ -630,6 +394,19 @@ 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";
@ -645,26 +422,7 @@ 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
@ -677,13 +435,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)
@ -708,6 +466,19 @@ 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;
@ -724,34 +495,12 @@ 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")
@ -762,13 +511,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)
@ -781,10 +530,7 @@ 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);
}
}
@ -831,4 +577,4 @@ async function getGymName(gymId: string): Promise<string> {
logger.error(`Error getting gym name for gym ${gymId}:`, error);
return "Unknown Gym";
}
}
}

View File

@ -181,8 +181,8 @@ function prepareNotificationMessage(
title =
notification.data?.title ||
(notification.data?.status === "accepted"
? "Trainer Invitation Accepted"
: "Trainer Invitation Update");
? "Trainer Request Accepted"
: "Trainer Request Update");
body =
notification.data?.message ||
`${
@ -240,28 +240,14 @@ function prepareNotificationMessage(
title = notification.data?.title || "Plan Expired";
body =
notification.data?.message ||
`${notification.data?.clientName}'s membership for ${notification.data?.planName} has expired.`;
`${notification.data?.clientName}'s membership subscription for ${notification.data?.planName} has expired.`;
break;
case "plan_expiring_soon":
title = notification.data?.title || "Plan Expiring Soon";
body =
notification.data?.message ||
`${notification.data?.clientName}'s membership for ${notification.data?.planName} will expire on ${notification.data?.formattedExpiryDate}.`;
break;
case "trainer_client_plan_expired":
title = notification.data?.title || "Client Plan Expired";
body =
notification.data?.message ||
`${notification.data?.clientName}'s membership for ${notification.data?.planName} has expired.`;
break;
case "trainer_client_plan_expiring":
title = notification.data?.title || "Client Plan Expiring Soon";
body =
notification.data?.message ||
`${notification.data?.clientName}'s membership for ${notification.data?.planName} will expire on ${notification.data?.formattedExpiryDate}.`;
`${notification.data?.clientName}'s membership subscription for ${notification.data?.planName} will expire on ${notification.data?.formattedExpiryDate}.`;
break;
case "schedule_update":