From 9e0dd3e0a5cf0c1f888de6644ec44593ad70a7f0 Mon Sep 17 00:00:00 2001 From: Sainan <63328889+Sainan@users.noreply.github.com> Date: Mon, 31 Mar 2025 04:26:44 -0700 Subject: [PATCH] chore: run save operations in parallel where possible (#1401) Reviewed-on: https://onlyg.it/OpenWF/SpaceNinjaServer/pulls/1401 Co-authored-by: Sainan <63328889+Sainan@users.noreply.github.com> Co-committed-by: Sainan <63328889+Sainan@users.noreply.github.com> --- .../api/contributeToDojoComponentController.ts | 4 +--- .../api/contributeToVaultController.ts | 16 +++++++++++----- .../api/dojoComponentRushController.ts | 6 ++---- src/controllers/api/guildTechController.ts | 4 +--- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/controllers/api/contributeToDojoComponentController.ts b/src/controllers/api/contributeToDojoComponentController.ts index acdbf8a4..6d0016eb 100644 --- a/src/controllers/api/contributeToDojoComponentController.ts +++ b/src/controllers/api/contributeToDojoComponentController.ts @@ -64,9 +64,7 @@ export const contributeToDojoComponentController: RequestHandler = async (req, r } } - await guild.save(); - await inventory.save(); - await guildMember.save(); + await Promise.all([guild.save(), inventory.save(), guildMember.save()]); res.json({ ...(await getDojoClient(guild, 0, component._id)), InventoryChanges: inventoryChanges diff --git a/src/controllers/api/contributeToVaultController.ts b/src/controllers/api/contributeToVaultController.ts index 74dc91a4..7960c951 100644 --- a/src/controllers/api/contributeToVaultController.ts +++ b/src/controllers/api/contributeToVaultController.ts @@ -4,14 +4,20 @@ import { addVaultMiscItems, getGuildForRequestEx } from "@/src/services/guildService"; -import { addFusionTreasures, addMiscItems, addShipDecorations, getInventory } from "@/src/services/inventoryService"; +import { + addFusionTreasures, + addMiscItems, + addShipDecorations, + getInventory, + updateCurrency +} from "@/src/services/inventoryService"; import { getAccountIdForRequest } from "@/src/services/loginService"; import { IFusionTreasure, IMiscItem, ITypeCount } from "@/src/types/inventoryTypes/inventoryTypes"; import { RequestHandler } from "express"; export const contributeToVaultController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); - const inventory = await getInventory(accountId); + const inventory = await getInventory(accountId, "GuildId RegularCredits MiscItems ShipDecorations FusionTreasures"); const guild = await getGuildForRequestEx(req, inventory); const guildMember = (await GuildMember.findOne( { accountId, guildId: guild._id }, @@ -20,6 +26,8 @@ export const contributeToVaultController: RequestHandler = async (req, res) => { const request = JSON.parse(String(req.body)) as IContributeToVaultRequest; if (request.RegularCredits) { + updateCurrency(inventory, request.RegularCredits, false); + guild.VaultRegularCredits ??= 0; guild.VaultRegularCredits += request.RegularCredits; @@ -52,9 +60,7 @@ export const contributeToVaultController: RequestHandler = async (req, res) => { } } - await guild.save(); - await inventory.save(); - await guildMember.save(); + await Promise.all([guild.save(), inventory.save(), guildMember.save()]); res.end(); }; diff --git a/src/controllers/api/dojoComponentRushController.ts b/src/controllers/api/dojoComponentRushController.ts index 33aec126..899ed0af 100644 --- a/src/controllers/api/dojoComponentRushController.ts +++ b/src/controllers/api/dojoComponentRushController.ts @@ -47,13 +47,11 @@ export const dojoComponentRushController: RequestHandler = async (req, res) => { } } - await guild.save(); - await inventory.save(); - const guildMember = (await GuildMember.findOne({ accountId, guildId: guild._id }, "PremiumCreditsContributed"))!; guildMember.PremiumCreditsContributed ??= 0; guildMember.PremiumCreditsContributed += request.Amount; - await guildMember.save(); + + await Promise.all([guild.save(), inventory.save(), guildMember.save()]); res.json({ ...(await getDojoClient(guild, 0, component._id)), diff --git a/src/controllers/api/guildTechController.ts b/src/controllers/api/guildTechController.ts index 08909b08..5b0b5374 100644 --- a/src/controllers/api/guildTechController.ts +++ b/src/controllers/api/guildTechController.ts @@ -157,9 +157,7 @@ export const guildTechController: RequestHandler = async (req, res) => { // Check if research is fully funded now. await processGuildTechProjectContributionsUpdate(guild, techProject); - await guild.save(); - await inventory.save(); - await guildMember.save(); + await Promise.all([guild.save(), inventory.save(), guildMember.save()]); res.json({ InventoryChanges: inventoryChanges, Vault: getGuildVault(guild)