Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 6x 6x 6x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 2x 2x 2x 1x 1x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 3x 3x 3x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 1x | import { Injectable, NotFoundException } from '@nestjs/common';
import { EventBus } from '@nestjs/cqrs';
import { InjectRepository } from '@nestjs/typeorm';
import { Brackets, In, Repository, type QueryRunner } from 'typeorm';
import { User, type Company } from '@amalia/core/models';
import { type AuthenticatedContext } from '@amalia/kernel/auth/types';
import { RecordObject, type RecordType } from '@amalia/tenants/monitoring/audit/types';
import { type UserRole } from '@amalia/tenants/users/types';
import { UserGenericEvent } from './events/UserGenericEvent';
type UsersSearchParams = {
active?: boolean;
userExternalIds?: string[];
userIds?: string[];
role?: UserRole;
userEmails?: User['email'][];
};
/**
* Service user.
*/
@Injectable()
export class AppUsersRepository {
public constructor(
private readonly eventBus: EventBus,
@InjectRepository(User)
private readonly userRepository: Repository<User>,
) {}
/**
* Find user by email.
* @param email email
*/
public async findUserByEmail(email: string): Promise<User | null> {
return this.userRepository
.createQueryBuilder('user')
.where('LOWER(user.email) = :email', { email: email.toLowerCase() })
.leftJoinAndSelect('user.company', 'company')
.leftJoinAndSelect('user.impersonate', 'impersonate')
.leftJoinAndSelect('impersonate.company', 'impersonateCompany')
.cache(true)
.getOne();
}
/**
* Find user by id.
* @id id of the period
* @throws {NotFoundException}
*/
public async findOne(company: Company, id: string): Promise<User> {
const relations = ['company', 'impersonate'];
const user = await this.userRepository.findOne({
relations,
where: { company: { id: company.id }, id },
cache: true,
});
if (!user) {
throw new NotFoundException();
}
return user;
}
/**
* Find user by externalId.
* @param company
* @param externalId
*/
public async findByExternalId(company: Company, externalId: string): Promise<User | null> {
return this.userRepository.findOne({
where: {
company: { id: company.id },
externalId,
},
});
}
/**
* Helper to record for audit.
* @param value value to record
* @returns the values to record
*/
private payloadAuditValues(value: User) {
return {
email: value.email,
firstName: value.firstName,
lastName: value.lastName,
role: value.role,
currency: value.currency,
sourceId: value.externalIdSource,
id: value.id,
status: value.clearedAt ? 'deactivated' : 'activated',
};
}
/**
* Audit record.
* @param authenticatedContext
* @param actionType type of action
* @param newValues new values
* @param oldValues old values
*/
public async auditRecordForUser(
authenticatedContext: AuthenticatedContext,
actionType: RecordType,
newValues: User,
oldValues?: User,
) {
// no currentUser means invite has been launch from internal process of import
const values = {
newValues: this.payloadAuditValues(newValues),
oldValues: oldValues ? this.payloadAuditValues(oldValues) : undefined,
};
await this.eventBus.publish(
new UserGenericEvent({
authenticatedContext,
object: RecordObject.DIRECTORY,
type: actionType,
values: {
target: {
id: newValues.id,
name: `${newValues.firstName} ${newValues.lastName}`,
},
...values,
},
}),
);
}
/**
* Update existing user.
* @param user user
* @param parentQueryRunner
*/
public async update(user: Partial<User>, parentQueryRunner?: QueryRunner): Promise<User> {
const existingUser = await this.userRepository.findOneBy({ id: user.id });
// Merge existing user with new user.
const userToUpdate = { ...existingUser, ...user };
const userRepository = parentQueryRunner?.manager.getRepository<User>(User) ?? this.userRepository;
return userRepository.save(userToUpdate);
}
/**
* Search users of a company.
* @param company company to search users
* @param params additional parameters
* @param relations
*/
public async searchUsers(
company: Company,
{ active, role, userExternalIds, userIds, userEmails }: UsersSearchParams,
relations?: string[],
): Promise<User[]> {
const queryBuilder = this.userRepository
.createQueryBuilder('user')
.where('user.companyId = :companyId', { companyId: company.id });
if (active) {
queryBuilder.andWhere('user.clearedAt IS NULL');
}
if (relations) {
queryBuilder.setFindOptions({ relations });
}
if (userIds?.length || userExternalIds?.length || userEmails?.length || role) {
queryBuilder.andWhere(
new Brackets((subQueryBuilder) => {
if (userIds?.length) {
subQueryBuilder.orWhere('user.id IN (:...userIds)', { userIds });
}
if (userExternalIds?.length) {
subQueryBuilder.orWhere('user.externalId IN (:...userExternalIds)', { userExternalIds });
}
if (userEmails?.length) {
subQueryBuilder.orWhere('user.email IN (:...userEmails)', { userEmails });
}
if (role) {
subQueryBuilder.orWhere('user.role = :role', { role });
}
}),
);
}
return queryBuilder.getMany();
}
/**
* Find all users which id is in the list
* @param company
* @param idList list of ids
*/
public async findAll(company: Company, idList?: string[]): Promise<User[]> {
return this.userRepository.find({
where: {
company: { id: company.id },
id: idList ? In(idList) : undefined,
},
});
}
/**
* Find user list matching emails.
* @param emails
*/
public async findUsersByEmail(emails: string[]): Promise<User[]> {
return this.userRepository.find({
select: [
'id',
'email',
'firstName',
'lastName',
// settings are needed to avoid losing them when updating the user
'settings',
'companyId',
],
where: {
email: In(emails),
},
});
}
}
|