Skip to content

Commit

Permalink
feat: count recently deleted users
Browse files Browse the repository at this point in the history
  • Loading branch information
kwasniew committed Nov 7, 2024
1 parent f5eafe6 commit 1cf2b81
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/lib/db/user-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,19 @@ class UserStore implements IUserStore {
.then((res) => Number(res[0].count));
}

async countRecentlyDeleted(): Promise<number> {
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<boolean> {
Expand Down
1 change: 1 addition & 0 deletions src/lib/types/stores/user-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,6 @@ export interface IUserStore extends Store<IUser, number> {
incLoginAttempts(user: IUser): Promise<void>;
successfullyLogin(user: IUser): Promise<number>;
count(): Promise<number>;
countRecentlyDeleted(): Promise<number>;
countServiceAccounts(): Promise<number>;
}
3 changes: 3 additions & 0 deletions src/test/e2e/stores/user-store.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
4 changes: 4 additions & 0 deletions src/test/fixtures/fake-user-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ class UserStoreMock implements IUserStore {
return this.data.length;
}

async countRecentlyDeleted(): Promise<number> {
return Promise.resolve(0);
}

async get(key: number): Promise<IUser> {
return this.data.find((u) => u.id === key)!;
}
Expand Down

0 comments on commit 1cf2b81

Please sign in to comment.