feature/fitlien-add-cors #18
@ -24,7 +24,7 @@ if (!admin.apps.length) {
|
||||
const corsHandler = cors({ origin: true });
|
||||
|
||||
export const sendEmailWithAttachment = onRequest({
|
||||
region: process.env.SERVICES_RGN
|
||||
region: '#{SERVICES_RGN}#'
|
||||
}, async (request: Request, response: express.Response) => {
|
||||
return corsHandler(request, response, async () => {
|
||||
try {
|
||||
@ -157,7 +157,7 @@ export const sendEmailMessage = onRequest({
|
||||
});
|
||||
|
||||
export const sendSMSMessage = onRequest({
|
||||
region: process.env.SERVICES_RGN
|
||||
region: '#{SERVICES_RGN}#'
|
||||
}, (request: Request, response: express.Response) => {
|
||||
return corsHandler(request, response, async () => {
|
||||
|
||||
@ -181,26 +181,26 @@ export const sendSMSMessage = onRequest({
|
||||
});
|
||||
|
||||
export const processNotificationOnCreate = onDocumentCreated({
|
||||
region: process.env.SERVICES_RGN,
|
||||
region: '#{SERVICES_RGN}#',
|
||||
document: 'notifications/{notificationId}'
|
||||
}, async (event) => {
|
||||
try {
|
||||
const notification = event.data?.data();
|
||||
const notificationId = event.params.notificationId;
|
||||
|
||||
|
||||
if (!notification) {
|
||||
logger.error(`No data found for notification ${notificationId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (notification.notificationSent === true) {
|
||||
logger.info(`Notification ${notificationId} already sent, skipping.`);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
let userId = null;
|
||||
let fcmToken = null;
|
||||
|
||||
|
||||
if (notification.userId) {
|
||||
userId = notification.userId;
|
||||
const userDoc = await admin.firestore().collection('users').doc(userId).get();
|
||||
@ -226,14 +226,14 @@ export const processNotificationOnCreate = onDocumentCreated({
|
||||
.where('phoneNumber', '==', notification.phoneNumber)
|
||||
.limit(1)
|
||||
.get();
|
||||
|
||||
|
||||
if (!userQuery.empty) {
|
||||
const userDoc = userQuery.docs[0];
|
||||
userId = userDoc.id;
|
||||
fcmToken = userDoc.data()?.fcmToken;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (!fcmToken) {
|
||||
logger.error(`FCM token not found for notification ${notificationId}`);
|
||||
await admin.firestore().collection('notifications').doc(notificationId).update({
|
||||
@ -242,35 +242,35 @@ export const processNotificationOnCreate = onDocumentCreated({
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
let title = 'New Notification';
|
||||
let body = notification.message || 'You have a new notification';
|
||||
let data: Record<string, string> = {
|
||||
type: notification.type,
|
||||
};
|
||||
|
||||
|
||||
switch (notification.type) {
|
||||
case 'day_pass_entry':
|
||||
const isAccepted = notification.status === 'ACCEPTED';
|
||||
title = isAccepted ? 'Day Pass Approved' : 'Day Pass Denied';
|
||||
body = notification.message || (isAccepted ?
|
||||
`Your day pass has been approved` :
|
||||
body = notification.message || (isAccepted ?
|
||||
`Your day pass has been approved` :
|
||||
`Your day pass has been denied`);
|
||||
data.gymName = notification.gymName || '';
|
||||
break;
|
||||
|
||||
|
||||
case 'trainer_assigned_to_client':
|
||||
title = 'Trainer Assigned';
|
||||
body = notification.message || `${notification.trainerName} has been assigned as your trainer`;
|
||||
data.trainerName = notification.trainerName || '';
|
||||
data.membershipId = notification.membershipId || '';
|
||||
break;
|
||||
|
||||
|
||||
case 'client_invitations':
|
||||
if (notification.userId || notification.invitorId) {
|
||||
const isAccept = notification.status === 'ACCEPTED';
|
||||
title = isAccept ? 'Invitation Accepted' : 'Invitation Rejected';
|
||||
body = notification.message || (isAccept ?
|
||||
body = notification.message || (isAccept ?
|
||||
`The invitation for ${notification.subscriptionName} you shared with ${notification.name} has been accepted` :
|
||||
`The invitation for ${notification.subscriptionName} you shared with ${notification.name} has been rejected`);
|
||||
} else if (notification.phoneNumber) {
|
||||
@ -278,17 +278,17 @@ export const processNotificationOnCreate = onDocumentCreated({
|
||||
if (notification.status === 'ACCEPTED') {
|
||||
invitationStatus = 'accepted';
|
||||
title = 'Invitation Accepted';
|
||||
body = notification.message ||
|
||||
body = notification.message ||
|
||||
`You have accepted the invitation from ${notification.name}`;
|
||||
} else if (notification.status === 'REJECTED') {
|
||||
invitationStatus = 'rejected';
|
||||
title = 'Invitation Rejected';
|
||||
body = notification.message ||
|
||||
body = notification.message ||
|
||||
`You have rejected the invitation from ${notification.name}`;
|
||||
} else if (notification.status === 'PENDING') {
|
||||
invitationStatus = 'pending';
|
||||
title = 'New Invitation';
|
||||
body = notification.message ||
|
||||
body = notification.message ||
|
||||
`You have a new invitation pending from ${notification.name}`;
|
||||
} else {
|
||||
invitationStatus = 'unknown';
|
||||
@ -297,19 +297,19 @@ export const processNotificationOnCreate = onDocumentCreated({
|
||||
}
|
||||
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}`);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
const message: admin.messaging.Message = {
|
||||
notification: {
|
||||
title: title,
|
||||
@ -337,18 +337,18 @@ export const processNotificationOnCreate = onDocumentCreated({
|
||||
},
|
||||
token: fcmToken,
|
||||
};
|
||||
|
||||
|
||||
try {
|
||||
const fcmResponse = await admin.messaging().send(message);
|
||||
logger.info(`FCM notification sent successfully: ${fcmResponse}`);
|
||||
|
||||
|
||||
await admin.firestore().collection('notifications').doc(notificationId).update({
|
||||
notificationSent: true,
|
||||
sentAt: admin.firestore.FieldValue?.serverTimestamp?.() || new Date()
|
||||
});
|
||||
} catch (error) {
|
||||
logger.error(`Error sending notification ${notificationId}:`, error);
|
||||
|
||||
|
||||
await admin.firestore().collection('notifications').doc(notificationId).update({
|
||||
notificationError: error instanceof Error ? error.message : String(error),
|
||||
updatedAt: admin.firestore.FieldValue?.serverTimestamp?.() || new Date()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user