feature/fitlien-cashfree #11

Merged
dhanshas merged 7 commits from feature/fitlien-cashfree into dev 2025-04-08 14:09:57 +00:00
2 changed files with 144 additions and 506 deletions
Showing only changes of commit 10edbc9fcf - Show all commits

View File

@ -26,7 +26,7 @@
"port": 5001 "port": 5001
}, },
"firestore": { "firestore": {
"port": 8084 "port": 8079
}, },
"storage": { "storage": {
"port": 9199 "port": 9199

View File

@ -8,6 +8,7 @@ import * as path from 'path';
import * as fs from 'fs'; import * as fs from 'fs';
import * as https from 'https'; import * as https from 'https';
import axios from "axios"; import axios from "axios";
import { onDocumentCreated } from "firebase-functions/firestore";
const formData = require('form-data'); const formData = require('form-data');
const Mailgun = require('mailgun.js'); const Mailgun = require('mailgun.js');
@ -275,279 +276,122 @@ export const verifyCashfreePayment = onRequest({
} }
}); });
export const sendFCMNotificationByType = onRequest({ export const processNotificationOnCreate = onDocumentCreated({
region: process.env.SERVICES_RGN region: process.env.SERVICES_RGN,
}, async (request: Request, response: express.Response) => { document: 'notifications/{notificationId}'
}, async (event) => {
try { try {
const notificationType = request.body.type || request.query.type; const notification = event.data?.data();
const userId = request.body.userId || request.body.clientId || request.query.userId || request.query.clientId || request.body.invitorId || request.query.invitorId; const notificationId = event.params.notificationId;
if (!notificationType) { if (!notification) {
response.status(400).json({ error: 'Notification type is required' }); logger.error(`No data found for notification ${notificationId}`);
return; return;
} }
if (!userId) { if (notification.notificationSent === true) {
response.status(400).json({ error: 'User ID is required' }); logger.info(`Notification ${notificationId} already sent, skipping.`);
return; return;
} }
const userDoc = await admin.firestore().collection('users').doc(userId).get(); let userId = null;
let fcmToken = null;
if (!userDoc.exists) { if (notification.userId) {
response.status(404).json({ error: `User not found for ID: ${userId}` }); userId = notification.userId;
return; const userDoc = await admin.firestore().collection('users').doc(userId).get();
if (userDoc.exists) {
fcmToken = userDoc.data()?.fcmToken;
}
} else if (notification.clientId) {
userId = notification.clientId;
const userDoc = await admin.firestore().collection('users').doc(userId).get();
if (userDoc.exists) {
fcmToken = userDoc.data()?.fcmToken;
}
} else if (notification.invitorId) {
userId = notification.invitorId;
const userDoc = await admin.firestore().collection('users').doc(userId).get();
if (userDoc.exists) {
fcmToken = userDoc.data()?.fcmToken;
}
} else if (notification.phoneNumber) {
const userQuery = await admin
.firestore()
.collection('users')
.where('phoneNumber', '==', notification.phoneNumber)
.limit(1)
.get();
if (!userQuery.empty) {
const userDoc = userQuery.docs[0];
userId = userDoc.id;
fcmToken = userDoc.data()?.fcmToken;
}
} }
const user = userDoc.data();
const fcmToken = user?.fcmToken;
if (!fcmToken) { if (!fcmToken) {
response.status(400).json({ error: `FCM token not found for user: ${userId}` }); logger.error(`FCM token not found for notification ${notificationId}`);
return; await admin.firestore().collection('notifications').doc(notificationId).update({
} notificationError: 'FCM token not found for user',
updatedAt: admin.firestore.FieldValue?.serverTimestamp?.() || new Date()
const userIdQuery = admin.firestore().collection('notifications')
.where('userId', '==', userId)
.where('type', '==', notificationType);
const clientIdQuery = admin.firestore().collection('notifications')
.where('clientId', '==', userId)
.where('type', '==', notificationType);
const invitorIdQuery = admin.firestore().collection('notifications')
.where('invitorId', '==', userId)
.where('type', '==', notificationType);
const [userIdResults, clientIdResults, invitorIdResults] = await Promise.all([
userIdQuery.get(),
clientIdQuery.get(),
invitorIdQuery.get()
]);
const notificationDocs = [...userIdResults.docs, ...clientIdResults.docs, ...invitorIdResults.docs]
.filter(doc => {
const data = doc.data();
return data.notificationSent !== true;
});
if (notificationDocs.length === 0) {
response.status(404).json({
error: `No unsent notifications of type '${notificationType}' found for user/client ${userId}`
}); });
return; return;
} }
const results = []; let title = 'New Notification';
let body = notification.message || 'You have a new notification';
let data: Record<string, string> = {
type: notification.type,
};
for (const doc of notificationDocs) { switch (notification.type) {
const notification = doc.data(); case 'day_pass_entry':
const docId = doc.id; const isAccepted = notification.status === 'ACCEPTED';
if (notification.notificationSent) { title = isAccepted ? 'Day Pass Approved' : 'Day Pass Denied';
logger.info(`Notification ${notificationType} already sent, skipping.`); body = notification.message || (isAccepted ?
continue; `Your day pass has been approved` :
} `Your day pass has been denied`);
data.gymName = notification.gymName || '';
break;
let title = 'New Notification'; case 'trainer_assigned_to_client':
let body = notification.message || 'You have a new notification'; title = 'Trainer Assigned';
let data: Record<string, string> = { body = notification.message || `${notification.trainerName} has been assigned as your trainer`;
type: notification.type, data.trainerName = notification.trainerName || '';
}; data.membershipId = notification.membershipId || '';
break;
switch (notification.type) { case 'client_invitations':
case 'day_pass_entry': if (notification.userId || notification.invitorId) {
const isAccepted = notification.status === 'ACCEPTED'; const isAccept = notification.status === 'ACCEPTED';
title = isAccepted ? 'Day Pass Approved' : 'Day Pass Denied'; title = isAccept ? 'Invitation Accepted' : 'Invitation Rejected';
body = notification.message || (isAccepted ? body = notification.message || (isAccept ?
`Your day pass has been approved` : `The invitation for ${notification.subscriptionName} you shared with ${notification.name} has been accepted` :
`Your day pass has been denied`); `The invitation for ${notification.subscriptionName} you shared with ${notification.name} has been rejected`);
data.gymName = notification.gymName || ''; } else if (notification.phoneNumber) {
break; let invitationStatus;
if (notification.status === 'ACCEPTED') {
case 'trainer_assigned_to_client': invitationStatus = 'accepted';
title = 'Trainer Assigned'; title = 'Invitation Accepted';
body = notification.message || `${notification.trainerName} has been assigned as your trainer`; body = notification.message ||
data.trainerName = notification.trainerName || ''; `You have accepted the invitation from ${notification.name}`;
data.membershipId = notification.membershipId || ''; } else if (notification.status === 'REJECTED') {
break; invitationStatus = 'rejected';
title = 'Invitation Rejected';
case 'client_invitations': body = notification.message ||
const isAccept = notification.status === 'ACCEPTED'; `You have rejected the invitation from ${notification.name}`;
title = isAccept ? 'Invitation Accepted' : 'Invitation Rejected'; } else if (notification.status === 'PENDING') {
body = notification.message || (isAccept ? invitationStatus = 'pending';
`The invitation for ${notification.subcriptionName} you shared with ${notification.name} has been accepted` : title = 'New Invitation';
`The invitation for ${notification.subcriptionName} you shared with ${notification.name} has been rejected`); body = notification.message ||
data.gymName = notification.gymName || ''; `You have a new invitation pending from ${notification.name}`;
data.clientEmail = notification.clientEmail || ''; } else {
data.clientName = notification.name || ''; invitationStatus = 'unknown';
data.invitationId = notification.invitationId || ''; title = 'Invitation Update';
data.subcriptionname= notification.subcriptionname || ''; body = notification.message || 'There is an update to your invitation';
break; }
data.status = invitationStatus;
default:
logger.info(`Using default handling for notification type: ${notification.type}`);
break;
}
const message: admin.messaging.Message = {
notification: {
title: title,
body: body,
},
data: data,
android: {
priority: 'high',
notification: {
channelId: 'notifications_channel',
priority: 'high',
defaultSound: true,
defaultVibrateTimings: true,
icon: '@mipmap/ic_launcher',
clickAction: 'FLUTTER_NOTIFICATION_CLICK',
},
},
apns: {
payload: {
aps: {
sound: 'default',
badge: 1,
},
},
},
token: fcmToken,
};
try {
const fcmResponse = await admin.messaging().send(message);
logger.info(`FCM notification sent to user ${userId} for type: ${notification.type}`);
await admin.firestore().collection('notifications').doc(docId).update({
notificationSent: true,
sentAt: admin.firestore.FieldValue?.serverTimestamp?.() || new Date()
});
results.push({
success: true,
messageId: fcmResponse
});
} catch (error) {
logger.error(`Error sending notification ${notificationType}:`, error);
results.push({
success: false,
type: notificationType,
error: error instanceof Error ? error.message : String(error)
});
}
}
response.json({
success: true,
processed: results.length,
results: results
});
} catch (error) {
logger.error('Error processing notifications:', error);
response.status(500).json({
success: false,
error: error instanceof Error ? error.message : String(error)
});
}
});
export const sendFCMNotificationByPhone = onRequest({
region: process.env.SERVICES_RGN
}, async (request: Request, response: express.Response) => {
try {
const notificationType = request.body.type || request.query.type;
const phoneNumber = request.body.phoneNumber || request.query.phoneNumber;
if (!notificationType) {
response.status(400).json({ error: 'Notification type is required' });
return;
}
if (!phoneNumber) {
response.status(400).json({ error: 'Phone number is required' });
return;
}
const userQuery = await admin
.firestore()
.collection('users')
.where('phoneNumber', '==', phoneNumber)
.get();
if (userQuery.empty) {
response.status(404).json({ error: `User not found for phone number: ${phoneNumber}` });
return;
}
const userDoc = userQuery.docs[0];
const user = userDoc.data();
const fcmToken = user?.fcmToken;
if (!fcmToken) {
response.status(400).json({ error: `FCM token not found for user with phone: ${phoneNumber}` });
return;
}
const invitorIdQuery = admin.firestore().collection('notifications')
.where('phoneNumber', '==', phoneNumber)
.where('type', '==', notificationType);
const [userIdResults] = await Promise.all([
invitorIdQuery.get()
]);
const notificationDocs = [...userIdResults.docs]
.filter(doc => {
const data = doc.data();
return data.notificationSent !== true;
});
if (notificationDocs.length === 0) {
response.status(404).json({
error: `No unsent notifications of type '${notificationType}' found for user with phone ${phoneNumber}`
});
return;
}
const results = [];
for (const doc of notificationDocs) {
const notification = doc.data();
const docId = doc.id;
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 'client_invitations':
let invitationStatus;
if (notification.status === 'ACCEPTED') {
invitationStatus = 'accepted';
title = 'Invitation Accepted';
body = notification.message ||
`You have rejected the invitation from ${notification.name}`;
} else if (notification.status === 'REJECTED') {
invitationStatus = 'rejected';
title = 'Invitation Rejected';
body = notification.message ||
`You have rejected the invitation from ${notification.name}`;
} else if (notification.status === 'PENDING') {
invitationStatus = 'pending';
title = 'New Invitation';
body = notification.message ||
`You have a new invitation pending from ${notification.name}`;
} else {
invitationStatus = 'unknown';
title = 'Invitation Update';
body = notification.message || 'There is an update to your invitation';
} }
data.gymName = notification.gymName || ''; data.gymName = notification.gymName || '';
@ -555,264 +399,58 @@ export const sendFCMNotificationByPhone = onRequest({
data.clientName = notification.name || ''; data.clientName = notification.name || '';
data.invitationId = notification.invitationId || ''; data.invitationId = notification.invitationId || '';
data.subscriptionName = notification.subscriptionName || ''; data.subscriptionName = notification.subscriptionName || '';
data.status = invitationStatus;
break; break;
default: default:
logger.info(`Using default handling for notification type: ${notification.type}`); logger.info(`Using default handling for notification type: ${notification.type}`);
break; break;
} }
const message: admin.messaging.Message = { const message: admin.messaging.Message = {
notification: {
title: title,
body: body,
},
data: data,
android: {
priority: 'high',
notification: { notification: {
title: title, channelId: 'notifications_channel',
body: body,
},
data: data,
android: {
priority: 'high', priority: 'high',
notification: { defaultSound: true,
channelId: 'notifications_channel', defaultVibrateTimings: true,
priority: 'high', icon: '@mipmap/ic_launcher',
defaultSound: true, clickAction: 'FLUTTER_NOTIFICATION_CLICK',
defaultVibrateTimings: true, },
icon: '@mipmap/ic_launcher', },
clickAction: 'FLUTTER_NOTIFICATION_CLICK', apns: {
payload: {
aps: {
sound: 'default',
badge: 1,
}, },
}, },
apns: { },
payload: { token: fcmToken,
aps: { };
sound: 'default',
badge: 1,
},
},
},
token: fcmToken,
};
try { try {
const fcmResponse = await admin.messaging().send(message); const fcmResponse = await admin.messaging().send(message);
logger.info(`FCM notification sent to user with phone ${phoneNumber} for type: ${notification.type}`); logger.info(`FCM notification sent successfully: ${fcmResponse}`);
await admin.firestore().collection('notifications').doc(docId).update({ await admin.firestore().collection('notifications').doc(notificationId).update({
notificationSent: true, notificationSent: true,
sentAt: admin.firestore.FieldValue?.serverTimestamp?.() || new Date() sentAt: admin.firestore.FieldValue?.serverTimestamp?.() || new Date()
}); });
} catch (error) {
results.push({ logger.error(`Error sending notification ${notificationId}:`, error);
success: true,
messageId: fcmResponse await admin.firestore().collection('notifications').doc(notificationId).update({
}); notificationError: error instanceof Error ? error.message : String(error),
} catch (error) { updatedAt: admin.firestore.FieldValue?.serverTimestamp?.() || new Date()
logger.error(`Error sending notification ${notification.type}:`, error);
results.push({
success: false,
type: notification.type,
error: error instanceof Error ? error.message : String(error)
});
}
}
response.json({
success: true,
processed: results.length,
results: results
});
} catch (error) {
logger.error('Error processing notifications by phone:', error);
response.status(500).json({
success: false,
error: error instanceof Error ? error.message : String(error)
});
}
});
export const sendTrainerAssignmentNotification = onRequest({
region: process.env.SERVICES_RGN
}, async (request: Request, response: express.Response) => {
try {
const notificationType = request.body.type || request.query.type || 'trainer_assigned_to_client';
const trainerName = request.body.trainerName || request.query.trainerName;
const clientId = request.body.clientId || request.query.clientId;
const gymId = request.body.gymId || request.query.gymId;
if (!trainerName) {
response.status(400).json({ error: 'Trainer name is required' });
return;
}
if (!clientId) {
response.status(400).json({ error: 'Client ID is required' });
return;
}
if (!gymId) {
response.status(400).json({ error: 'Gym ID is required' });
return;
}
// Find the trainer by name
const trainerQuery = await admin
.firestore()
.collection('trainer_profiles')
.where('fullName', '==', trainerName)
.limit(1)
.get();
if (trainerQuery.empty) {
response.status(404).json({ error: `Trainer not found with name: ${trainerName}` });
return;
}
const trainerDoc = trainerQuery.docs[0];
const trainer = trainerDoc.data();
const trainerId = trainerDoc.id;
if (!trainer.phoneNumber) {
response.status(400).json({ error: `Phone number not found for trainer: ${trainerName}` });
return;
}
// Get client name from client collection
const clientDoc = await admin.firestore().collection('clients').doc(clientId).get();
if (!clientDoc.exists) {
response.status(404).json({ error: `Client not found with ID: ${clientId}` });
return;
}
const client = clientDoc.data();
const clientName = client?.name || client?.fullName || client?.displayName || 'A new client';
// Get gym name from gym ID
const gymDoc = await admin.firestore().collection('gyms').doc(gymId).get();
if (!gymDoc.exists) {
response.status(404).json({ error: `Gym not found with ID: ${gymId}` });
return;
}
const gym = gymDoc.data();
const gymName = gym?.name || gym?.gymName || 'your gym';
// Find the user document for the trainer using their phone number
const userQuery = await admin
.firestore()
.collection('users')
.where('phoneNumber', '==', trainer.phoneNumber)
.limit(1)
.get();
if (userQuery.empty) {
response.status(404).json({ error: `User not found for trainer with phone: ${trainer.phoneNumber}` });
return;
}
const userDoc = userQuery.docs[0];
const user = userDoc.data();
const fcmToken = user?.fcmToken;
if (!fcmToken) {
response.status(400).json({ error: `FCM token not found for trainer: ${trainerName}` });
return;
}
const notificationQuery = admin.firestore().collection('notifications')
.where('type', '==', notificationType)
.where('clientId', '==', clientId)
.where('gymId', '==', gymId)
.where('trainerName', '==', trainerName);
const notificationSnapshot = await notificationQuery.get();
const unsentNotifications = notificationSnapshot.docs.filter(doc => {
const data = doc.data();
return data.notificationSent !== true;
});
if (unsentNotifications.length === 0) {
response.status(404).json({
error: `No unsent notifications found for trainer: ${trainerName}, client: ${clientId}, gym: ${gymId}`
}); });
return;
} }
const results = [];
for (const doc of unsentNotifications) {
const docId = doc.id;
const title = 'New Client Assignment';
const body = `You have been assigned to ${clientName} at ${gymName}`;
const data: Record<string, string> = {
type: notificationType,
trainerId: trainerId,
clientId: clientId,
clientName: clientName,
gymId: gymId,
gymName: gymName
};
const message: admin.messaging.Message = {
notification: {
title: title,
body: body,
},
data: data,
android: {
priority: 'high',
notification: {
channelId: 'notifications_channel',
priority: 'high',
defaultSound: true,
defaultVibrateTimings: true,
icon: '@mipmap/ic_launcher',
clickAction: 'FLUTTER_NOTIFICATION_CLICK',
},
},
apns: {
payload: {
aps: {
sound: 'default',
badge: 1,
},
},
},
token: fcmToken,
};
try {
const fcmResponse = await admin.messaging().send(message);
logger.info(`Trainer assignment notification sent to ${trainerName}`);
await admin.firestore().collection('notifications').doc(docId).update({
notificationSent: true,
sentAt: admin.firestore.FieldValue?.serverTimestamp?.() || new Date()
});
results.push({
success: true,
messageId: fcmResponse
});
} catch (error) {
logger.error(`Error sending trainer assignment notification:`, error);
results.push({
success: false,
type: notificationType,
error: error instanceof Error ? error.message : String(error)
});
}
}
response.json({
success: true,
processed: results.length,
results: results
});
} catch (error) { } catch (error) {
logger.error('Error processing trainer assignment notification:', error); logger.error('Error processing notification:', error);
response.status(500).json({
success: false,
error: error instanceof Error ? error.message : String(error)
});
} }
}); });