From 1a101bf3e5a4fd3637cebee4c0233588122ab1f4 Mon Sep 17 00:00:00 2001 From: Sainan Date: Sat, 18 Jan 2025 16:27:13 +0100 Subject: [PATCH] chore: optimise creditsController ~37ms to ~5ms --- src/controllers/api/creditsController.ts | 2 +- src/services/inventoryService.ts | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/controllers/api/creditsController.ts b/src/controllers/api/creditsController.ts index 10ac10b3..c5dea4f7 100644 --- a/src/controllers/api/creditsController.ts +++ b/src/controllers/api/creditsController.ts @@ -6,7 +6,7 @@ import { getInventory } from "@/src/services/inventoryService"; export const creditsController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); - const inventory = await getInventory(accountId); + const inventory = await getInventory(accountId, "RegularCredits TradesRemaining PremiumCreditsFree PremiumCredits"); const response = { RegularCredits: inventory.RegularCredits, diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index e7567772..c58a17df 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -96,8 +96,11 @@ export const combineInventoryChanges = (InventoryChanges: IInventoryChanges, del } }; -export const getInventory = async (accountOwnerId: string): Promise => { - const inventory = await Inventory.findOne({ accountOwnerId: accountOwnerId }); +export const getInventory = async ( + accountOwnerId: string, + projection: string | undefined = undefined +): Promise => { + const inventory = await Inventory.findOne({ accountOwnerId: accountOwnerId }, projection); if (!inventory) { throw new Error(`Didn't find an inventory for ${accountOwnerId}`);