Skip to content

Commit

Permalink
fix function
Browse files Browse the repository at this point in the history
  • Loading branch information
annarhughes committed Apr 16, 2024
1 parent 149afd5 commit aa425e7
Showing 1 changed file with 25 additions and 40 deletions.
65 changes: 25 additions & 40 deletions src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -407,46 +407,31 @@ export class UserService {
fields: Array<string>,
limit: number,
): Promise<GetUserDto[] | undefined> {
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;
}
}

0 comments on commit aa425e7

Please sign in to comment.