-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
984c33f
commit d6e6ced
Showing
4 changed files
with
135 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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
57 changes: 57 additions & 0 deletions
57
backend/typescript/services/implementations/donationService.ts
This file contains 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,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; |
This file contains 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,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; |
This file contains 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