notification-bug-fix (#85)
All checks were successful
Deploy FitLien services to Dev / Deploy to Dev (push) Successful in 3m38s
All checks were successful
Deploy FitLien services to Dev / Deploy to Dev (push) Successful in 3m38s
Reviewed-on: #85 Reviewed-by: Dhansh A S <dhanshas@cosq.net> Co-authored-by: Sharon Dcruz <sharondcruz@cosq.net> Co-committed-by: Sharon Dcruz <sharondcruz@cosq.net>
This commit is contained in:
parent
b66f1603cc
commit
c2914b16bb
@ -406,14 +406,12 @@ async function getTrainerName(trainerId: string): Promise<string> {
|
|||||||
.collection("trainer_profiles")
|
.collection("trainer_profiles")
|
||||||
.doc(trainerId)
|
.doc(trainerId)
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
if (!doc.exists) {
|
if (!doc.exists) {
|
||||||
const userDoc = await app
|
const userDoc = await app
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection("users")
|
.collection("users")
|
||||||
.doc(trainerId)
|
.doc(trainerId)
|
||||||
.get();
|
.get();
|
||||||
|
|
||||||
if (userDoc.exists) {
|
if (userDoc.exists) {
|
||||||
const userData = userDoc.data();
|
const userData = userDoc.data();
|
||||||
return userData?.name || userData?.displayName || "Unknown Trainer";
|
return userData?.name || userData?.displayName || "Unknown Trainer";
|
||||||
@ -428,7 +426,6 @@ async function getTrainerName(trainerId: string): Promise<string> {
|
|||||||
const lastName = fields["last-name"] || "";
|
const lastName = fields["last-name"] || "";
|
||||||
return `${firstName} ${lastName}`.trim() || "Unknown Trainer";
|
return `${firstName} ${lastName}`.trim() || "Unknown Trainer";
|
||||||
}
|
}
|
||||||
|
|
||||||
return data?.name || data?.displayName || "Unknown Trainer";
|
return data?.name || data?.displayName || "Unknown Trainer";
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(`Error getting trainer name for ${trainerId}:`, error);
|
logger.error(`Error getting trainer name for ${trainerId}:`, error);
|
||||||
@ -447,9 +444,9 @@ async function processExpiredMembership(
|
|||||||
});
|
});
|
||||||
|
|
||||||
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);
|
||||||
@ -462,9 +459,9 @@ async function processExpiringMembership(
|
|||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
logger.info(`Processing expiring membership ${membershipId}`);
|
logger.info(`Processing expiring membership ${membershipId}`);
|
||||||
|
|
||||||
await sendPlanExpiringNotification(membershipId, membershipData);
|
await sendPlanExpiringNotification(membershipId, membershipData);
|
||||||
|
|
||||||
await sendTrainerNotifications(membershipId, membershipData, "expiring");
|
await sendTrainerNotifications(membershipId, membershipData, "expiring");
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logger.error(
|
logger.error(
|
||||||
@ -474,16 +471,44 @@ async function processExpiringMembership(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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(
|
async function sendTrainerNotifications(
|
||||||
membershipId: string,
|
membershipId: string,
|
||||||
membershipData: MembershipData,
|
membershipData: MembershipData,
|
||||||
notificationType: "expired" | "expiring"
|
notificationType: "expired" | "expiring"
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const trainerAssignments = await getTrainerAssignmentsForMembership(membershipId);
|
const trainerAssignments = await getTrainerAssignmentsForMembership(
|
||||||
|
membershipId
|
||||||
|
);
|
||||||
|
|
||||||
if (trainerAssignments.length === 0) {
|
if (trainerAssignments.length === 0) {
|
||||||
logger.info(`No trainer assignments found for membership ${membershipId}`);
|
logger.info(
|
||||||
|
`No trainer assignments found for membership ${membershipId}`
|
||||||
|
);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -519,13 +544,17 @@ async function sendTrainerNotifications(
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const trainerName = await getTrainerName(assignment.trainerId);
|
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 notifType =
|
||||||
|
notificationType === "expired"
|
||||||
|
? "trainer_client_plan_expired"
|
||||||
|
: "trainer_client_plan_expiring";
|
||||||
const existing = await app
|
const existing = await app
|
||||||
.firestore()
|
.firestore()
|
||||||
.collection("notifications")
|
.collection("notifications")
|
||||||
.where("type", "==", notifType)
|
.where("type", "==", notifType)
|
||||||
.where("recipientId", "==", assignment.trainerId)
|
.where("recipientId", "==", trainerUserId)
|
||||||
.where("data.membershipId", "==", membershipId)
|
.where("data.membershipId", "==", membershipId)
|
||||||
.where(
|
.where(
|
||||||
"data.expiryDate",
|
"data.expiryDate",
|
||||||
@ -546,7 +575,7 @@ async function sendTrainerNotifications(
|
|||||||
|
|
||||||
const notificationData: any = {
|
const notificationData: any = {
|
||||||
senderId: "system",
|
senderId: "system",
|
||||||
recipientId: assignment.trainerId,
|
recipientId: trainerUserId,
|
||||||
type: notifType,
|
type: notifType,
|
||||||
notificationSent: false,
|
notificationSent: false,
|
||||||
timestamp: admin.firestore.FieldValue.serverTimestamp(),
|
timestamp: admin.firestore.FieldValue.serverTimestamp(),
|
||||||
@ -799,4 +828,4 @@ async function getGymName(gymId: string): Promise<string> {
|
|||||||
logger.error(`Error getting gym name for gym ${gymId}:`, error);
|
logger.error(`Error getting gym name for gym ${gymId}:`, error);
|
||||||
return "Unknown Gym";
|
return "Unknown Gym";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user