chore: optimise creditsController

~37ms to ~5ms
This commit is contained in:
Sainan 2025-01-18 16:27:13 +01:00
parent f1c3dcbefc
commit 1a101bf3e5
2 changed files with 6 additions and 3 deletions

View File

@ -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,

View File

@ -96,8 +96,11 @@ export const combineInventoryChanges = (InventoryChanges: IInventoryChanges, del
}
};
export const getInventory = async (accountOwnerId: string): Promise<TInventoryDatabaseDocument> => {
const inventory = await Inventory.findOne({ accountOwnerId: accountOwnerId });
export const getInventory = async (
accountOwnerId: string,
projection: string | undefined = undefined
): Promise<TInventoryDatabaseDocument> => {
const inventory = await Inventory.findOne({ accountOwnerId: accountOwnerId }, projection);
if (!inventory) {
throw new Error(`Didn't find an inventory for ${accountOwnerId}`);