From 1a413d021aaca73db6706d719743e122309541bb Mon Sep 17 00:00:00 2001 From: Gaurika Mahajan Date: Wed, 31 Jul 2024 18:11:37 -0400 Subject: [PATCH] added payment schema to user model, cahnged PaymentServices function to include user --- .../src/pages/FroshRetreat/FroshRetreat.jsx | 10 +- server/src/models/UserModel.js | 33 ++++- server/src/services/FroshServices.js | 72 +++++------ server/src/services/PaymentServices.js | 114 ++++++++++++------ 4 files changed, 146 insertions(+), 83 deletions(-) diff --git a/client/src/pages/FroshRetreat/FroshRetreat.jsx b/client/src/pages/FroshRetreat/FroshRetreat.jsx index 8c68afa7..653665ed 100644 --- a/client/src/pages/FroshRetreat/FroshRetreat.jsx +++ b/client/src/pages/FroshRetreat/FroshRetreat.jsx @@ -131,12 +131,11 @@ const retreatFAQs = [ ], }, { - title: - 'What time does the bus leave?', + title: 'What time does the bus leave?', description: [ 'The bus leaves from campus at 11:00am on August 31st and returns to campus at 1:00pm on September 1st.', ], - } + }, ]; const FroshRetreatFAQ = () => { @@ -155,9 +154,7 @@ const FroshRetreatFAQ = () => { }} > -

- FAQS: PREPARE FOR A GOOD TIME DOWN ON THE FARM! -

+

FAQS: PREPARE FOR A GOOD TIME DOWN ON THE FARM!

