diff --git a/src/services/main/appUserManager.ts b/src/services/main/appUserManager.ts index b81f8e2e..b87a648d 100644 --- a/src/services/main/appUserManager.ts +++ b/src/services/main/appUserManager.ts @@ -176,6 +176,11 @@ export default class { this.log("Deleting", toDelete.length, "inactive users") for (let i = 0; i < toDelete.length; i++) { const { userId, appUserIds } = toDelete[i] + const user = await this.storage.userStorage.FindUser(userId) + if (!user || user.balance_sats > 0) { + if (user) this.log("Skipping user", userId, "has balance", user.balance_sats) + continue + } this.log("Deleting user", userId, "progress", i + 1, "/", toDelete.length) await this.storage.StartTransaction(async tx => { for (const appUserId of appUserIds) { @@ -183,11 +188,12 @@ export default class { await this.storage.offerStorage.DeleteUserOffers(appUserId, tx) await this.storage.debitStorage.RemoveUserDebitAccess(appUserId, tx) await this.storage.applicationStorage.RemoveAppUserDevices(appUserId, tx) - } await this.storage.paymentStorage.RemoveUserInvoices(userId, tx) await this.storage.productStorage.RemoveUserProducts(userId, tx) await this.storage.paymentStorage.RemoveUserEphemeralKeys(userId, tx) + await this.storage.userStorage.DeleteUserAccess(userId, tx) + await this.storage.applicationStorage.RemoveAppUsersAndBaseUsers(appUserIds, userId, tx) }) } this.log("Cleaned up inactive users") diff --git a/src/services/storage/applicationStorage.ts b/src/services/storage/applicationStorage.ts index 08aad37a..77438ee3 100644 --- a/src/services/storage/applicationStorage.ts +++ b/src/services/storage/applicationStorage.ts @@ -161,10 +161,16 @@ export default class { this.dbs.Remove('User', baseUser, txId) } - async RemoveAppUsersAndBaseUsers(appUserIds: string[],baseUser:string, txId?: string) { - await this.dbs.Delete('ApplicationUser', { identifier: In(appUserIds) }, txId) - await this.dbs.Delete('User', { user_id: baseUser }, txId) - + async RemoveAppUsersAndBaseUsers(appUserIds: string[], baseUser: string, txId?: string) { + if (appUserIds.length > 0) { + const appUsers = await this.dbs.Find('ApplicationUser', { where: { identifier: In(appUserIds) } }, txId) + for (const appUser of appUsers) { + await this.dbs.Delete('ApplicationUser', appUser.serial_id, txId) + } + } + const user = await this.userStorage.FindUser(baseUser, txId) + if (!user) return + await this.dbs.Delete('User', user.serial_id, txId) } diff --git a/src/services/storage/userStorage.ts b/src/services/storage/userStorage.ts index d3453346..a1541bac 100644 --- a/src/services/storage/userStorage.ts +++ b/src/services/storage/userStorage.ts @@ -126,4 +126,8 @@ export default class { const lastSeenAtUnix = now - seconds return this.dbs.Find('UserAccess', { where: { last_seen_at_unix: LessThan(lastSeenAtUnix) } }) } + + async DeleteUserAccess(userId: string, txId?: string) { + return this.dbs.Delete('UserAccess', { user_id: userId }, txId) + } }