From aaa632d52ce2fe69f6098860e35d0912b8749264 Mon Sep 17 00:00:00 2001 From: anthonyyoussef01 <30884253+anthonyyoussef01@users.noreply.github.com> Date: Sun, 26 Apr 2026 05:13:15 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Performance:=20Use=20bulk=20updates?= =?UTF-8?q?=20for=20reassigning=20words=20and=20definitions?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../definitions/definitions.service.ts | 11 +++++++++ src/users/services/users/users.service.ts | 23 ++----------------- src/words/services/words/words.service.ts | 11 +++++++++ 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/definitions/services/definitions/definitions.service.ts b/src/definitions/services/definitions/definitions.service.ts index e89608f..cc61a24 100644 --- a/src/definitions/services/definitions/definitions.service.ts +++ b/src/definitions/services/definitions/definitions.service.ts @@ -314,4 +314,15 @@ export class DefinitionsService { }); return count === 0; } + + /** + * This updates the user id of all definitions posted by a specific user + * + * @param {number} oldUserId - the current user id + * @param {number} newUserId - the new user id + * @returns {Promise} - the update result + */ + async updateUserId(oldUserId: number, newUserId: number): Promise { + return await this.definitionsRepository.update({ userId: oldUserId }, { userId: newUserId }); + } } diff --git a/src/users/services/users/users.service.ts b/src/users/services/users/users.service.ts index b1e5ba7..3fd66d5 100644 --- a/src/users/services/users/users.service.ts +++ b/src/users/services/users/users.service.ts @@ -99,27 +99,8 @@ export class UsersService { } // Set all of the user's posted words and definitions to user with id=7 - const words = await this.wordsService.findByUserId(id); - const definitions = await this.definitionsService.findByUserId(id); - - for (const word of words) { - word.userId = 7; - const updateWordDto: UpdateWordDto = { - FrancoArabicWord: word.francoArabicWord, - arabicWord: word.arabicWord, - userId: 7, - }; - await this.wordsService.updateWord(user, word.id, updateWordDto); - } - - for (const definition of definitions) { - definition.userId = 7; - await this.definitionsService.updateDefinitionById( - user, - definition.id, - definition, - ); - } + await this.wordsService.updateUserId(id, 7); + await this.definitionsService.updateUserId(id, 7); // Delete the user return await this.usersRepository.delete(id); diff --git a/src/words/services/words/words.service.ts b/src/words/services/words/words.service.ts index 29a0d1f..9737c8d 100644 --- a/src/words/services/words/words.service.ts +++ b/src/words/services/words/words.service.ts @@ -255,4 +255,15 @@ export class WordsService { async findByUserId(userId: number): Promise { return this.wordsRepository.find({ where: { userId } }); } + + /** + * This updates the user id of all words posted by a specific user + * + * @param {number} oldUserId - the current user id + * @param {number} newUserId - the new user id + * @returns {Promise} - the update result + */ + async updateUserId(oldUserId: number, newUserId: number): Promise { + return await this.wordsRepository.update({ userId: oldUserId }, { userId: newUserId }); + } }