From bc6e1d94f4c9e84b385a6ec28184d68285d01d8b Mon Sep 17 00:00:00 2001 From: Jeremy Zongker Date: Mon, 18 Nov 2024 22:14:12 -0600 Subject: [PATCH] Added method to load fundDonations by person --- src/controllers/DonationController.ts | 8 ++++++++ src/controllers/FundDonationController.ts | 11 +++++++++++ src/repositories/FundDonationRepository.ts | 4 ++++ 3 files changed, 23 insertions(+) diff --git a/src/controllers/DonationController.ts b/src/controllers/DonationController.ts index 732e574..81ff61a 100644 --- a/src/controllers/DonationController.ts +++ b/src/controllers/DonationController.ts @@ -43,6 +43,14 @@ export class DonationController extends GivingBaseController { }); } + @httpGet("/my") + public async getMy(req: express.Request<{}, {}, null>, res: express.Response): Promise { + return this.actionWrapper(req, res, async (au) => { + const result = await this.repositories.donation.loadByPersonId(au.churchId, au.personId); + return this.repositories.donation.convertAllToModel(au.churchId, result); + }); + } + @httpGet("/:id") public async get(@requestParam("id") id: string, req: express.Request<{}, {}, null>, res: express.Response): Promise { return this.actionWrapper(req, res, async (au) => { diff --git a/src/controllers/FundDonationController.ts b/src/controllers/FundDonationController.ts index bf14f46..94f2dc8 100644 --- a/src/controllers/FundDonationController.ts +++ b/src/controllers/FundDonationController.ts @@ -7,6 +7,13 @@ import { Permissions } from '../helpers/Permissions' @controller("/funddonations") export class FundDonationController extends GivingBaseController { + @httpGet("/my") + public async getMy(req: express.Request<{}, {}, null>, res: express.Response): Promise { + return this.actionWrapper(req, res, async (au) => { + return this.repositories.fundDonation.loadByPersonId(au.churchId, au.personId); + }); + } + @httpGet("/:id") public async get(@requestParam("id") id: string, req: express.Request<{}, {}, null>, res: express.Response): Promise { return this.actionWrapper(req, res, async (au) => { @@ -15,13 +22,17 @@ export class FundDonationController extends GivingBaseController { }); } + + @httpGet("/") public async getAll(req: express.Request<{}, {}, null>, res: express.Response): Promise { return this.actionWrapper(req, res, async (au) => { if (!au.checkAccess(Permissions.donations.view)) return this.json({}, 401); else { let result; + if (req.query.donationId !== undefined) result = await this.repositories.fundDonation.loadByDonationId(au.churchId, req.query.donationId.toString()); + else if (req.query.personId !== undefined) result = await this.repositories.fundDonation.loadByPersonId(au.churchId, req.query.personId.toString()); else if (req.query.fundId !== undefined) { if (req.query.startDate === undefined) result = await this.repositories.fundDonation.loadByFundId(au.churchId, req.query.fundId.toString()); else { diff --git a/src/repositories/FundDonationRepository.ts b/src/repositories/FundDonationRepository.ts index 5496900..ddd7e5e 100644 --- a/src/repositories/FundDonationRepository.ts +++ b/src/repositories/FundDonationRepository.ts @@ -45,6 +45,10 @@ export class FundDonationRepository { return DB.query("SELECT * FROM fundDonations WHERE churchId=? AND donationId=?;", [churchId, donationId]); } + public loadByPersonId(churchId: string, personId: string) { + return DB.query("SELECT fd.* FROM donations d inner join fundDonations fd on fd.churchId=d.churchId and fd.donationId=d.id WHERE d.churchId=? AND d.personId=?;", [churchId, personId]); + } + public loadByFundId(churchId: string, fundId: string) { return DB.query("SELECT fd.*, d.donationDate, d.batchId, d.personId FROM fundDonations fd INNER JOIN donations d ON d.id=fd.donationId WHERE fd.churchId=? AND fd.fundId=? ORDER by d.donationDate desc;", [churchId, fundId]); }