Skip to content

Commit 50c53dd

Browse files
committed
Add a new isPayment flag to Transaction and use it as an index
This makes the payment requests much faster.
1 parent ed46b9e commit 50c53dd

4 files changed

Lines changed: 16 additions & 6 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Add isPayment column
2+
ALTER TABLE `Transaction` ADD COLUMN `isPayment` BOOLEAN NOT NULL DEFAULT FALSE;
3+
4+
-- Populate isPayment for existing data
5+
UPDATE `Transaction` SET `isPayment` = TRUE WHERE `amount` > 0;
6+
7+
-- Add composite index for addressId + isPayment queries
8+
CREATE INDEX `Transaction_addressId_isPayment_idx` ON `Transaction`(`addressId`, `isPayment`);

prisma-local/schema.prisma

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ model Transaction {
7272
amount Decimal @db.Decimal(24, 8)
7373
confirmed Boolean @default(false)
7474
orphaned Boolean @default(false)
75+
isPayment Boolean @default(false)
7576
timestamp Int
7677
addressId String
7778
opReturn String @db.LongText @default("")
@@ -85,6 +86,7 @@ model Transaction {
8586
8687
@@unique([hash, addressId], name: "Transaction_hash_addressId_unique_constraint")
8788
@@index([addressId, timestamp], map: "Transaction_addressId_timestamp_idx")
89+
@@index([addressId, isPayment])
8890
}
8991

9092
model TransactionInput {

services/chronikService.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,7 @@ export class ChronikBlockchainClient {
298298
timestamp: transaction.block !== undefined ? transaction.block.timestamp : transaction.timeFirstSeen,
299299
addressId: address.id,
300300
confirmed: transaction.block !== undefined,
301+
isPayment: amount > 0,
301302
opReturn,
302303
inputs: {
303304
create: inputAddresses

services/transactionService.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,7 @@ export async function createManyTransactions (
579579
timestamp: tx.timestamp,
580580
addressId: tx.addressId,
581581
confirmed: tx.confirmed ?? false,
582+
isPayment: tx.amount > 0,
582583
opReturn: tx.opReturn ?? '',
583584
orphaned: false
584585
}))
@@ -948,7 +949,7 @@ export async function getPaymentsByUserIdOrderedByButtonName (
948949
LEFT JOIN \`PricesOnTransactions\` pt ON t.\`id\` = pt.\`transactionId\`
949950
LEFT JOIN \`Price\` pb ON pt.\`priceId\` = pb.\`id\`
950951
LEFT JOIN \`Invoice\` i ON i.\`transactionId\` = t.\`id\`
951-
WHERE t.\`amount\` > 0
952+
WHERE t.\`isPayment\` = TRUE
952953
AND EXISTS (
953954
SELECT 1
954955
FROM \`AddressesOnUserProfiles\` au
@@ -1056,7 +1057,7 @@ export async function fetchAllPaymentsByUserIdWithPagination (
10561057
address: {
10571058
userProfiles: { some: { userId } }
10581059
},
1059-
amount: { gt: 0 }
1060+
isPayment: true
10601061
}
10611062

10621063
if (startDate !== undefined && endDate !== undefined && startDate !== '' && endDate !== '') {
@@ -1182,9 +1183,7 @@ export async function fetchAllPaymentsByUserId (
11821183
in: networkIds ?? Object.values(NETWORK_IDS)
11831184
}
11841185
},
1185-
amount: {
1186-
gt: 0
1187-
}
1186+
isPayment: true
11881187
}
11891188

11901189
if (buttonIds !== undefined && buttonIds.length > 0) {
@@ -1238,7 +1237,7 @@ export const getFilteredTransactionCount = async (
12381237
some: { userId }
12391238
}
12401239
},
1241-
amount: { gt: 0 }
1240+
isPayment: true
12421241
}
12431242
if (buttonIds !== undefined && buttonIds.length > 0) {
12441243
where.address!.paybuttons = {

0 commit comments

Comments
 (0)