-
Notifications
You must be signed in to change notification settings - Fork 30
added membership payment validation #1994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Tyffoni
wants to merge
7
commits into
dev
Choose a base branch
from
pending-payment
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+199
−0
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fadaefa
added membership payment schema
Tyffoni 469da46
revised spacing
Tyffoni 79276c8
Update api/main_endpoints/models/MembershipPayment.js
Tyffoni 1eec680
fixed syntax
Tyffoni 197dd13
Update api/main_endpoints/models/MembershipPayment.js
adarshm11 56ca2bb
Update api/main_endpoints/models/MembershipPayment.js
adarshm11 21c0284
Emily/20250104 handle membership payment confirmation (#1993)
codebyemily File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| const mongoose = require('mongoose'); | ||
| const Schema = mongoose.Schema; | ||
|
|
||
|
|
||
| const MembershipPaymentSchema = new Schema( | ||
| { | ||
| createdAt: { | ||
| type: Date, | ||
| default: Date.now | ||
| }, | ||
| userId: { | ||
| type: Schema.Types.ObjectId, | ||
| ref: 'User', | ||
| default: null, | ||
| }, | ||
| status: { | ||
| type: String, | ||
| enum: ['pending', 'completed', 'rejected'], | ||
| default: 'pending', | ||
| required: true, | ||
| }, | ||
| confirmationCode: { | ||
| type: String, | ||
| unique: true, | ||
| required: true, | ||
| }, | ||
| amount: { | ||
| type: Number, | ||
| required: true, | ||
| }, | ||
| venmoDetails:{ | ||
| transactionId: { type: String }, | ||
| payerName: { type: String }, | ||
| note: { type: String }, | ||
| } | ||
| }, | ||
| { collection: 'MembershipPayments' } | ||
| ); | ||
|
|
||
|
|
||
| module.exports = mongoose.model('MembershipPayment', MembershipPaymentSchema); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| const express = require('express'); | ||
| const router = express.Router(); | ||
| const bodyParser = require('body-parser'); | ||
| router.use(bodyParser.json()); | ||
| const { | ||
| BAD_REQUEST, | ||
| SERVER_ERROR, | ||
| NOT_FOUND, | ||
| OK, | ||
| } = require('../../util/constants').STATUS_CODES; | ||
| const membershipState = require('../../util/constants').MEMBERSHIP_STATE; | ||
| const User = require('../models/User'); | ||
| const { getMemberExpirationDate, updateMembershipExpiration } = require('../util/userHelpers'); | ||
| const { findVerifyPayment, rejectPayment } = require('../util/membershipPaymentQueries'); | ||
| const { decodeToken } = require('../util/token-functions.js'); | ||
|
|
||
| router.post('/verifyMembership', async (req, res) => { | ||
| const decoded = await decodeToken(req, membershipState.PENDING); | ||
| if (decoded.status !== OK) { | ||
| return res.sendStatus(decoded.status); | ||
| } | ||
|
|
||
| const { confirmationCode } = req.body; | ||
| const userId = decoded.token._id; | ||
|
|
||
| if (!confirmationCode) { | ||
| return res.sendStatus(BAD_REQUEST); | ||
| } | ||
|
|
||
| const paymentDocument = await findVerifyPayment(confirmationCode, userId); | ||
| if (paymentDocument === null){ | ||
| return res.sendStatus(SERVER_ERROR); | ||
| } | ||
| if (paymentDocument === false){ | ||
| return res.sendStatus(NOT_FOUND); | ||
| } | ||
|
|
||
| const paymentId = paymentDocument._id; | ||
| const { amount } = paymentDocument; | ||
|
|
||
| if (amount < 20){ | ||
| const rejected = await rejectPayment(paymentId); | ||
| if (rejected === null){ | ||
| return res.sendStatus(SERVER_ERROR); | ||
| } | ||
| if (rejected === false){ | ||
| return res.sendStatus(NOT_FOUND); | ||
| } | ||
| return res.sendStatus(BAD_REQUEST); | ||
| } | ||
|
|
||
| let semestersToAdd = 0; | ||
| if (amount >= 30) { | ||
| semestersToAdd = 2; | ||
| } else { | ||
| semestersToAdd = 1; | ||
| } | ||
|
|
||
| const membershipUpdateResult = await updateMembershipExpiration( | ||
| decoded.token._id, | ||
| semestersToAdd | ||
| ); | ||
|
|
||
| if (membershipUpdateResult === null) { | ||
| return res.sendStatus(SERVER_ERROR); | ||
| } | ||
| if (membershipUpdateResult === false) { | ||
| return res.status(NOT_FOUND).send('User not found.'); | ||
| } | ||
| return res.sendStatus(OK); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| import MembershipPayment from '../main_endpoints/models/MembershipPayment.js'; | ||
|
|
||
| const status = { | ||
| PENDING: 'pending', | ||
| COMPLETED: 'completed', | ||
| REJECTED: 'rejected', | ||
| }; | ||
|
|
||
| function findVerifyPayment(confirmationCode, userId) { | ||
| return new Promise((resolve) => { | ||
| try { | ||
| MembershipPayment.findOneAndUpdate( | ||
| { | ||
| confirmationCode, | ||
| status: status.PENDING, | ||
| }, | ||
| { | ||
| $set: { userId, status: status.COMPLETED }, | ||
| }, | ||
| { | ||
| new: true, | ||
| runValidators: true, | ||
| }, | ||
| (error, result) => { | ||
| if (error) { | ||
| return resolve(null); | ||
| } | ||
| if (!result) { | ||
| return resolve(false); | ||
| } | ||
| return resolve(result); | ||
| } | ||
| ); | ||
| } catch (error) { | ||
| return resolve(null); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| function rejectPayment(paymentId) { | ||
| return new Promise((resolve) => { | ||
| try { | ||
| MembershipPayment.findByIdAndUpdate( | ||
| paymentId, | ||
| { $set: { status: status.REJECTED } }, | ||
| (error, result) => { | ||
| if (error) { | ||
| return resolve(null); | ||
| } | ||
| if (!result) { | ||
| return resolve(false); | ||
| } | ||
| return resolve(true); | ||
| } | ||
| ); | ||
| } catch (error) { | ||
| return resolve(null); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| module.exports = { findVerifyPayment, rejectPayment }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.