fix: fixes after @Berhalak and @fflorent reviews

This commit is contained in:
Grégoire Cutzach 2024-09-24 11:36:43 +02:00
parent 9fbdafd7de
commit 06ff057082
No known key found for this signature in database
GPG Key ID: AA4155BE23C375E6

View File

@ -337,12 +337,7 @@ export class UsersManager {
email: string, email: string,
manager?: EntityManager manager?: EntityManager
): Promise<User|undefined> { ): Promise<User|undefined> {
const normalizedEmail = normalizeEmail(email); return await this._buildExistingUsersByLoginRequest([email], manager)
return await (manager || this._connection).createQueryBuilder()
.select('user')
.from(User, 'user')
.leftJoinAndSelect('user.logins', 'logins')
.where('email = :email', {email: normalizedEmail})
.getOne() || undefined; .getOne() || undefined;
} }
@ -353,12 +348,7 @@ export class UsersManager {
emails: string[], emails: string[],
manager?: EntityManager manager?: EntityManager
): Promise<User[]> { ): Promise<User[]> {
const normalizedEmails = emails.map(email=> normalizeEmail(email)); return await this._buildExistingUsersByLoginRequest(emails, manager)
return await (manager || this._connection).createQueryBuilder()
.select('user')
.from(User, 'user')
.leftJoinAndSelect('user.logins', 'logins')
.where('email IN (:...emails)', {emails: normalizedEmails})
.getMany(); .getMany();
} }
@ -487,6 +477,13 @@ export class UsersManager {
}); });
} }
/*
* This function is an alias of getUserByLogin
* Its purpose is to be more expressive and avoid confusion when reading code.
* FIXME :In the future it may be used to split getUserByLogin in two distinct functions
* One for creation
* the other for retrieving users in order to make it more maintainable
*/
public async createUser(email: string, options: GetUserOptions = {}): Promise<User|undefined> { public async createUser(email: string, options: GetUserOptions = {}): Promise<User|undefined> {
return await this.getUserByLogin(email, options); return await this.getUserByLogin(email, options);
} }
@ -633,15 +630,15 @@ export class UsersManager {
const emailMap = delta.users; const emailMap = delta.users;
const emails = Object.keys(emailMap); const emails = Object.keys(emailMap);
const existingUsers = await this.getExistingUsersByLogin(emails, transaction); const existingUsers = await this.getExistingUsersByLogin(emails, transaction);
const emailsExistingUsers = existingUsers.map(user=>user.loginEmail); const emailsExistingUsers = existingUsers.map(user => user.loginEmail);
const emailsUsersToCreate = emails.filter(email => ! emailsExistingUsers.includes(email)); const emailsUsersToCreate = emails.filter(email => !emailsExistingUsers.includes(email));
for (const email of emailsUsersToCreate){ const emailUsers = [...existingUsers];
for (const email of emailsUsersToCreate) {
const user = await this.createUser(email, {manager: transaction}); const user = await this.createUser(email, {manager: transaction});
if (user !== undefined) { if (user !== undefined) {
existingUsers.push(user); emailUsers.push(user);
} }
} }
const emailUsers = [...existingUsers];
emails.forEach((email, i) => { emails.forEach((email, i) => {
const userIdAffected = emailUsers[i]!.id; const userIdAffected = emailUsers[i]!.id;
// Org-level sharing with everyone would allow serious spamming - forbid it. // Org-level sharing with everyone would allow serious spamming - forbid it.
@ -793,4 +790,16 @@ export class UsersManager {
} }
delta.users = users; delta.users = users;
} }
private _buildExistingUsersByLoginRequest(
emails: string[],
manager?: EntityManager
) {
const normalizedEmails = emails.map(email=> normalizeEmail(email));
return (manager || this._connection).createQueryBuilder()
.select('user')
.from(User, 'user')
.leftJoinAndSelect('user.logins', 'logins')
.where('email IN (:...emails)', {emails: normalizedEmails});
}
} }