Skip to content
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

support userId in donation schema #373

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
Prev Previous commit
Next Next commit
support null typing and no required value on userid, check for null u…
…serid in donation server action
Fattimo committed Nov 12, 2020
commit 4018c6061818af4dc0ab84fd5f4f201e052bd578
7 changes: 6 additions & 1 deletion server/actions/donation.ts
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ import Stripe from "stripe";
import Mongo, { stripeConstructor } from "server/index";
import Donation from "server/models/donation";
import Nonprofit from "server/models/nonprofit";
import User from "server/models/user";
import errors from "utils/errors";
import { Donation as DonationType } from "utils/types";

@@ -17,12 +18,16 @@ export async function logDonation({
await Mongo();

const nonprofit = await Nonprofit.findOne({ _id: nonprofitId });
const user = await Donation.findOne({ _id: userId }); // TODO: Don't allow front end to pass null resulting userId
const user = await User.findOne({ _id: userId }); // TODO: Don't allow front end to pass null resulting userId
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple of things:
can we do this and line 20 in parallel, using Promise.all?

can we modify line 27 to not throw the error if userId equals the empty string (as you have done in DonationPageForm.tsx), just temprarily? Because otherwise, donations will fail when DMS team is testing their UI.


if (!nonprofit) {
throw new Error(errors.nonprofit.INVALID_ID);
}

if (!user) {
throw new Error(errors.user.INVALID_ID);
}

const donation = await Donation.create({
name,
email,
3 changes: 1 addition & 2 deletions server/models/donation.js
Original file line number Diff line number Diff line change
@@ -22,8 +22,7 @@ const donationSchema = new Schema({
},
userId: {
type: String,
ref: "User",
required: true
ref: "User"
},
timestamp: {
type: Date,
3 changes: 3 additions & 0 deletions utils/errors.ts
Original file line number Diff line number Diff line change
@@ -15,5 +15,8 @@ export default {
},
event: {
INVALID_ID: "An event with the provided ID does not exist in our database."
},
user: {
INVALID_ID: "A user with the provided ID does not exist in our database."
}
};
2 changes: 1 addition & 1 deletion utils/types.ts
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ export interface Donation {
email: string;
amount: number;
nonprofitId: string;
userId: string;
userId: string | null;
timestamp: Date;
}