From 3ae9dccf029a3f44c6303529289cf37b67b73851 Mon Sep 17 00:00:00 2001 From: boufni95 Date: Wed, 8 Oct 2025 16:22:50 +0000 Subject: [PATCH] old with small balance + old invoices --- src/services/main/appUserManager.ts | 23 ++++++++++++++++++++--- src/services/main/init.ts | 2 ++ src/services/main/paymentManager.ts | 6 ++++++ src/services/storage/paymentStorage.ts | 6 +++++- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/services/main/appUserManager.ts b/src/services/main/appUserManager.ts index fa457a93..77b67653 100644 --- a/src/services/main/appUserManager.ts +++ b/src/services/main/appUserManager.ts @@ -123,6 +123,23 @@ export default class { async CleanupInactiveUsers() { this.log("Cleaning up inactive users") + const inactiveUsers = await this.storage.userStorage.GetInactiveUsers(365) + const toDelete:{userId: string, appUserIds: string[]}[] = [] + for (const u of inactiveUsers) { + const user = await this.storage.userStorage.GetUser(u.user_id) + if (user.balance_sats > 10_000) { + continue + } + const appUsers = await this.storage.applicationStorage.GetAllAppUsersFromUser(u.user_id) + toDelete.push({userId: u.user_id, appUserIds: appUsers.map(a => a.identifier)}) + } + + this.log("Found",toDelete.length, "inactive users to delete") + // await this.RemoveUsers(toDelete) + } + + async CleanupNeverActiveUsers() { + this.log("Cleaning up never active users") const inactiveUsers = await this.storage.userStorage.GetInactiveUsers(30) const toDelete:{userId: string, appUserIds: string[]}[] = [] for (const u of inactiveUsers) { @@ -146,11 +163,11 @@ export default class { toDelete.push({userId: u.user_id, appUserIds: appUsers.map(a => a.identifier)}) } - this.log("Found",toDelete.length, "inactive users to delete") - // await this.RemoveIntactiveUsers(toDelete) TODO: activate deletion + this.log("Found",toDelete.length, "never active users to delete") + // await this.RemoveUsers(toDelete) TODO: activate deletion } - async RemoveIntactiveUsers(toDelete: { userId: string, appUserIds: string[] }[]) { + async RemoveUsers(toDelete: { userId: string, appUserIds: string[] }[]) { this.log("Deleting",toDelete.length, "inactive users") for (let i = 0; i < toDelete.length; i++) { const {userId,appUserIds} = toDelete[i] diff --git a/src/services/main/init.ts b/src/services/main/init.ts index e21eab53..0d0cb245 100644 --- a/src/services/main/init.ts +++ b/src/services/main/init.ts @@ -75,7 +75,9 @@ export const initMainHandler = async (log: PubLogger, mainSettings: MainSettings return } await mainHandler.paymentManager.checkPendingPayments() + await mainHandler.paymentManager.CleanupOldUnpaidInvoices() await mainHandler.appUserManager.CleanupInactiveUsers() + await mainHandler.appUserManager.CleanupNeverActiveUsers() await mainHandler.paymentManager.watchDog.Start() return { mainHandler, apps, liquidityProviderInfo, liquidityProviderApp, wizard, adminManager } } diff --git a/src/services/main/paymentManager.ts b/src/services/main/paymentManager.ts index e6db1af1..2058570d 100644 --- a/src/services/main/paymentManager.ts +++ b/src/services/main/paymentManager.ts @@ -759,6 +759,12 @@ export default class { return newlyConfirmedTxs.filter(t => t !== undefined) as (PendingTx & { confs: number })[] } + async CleanupOldUnpaidInvoices() { + this.log("Cleaning up old unpaid invoices") + const affected = await this.storage.paymentStorage.RemoveOldUnpaidInvoices() + this.log("Cleaned up",affected, "old unpaid invoices") + } + async GetLndBalance() { return this.lnd.GetBalance() } diff --git a/src/services/storage/paymentStorage.ts b/src/services/storage/paymentStorage.ts index b327a444..5f3d15a9 100644 --- a/src/services/storage/paymentStorage.ts +++ b/src/services/storage/paymentStorage.ts @@ -1,5 +1,5 @@ import crypto from 'crypto'; -import { And, Between, Equal, FindOperator, IsNull, LessThanOrEqual, MoreThan, MoreThanOrEqual } from "typeorm" +import { And, Between, Equal, FindOperator, IsNull, LessThan, LessThanOrEqual, MoreThan, MoreThanOrEqual } from "typeorm" import { User } from './entity/User.js'; import { UserTransactionPayment } from './entity/UserTransactionPayment.js'; import { EphemeralKeyType, UserEphemeralKey } from './entity/UserEphemeralKey.js'; @@ -111,6 +111,10 @@ export default class { return items } + async RemoveOldUnpaidInvoices(txId?: string) { + return this.dbs.Delete('UserReceivingInvoice', { paid_at_unix: 0, expires_at_unix: LessThan(Math.floor(Date.now() / 1000)) }, txId) + } + async AddUserInvoice(user: User, invoice: string, options: InboundOptionals = { expiry: defaultInvoiceExpiry }, providerDestination?: string, txId?: string): Promise { return this.dbs.CreateAndSave('UserReceivingInvoice', { invoice: invoice,