Skip to content

Commit 6602b27

Browse files
authored
Merge pull request #891 from PayButton/feat/get-payments-count
feat: get payments count
2 parents ca69fa9 + 77472d8 commit 6602b27

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

pages/api/payments/count/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { CacheGet } from 'redis/index'
2+
import { setSession } from 'utils/setSession'
3+
4+
export default async (req: any, res: any): Promise<void> => {
5+
if (req.method === 'GET') {
6+
await setSession(req, res)
7+
const userId = req.session.userId
8+
9+
const resJSON = await CacheGet.paymentsCount(userId)
10+
res.status(200).json(resJSON)
11+
}
12+
}

redis/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { TransactionWithAddressAndPrices } from 'services/transactionService'
44
import { fetchUsersForAddress } from 'services/userService'
55
import { cacheBalanceForAddress, clearBalanceCache, getBalanceForAddress, updateBalanceCacheFromTx } from './balanceCache'
66
import { clearDashboardCache, getUserDashboardData } from './dashboardCache'
7-
import { appendPaybuttonToAddressesCache, cacheGroupedPayments, cacheManyTxs, generateGroupedPaymentsForAddress, getPaymentList, initPaymentCache, removePaybuttonToAddressesCache } from './paymentCache'
7+
import { appendPaybuttonToAddressesCache, cacheGroupedPayments, cacheManyTxs, generateGroupedPaymentsForAddress, getCachedPaymentsCountForUser, getPaymentList, initPaymentCache, removePaybuttonToAddressesCache } from './paymentCache'
88
import { DashboardData, Payment } from './types'
99

1010
interface PaybuttonCreationParams {
@@ -84,5 +84,8 @@ export const CacheGet = {
8484
},
8585
addressBalance: async (addressString: string): Promise<AddressPaymentInfo> => {
8686
return await getBalanceForAddress(addressString)
87+
},
88+
paymentsCount: async (userId: string) => {
89+
return await getCachedPaymentsCountForUser(userId)
8790
}
8891
}

redis/paymentCache.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { RESPONSE_MESSAGES, PAYMENT_WEEK_KEY_FORMAT, KeyValueT } from 'constants
88
import moment from 'moment'
99
import { CacheSet } from 'redis/index'
1010
import { ButtonDisplayData, Payment } from './types'
11+
import { getUserDashboardData } from './dashboardCache'
1112
// ADDRESS:payments:YYYY:MM
1213
const getPaymentsWeekKey = (addressString: string, timestamp: number): string => {
1314
return `${addressString}:payments:${moment.unix(timestamp).format(PAYMENT_WEEK_KEY_FORMAT)}`
@@ -112,6 +113,12 @@ export const getCachedPaymentsForUser = async (userId: string): Promise<Payment[
112113
return allPayments
113114
}
114115

116+
export const getCachedPaymentsCountForUser = async (userId: string): Promise<number> => {
117+
const dashboardData = await getUserDashboardData(userId)
118+
119+
return dashboardData.total.payments
120+
}
121+
115122
export const cacheGroupedPayments = async (paymentsGroupedByKey: KeyValueT<Payment[]>): Promise<void> => {
116123
await Promise.all(
117124
Object.keys(paymentsGroupedByKey).map(async key =>

0 commit comments

Comments
 (0)