Skip to content

Commit

Permalink
add more donation routes
Browse files Browse the repository at this point in the history
  • Loading branch information
jessica2673 committed Mar 14, 2024
1 parent 121cb2d commit 609830b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
65 changes: 60 additions & 5 deletions backend/typescript/rest/donationsRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ import prisma from '../prisma';

const donationRouter = Router();

// display all donations
donationRouter.get("/test", async (req: any, res: any) => {
res.status(200).json('endpoint hath been hit');
});

// display all donations
donationRouter.get("/", async (req: any, res: any) => {
try {
Expand All @@ -21,5 +16,65 @@ donationRouter.get("/", async (req: any, res: any) => {
}
});

// display all donations made by a user
donationRouter.get("/:id", async (req: any, res: any) => {
const id = req.params.id;

try {
const userDonation = await prisma.donation.findUnique({
where: {
id: parseInt(id)
}
});
res.status(200).json(userDonation);
} catch (e) {
console.error(`Error fetching donation for user ${id}: `, e);
res.status(500).send("An error occurred while fetching donations.");
}
});

async function createCauseDonation(donationId: number, causeId: number) {
try {
const causeDonation = await prisma.causeDonation.create({
data: {
donation: {
connect: { id: donationId },
},
cause: {
connect: { id: causeId },
},
},
});
console.log(causeDonation);
return causeDonation;
} catch (error) {
console.error("Error creating CauseDonation:", error);
throw error;
}
}

// THIS ROUTE IS WORK-IN-PROGRESS. Need to potentially create a CauseDonation first, or refactor schema.prisma
donationRouter.post("/give", async (req: any, res: any) => {
try {
const { user_id, amount, donation_date, causes, is_recurring, confirmation_email_sent } = req.body;

const newUser = await prisma.donation.create({
data: {
user: { connect: { id: user_id } },
amount,
donation_date: new Date(donation_date),
causes: {
connect: causes.map((cause_id: number) => ({ cause_id: cause_id }))
},
is_recurring,
confirmation_email_sent
}
});
res.status(200).json(newUser);
} catch (e) {
console.error("Error creating new donation: ", e);
res.status(500).send("An error occurred while creating donation.");
}
})

export default donationRouter;
5 changes: 5 additions & 0 deletions backend/typescript/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ app.use("/users", userRouter);
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
app.use('/donations', donationRouter);

// Health check
app.get("/test", async (req: any, res: any) => {
res.status(200).json('endpoint hath been hit');
});

sequelize.authenticate();

firebaseAdmin.initializeApp({
Expand Down

0 comments on commit 609830b

Please sign in to comment.