UserReceivingInvoice index usage with And find operator,

fetch all operations in ASC
This commit is contained in:
Mothana 2025-07-24 13:33:15 +04:00
parent 409b0beb95
commit 6db06aebe0
2 changed files with 38 additions and 29 deletions

View file

@ -632,8 +632,9 @@ export default class {
}
}
return {
toIndex: { ts: operations[0].paid_at_unix, id: operations[0]!.serial_id },
fromIndex: { ts: operations.at(-1)!.paid_at_unix, id: operations.at(-1)!.serial_id },
// We fetch in ascending order
toIndex: { ts: operations.at(-1)!.paid_at_unix, id: operations.at(-1)!.serial_id } ,
fromIndex: { ts: operations[0].paid_at_unix, id: operations[0]!.serial_id },
operations: operations.map((o: UserOperationInfo): Types.UserOperation => {
let identifier = "";
if (o.invoice) {
@ -689,7 +690,7 @@ export default class {
const [outgoingInvoices, outgoingTransactions, incomingInvoices, incomingTransactions, incomingUserToUser, outgoingUserToUser] = await Promise.all([
this.storage.paymentStorage.GetUserInvoicePayments(userId, req.latestOutgoingInvoice.id, req.max_size), //
this.storage.paymentStorage.GetUserTransactionPayments(userId, req.latestOutgoingTx.id, req.max_size),
this.storage.paymentStorage.GetUserInvoicesFlaggedAsPaid(userId, req.latestIncomingInvoice.id, req.latestIncomingInvoice.ts, req.max_size),
this.storage.paymentStorage.GetUserInvoicesFlaggedAsPaid(user.serial_id, req.latestIncomingInvoice.id, req.latestIncomingInvoice.ts, req.max_size),
this.storage.paymentStorage.GetUserReceivingTransactions(userId, req.latestIncomingTx.id, req.max_size),
this.storage.paymentStorage.GetUserToUserReceivedPayments(userId, req.latestIncomingUserToUserPayment.id, req.max_size),
this.storage.paymentStorage.GetUserToUserSentPayments(userId, req.latestOutgoingUserToUserPayment.id, req.max_size)

View file

@ -1,5 +1,5 @@
import crypto from 'crypto';
import { Between, FindOperator, FindOptionsWhere, IsNull, LessThanOrEqual, MoreThan, MoreThanOrEqual, Not } from "typeorm"
import { And, Between, Equal, FindOperator, IsNull, LessThanOrEqual, MoreThan, MoreThanOrEqual } from "typeorm"
import { User } from './entity/User.js';
import { UserTransactionPayment } from './entity/UserTransactionPayment.js';
import { EphemeralKeyType, UserEphemeralKey } from './entity/UserEphemeralKey.js';
@ -46,7 +46,7 @@ export default class {
paid_at_unix: MoreThan(0),
},
order: {
paid_at_unix: 'DESC'
paid_at_unix: 'ASC'
},
take
}, txId)
@ -73,30 +73,38 @@ export default class {
return this.dbs.Update<UserReceivingInvoice>('UserReceivingInvoice', invoice.serial_id, i, txId)
}
async GetUserInvoicesFlaggedAsPaid(userId: string, fromIndex: number, fromPaidTimestamp: number, take = 50, txId?: string): Promise<UserReceivingInvoice[]> {
const whereArray: FindOptionsWhere<UserReceivingInvoice>[] = [
// Later paid_at_unix timestamp
{
user: { user_id: userId },
paid_at_unix: MoreThan(Math.max(fromPaidTimestamp, 0)), // also excludes 0
}
];
// Same timestamp, higher serial_id
if (fromPaidTimestamp > 0) {
whereArray.push({
user: { user_id: userId },
paid_at_unix: fromPaidTimestamp,
serial_id: MoreThan(fromIndex),
});
}
return this.dbs.Find<UserReceivingInvoice>('UserReceivingInvoice', {
where: whereArray,
async GetUserInvoicesFlaggedAsPaid(userSerialId: number, fromIndex: number, fromPaidTimestamp: number, take = 50, txId?: string): Promise<UserReceivingInvoice[]> {
// First fetch same paid_at_unix, higher serial_id
let items = await this.dbs.Find<UserReceivingInvoice>('UserReceivingInvoice', {
where: {
user: { serial_id: userSerialId },
paid_at_unix: And(MoreThan(0), Equal(fromPaidTimestamp)),
serial_id: MoreThan(fromIndex)
},
order: {
paid_at_unix: 'DESC',
serial_id: 'DESC'
paid_at_unix: 'ASC',
serial_id: 'ASC'
},
take
}, txId)
const needMore = take - items.length
// If need more, fetch higher paid_at_unix
if (needMore > 0) {
const more = await this.dbs.Find<UserReceivingInvoice>('UserReceivingInvoice', {
where: {
user: { serial_id: userSerialId },
paid_at_unix: And(MoreThan(0), MoreThan(fromPaidTimestamp)),
},
order: {
paid_at_unix: 'ASC',
serial_id: 'ASC'
},
take: needMore
}, txId)
items.push(...more)
}
return items
}
async AddUserInvoice(user: User, invoice: string, options: InboundOptionals = { expiry: defaultInvoiceExpiry }, providerDestination?: string, txId?: string): Promise<UserReceivingInvoice> {
@ -198,7 +206,7 @@ export default class {
paid_at_unix: MoreThan(-1),
},
order: {
paid_at_unix: 'DESC'
paid_at_unix: 'ASC'
},
take
}, txId)
@ -246,7 +254,7 @@ export default class {
paid_at_unix: MoreThan(0),
},
order: {
paid_at_unix: 'DESC'
paid_at_unix: 'ASC'
},
take
}, txId)
@ -315,7 +323,7 @@ export default class {
paid_at_unix: MoreThan(0),
},
order: {
paid_at_unix: 'DESC'
paid_at_unix: 'ASC'
},
take
}, txId)
@ -332,7 +340,7 @@ export default class {
paid_at_unix: MoreThan(0),
},
order: {
paid_at_unix: 'DESC'
paid_at_unix: 'ASC'
},
take
}, txId)