From dc2039b45a9af8afdec5e724ab0f3b93f51d7b61 Mon Sep 17 00:00:00 2001 From: naskya Date: Wed, 20 Mar 2024 18:45:50 +0900 Subject: [PATCH] refactor (backend): change variable name, await asyncronous processes --- packages/backend/src/services/note/delete.ts | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/packages/backend/src/services/note/delete.ts b/packages/backend/src/services/note/delete.ts index d7fddc7ac1..cf20a2015f 100644 --- a/packages/backend/src/services/note/delete.ts +++ b/packages/backend/src/services/note/delete.ts @@ -148,11 +148,11 @@ export default async function ( instanceNotesCountDecreasement[user.host] ??= 0; instanceNotesCountDecreasement[user.host]++; } - for (const [host, number] of Object.entries( + for (const [host, count] of Object.entries( instanceNotesCountDecreasement, )) { registerOrFetchInstanceDoc(host).then((i) => { - Instances.decrement({ id: i.id }, "notesCount", number); + Instances.decrement({ id: i.id }, "notesCount", count); }); } } @@ -163,10 +163,12 @@ export default async function ( userId: user.id, }); - for (const [_, affectedUser] of Object.entries(affectedLocalUsers)) { - // For the case of cascading deletion, it cannot be solved by simply reducing the notesCount by 1. - recalculateNotesCountOfUser(affectedUser); - } + // Handle cascading deletion (it's not as simple as notesCount -= 1) + await Promise.all( + Object.values(affectedLocalUsers).map((user) => + recalculateNotesCountOfUser(user), + ), + ); } }