2022-06-18 03:28:16 +00:00
|
|
|
import { DonationTxn, TipTxn } from 'common/txn'
|
|
|
|
import { collection, orderBy, query, where } from 'firebase/firestore'
|
2022-04-29 23:35:56 +00:00
|
|
|
import { db } from './init'
|
2022-05-03 17:25:14 +00:00
|
|
|
import { getValues, listenForValues } from './utils'
|
2022-04-29 23:35:56 +00:00
|
|
|
|
|
|
|
const txnCollection = collection(db, 'txns')
|
|
|
|
|
|
|
|
const getCharityQuery = (charityId: string) =>
|
|
|
|
query(
|
|
|
|
txnCollection,
|
|
|
|
where('toType', '==', 'CHARITY'),
|
|
|
|
where('toId', '==', charityId),
|
|
|
|
orderBy('createdTime', 'desc')
|
|
|
|
)
|
|
|
|
|
|
|
|
export function listenForCharityTxns(
|
|
|
|
charityId: string,
|
2022-06-18 03:28:16 +00:00
|
|
|
setTxns: (txns: DonationTxn[]) => void
|
2022-04-29 23:35:56 +00:00
|
|
|
) {
|
2022-06-18 03:28:16 +00:00
|
|
|
return listenForValues<DonationTxn>(getCharityQuery(charityId), setTxns)
|
2022-04-29 23:35:56 +00:00
|
|
|
}
|
2022-05-02 17:55:40 +00:00
|
|
|
|
|
|
|
const charitiesQuery = query(txnCollection, where('toType', '==', 'CHARITY'))
|
|
|
|
|
2022-05-03 17:25:14 +00:00
|
|
|
export function getAllCharityTxns() {
|
2022-06-18 03:28:16 +00:00
|
|
|
return getValues<DonationTxn>(charitiesQuery)
|
|
|
|
}
|
|
|
|
|
|
|
|
const getTipsQuery = (contractId: string) =>
|
|
|
|
query(
|
|
|
|
txnCollection,
|
|
|
|
where('category', '==', 'TIP'),
|
|
|
|
where('data.contractId', '==', contractId)
|
|
|
|
)
|
|
|
|
|
|
|
|
export function listenForTipTxns(
|
|
|
|
contractId: string,
|
|
|
|
setTxns: (txns: TipTxn[]) => void
|
|
|
|
) {
|
|
|
|
return listenForValues<TipTxn>(getTipsQuery(contractId), setTxns)
|
2022-05-02 17:55:40 +00:00
|
|
|
}
|