Skip to content

Commit

Permalink
improve: recurring donation log (#72)
Browse files Browse the repository at this point in the history
* improve: recurring donation log

* chore: update comment
  • Loading branch information
Himali-Malvawala authored Dec 5, 2024
1 parent 14fab48 commit af604c9
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/controllers/FundDonationController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class FundDonationController extends GivingBaseController {
});
}



@httpGet("/")
public async getAll(req: express.Request<{}, {}, null>, res: express.Response): Promise<interfaces.IHttpActionResult> {
Expand Down
4 changes: 2 additions & 2 deletions src/helpers/StripeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<FundDonation>[] = [];
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);
Expand Down
4 changes: 2 additions & 2 deletions src/repositories/FundRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

Expand Down
21 changes: 21 additions & 0 deletions src/repositories/SubscriptionFundsRepository.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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]);
}
Expand Down

0 comments on commit af604c9

Please sign in to comment.