notification-issue #73
| @ -9,13 +9,14 @@ const logger = getLogger(); | |||||||
| interface NotificationData { | interface NotificationData { | ||||||
|   senderId?: string; |   senderId?: string; | ||||||
|   recipientId?: string; |   recipientId?: string; | ||||||
|  |   ownerId?: string; | ||||||
|  |   type?: string; | ||||||
|   notificationSent?: boolean; |   notificationSent?: boolean; | ||||||
|   userId?: string; |   userId?: string; | ||||||
|   clientId?: string; |   clientId?: string; | ||||||
|   invitorId?: string; |   invitorId?: string; | ||||||
|   phoneNumber?: string; |   phoneNumber?: string; | ||||||
|   message?: string; |   message?: string; | ||||||
|   type?: string; |  | ||||||
|   status?: string; |   status?: string; | ||||||
|   gymName?: string; |   gymName?: string; | ||||||
|   trainerName?: string; |   trainerName?: string; | ||||||
| @ -24,6 +25,17 @@ interface NotificationData { | |||||||
|   name?: string; |   name?: string; | ||||||
|   clientEmail?: string; |   clientEmail?: string; | ||||||
|   invitationId?: 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; |   [key: string]: any; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| @ -97,6 +109,10 @@ async function getUserAndFCMToken( | |||||||
|     userId = notification.recipientId; |     userId = notification.recipientId; | ||||||
|     fcmToken = await getFCMTokenFromUserDoc(userId); |     fcmToken = await getFCMTokenFromUserDoc(userId); | ||||||
|     logger.info(`Using recipientId: ${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) { |   } else if (notification.userId) { | ||||||
|     userId = notification.userId; |     userId = notification.userId; | ||||||
|     fcmToken = await getFCMTokenFromUserDoc(userId); |     fcmToken = await getFCMTokenFromUserDoc(userId); | ||||||
| @ -163,108 +179,122 @@ function prepareNotificationMessage( | |||||||
|   notification: NotificationData, |   notification: NotificationData, | ||||||
|   fcmToken: string |   fcmToken: string | ||||||
| ): admin.messaging.Message { | ): admin.messaging.Message { | ||||||
|   let title = "New Notification"; |   let title = notification.title || "New Notification"; | ||||||
|   let body = notification.message || "You have a new notification"; |   let body = notification.message || "You have a new notification"; | ||||||
|   let data: Record<string, string> = { |   let data: Record<string, string> = { | ||||||
|     type: notification.type || "general", |     type: notification.type || "general", | ||||||
|     notificationId: "notification_" + Date.now(), |     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) { |   switch (notification.type) { | ||||||
|     case "trainer_response": |     case "trainer_response": | ||||||
|       title = |       title = notification.title ||  | ||||||
|         notification.status === "ACCEPTED" |         (notification.status === "ACCEPTED" | ||||||
|           ? "Trainer Request Accepted" |           ? "Trainer Request Accepted" | ||||||
|           : "Trainer Request Update"; |           : "Trainer Request Update"); | ||||||
|       body = |       body = | ||||||
|         notification.message || |         notification.message || | ||||||
|         `${ |         `${ | ||||||
|           notification.trainerName |           notification.trainerName | ||||||
|         } has ${notification.status?.toLowerCase()} your request`;
 |         } has ${notification.status?.toLowerCase()} your request`;
 | ||||||
|       data.trainerName = notification.trainerName || ""; |  | ||||||
|       data.status = notification.status || ""; |  | ||||||
|       break; |       break; | ||||||
| 
 | 
 | ||||||
|     case "trainer_assignment": |     case "trainer_assignment": | ||||||
|       title = "New Client Assignment"; |       title = notification.title || "New Client Assignment"; | ||||||
|       body = |       body = | ||||||
|         notification.message || |         notification.message || | ||||||
|         `You have been assigned to ${notification.name}`; |         `You have been assigned to ${notification.name}`; | ||||||
|       data.clientName = notification.name || ""; |  | ||||||
|       data.membershipId = notification.membershipId || ""; |  | ||||||
|       break; |       break; | ||||||
| 
 | 
 | ||||||
|     case "trainer_assigned_to_client": |     case "trainer_assigned_to_client": | ||||||
|       title = "Trainer Assigned"; |       title = notification.title || "Trainer Assigned"; | ||||||
|       body = |       body = | ||||||
|         notification.message || |         notification.message || | ||||||
|         `${notification.trainerName} has been assigned as your trainer`; |         `${notification.trainerName} has been assigned as your trainer`; | ||||||
|       data.trainerName = notification.trainerName || ""; |  | ||||||
|       data.membershipId = notification.membershipId || ""; |  | ||||||
|       break; |       break; | ||||||
| 
 | 
 | ||||||
|     case "trainer_update_owner": |     case "trainer_update_owner": | ||||||
|       title = "Trainer Schedule Update"; |       title = notification.title || "Trainer Schedule Update"; | ||||||
|       body = notification.message || "A trainer has updated their schedule"; |       body = notification.message || "A trainer has updated their schedule"; | ||||||
|       data.membershipId = notification.membershipId || ""; |  | ||||||
|       break; |       break; | ||||||
| 
 | 
 | ||||||
|     case "trainer_update_client": |     case "trainer_update_client": | ||||||
|       title = "Schedule Update"; |       title = notification.title || "Schedule Update"; | ||||||
|       body = notification.message || "Your training schedule has been updated"; |       body = notification.message || "Your training schedule has been updated"; | ||||||
|       data.membershipId = notification.membershipId || ""; |  | ||||||
|       break; |       break; | ||||||
| 
 | 
 | ||||||
|     case "plan_renewal": |     case "plan_renewal": | ||||||
|       title = "Plan Renewal"; |       title = notification.title || "Plan Renewal"; | ||||||
|       body = |       body = | ||||||
|         notification.message || |         notification.message || | ||||||
|         `Plan ${notification.subscriptionName} has been renewed`; |         `Plan ${notification.subscriptionName} has been renewed`; | ||||||
|       data.planName = notification.subscriptionName || ""; |  | ||||||
|       data.membershipId = notification.membershipId || ""; |  | ||||||
|       break; |       break; | ||||||
| 
 | 
 | ||||||
|     case "plan_assigned": |     case "plan_assigned": | ||||||
|       title = "New Plan Assigned"; |       title = notification.title || "New Plan Assigned"; | ||||||
|       body = |       body = | ||||||
|         notification.message || |         notification.message || | ||||||
|         `You have been assigned ${notification.subscriptionName} at ${notification.gymName}`; |         `You have been assigned ${notification.subscriptionName} at ${notification.gymName}`; | ||||||
|       data.planName = notification.subscriptionName || ""; |  | ||||||
|       data.gymName = notification.gymName || ""; |  | ||||||
|       data.membershipId = notification.membershipId || ""; |  | ||||||
|       break; |       break; | ||||||
| 
 | 
 | ||||||
|     case "schedule_update": |     case "schedule_update": | ||||||
|       title = "Schedule Update"; |       title = notification.title || "Schedule Update"; | ||||||
|       body = notification.message || "Your training schedule has been updated"; |       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; |       break; | ||||||
| 
 | 
 | ||||||
|     case "attendance_dispute": |     case "attendance_dispute": | ||||||
|       title = "Attendance Dispute"; |       title = notification.title || "Attendance Dispute"; | ||||||
|       body = |       body = | ||||||
|         notification.message || |         notification.message || | ||||||
|         `${notification.name} has disputed an attendance record`; |         `${notification.name} has disputed an attendance record`; | ||||||
|       data.disputedBy = notification.name || ""; |       if (notification.logTime) { | ||||||
|       data.membershipId = notification.membershipId || ""; |         body += ` for ${notification.logTime}`; | ||||||
|  |       } | ||||||
|       break; |       break; | ||||||
| 
 | 
 | ||||||
|     case "day_pass_entry": |     case "day_pass_entry": | ||||||
|       const isAccepted = notification.status === "ACCEPTED"; |       const isAccepted = notification.status === "ACCEPTED"; | ||||||
|       title = isAccepted ? "Day Pass Approved" : "Day Pass Denied"; |       title = notification.title || (isAccepted ? "Day Pass Approved" : "Day Pass Denied"); | ||||||
|       body = |       body = | ||||||
|         notification.message || |         notification.message || | ||||||
|         (isAccepted |         (isAccepted | ||||||
|           ? "Your day pass has been approved" |           ? "Your day pass has been approved" | ||||||
|           : "Your day pass has been denied"); |           : "Your day pass has been denied"); | ||||||
|       data.gymName = notification.gymName || ""; |  | ||||||
|       data.status = notification.status || ""; |  | ||||||
|       break; |       break; | ||||||
| 
 | 
 | ||||||
|     case "client_invitations": |     case "client_invitations": | ||||||
|       if (notification.userId || notification.invitorId) { |       if (notification.userId || notification.invitorId) { | ||||||
|         const isAccept = notification.status === "ACCEPTED"; |         const isAccept = notification.status === "ACCEPTED"; | ||||||
|         title = isAccept ? "Invitation Accepted" : "Invitation Rejected"; |         title = notification.title || (isAccept ? "Invitation Accepted" : "Invitation Rejected"); | ||||||
|         body = |         body = | ||||||
|           notification.message || |           notification.message || | ||||||
|           (isAccept |           (isAccept | ||||||
| @ -272,26 +302,22 @@ function prepareNotificationMessage( | |||||||
|             : `The invitation for ${notification.subscriptionName} you shared with ${notification.name} has been rejected`); |             : `The invitation for ${notification.subscriptionName} you shared with ${notification.name} has been rejected`); | ||||||
|       } else if (notification.phoneNumber) { |       } else if (notification.phoneNumber) { | ||||||
|         const invitationStatus = getInvitationStatus(notification.status); |         const invitationStatus = getInvitationStatus(notification.status); | ||||||
|         title = getInvitationTitle(invitationStatus); |         title = notification.title || getInvitationTitle(invitationStatus); | ||||||
|         body = |         body = | ||||||
|           notification.message || |           notification.message || | ||||||
|           getInvitationBody(invitationStatus, notification.name); |           getInvitationBody(invitationStatus, notification.name); | ||||||
|         data.status = invitationStatus; |         data.status = invitationStatus; | ||||||
|       } |       } | ||||||
|       data.gymName = notification.gymName || ""; |  | ||||||
|       data.clientEmail = notification.clientEmail || ""; |  | ||||||
|       data.clientName = notification.name || ""; |  | ||||||
|       data.invitationId = notification.invitationId || ""; |  | ||||||
|       data.subscriptionName = notification.subscriptionName || ""; |  | ||||||
|       break; |       break; | ||||||
| 
 | 
 | ||||||
|     default: |     default: | ||||||
|       logger.info( |       logger.info( | ||||||
|         `Using default handling for notification type: ${notification.type}` |         `Using default handling for notification type: ${notification.type}` | ||||||
|       ); |       ); | ||||||
|       title = notification.type |       title = notification.title ||  | ||||||
|  |         (notification.type | ||||||
|           ? `${notification.type.replace("_", " ").toUpperCase()}` |           ? `${notification.type.replace("_", " ").toUpperCase()}` | ||||||
|         : "Notification"; |           : "Notification"); | ||||||
|       break; |       break; | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user