diff --git a/src/lib/db/user-store.ts b/src/lib/db/user-store.ts index fb3d749ba5..18e5b43092 100644 --- a/src/lib/db/user-store.ts +++ b/src/lib/db/user-store.ts @@ -281,6 +281,19 @@ class UserStore implements IUserStore { .then((res) => Number(res[0].count)); } + async countRecentlyDeleted(): Promise { + return this.db(TABLE) + .whereNotNull('deleted_at') + .andWhere( + 'deleted_at', + '>=', + this.db.raw(`NOW() - INTERVAL '1 month'`), + ) + .andWhere({ is_service: false, is_system: false }) + .count('*') + .then((res) => Number(res[0].count)); + } + destroy(): void {} async exists(id: number): Promise { diff --git a/src/lib/types/stores/user-store.ts b/src/lib/types/stores/user-store.ts index 2ca1f39a1e..e3fe1aea44 100644 --- a/src/lib/types/stores/user-store.ts +++ b/src/lib/types/stores/user-store.ts @@ -38,5 +38,6 @@ export interface IUserStore extends Store { incLoginAttempts(user: IUser): Promise; successfullyLogin(user: IUser): Promise; count(): Promise; + countRecentlyDeleted(): Promise; countServiceAccounts(): Promise; } diff --git a/src/test/e2e/stores/user-store.e2e.test.ts b/src/test/e2e/stores/user-store.e2e.test.ts index 93f587fa97..230699e41f 100644 --- a/src/test/e2e/stores/user-store.e2e.test.ts +++ b/src/test/e2e/stores/user-store.e2e.test.ts @@ -192,4 +192,7 @@ test('should delete user', async () => { await expect(() => stores.userStore.get(user.id)).rejects.toThrow( new NotFoundError('No user found'), ); + + const deletedCount = await stores.userStore.countRecentlyDeleted(); + expect(deletedCount).toBe(1); }); diff --git a/src/test/fixtures/fake-user-store.ts b/src/test/fixtures/fake-user-store.ts index a201d70c40..f37b6c67d3 100644 --- a/src/test/fixtures/fake-user-store.ts +++ b/src/test/fixtures/fake-user-store.ts @@ -67,6 +67,10 @@ class UserStoreMock implements IUserStore { return this.data.length; } + async countRecentlyDeleted(): Promise { + return Promise.resolve(0); + } + async get(key: number): Promise { return this.data.find((u) => u.id === key)!; }