Skip to content

Commit

Permalink
added payment schema to user model, cahnged PaymentServices function …
Browse files Browse the repository at this point in the history
…to include user
  • Loading branch information
gaurikam2003 committed Jul 31, 2024
1 parent 658572e commit 1a413d0
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 83 deletions.
10 changes: 4 additions & 6 deletions client/src/pages/FroshRetreat/FroshRetreat.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = () => {
Expand All @@ -155,9 +154,7 @@ const FroshRetreatFAQ = () => {
}}
>
<img src={dragon} style={{ width: '350px', margin: '20px' }}></img>
<h2 style={{ marginBottom: '20px' }}>
FAQS: PREPARE FOR A GOOD TIME DOWN ON THE FARM!
</h2>
<h2 style={{ marginBottom: '20px' }}>FAQS: PREPARE FOR A GOOD TIME DOWN ON THE FARM!</h2>
{retreatFAQs.map((item, index) => {
const [isOpen, setIsOpen] = useState(false);
return (
Expand Down Expand Up @@ -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);
Expand Down
33 changes: 32 additions & 1 deletion server/src/models/UserModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -104,6 +134,7 @@ const UserSchema = new mongoose.Schema(
required: true,
default: false,
},
payments: [paymentSchema],
phoneNumberCountryCode: {
type: String,
required: false,
Expand Down Expand Up @@ -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' },
Expand Down
72 changes: 36 additions & 36 deletions server/src/services/FroshServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
114 changes: 74 additions & 40 deletions server/src/services/PaymentServices.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
/**
Expand All @@ -27,52 +28,85 @@ const PaymentServices = {
* @return {Promise<Query<Document<unknown, any, Omit<unknown, never>> & Omit<unknown, never> & {_id: Omit<unknown, never>["_id"]}, Document<unknown, any, Omit<unknown, never>> & Omit<unknown, never> & {_id: Omit<unknown, never>["_id"]}, {}, Omit<unknown, never>>>}
*/
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');
}
}
},

Expand Down

0 comments on commit 1a413d0

Please sign in to comment.