Skip to content

Commit

Permalink
Refactor routes location
Browse files Browse the repository at this point in the history
  • Loading branch information
jessica2673 committed Mar 24, 2024
1 parent 984c33f commit d6e6ced
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 29 deletions.
54 changes: 25 additions & 29 deletions backend/typescript/rest/donationsRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
// import { Prisma } from "@prisma/client";
import { Router } from "express";
import prisma from '../prisma';
import DonationService from "../services/implementations/donationService";
import IDonationService from "../services/interfaces/donationService";
import { getErrorMessage } from "../utilities/errorUtils";

const donationService: IDonationService = new DonationService();

const donationRouter = Router();

// display all donations
donationRouter.get("/", async (req: any, res: any) => {
try {
const allDonations = await prisma.donation.findMany();
console.log(allDonations);
const allDonations = await donationService.getAllDonations();

res.status(200).json(allDonations);
} catch (e) {
console.error("Error fetching donations: ", e);
res.status(500).send("An error occurred while fetching donations.");
} catch (error) {
res.status(500).json({ error: getErrorMessage(error) });
}
});

Expand All @@ -21,37 +25,29 @@ 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.");
const userDonations = donationService.getUserDonation(id);

res.status(200).json(userDonations);
} catch (error) {
res.status(500).json({ error: getErrorMessage(error) });
}
});

// Each donation has 1 cause associated with it, the donation from user will be split before calling this route.
donationRouter.post("/give", async (req: any, res: any) => {
try {
const { user_id, amount, donation_date, cause_id, is_recurring, confirmation_email_sent } = req.body;

const newDonation = await prisma.donation.create({
data: {
user: { connect: { id: user_id } },
amount,
donation_date: new Date(donation_date),
cause: { connect: { id: cause_id } },
is_recurring,
confirmation_email_sent
},
});
const { user_id, amount, cause_id, is_recurring, confirmation_email_sent } = req.body;

const newDonation = await donationService.createDonation(
user_id,
amount,
cause_id,
is_recurring,
confirmation_email_sent
);
res.status(200).json(newDonation);
} catch (e) {
console.error("Error creating new donation: ", e);
res.status(500).send("An error occurred while creating donation.");
} catch (error) {
res.status(500).json({ error: getErrorMessage(error) });
}
})

Expand Down
57 changes: 57 additions & 0 deletions backend/typescript/services/implementations/donationService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import IDonationService from "../interfaces/donationService";
import { DonationDTO } from "../../types"
import prisma from "../../prisma";
import logger from "../../utilities/logger";
import { getErrorMessage } from "../../utilities/errorUtils";

const Logger = logger(__filename);

class DonationService implements IDonationService {
async getAllDonations(): Promise<Array<DonationDTO>> {
try {
const allDonations = await prisma.donation.findMany();

return allDonations;
} catch (error) {
Logger.error(`Error fetching donations. Reason = ${getErrorMessage(error)}`);
throw error;
}
}

async getUserDonation(user_id: string): Promise<Array<DonationDTO>> {
try {
const userDonations = await prisma.donation.findMany({
where: {
user_id: user_id,
}
});

return userDonations;
} catch (error) {
Logger.error(`Error fetching donation for user ${user_id} = ${error}`);
throw error;
}
}

async createDonation(user_id: string, amount: number, cause_id: number, is_recurring: boolean, confirmation_email_sent: boolean): Promise<DonationDTO> {
try {
const newDonation = await prisma.donation.create({
data: {
user: { connect: { id: user_id } },
amount,
donation_date: new Date(),
cause: { connect: { id: cause_id } },
is_recurring,
confirmation_email_sent
},
});

return newDonation;
} catch (error) {
console.error(`Error creating new donation: = ${error}`);
throw error;
}
}
}

export default DonationService;
39 changes: 39 additions & 0 deletions backend/typescript/services/interfaces/donationService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { DateDataType, FloatDataType, IntegerDataType } from "sequelize";
import { DonationDTO } from "../../types";

interface IDonationService {
/**
* Get all donations across all users
* @returns an array of DonationDTO
* @throws Error if donation retrieval fails
*/
getAllDonations(): Promise<Array<DonationDTO>>;

/**
* Get all donations associated with user_id
* @param user_id user's id
* @returns an array of DonationDTO
* @throws Error if donation retrieval fails
*/
getUserDonation(user_id: string): Promise<Array<DonationDTO>>;

/**
* Create a donation
* @param user_id user's id
* @param amount the amount the user is donating toward this cause in the donation
* @param cause_id the id of the cause the donation is associated with
* @param is_recurring whether or not the donation is reoccurring
* @param confirmation_email_sent whether or not a confirmation email has been sent
* @returns the newly created DonationDTO
* @throws Error if donation cannot be created
*/
createDonation(
user_id: string,
amount: number,
cause_id: number,
is_recurring: boolean,
confirmation_email_sent: boolean
): Promise<DonationDTO>;
}

export default IDonationService;
14 changes: 14 additions & 0 deletions backend/typescript/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { DateDataType } from "sequelize";
import { FloatDataType, IntegerDataType } from "sequelize";

export type Role = "User" | "Admin";

export type Token = {
Expand Down Expand Up @@ -35,3 +38,14 @@ export type NodemailerConfig = {
};

export type SignUpMethod = "PASSWORD" | "GOOGLE";

export type DonationDTO = {
user_id: string;
amount: number;
donation_date: Date;
cause_id: number;
is_recurring: boolean;
confirmation_email_sent: boolean;
}

export type CreateDonationDTO = Omit<DonationDTO, "user_id">;

0 comments on commit d6e6ced

Please sign in to comment.