diff --git a/src/controllers/FundDonationController.ts b/src/controllers/FundDonationController.ts index 94f2dc8..767f2f4 100644 --- a/src/controllers/FundDonationController.ts +++ b/src/controllers/FundDonationController.ts @@ -22,7 +22,7 @@ export class FundDonationController extends GivingBaseController { }); } - + @httpGet("/") public async getAll(req: express.Request<{}, {}, null>, res: express.Response): Promise { diff --git a/src/helpers/StripeHelper.ts b/src/helpers/StripeHelper.ts index 89cf49e..b8ce734 100644 --- a/src/helpers/StripeHelper.ts +++ b/src/helpers/StripeHelper.ts @@ -189,11 +189,11 @@ export class StripeHelper { const { method, methodDetails } = await this.getPaymentDetails(secretKey, eventData); const batch: DonationBatch = await Repositories.getCurrent().donationBatch.getOrCreateCurrent(churchId); const donationData: Donation = { batchId: batch.id, amount, churchId, personId, method, methodDetails, donationDate: new Date(eventData.created * 1000), notes: eventData?.metadata?.notes }; - const funds = eventData.metadata.funds ? JSON.parse(eventData.metadata.funds) : await Repositories.getCurrent().subscriptionFund.loadBySubscriptionId(churchId, eventData.subscription); + const funds = eventData.metadata.funds ? JSON.parse(eventData.metadata.funds) : await Repositories.getCurrent().subscriptionFund.loadForSubscriptionLog(churchId, eventData.subscription); const donation: Donation = await Repositories.getCurrent().donation.save(donationData); const promises: Promise[] = []; funds.forEach((fund: FundDonation) => { - const fundDonation: FundDonation = { churchId, amount: fund.amount, donationId: donation.id, fundId: fund.id }; + const fundDonation: FundDonation = { churchId, amount: fund.amount, donationId: donation.id, fundId: fund.fundId }; promises.push(Repositories.getCurrent().fundDonation.save(fundDonation)); }); return await Promise.all(promises); diff --git a/src/repositories/FundRepository.ts b/src/repositories/FundRepository.ts index 2a6afab..02235f2 100644 --- a/src/repositories/FundRepository.ts +++ b/src/repositories/FundRepository.ts @@ -11,8 +11,8 @@ export class FundRepository { if (data !== null) return this.convertToModel(churchId, data); else { const fund: Fund = { churchId, name: "(General Fund)" }; - await this.save(fund); - return fund; + const result =await this.save(fund); + return result; } } diff --git a/src/repositories/SubscriptionFundsRepository.ts b/src/repositories/SubscriptionFundsRepository.ts index e95aaba..91d7c16 100644 --- a/src/repositories/SubscriptionFundsRepository.ts +++ b/src/repositories/SubscriptionFundsRepository.ts @@ -1,6 +1,7 @@ import { injectable } from "inversify"; import { DB, UniqueIdHelper } from "@churchapps/apihelper"; import { SubscriptionFund } from "../models"; +import { Repositories } from "./Repositories"; @injectable() export class SubscriptionFundsRepository { @@ -43,6 +44,26 @@ export class SubscriptionFundsRepository { return DB.query(sql, [churchId, subscriptionId]); } + //If the fund gets deleted for a recurring donation, the donations will go to '(General Fund)' + public async loadForSubscriptionLog(churchId: string, subscriptionId: string) { + let result; + const sql = "SELECT subscriptionFunds.*, funds.name, funds.removed FROM subscriptionFunds" + + " LEFT JOIN funds ON subscriptionFunds.fundId = funds.id" + + " WHERE subscriptionFunds.churchId=? AND subscriptionFunds.subscriptionId=?"; + const subscriptionFund = await DB.query(sql, [churchId, subscriptionId]); + if (subscriptionFund && subscriptionFund[0].removed === false) { + const { removed, ...sf } = subscriptionFund[0]; + result = [sf]; + } else { + const generalFund = await Repositories.getCurrent().fund.getOrCreateGeneral(churchId); + const { removed, ...sf } = subscriptionFund[0]; + sf.fundId = generalFund.id; + sf.name = generalFund.name; + result = [sf]; + } + return result; + } + public async loadAll(churchId: string) { return DB.query("SELECT * FROM subscriptionFunds WHERE churchId=?;", [churchId]); }