From 2279bd9ced1901ad628755f9cf37c9b728196b6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Cutzach?= Date: Tue, 15 Oct 2024 16:05:54 +0200 Subject: [PATCH] fix: Ensure emailUsers sync with delta.users using a map --- app/gen-server/lib/homedb/UsersManager.ts | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/app/gen-server/lib/homedb/UsersManager.ts b/app/gen-server/lib/homedb/UsersManager.ts index 5d6ff72448..2f1418acca 100644 --- a/app/gen-server/lib/homedb/UsersManager.ts +++ b/app/gen-server/lib/homedb/UsersManager.ts @@ -484,7 +484,7 @@ export class UsersManager { * One for creation * the other for retrieving users in order to make it more maintainable */ - public async createUser(email: string, options: GetUserOptions = {}): Promise { + public async createUser(email: string, options: GetUserOptions = {}): Promise { return await this.getUserByLogin(email, options); } @@ -632,15 +632,13 @@ export class UsersManager { const existingUsers = await this.getExistingUsersByLogin(emails, transaction); const emailsExistingUsers = existingUsers.map(user => user.loginEmail); 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) { const user = await this.createUser(email, {manager: transaction}); - if (user !== undefined) { - emailUsers.push(user); - } + emailUsers.set(user.loginEmail, user); } - emails.forEach((email, i) => { - const userIdAffected = emailUsers[i]!.id; + emails.forEach((email) => { + const userIdAffected = emailUsers.get(email)!.id; // Org-level sharing with everyone would allow serious spamming - forbid it. if (emailMap[email] !== null && // allow removing anything userId !== this.getSupportUserId() && // allow support user latitude