From 230c0303b1c9918329b3668cf3f0318c3176012f Mon Sep 17 00:00:00 2001 From: Sainan Date: Tue, 31 Dec 2024 01:36:28 +0100 Subject: [PATCH] chore: remove recipeService (#659) --- src/controllers/api/startRecipeController.ts | 39 ++++++++++++++++++-- src/services/recipeService.ts | 39 -------------------- 2 files changed, 36 insertions(+), 42 deletions(-) delete mode 100644 src/services/recipeService.ts diff --git a/src/controllers/api/startRecipeController.ts b/src/controllers/api/startRecipeController.ts index c9d9fd40..04ad7a65 100644 --- a/src/controllers/api/startRecipeController.ts +++ b/src/controllers/api/startRecipeController.ts @@ -1,8 +1,11 @@ import { getAccountIdForRequest } from "@/src/services/loginService"; import { getJSONfromString } from "@/src/helpers/stringHelpers"; -import { startRecipe } from "@/src/services/recipeService"; import { logger } from "@/src/utils/logger"; import { RequestHandler } from "express"; +import { getRecipe } from "@/src/services/itemDataService"; +import { addMiscItems, getInventory, updateCurrency } from "@/src/services/inventoryService"; +import { unixTimesInMs } from "@/src/constants/timeConstants"; +import { Types } from "mongoose"; interface IStartRecipeRequest { RecipeName: string; @@ -15,6 +18,36 @@ export const startRecipeController: RequestHandler = async (req, res) => { const accountId = await getAccountIdForRequest(req); - const newRecipeId = await startRecipe(startRecipeRequest.RecipeName, accountId); - res.json(newRecipeId); + const recipeName = startRecipeRequest.RecipeName; + const recipe = getRecipe(recipeName); + + if (!recipe) { + logger.error(`unknown recipe ${recipeName}`); + throw new Error(`unknown recipe ${recipeName}`); + } + + await updateCurrency(recipe.buildPrice, false, accountId); + + const ingredientsInverse = recipe.ingredients.map(component => ({ + ItemType: component.ItemType, + ItemCount: component.ItemCount * -1 + })); + + const inventory = await getInventory(accountId); + addMiscItems(inventory, ingredientsInverse); + + //buildtime is in seconds + const completionDate = new Date(Date.now() + recipe.buildTime * unixTimesInMs.second); + + inventory.PendingRecipes.push({ + ItemType: recipeName, + CompletionDate: completionDate, + _id: new Types.ObjectId() + }); + + const newInventory = await inventory.save(); + + res.json({ + RecipeId: { $oid: newInventory.PendingRecipes[newInventory.PendingRecipes.length - 1]._id.toString() } + }); }; diff --git a/src/services/recipeService.ts b/src/services/recipeService.ts deleted file mode 100644 index 85309273..00000000 --- a/src/services/recipeService.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { unixTimesInMs } from "@/src/constants/timeConstants"; -import { addMiscItems, getInventory, updateCurrency } from "@/src/services/inventoryService"; -import { getRecipe } from "@/src/services/itemDataService"; -import { logger } from "@/src/utils/logger"; -import { Types } from "mongoose"; - -export const startRecipe = async (recipeName: string, accountId: string): Promise<{ RecipeId: { $oid: string } }> => { - const recipe = getRecipe(recipeName); - - if (!recipe) { - logger.error(`unknown recipe ${recipeName}`); - throw new Error(`unknown recipe ${recipeName}`); - } - - await updateCurrency(recipe.buildPrice, false, accountId); - - const ingredientsInverse = recipe.ingredients.map(component => ({ - ItemType: component.ItemType, - ItemCount: component.ItemCount * -1 - })); - - const inventory = await getInventory(accountId); - addMiscItems(inventory, ingredientsInverse); - - //buildtime is in seconds - const completionDate = new Date(Date.now() + recipe.buildTime * unixTimesInMs.second); - - inventory.PendingRecipes.push({ - ItemType: recipeName, - CompletionDate: completionDate, - _id: new Types.ObjectId() - }); - - const newInventory = await inventory.save(); - - return { - RecipeId: { $oid: newInventory.PendingRecipes[newInventory.PendingRecipes.length - 1]._id.toString() } - }; -};