fix: Ensure emailUsers sync with delta.users using a map

This commit is contained in:
Grégoire Cutzach 2024-10-15 16:05:54 +02:00
parent 1a89799192
commit 32785609f5
No known key found for this signature in database
GPG Key ID: AA4155BE23C375E6

View File

@ -484,7 +484,7 @@ export class UsersManager {
* One for creation * One for creation
* the other for retrieving users in order to make it more maintainable * 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> {
return await this.getUserByLogin(email, options); return await this.getUserByLogin(email, options);
} }
@ -632,15 +632,13 @@ export class UsersManager {
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));
const emailUsers = [...existingUsers]; const emailUsers = new Map(existingUsers.map(user => [user.loginEmail, user]));
for (const email of emailsUsersToCreate) { for (const email of emailsUsersToCreate) {
const user = await this.createUser(email, {manager: transaction}); const user = await this.createUser(email, {manager: transaction});
if (user !== undefined) { emailUsers.set(user.loginEmail, user);
emailUsers.push(user);
} }
} emails.forEach((email) => {
emails.forEach((email, i) => { const userIdAffected = emailUsers.get(email)!.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.
if (emailMap[email] !== null && // allow removing anything if (emailMap[email] !== null && // allow removing anything
userId !== this.getSupportUserId() && // allow support user latitude userId !== this.getSupportUserId() && // allow support user latitude