Skip to content

Commit

Permalink
Merge pull request #833 from UofT-Frosh-Orientation/fixing-unregister…
Browse files Browse the repository at this point in the history
…ed-user-payment

fixing unregistered user payment
  • Loading branch information
gaurikam2003 authored Jul 31, 2024
2 parents 658572e + 7939acd commit 17810cd
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 91 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: 10300,
},
],
},
}).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
116 changes: 75 additions & 41 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 Expand Up @@ -173,7 +207,7 @@ const PaymentServices = {
const frosh = await FroshModel.findOne({ 'payments.paymentIntent': paymentIntent }).then(
(frosh) => {
if (!frosh) {
throw new Error('FROSH_NOT_FOUND');
throw new Error('PAYMENT_NOT_FOUND');
}
return frosh;
},
Expand Down
2 changes: 1 addition & 1 deletion server/test/services/FroshServices.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ describe('FroshServices', () => {
assert(frosh.firstName === 'TestButUpdated');
});

it('.updateFroshInfo(userId, updateInfo)\t\t\t|\tUpdating Frosh Information (INVALID USER ID)', async () => {
it('.updateFroshInfo(userId, updateInfo)\t\t\t|\tUpdating Frosh Information (UNABLE_TO_UPDATE_FROSH)', async () => {
await assert.rejects(FroshServices.updateFroshInfo('999999', updateInfo), {
name: 'Error',
message: 'UNABLE_TO_UPDATE_FROSH',
Expand Down
12 changes: 6 additions & 6 deletions server/test/services/PaymentServices.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ describe('PaymentServices', () => {
assert.equal(updatedFrosh.isRetreat, true);
});

it('.updatePayment(...)\t|\tUpdating a payment (FROSH_NOT_FOUND)', async () => {
it('.updatePayment(...)\t|\tUpdating a payment (PAYMENT_NOT_FOUND)', async () => {
await assert.rejects(PaymentServices.updatePayment('123452131326', 123), {
name: 'Error',
message: 'FROSH_NOT_FOUND',
message: 'PAYMENT_NOT_FOUND',
});
});

Expand Down Expand Up @@ -148,7 +148,7 @@ describe('PaymentServices', () => {
assert.equal(updatedFrosh.isRegistered, true);
});

it('.updatePayment(...)\t|\tUpdating a payment (UNABLE_TO_UPDATE_FROSH)', async () => {
it('.updatePayment(...)\t|\tUpdating a payment (INVALID_PAYMENT_ITEM)', async () => {
await FroshModel.create({
scuntPreferredMembers: [1, 2, 3],
hashedPassword: 'test',
Expand Down Expand Up @@ -184,7 +184,7 @@ describe('PaymentServices', () => {
});
await assert.rejects(PaymentServices.updatePayment('test2', 123), {
name: 'Error',
message: 'UNABLE_TO_UPDATE_FROSH',
message: 'INVALID_PAYMENT_ITEM',
});
});

Expand Down Expand Up @@ -303,10 +303,10 @@ describe('PaymentServices', () => {
assert.equal(updatedFrosh.payments[0].expired, true);
});

it('.expirePayment(...)\t|\tExpiring a payment (FROSH_NOT_FOUND)', async () => {
it('.expirePayment(...)\t|\tExpiring a payment (PAYMENT_NOT_FOUND)', async () => {
await assert.rejects(PaymentServices.expirePayment('none existent'), {
name: 'Error',
message: 'FROSH_NOT_FOUND',
message: 'PAYMENT_NOT_FOUND',
});
});
});

0 comments on commit 17810cd

Please sign in to comment.