{retreatFAQs.map((item, index) => { const [isOpen, setIsOpen] = useState(false); return ( @@ -219,6 +216,7 @@ const RetreatRegistration = () => { const { setSnackbar } = useContext(SnackbarContext); const { axios } = useAxios(); const isRetreat = user?.isRetreat === true; + console.log(isRetreat); const isWaiverUploaded = user?.waiver?.filename !== undefined; const [file, setFile] = useState(null); diff --git a/server/src/models/UserModel.js b/server/src/models/UserModel.js index 55966371..3ef2853b 100644 --- a/server/src/models/UserModel.js +++ b/server/src/models/UserModel.js @@ -5,6 +5,36 @@ const validateName = function (name) { return !(name === '' || name === null || name === undefined); }; +const paymentSchema = new mongoose.Schema({ + item: { + type: String, + required: true, + }, + paymentIntent: { + type: String, + required: true, + }, + amountDue: { + type: Number, + required: true, + }, + bursaryRequested: { + type: Boolean, + required: true, + default: false, + }, + bursaryApproved: { + type: Boolean, + required: true, + default: false, + }, + expired: { + type: Boolean, + required: true, + default: false, + }, +}); + const UserSchema = new mongoose.Schema( { firstName: { @@ -104,6 +134,7 @@ const UserSchema = new mongoose.Schema( required: true, default: false, }, + payments: [paymentSchema], phoneNumberCountryCode: { type: String, required: false, @@ -154,9 +185,9 @@ const UserSchema = new mongoose.Schema( data: Buffer, }, isRetreat: { - // used for F!rosh that paid for retreat type: Boolean, required: false, + // default: false, }, }, { discriminatorKey: 'userType' }, diff --git a/server/src/services/FroshServices.js b/server/src/services/FroshServices.js index ce32c20b..84839fec 100644 --- a/server/src/services/FroshServices.js +++ b/server/src/services/FroshServices.js @@ -65,46 +65,46 @@ const FroshServices = { * @returns {User} updated user */ async addRetreatPayment(user, paymentIntent) { - try { - const updatedUser = await UserModel.findByIdAndUpdate(user.id, { - $push: { - payments: [ - { - item: 'Retreat Ticket', - paymentIntent: paymentIntent.toString(), - amountDue: 9500, - }, - ], - }, - }, { new: true}); + // try { + // const updatedUser = await UserModel.findByIdAndUpdate(user.id, { + // $push: { + // payments: [ + // { + // item: 'Retreat Ticket', + // paymentIntent: paymentIntent.toString(), + // amountDue: 10300, + // }, + // ], + // }, + // }, { new: true}); - if (!updatedUser){ - throw new Error('user not found'); - } + // if (!updatedUser){ + // throw new Error('user not found'); + // } - return updatedUser; + // return updatedUser; - } catch (error) { - throw new Error('UNABLE_TO_ADD_PAYMENT', { cause: error }); - } + // } catch (error) { + // throw new Error('UNABLE_TO_ADD_PAYMENT', { cause: error }); + // } + // }, + return UserModel.findByIdAndUpdate(user.id, { + $push: { + payments: [ + { + item: 'Retreat Ticket', + paymentIntent: paymentIntent.toString(), + amountDue: 9500, + }, + ], + }, + }).then( + (user) => user, + (error) => { + throw new Error('UNABLE_TO_ADD_PAYMENT', { cause: error }); + }, + ); }, - // return FroshModel.findByIdAndUpdate(user.id, { - // $push: { - // payments: [ - // { - // item: 'Retreat Ticket', - // paymentIntent: paymentIntent.toString(), - // amountDue: 9500, - // }, - // ], - // }, - // }).then( - // (frosh) => frosh, - // (error) => { - // throw new Error('UNABLE_TO_ADD_PAYMENT', { cause: error }); - // }, - // ); - // }, /** * @description Gets the frosh info from ID. diff --git a/server/src/services/PaymentServices.js b/server/src/services/PaymentServices.js index e4f1d3e0..cb96a074 100644 --- a/server/src/services/PaymentServices.js +++ b/server/src/services/PaymentServices.js @@ -2,6 +2,7 @@ const stripe = require('stripe')(process.env.STRIPE_SECRET_KEY); const FroshModel = require('../models/FroshModel'); const FroshGroupModel = require('../models/FroshGroupModel'); +const UserModel = require('../models/UserModel'); const PaymentServices = { /** @@ -27,52 +28,85 @@ const PaymentServices = { * @return {Promise> & Omit & {_id: Omit["_id"]}, Document> & Omit & {_id: Omit["_id"]}, {}, Omit>>} */ async updatePayment(paymentId, amountReceived) { - const frosh = await FroshModel.findOne({ 'payments.paymentIntent': paymentId }).then( - (frosh) => { - if (!frosh) throw new Error('FROSH_NOT_FOUND'); - return frosh; - }, + let user; + + const frosh = await FroshModel.findOne({ 'payments.paymentIntent': paymentId }).catch( (error) => { throw new Error('UNABLE_TO_FIND_FROSH', { cause: error }); }, ); - frosh.set({ authScopes: { requested: [], approved: [] } }); - const idx = frosh.payments.findIndex((p) => p.paymentIntent === paymentId); - frosh.payments[idx].amountDue = frosh.payments[idx].amountDue - amountReceived; - if (frosh.payments[idx].item === 'Orientation Ticket') { - frosh.set({ isRegistered: true }); - await frosh.save({ validateModifiedOnly: true }).then( - (frosh) => frosh, - (error) => { + console.log(); + + if (!frosh) { + console.log(`Cant not in frosh model`); + user = await UserModel.findOne({ 'payments.paymentIntent': paymentId }).catch((error) => { + throw new Error('UNABLE_TO_FIND_USER', { cause: error }); + }); + + if (!user) { + throw new Error('PAYMENT_NOT_FOUND'); + } + } + + //if found in FroshModel + if (frosh) { + console.log(`found in frosh model`); + const idx = frosh.payments.findIndex((p) => p.paymentIntent === paymentId); + frosh.payments[idx].amountDue -= amountReceived; + + if (frosh.payments[idx].item === 'Orientation Ticket') { + frosh.set({ isRegistered: true }); + await frosh.save({ validateModifiedOnly: true }).catch((error) => { throw new Error('UNABLE_TO_UPDATE_FROSH', { cause: error }); - }, - ); - console.log('Frosh payment completed! Frosh info: ', frosh); - return FroshGroupModel.findOneAndUpdate( - { name: frosh.froshGroup }, - { $inc: { totalNum: 1 } }, - { new: true }, - ).then( - (doc) => { - if (!doc) console.log(new Error('FROSH_GROUP_NOT_FOUND')); - else console.log(`Group ${doc.name} total: ${doc.totalNum}`); - return frosh; - }, - (error) => { - console.log(new Error('UNABLE_TO_UPDATE_FROSH_GROUP', { cause: error })); - return frosh; - }, - ); - } else if (frosh.payments[idx].item === 'Retreat Ticket') { - frosh.set({ isRetreat: true }); - return frosh.save({ validateModifiedOnly: true }).then( - (frosh) => frosh, - (error) => { + }); + + console.log(`Frosh payment completed! Frosh info: `, frosh); + return FroshGroupModel.findOneAndUpdate( + { name: frosh.froshGroup }, + { $inc: { totalNum: 1 } }, + { new: true }, + ).then( + (doc) => { + if (!doc) console.log(new Error('FROSH_GROUP_NOT_FOUND')); + else console.log(`Group ${doc.name} total: ${doc.totalNum}`); + return frosh; + }, + (error) => { + console.log(new Error('UNABLE_TO_UPDATE_FROSH_GROUP', { cause: error })); + return frosh; + }, + ); + } else if (frosh.payments[idx].item === 'Retreat Ticket') { + console.log(`frosh payment type is retreat ticket`); + frosh.set({ isRetreat: true }); + await frosh.save({ validateModifiedOnly: true }).catch((error) => { throw new Error('UNABLE_TO_UPDATE_FROSH', { cause: error }); - }, - ); - } else { - throw new Error('UNABLE_TO_UPDATE_FROSH', { cause: new Error('INVALID_PAYMENT_ITEM') }); + }); + + console.log('Frosh retreat payment completed! Frosh info: ', frosh); + return frosh; + } else { + throw new Error('INVALID_PAYMENT_ITEM'); + } + } + + // If found in UserModel + if (user) { + const idx = user.payments.findIndex((p) => p.paymentIntent === paymentId); + user.payments[idx].amountDue -= amountReceived; + + if (user.payments[idx].item === 'Retreat Ticket') { + console.log(`frosh payment type is retreat ticket`); + user.set({ isRetreat: true }); + await user.save({ validateModifiedOnly: true }).catch((error) => { + throw new Error('UNABLE_TO_UPDATE_USER', { cause: error }); + }); + + console.log(`User retreat payment completed! User info: `, user); + return user; + } else { + throw new Error('INVALID_PAYMENT_ITEM'); + } } },