chore: remove recipeService (#659)
This commit is contained in:
parent
b8ef39bada
commit
230c0303b1
@ -1,8 +1,11 @@
|
|||||||
import { getAccountIdForRequest } from "@/src/services/loginService";
|
import { getAccountIdForRequest } from "@/src/services/loginService";
|
||||||
import { getJSONfromString } from "@/src/helpers/stringHelpers";
|
import { getJSONfromString } from "@/src/helpers/stringHelpers";
|
||||||
import { startRecipe } from "@/src/services/recipeService";
|
|
||||||
import { logger } from "@/src/utils/logger";
|
import { logger } from "@/src/utils/logger";
|
||||||
import { RequestHandler } from "express";
|
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 {
|
interface IStartRecipeRequest {
|
||||||
RecipeName: string;
|
RecipeName: string;
|
||||||
@ -15,6 +18,36 @@ export const startRecipeController: RequestHandler = async (req, res) => {
|
|||||||
|
|
||||||
const accountId = await getAccountIdForRequest(req);
|
const accountId = await getAccountIdForRequest(req);
|
||||||
|
|
||||||
const newRecipeId = await startRecipe(startRecipeRequest.RecipeName, accountId);
|
const recipeName = startRecipeRequest.RecipeName;
|
||||||
res.json(newRecipeId);
|
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() }
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
@ -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() }
|
|
||||||
};
|
|
||||||
};
|
|
Loading…
x
Reference in New Issue
Block a user