chore: optimise creditsController #815

Merged
Sainan merged 1 commits from optimise-creditsController into main 2025-01-18 16:58:47 -08:00
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) => { export const creditsController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req); const accountId = await getAccountIdForRequest(req);
const inventory = await getInventory(accountId); const inventory = await getInventory(accountId, "RegularCredits TradesRemaining PremiumCreditsFree PremiumCredits");
const response = { const response = {
RegularCredits: inventory.RegularCredits, RegularCredits: inventory.RegularCredits,

View File

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