All checks were successful
Deploy FitLien services to Dev / Deploy to Dev (push) Successful in 3m35s
Co-authored-by: AllenTJ7 <163137620+AllenTJ7@users.noreply.github.com> Reviewed-on: #43
123 lines
3.5 KiB
TypeScript
123 lines
3.5 KiB
TypeScript
import { getAdmin, getLogger } from "../../shared/config";
|
|
|
|
const admin = getAdmin();
|
|
const logger = getLogger();
|
|
|
|
interface PaymentData {
|
|
id: string;
|
|
date: string;
|
|
dateTimestamp: FirebaseFirestore.Timestamp;
|
|
amount: any;
|
|
paymentMethod: string;
|
|
referenceNumber: string;
|
|
discount: any;
|
|
transactionId: string;
|
|
createdAt: Date;
|
|
invoicePath?: string;
|
|
}
|
|
|
|
export async function updatePaymentDataAfterSuccess(
|
|
merchantOrderId: string,
|
|
orderId: string,
|
|
paymentDetails: any,
|
|
invoicePath?: string
|
|
): Promise<boolean> {
|
|
try {
|
|
const orderQuery = await admin.firestore()
|
|
.collection('payment_orders')
|
|
.where('merchantOrderId', '==', merchantOrderId)
|
|
.limit(1)
|
|
.get();
|
|
|
|
if (orderQuery.empty) {
|
|
logger.error(`No payment order found with merchantOrderId: ${merchantOrderId}`);
|
|
return false;
|
|
}
|
|
|
|
const orderDoc = orderQuery.docs[0];
|
|
const orderData = orderDoc.data();
|
|
|
|
const membershipId = orderData.metaInfo?.membershipId;
|
|
if (!membershipId) {
|
|
logger.error(`No membershipId found in metaInfo for order: ${merchantOrderId}`);
|
|
return false;
|
|
}
|
|
|
|
const isoDate = new Date().toLocaleDateString('en-GB').split('/').join('-');
|
|
const dateTimestamp = admin.firestore.Timestamp.now();
|
|
|
|
const paymentData: PaymentData = {
|
|
id: admin.firestore().collection('_').doc().id,
|
|
date: isoDate,
|
|
dateTimestamp: dateTimestamp,
|
|
amount: orderData.amount,
|
|
paymentMethod: 'Online',
|
|
referenceNumber: merchantOrderId,
|
|
discount: orderData.metaInfo?.discount || null,
|
|
transactionId: orderId,
|
|
createdAt: new Date()
|
|
};
|
|
|
|
if (invoicePath) {
|
|
paymentData.invoicePath = invoicePath;
|
|
}
|
|
|
|
const membershipPaymentsRef = admin.firestore()
|
|
.collection('membership_payments')
|
|
.doc(membershipId);
|
|
|
|
const docSnapshot = await membershipPaymentsRef.get();
|
|
|
|
if (docSnapshot.exists) {
|
|
await membershipPaymentsRef.update({
|
|
'payments': admin.firestore.FieldValue.arrayUnion(paymentData),
|
|
'updatedAt': admin.firestore.FieldValue.serverTimestamp(),
|
|
'updatedBy': orderData.userId,
|
|
});
|
|
} else {
|
|
await membershipPaymentsRef.set({
|
|
'membershipId': membershipId,
|
|
'payments': [paymentData],
|
|
'createdAt': admin.firestore.FieldValue.serverTimestamp(),
|
|
'createdBy': orderData.userId,
|
|
'updatedAt': admin.firestore.FieldValue.serverTimestamp(),
|
|
'updatedBy': orderData.userId,
|
|
});
|
|
}
|
|
|
|
await updateMembershipStatus(membershipId, orderData.userId);
|
|
|
|
logger.info(`Successfully updated payment data for membership: ${membershipId}`);
|
|
return true;
|
|
} catch (error: any) {
|
|
logger.error('Error updating payment data:', error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
async function updateMembershipStatus(membershipId: string, userId: string): Promise<void> {
|
|
try {
|
|
const membershipDoc = await admin.firestore()
|
|
.collection('memberships')
|
|
.doc(membershipId)
|
|
.get();
|
|
|
|
if (!membershipDoc.exists) {
|
|
throw new Error(`Membership not found for id: ${membershipId}`);
|
|
}
|
|
|
|
await admin.firestore()
|
|
.collection('memberships')
|
|
.doc(membershipId)
|
|
.update({
|
|
'status': 'ACTIVE',
|
|
'updatedAt': admin.firestore.FieldValue.serverTimestamp(),
|
|
});
|
|
|
|
logger.info(`Successfully updated membership status for: ${membershipId}`);
|
|
} catch (error: any) {
|
|
logger.error('Error updating membership status:', error);
|
|
throw error;
|
|
}
|
|
}
|