Skip to content

Commit

Permalink
feature(functions): update campaign amount_collected_chf in stripe ho…
Browse files Browse the repository at this point in the history
…ok (#714)

* feature(admin): fundraising campaign

* appending amount_collected

* feature(functions): update campaign amount_collected_chf in stripe hook

* associate contribution with campaign

* remove firecms dependency

* rename
  • Loading branch information
andrashee authored Jan 26, 2024
1 parent 7d55f78 commit 0b69374
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
6 changes: 6 additions & 0 deletions admin/src/collections/Contributions.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CAMPAIGN_FIRESTORE_PATH } from '@socialincome/shared/src/types/campaign';
import {
CONTRIBUTION_FIRESTORE_PATH,
Contribution,
Expand Down Expand Up @@ -103,6 +104,11 @@ export function buildContributionsCollection(
validation: { required: true },
enumValues: { 0: 'One time', 1: 'Monthly', 3: 'Quarterly', 12: 'Annually' },
},
campaign_path: {
dataType: 'reference',
name: 'Campaign',
path: CAMPAIGN_FIRESTORE_PATH,
},
}),
...collectionProps,
});
Expand Down
35 changes: 33 additions & 2 deletions shared/src/stripe/StripeEventHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DateTime } from 'luxon';
import Stripe from 'stripe';
import { FirestoreAdmin } from '../firebase/admin/FirestoreAdmin';
import { toFirebaseAdminTimestamp } from '../firebase/admin/utils';
import { CAMPAIGN_FIRESTORE_PATH, Campaign } from '../types/campaign';
import {
CONTRIBUTION_FIRESTORE_PATH,
ContributionSourceKey,
Expand Down Expand Up @@ -99,7 +100,8 @@ export class StripeEventHandler {
const plan = (charge.invoice as Stripe.Invoice)?.lines?.data[0]?.plan;
const monthlyInterval = plan?.interval === 'month' ? plan?.interval_count : plan?.interval === 'year' ? 12 : 0;
const balanceTransaction = charge.balance_transaction as Stripe.BalanceTransaction;
return {

const contribution = {
source: ContributionSourceKey.STRIPE,
created: toFirebaseAdminTimestamp(DateTime.fromSeconds(charge.created)),
amount: charge.amount / 100,
Expand All @@ -109,7 +111,35 @@ export class StripeEventHandler {
monthly_interval: monthlyInterval,
reference_id: charge.id,
status: this.constructStatus(charge.status),
};
} as StripeContribution;

return charge.metadata?.campaignId
? ({
...contribution,
campaign_path: `${CAMPAIGN_FIRESTORE_PATH}/${charge.metadata?.campaignId}`,
} as StripeContribution)
: contribution;
};

/**
* Increments the total donations of a campaign if the charge is associated with a campaignId.
*/
maybeUpdateCampaign = async (contribution: StripeContribution): Promise<void> => {
if (contribution.campaign_path) {
const campaignRef = this.firestoreAdmin
.collection<Campaign>(CAMPAIGN_FIRESTORE_PATH)
.doc(contribution.campaign_path);
try {
const campaign = await campaignRef.get();
const current_amount_chf = campaign.data()?.amount_collected_chf ?? 0;
await campaignRef.update({
amount_collected_chf: current_amount_chf + contribution.amount_chf,
});
console.log(`Campaign amount ${contribution.campaign_path} updated.`);
} catch (error) {
console.error(`Error updating campaign amount ${contribution.campaign_path}.`, error);
}
}
};

constructStatus = (status: Stripe.Charge.Status) => {
Expand Down Expand Up @@ -162,6 +192,7 @@ export class StripeEventHandler {
).doc(charge.id);
await contributionRef.set(contribution);
console.info(`Ingested ${charge.id} into firestore for user ${userRef.id}`);
await this.maybeUpdateCampaign(contribution);
return contributionRef;
};
}
1 change: 1 addition & 0 deletions shared/src/types/contribution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ type BaseContribution = {
amount_chf: number;
fees_chf: number;
currency: Currency;
campaign_path?: string;
};

export type StripeContribution = BaseContribution & {
Expand Down

0 comments on commit 0b69374

Please sign in to comment.