From c2be9b605aa6786ee4c4edafb2b07e644609e1e8 Mon Sep 17 00:00:00 2001 From: Gaurika Mahajan Date: Wed, 31 Jul 2024 19:14:27 -0400 Subject: [PATCH 1/2] pls work --- server/src/services/PaymentServices.js | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/server/src/services/PaymentServices.js b/server/src/services/PaymentServices.js index 96fd9608..1c569b11 100644 --- a/server/src/services/PaymentServices.js +++ b/server/src/services/PaymentServices.js @@ -116,14 +116,23 @@ const PaymentServices = { * @returns {Number} number of non-expired payments for a given item */ async getNonExpiredPaymentsCountForItem(item) { - return FroshModel.countDocuments({ - payments: { $elemMatch: { item, expired: false } }, - }).then( - (count) => count, - (error) => { - throw new Error('UNABLE_TO_GET_COUNT_OF_PAYMENTS', { cause: error }); - }, - ); + try { + // Count non-expired payments for the item in FroshModel + const froshCount = await FroshModel.countDocuments({ + payments: { $elemMatch: { item, expired: false } }, + }); + + // Count non-expired payments for the item in UserModel + const userCount = await UserModel.countDocuments({ + payments: { $elemMatch: { item, expired: false } }, + }); + + // Sum the counts from both models + const totalCount = froshCount + userCount; + return totalCount; + } catch (error) { + throw new Error('UNABLE_TO_GET_COUNT_OF_PAYMENTS', { cause: error }); + } }, /** From 009a6953866aebd4551752ae418dc269be883207 Mon Sep 17 00:00:00 2001 From: Gaurika Mahajan Date: Wed, 31 Jul 2024 19:32:37 -0400 Subject: [PATCH 2/2] counting user model payments for retreat ticekts --- server/src/services/PaymentServices.js | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/server/src/services/PaymentServices.js b/server/src/services/PaymentServices.js index 1c569b11..c1f37381 100644 --- a/server/src/services/PaymentServices.js +++ b/server/src/services/PaymentServices.js @@ -117,17 +117,19 @@ const PaymentServices = { */ async getNonExpiredPaymentsCountForItem(item) { try { - // Count non-expired payments for the item in FroshModel - const froshCount = await FroshModel.countDocuments({ - payments: { $elemMatch: { item, expired: false } }, - }); + let froshCount = 0; + let userCount = 0; - // Count non-expired payments for the item in UserModel - const userCount = await UserModel.countDocuments({ - payments: { $elemMatch: { item, expired: false } }, - }); + if (item === 'Retreat Ticket') { + userCount = await UserModel.countDocuments({ + payments: { $elemMatch: { item, expired: false } }, + }); + } else { + froshCount = await FroshModel.countDocuments({ + payments: { $elemMatch: { item, expired: false } }, + }); + } - // Sum the counts from both models const totalCount = froshCount + userCount; return totalCount; } catch (error) {