UserReceivingInvoice index usage with And find operator,
fetch all operations in ASC
This commit is contained in:
parent
409b0beb95
commit
6db06aebe0
2 changed files with 38 additions and 29 deletions
|
|
@ -632,8 +632,9 @@ export default class {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return {
|
return {
|
||||||
toIndex: { ts: operations[0].paid_at_unix, id: operations[0]!.serial_id },
|
// We fetch in ascending order
|
||||||
fromIndex: { ts: operations.at(-1)!.paid_at_unix, id: operations.at(-1)!.serial_id },
|
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 => {
|
operations: operations.map((o: UserOperationInfo): Types.UserOperation => {
|
||||||
let identifier = "";
|
let identifier = "";
|
||||||
if (o.invoice) {
|
if (o.invoice) {
|
||||||
|
|
@ -689,7 +690,7 @@ export default class {
|
||||||
const [outgoingInvoices, outgoingTransactions, incomingInvoices, incomingTransactions, incomingUserToUser, outgoingUserToUser] = await Promise.all([
|
const [outgoingInvoices, outgoingTransactions, incomingInvoices, incomingTransactions, incomingUserToUser, outgoingUserToUser] = await Promise.all([
|
||||||
this.storage.paymentStorage.GetUserInvoicePayments(userId, req.latestOutgoingInvoice.id, req.max_size), //
|
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.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.GetUserReceivingTransactions(userId, req.latestIncomingTx.id, req.max_size),
|
||||||
this.storage.paymentStorage.GetUserToUserReceivedPayments(userId, req.latestIncomingUserToUserPayment.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)
|
this.storage.paymentStorage.GetUserToUserSentPayments(userId, req.latestOutgoingUserToUserPayment.id, req.max_size)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
import crypto from 'crypto';
|
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 { User } from './entity/User.js';
|
||||||
import { UserTransactionPayment } from './entity/UserTransactionPayment.js';
|
import { UserTransactionPayment } from './entity/UserTransactionPayment.js';
|
||||||
import { EphemeralKeyType, UserEphemeralKey } from './entity/UserEphemeralKey.js';
|
import { EphemeralKeyType, UserEphemeralKey } from './entity/UserEphemeralKey.js';
|
||||||
|
|
@ -46,7 +46,7 @@ export default class {
|
||||||
paid_at_unix: MoreThan(0),
|
paid_at_unix: MoreThan(0),
|
||||||
},
|
},
|
||||||
order: {
|
order: {
|
||||||
paid_at_unix: 'DESC'
|
paid_at_unix: 'ASC'
|
||||||
},
|
},
|
||||||
take
|
take
|
||||||
}, txId)
|
}, txId)
|
||||||
|
|
@ -73,30 +73,38 @@ export default class {
|
||||||
return this.dbs.Update<UserReceivingInvoice>('UserReceivingInvoice', invoice.serial_id, i, txId)
|
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[]> {
|
async GetUserInvoicesFlaggedAsPaid(userSerialId: number, fromIndex: number, fromPaidTimestamp: number, take = 50, txId?: string): Promise<UserReceivingInvoice[]> {
|
||||||
const whereArray: FindOptionsWhere<UserReceivingInvoice>[] = [
|
// First fetch same paid_at_unix, higher serial_id
|
||||||
// Later paid_at_unix timestamp
|
let items = await this.dbs.Find<UserReceivingInvoice>('UserReceivingInvoice', {
|
||||||
{
|
where: {
|
||||||
user: { user_id: userId },
|
user: { serial_id: userSerialId },
|
||||||
paid_at_unix: MoreThan(Math.max(fromPaidTimestamp, 0)), // also excludes 0
|
paid_at_unix: And(MoreThan(0), Equal(fromPaidTimestamp)),
|
||||||
}
|
serial_id: MoreThan(fromIndex)
|
||||||
];
|
},
|
||||||
// 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,
|
|
||||||
order: {
|
order: {
|
||||||
paid_at_unix: 'DESC',
|
paid_at_unix: 'ASC',
|
||||||
serial_id: 'DESC'
|
serial_id: 'ASC'
|
||||||
},
|
},
|
||||||
take
|
take
|
||||||
}, txId)
|
}, 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> {
|
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),
|
paid_at_unix: MoreThan(-1),
|
||||||
},
|
},
|
||||||
order: {
|
order: {
|
||||||
paid_at_unix: 'DESC'
|
paid_at_unix: 'ASC'
|
||||||
},
|
},
|
||||||
take
|
take
|
||||||
}, txId)
|
}, txId)
|
||||||
|
|
@ -246,7 +254,7 @@ export default class {
|
||||||
paid_at_unix: MoreThan(0),
|
paid_at_unix: MoreThan(0),
|
||||||
},
|
},
|
||||||
order: {
|
order: {
|
||||||
paid_at_unix: 'DESC'
|
paid_at_unix: 'ASC'
|
||||||
},
|
},
|
||||||
take
|
take
|
||||||
}, txId)
|
}, txId)
|
||||||
|
|
@ -315,7 +323,7 @@ export default class {
|
||||||
paid_at_unix: MoreThan(0),
|
paid_at_unix: MoreThan(0),
|
||||||
},
|
},
|
||||||
order: {
|
order: {
|
||||||
paid_at_unix: 'DESC'
|
paid_at_unix: 'ASC'
|
||||||
},
|
},
|
||||||
take
|
take
|
||||||
}, txId)
|
}, txId)
|
||||||
|
|
@ -332,7 +340,7 @@ export default class {
|
||||||
paid_at_unix: MoreThan(0),
|
paid_at_unix: MoreThan(0),
|
||||||
},
|
},
|
||||||
order: {
|
order: {
|
||||||
paid_at_unix: 'DESC'
|
paid_at_unix: 'ASC'
|
||||||
},
|
},
|
||||||
take
|
take
|
||||||
}, txId)
|
}, txId)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue