diff --git a/functions/src/notifications/processNotification.ts b/functions/src/notifications/processNotification.ts index ada5636..0f80d70 100644 --- a/functions/src/notifications/processNotification.ts +++ b/functions/src/notifications/processNotification.ts @@ -9,13 +9,14 @@ const logger = getLogger(); interface NotificationData { senderId?: string; recipientId?: string; + ownerId?: string; + type?: string; notificationSent?: boolean; userId?: string; clientId?: string; invitorId?: string; phoneNumber?: string; message?: string; - type?: string; status?: string; gymName?: string; trainerName?: string; @@ -24,6 +25,17 @@ interface NotificationData { name?: string; clientEmail?: string; invitationId?: string; + gymId?: string; + trainerId?: string; + timeSlots?: Array<{[key: string]: any}>; + timestamp?: any; + read?: boolean; + title?: string; + updatedBy?: string; + oldTimeSlot?: string; + newTimeSlot?: string; + formattedDate?: string; + logTime?: string; [key: string]: any; } @@ -97,6 +109,10 @@ async function getUserAndFCMToken( userId = notification.recipientId; fcmToken = await getFCMTokenFromUserDoc(userId); logger.info(`Using recipientId: ${userId}`); + } else if (notification.ownerId) { + userId = notification.ownerId; + fcmToken = await getFCMTokenFromUserDoc(userId); + logger.info(`Using ownerId: ${userId}`); } else if (notification.userId) { userId = notification.userId; fcmToken = await getFCMTokenFromUserDoc(userId); @@ -163,108 +179,122 @@ function prepareNotificationMessage( notification: NotificationData, fcmToken: string ): admin.messaging.Message { - let title = "New Notification"; + let title = notification.title || "New Notification"; let body = notification.message || "You have a new notification"; let data: Record = { type: notification.type || "general", notificationId: "notification_" + Date.now(), }; + if (notification.senderId) data.senderId = notification.senderId; + if (notification.recipientId) data.recipientId = notification.recipientId; + if (notification.ownerId) data.ownerId = notification.ownerId; + if (notification.userId) data.userId = notification.userId; + if (notification.clientId) data.clientId = notification.clientId; + if (notification.gymId) data.gymId = notification.gymId; + if (notification.trainerId) data.trainerId = notification.trainerId; + if (notification.membershipId) data.membershipId = notification.membershipId; + if (notification.invitationId) data.invitationId = notification.invitationId; + if (notification.phoneNumber) data.phoneNumber = notification.phoneNumber; + if (notification.gymName) data.gymName = notification.gymName; + if (notification.trainerName) data.trainerName = notification.trainerName; + if (notification.subscriptionName) data.subscriptionName = notification.subscriptionName; + if (notification.name) data.name = notification.name; + if (notification.clientEmail) data.clientEmail = notification.clientEmail; + if (notification.status) data.status = notification.status; + if (notification.updatedBy) data.updatedBy = notification.updatedBy; + if (notification.oldTimeSlot) data.oldTimeSlot = notification.oldTimeSlot; + if (notification.newTimeSlot) data.newTimeSlot = notification.newTimeSlot; + if (notification.formattedDate) data.formattedDate = notification.formattedDate; + if (notification.logTime) data.logTime = notification.logTime; + if (notification.timeSlots) data.timeSlots = JSON.stringify(notification.timeSlots); + switch (notification.type) { case "trainer_response": - title = - notification.status === "ACCEPTED" + title = notification.title || + (notification.status === "ACCEPTED" ? "Trainer Request Accepted" - : "Trainer Request Update"; + : "Trainer Request Update"); body = notification.message || `${ notification.trainerName } has ${notification.status?.toLowerCase()} your request`; - data.trainerName = notification.trainerName || ""; - data.status = notification.status || ""; break; case "trainer_assignment": - title = "New Client Assignment"; + title = notification.title || "New Client Assignment"; body = notification.message || `You have been assigned to ${notification.name}`; - data.clientName = notification.name || ""; - data.membershipId = notification.membershipId || ""; break; case "trainer_assigned_to_client": - title = "Trainer Assigned"; + title = notification.title || "Trainer Assigned"; body = notification.message || `${notification.trainerName} has been assigned as your trainer`; - data.trainerName = notification.trainerName || ""; - data.membershipId = notification.membershipId || ""; break; case "trainer_update_owner": - title = "Trainer Schedule Update"; + title = notification.title || "Trainer Schedule Update"; body = notification.message || "A trainer has updated their schedule"; - data.membershipId = notification.membershipId || ""; break; case "trainer_update_client": - title = "Schedule Update"; + title = notification.title || "Schedule Update"; body = notification.message || "Your training schedule has been updated"; - data.membershipId = notification.membershipId || ""; break; case "plan_renewal": - title = "Plan Renewal"; + title = notification.title || "Plan Renewal"; body = notification.message || `Plan ${notification.subscriptionName} has been renewed`; - data.planName = notification.subscriptionName || ""; - data.membershipId = notification.membershipId || ""; break; case "plan_assigned": - title = "New Plan Assigned"; + title = notification.title || "New Plan Assigned"; body = notification.message || `You have been assigned ${notification.subscriptionName} at ${notification.gymName}`; - data.planName = notification.subscriptionName || ""; - data.gymName = notification.gymName || ""; - data.membershipId = notification.membershipId || ""; break; case "schedule_update": - title = "Schedule Update"; + title = notification.title || "Schedule Update"; body = notification.message || "Your training schedule has been updated"; - data.gymName = notification.gymName || ""; + if (notification.oldTimeSlot && notification.newTimeSlot) { + body += ` from ${notification.oldTimeSlot} to ${notification.newTimeSlot}`; + if (notification.formattedDate) { + body += ` on ${notification.formattedDate}`; + } + } break; case "attendance_dispute": - title = "Attendance Dispute"; + title = notification.title || "Attendance Dispute"; body = notification.message || `${notification.name} has disputed an attendance record`; - data.disputedBy = notification.name || ""; - data.membershipId = notification.membershipId || ""; + if (notification.logTime) { + body += ` for ${notification.logTime}`; + } break; case "day_pass_entry": const isAccepted = notification.status === "ACCEPTED"; - title = isAccepted ? "Day Pass Approved" : "Day Pass Denied"; + title = notification.title || (isAccepted ? "Day Pass Approved" : "Day Pass Denied"); body = notification.message || (isAccepted ? "Your day pass has been approved" : "Your day pass has been denied"); - data.gymName = notification.gymName || ""; - data.status = notification.status || ""; break; case "client_invitations": if (notification.userId || notification.invitorId) { const isAccept = notification.status === "ACCEPTED"; - title = isAccept ? "Invitation Accepted" : "Invitation Rejected"; + title = notification.title || (isAccept ? "Invitation Accepted" : "Invitation Rejected"); body = notification.message || (isAccept @@ -272,26 +302,22 @@ function prepareNotificationMessage( : `The invitation for ${notification.subscriptionName} you shared with ${notification.name} has been rejected`); } else if (notification.phoneNumber) { const invitationStatus = getInvitationStatus(notification.status); - title = getInvitationTitle(invitationStatus); + title = notification.title || getInvitationTitle(invitationStatus); body = notification.message || getInvitationBody(invitationStatus, notification.name); data.status = invitationStatus; } - data.gymName = notification.gymName || ""; - data.clientEmail = notification.clientEmail || ""; - data.clientName = notification.name || ""; - data.invitationId = notification.invitationId || ""; - data.subscriptionName = notification.subscriptionName || ""; break; default: logger.info( `Using default handling for notification type: ${notification.type}` ); - title = notification.type - ? `${notification.type.replace("_", " ").toUpperCase()}` - : "Notification"; + title = notification.title || + (notification.type + ? `${notification.type.replace("_", " ").toUpperCase()}` + : "Notification"); break; } @@ -391,4 +417,4 @@ async function updateNotificationWithError( } catch (updateError) { logger.error(`Error updating notification with error: ${updateError}`); } -} +} \ No newline at end of file