From e039cdc85cc95da25bbef086fd56199b367c841f Mon Sep 17 00:00:00 2001 From: Mateusz Kwasniewski Date: Thu, 7 Nov 2024 11:57:47 +0100 Subject: [PATCH] feat: count recently deleted users (#8688) --- src/lib/db/user-store.ts | 13 +++++++++++++ src/lib/types/stores/user-store.ts | 1 + src/test/e2e/stores/user-store.e2e.test.ts | 3 +++ src/test/fixtures/fake-user-store.ts | 4 ++++ 4 files changed, 21 insertions(+) diff --git a/src/lib/db/user-store.ts b/src/lib/db/user-store.ts index fb3d749ba5eb..18e5b4309239 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 2ca1f39a1e32..e3fe1aea44a2 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 93f587fa9713..230699e41fa9 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 a201d70c4029..f37b6c67d363 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)!; }