Fix: skip first query when fromPaidTimestamp is 0

This commit is contained in:
Mothana 2025-07-24 22:09:06 +04:00
parent 9e21c515d6
commit d6b6ee61d1

View file

@ -74,24 +74,28 @@ export default class {
} }
async GetUserInvoicesFlaggedAsPaid(userSerialId: number, fromIndex: number, fromPaidTimestamp: number, take = 50, txId?: string): Promise<UserReceivingInvoice[]> { 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: UserReceivingInvoice[] = [];
let items = await this.dbs.Find<UserReceivingInvoice>('UserReceivingInvoice', { if (fromPaidTimestamp > 0) {
where: { // First fetch same paid_at_unix, higher serial_id
user: { serial_id: userSerialId }, const firstBatch = await this.dbs.Find<UserReceivingInvoice>('UserReceivingInvoice', {
paid_at_unix: And(MoreThan(0), Equal(fromPaidTimestamp)), where: {
serial_id: MoreThan(fromIndex) user: { serial_id: userSerialId },
}, paid_at_unix: And(MoreThan(0), Equal(fromPaidTimestamp)),
order: { serial_id: MoreThan(fromIndex)
paid_at_unix: 'ASC', },
serial_id: 'ASC' order: {
}, paid_at_unix: 'ASC',
take serial_id: 'ASC'
}, txId) },
take
}, txId);
items.push(...firstBatch);
}
const needMore = take - items.length const needMore = take - items.length
// If need more, fetch higher paid_at_unix // If need more, fetch higher paid_at_unix
if (needMore > 0) { if (needMore > 0) {
const more = await this.dbs.Find<UserReceivingInvoice>('UserReceivingInvoice', { const secondBatch = await this.dbs.Find<UserReceivingInvoice>('UserReceivingInvoice', {
where: { where: {
user: { serial_id: userSerialId }, user: { serial_id: userSerialId },
paid_at_unix: And(MoreThan(0), MoreThan(fromPaidTimestamp)), paid_at_unix: And(MoreThan(0), MoreThan(fromPaidTimestamp)),
@ -102,7 +106,7 @@ export default class {
}, },
take: needMore take: needMore
}, txId) }, txId)
items.push(...more) items.push(...secondBatch)
} }
return items return items
} }