From 825a0277b8c65136fbb9d1f5597a41b732355d79 Mon Sep 17 00:00:00 2001 From: Anna Hughes Date: Tue, 16 Apr 2024 17:20:18 +0100 Subject: [PATCH] fix: getUsers function (#410) --- src/user/user.service.spec.ts | 8 ++--- src/user/user.service.ts | 65 ++++++++++++++--------------------- test/utils/mockedServices.ts | 3 ++ 3 files changed, 31 insertions(+), 45 deletions(-) diff --git a/src/user/user.service.spec.ts b/src/user/user.service.spec.ts index 6c4c6561..17ec0bd2 100644 --- a/src/user/user.service.spec.ts +++ b/src/user/user.service.spec.ts @@ -293,11 +293,9 @@ describe('UserService', () => { eventLog, ...userBase } = mockUserEntity; - jest.spyOn(repo, 'createQueryBuilder').mockImplementationOnce( - createQueryBuilderMock({ - getMany: jest.fn().mockResolvedValue([{ ...mockUserEntity, email: 'a@b.com' }]), - }) as never, - ); + jest + .spyOn(repo, 'find') + .mockImplementationOnce(async () => [{ ...mockUserEntity, email: 'a@b.com' }]); const users = await service.getUsers({ email: 'a@b.com' }, {}, [], 10); expect(users).toEqual([{ user: { ...userBase, email: 'a@b.com' }, partnerAccesses: [] }]); }); diff --git a/src/user/user.service.ts b/src/user/user.service.ts index 012b471d..8a0be8ef 100644 --- a/src/user/user.service.ts +++ b/src/user/user.service.ts @@ -11,7 +11,7 @@ import { CREATE_USER_INVALID_EMAIL, CREATE_USER_WEAK_PASSWORD, } from 'src/utils/errors'; -import { Repository } from 'typeorm'; +import { ILike, Repository } from 'typeorm'; import { addCrispProfile, deleteCrispProfile, @@ -407,46 +407,31 @@ export class UserService { fields: Array, limit: number, ): Promise { - const query = this.userRepository.createQueryBuilder('user'); - // TODO this needs some refactoring but deprioritised for now - if (relations.partnerAccess) { - query.leftJoinAndSelect('user.partnerAccess', 'partnerAccess'); - } - - if (relations.partnerAdmin) { - query.leftJoinAndSelect('user.partnerAdmin', 'partnerAdmin'); - } - - if (filters?.partnerAdmin?.partnerAdminId === 'IS NOT NULL') { - query.andWhere('partnerAdmin.partnerAdminId IS NOT NULL'); - } - - if (filters.partnerAccess?.userId === 'IS NOT NULL') { - query.andWhere('partnerAccess.userId IS NOT NULL'); - } - - if (filters.partnerAccess?.featureTherapy) { - query.andWhere('partnerAccess.featureTherapy = :featureTherapy', { - featureTherapy: filters.partnerAccess.featureTherapy, - }); - } - - if (filters.partnerAccess?.active) { - query.andWhere('partnerAccess.active = :active', { - active: filters.partnerAccess.active, - }); - } - - if (filters.email) { - query.andWhere('user.email ILike :email', { email: `%${filters.email}%` }); - } - - if (limit) { - query.limit(limit); - } + const users = await this.userRepository.find({ + relations: relations, + where: { + ...(filters.email && { email: ILike(filters.email) }), + ...(filters.partnerAccess && { + partnerAccess: { + ...(filters.partnerAccess.userId && { userId: filters.partnerAccess.userId }), + ...(typeof filters.partnerAccess.featureTherapy !== 'undefined' && { + featureTherapy: filters.partnerAccess.featureTherapy, + }), + ...(typeof filters.partnerAccess.active !== 'undefined' && { + active: filters.partnerAccess.active, + }), + }, + }), + ...(filters.partnerAdmin && { + partnerAdmin: { + ...(filters.partnerAdmin && { id: filters.partnerAdmin.partnerAdminId }), + }, + }), + }, + ...(limit && { take: limit }), + }); - const queryResult = await query.getMany(); - const formattedUsers = queryResult.map((user) => formatGetUsersObject(user)); + const formattedUsers = users.map((user) => formatGetUsersObject(user)); return formattedUsers; } } diff --git a/test/utils/mockedServices.ts b/test/utils/mockedServices.ts index 17ed5110..97273cc2 100644 --- a/test/utils/mockedServices.ts +++ b/test/utils/mockedServices.ts @@ -197,6 +197,9 @@ export const mockUserRepositoryMethodsFactory = { ...dto, }; }, + find: async () => { + return [mockUserEntity]; + }, findOneBy: ({ email: client_email }) => { return { ...mockUserEntity, ...(client_email ? { email: client_email } : {}) }; },