notification-bug-fix #105
@ -474,16 +474,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 +547,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 +578,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(),
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user