Skip to content

Commit 1aa1870

Browse files
committed
Don't fetch inputs unless requested
This avoids loading the tx inputs where they are unlikely needed. Can still be requested by passing`includeInputs=true` to the query. The following api endpoints are affected: - /api/payments - /api/paybutton/transactions/[id] None of these needs the inputs by default.
1 parent f44fa99 commit 1aa1870

3 files changed

Lines changed: 36 additions & 11 deletions

File tree

pages/api/paybutton/transactions/[id].ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RESPONSE_MESSAGES, TX_PAGE_SIZE_LIMIT } from 'constants/index'
1+
import { RESPONSE_MESSAGES, TX_PAGE_SIZE_LIMIT, DEFAULT_TX_PAGE_SIZE } from 'constants/index'
22
import { fetchTransactionsByPaybuttonIdWithPagination } from 'services/transactionService'
33
import * as paybuttonService from 'services/paybuttonService'
44
import { setSession } from 'utils/setSession'
@@ -13,6 +13,7 @@ export default async (req: any, res: any): Promise<void> => {
1313
const pageSize = (req.query.pageSize === '' || req.query.pageSize === undefined) ? DEFAULT_TX_PAGE_SIZE : Number(req.query.pageSize)
1414
const orderBy = (req.query.orderBy === '' || req.query.orderBy === undefined) ? undefined : req.query.orderBy as string
1515
const orderDesc: boolean = !!(req.query.orderDesc === '' || req.query.orderDesc === undefined || req.query.orderDesc === 'true')
16+
const includeInputs: boolean = req.query.includeInputs === 'true'
1617

1718
if (isNaN(page) || isNaN(pageSize)) {
1819
throw new Error(RESPONSE_MESSAGES.PAGE_SIZE_AND_PAGE_SHOULD_BE_NUMBERS_400.message)
@@ -27,7 +28,7 @@ export default async (req: any, res: any): Promise<void> => {
2728
throw new Error(RESPONSE_MESSAGES.RESOURCE_DOES_NOT_BELONG_TO_USER_400.message)
2829
}
2930

30-
const transactions = await fetchTransactionsByPaybuttonIdWithPagination(paybuttonId, page, pageSize, orderDesc, orderBy)
31+
const transactions = await fetchTransactionsByPaybuttonIdWithPagination(paybuttonId, page, pageSize, orderDesc, orderBy, undefined, includeInputs)
3132

3233
res.status(200).json({ transactions })
3334
} catch (err: any) {

pages/api/payments/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export default async (req: any, res: any): Promise<void> => {
2828
if (typeof req.query.endDate === 'string' && req.query.endDate !== '') {
2929
endDate = req.query.endDate as string
3030
}
31+
const includeInputs = req.query.includeInputs === 'true'
3132
const userReqTimezone = req.headers.timezone as string
3233
const userPreferredTimezone = user?.preferredTimezone
3334
let timezone = userPreferredTimezone !== '' ? userPreferredTimezone : userReqTimezone
@@ -47,7 +48,8 @@ export default async (req: any, res: any): Promise<void> => {
4748
buttonIds,
4849
years,
4950
startDate,
50-
endDate
51+
endDate,
52+
includeInputs
5153
)
5254
res.status(200).json(resJSON)
5355
}

services/transactionService.ts

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ export async function fetchTransactionsByAddressListWithPagination (
183183
pageSize: number,
184184
orderBy?: string,
185185
orderDesc = true,
186-
networkIdsListFilter?: number[]
186+
networkIdsListFilter?: number[],
187+
includeInputs = false
187188
): Promise<TransactionsWithPaybuttonsAndPrices[]> {
188189
const orderDescString: Prisma.SortOrder = orderDesc ? 'desc' : 'asc'
189190

@@ -209,6 +210,14 @@ export async function fetchTransactionsByAddressListWithPagination (
209210
}
210211
}
211212

213+
// Build include conditionally - exclude inputs by default unless explicitly requested
214+
const include = includeInputs
215+
? includePaybuttonsAndPricesAndInvoices
216+
: (() => {
217+
const { inputs, ...rest } = includePaybuttonsAndPricesAndInvoices
218+
return rest
219+
})()
220+
212221
return await prisma.transaction.findMany({
213222
where: {
214223
addressId: {
@@ -220,11 +229,11 @@ export async function fetchTransactionsByAddressListWithPagination (
220229
}
221230
}
222231
},
223-
include: includePaybuttonsAndPricesAndInvoices,
232+
include,
224233
orderBy: orderByQuery,
225234
skip: page * pageSize,
226235
take: pageSize
227-
})
236+
}) as unknown as TransactionsWithPaybuttonsAndPrices[]
228237
}
229238

230239
export async function fetchTxCountByAddressString (addressString: string): Promise<number> {
@@ -823,15 +832,19 @@ export async function fetchTransactionsByPaybuttonIdWithPagination (
823832
pageSize: number,
824833
orderDesc: boolean,
825834
orderBy?: string,
826-
networkIds?: number[]): Promise<TransactionsWithPaybuttonsAndPrices[]> {
835+
networkIds?: number[],
836+
includeInputs = false
837+
): Promise<TransactionsWithPaybuttonsAndPrices[]> {
827838
const addressIdList = await fetchAddressesByPaybuttonId(paybuttonId)
828839
const transactions = await fetchTransactionsByAddressListWithPagination(
829840
addressIdList,
830841
page,
831842
pageSize,
832843
orderBy,
833844
orderDesc,
834-
networkIds)
845+
networkIds,
846+
includeInputs
847+
)
835848

836849
return transactions
837850
}
@@ -1005,7 +1018,8 @@ export async function fetchAllPaymentsByUserIdWithPagination (
10051018
buttonIds?: string[],
10061019
years?: string[],
10071020
startDate?: string,
1008-
endDate?: string
1021+
endDate?: string,
1022+
includeInputs = false
10091023
): Promise<Payment[]> {
10101024
const orderDescString: Prisma.SortOrder = orderDesc ? 'desc' : 'asc'
10111025

@@ -1061,9 +1075,17 @@ export async function fetchAllPaymentsByUserIdWithPagination (
10611075
}
10621076
}
10631077

1078+
// Build include conditionally - exclude inputs by default unless explicitly requested
1079+
const include = includeInputs
1080+
? includePaybuttonsAndPricesAndInvoices
1081+
: (() => {
1082+
const { inputs, ...rest } = includePaybuttonsAndPricesAndInvoices
1083+
return rest
1084+
})()
1085+
10641086
const transactions = await prisma.transaction.findMany({
10651087
where,
1066-
include: includePaybuttonsAndPricesAndInvoices,
1088+
include,
10671089
orderBy: orderByQuery,
10681090
skip: page * Number(pageSize),
10691091
take: Number(pageSize)
@@ -1073,7 +1095,7 @@ export async function fetchAllPaymentsByUserIdWithPagination (
10731095
for (let index = 0; index < transactions.length; index++) {
10741096
const tx = transactions[index]
10751097
if (Number(tx.amount) > 0) {
1076-
const payment = await generatePaymentFromTxWithInvoices(tx, userId)
1098+
const payment = await generatePaymentFromTxWithInvoices(tx as unknown as TransactionWithAddressAndPricesAndInvoices, userId)
10771099
transformedData.push(payment)
10781100
}
10791101
}

0 commit comments

Comments
 (0)