Skip to content

Commit

Permalink
bugfix(website): filter out test recipients in payment stats (#681)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkue authored Dec 20, 2023
1 parent e2171a8 commit e13b7dd
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions shared/src/utils/stats/PaymentStatsCalculator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import _ from 'lodash';
import { FirestoreAdmin } from '../../firebase/admin/FirestoreAdmin';
import { Payment, PAYMENT_FIRESTORE_PATH } from '../../types/payment';
import { RECIPIENT_FIRESTORE_PATH } from '../../types/recipient';
import { User } from '../../types/user';
import { toDateTime } from '../date';
import { getLatestExchangeRate } from '../exchangeRates';
import { cumulativeSum, groupByAndSort, StatsEntry } from './utils';
Expand Down Expand Up @@ -43,11 +45,21 @@ export class PaymentStatsCalculator {
* PaymentStatsCalculator with the flattened intermediate data structure.
*/
static async build(firestoreAdmin: FirestoreAdmin, currency: string): Promise<PaymentStatsCalculator> {
const payments = await firestoreAdmin.collectionGroup<Payment>(PAYMENT_FIRESTORE_PATH).get();
const recipients = await firestoreAdmin.collection<User>(RECIPIENT_FIRESTORE_PATH).get();
const payments = (
await Promise.all(
recipients.docs
.filter((recipient) => !recipient.get('test_recipient'))
.map(async (recipient) => {
return await firestoreAdmin
.collection<Payment>(`${RECIPIENT_FIRESTORE_PATH}/${recipient.id}/${PAYMENT_FIRESTORE_PATH}`)
.get();
}),
)
).flatMap((snapshot) => snapshot.docs);
const exchangeRate = await getLatestExchangeRate(firestoreAdmin, currency);
// TODO: filter out payments from test users
const paymentDocs = payments.docs
.filter((payment) => payment.data().amount_chf != undefined)
const paymentStatsEntries = payments
.filter((payment) => payment.get('amount_chf') != undefined)
.map((paymentDoc) => {
const payment = paymentDoc.data();
return {
Expand All @@ -57,7 +69,7 @@ export class PaymentStatsCalculator {
month: toDateTime(payment.payment_at).toFormat('yyyy-MM'),
} as PaymentStatsEntry;
});
return new PaymentStatsCalculator(_(paymentDocs));
return new PaymentStatsCalculator(_(paymentStatsEntries));
}

totalPayments = () => {
Expand Down

0 comments on commit e13b7dd

Please sign in to comment.