SpaceNinjaServer/src/controllers/api/claimCompletedRecipeController.ts

88 lines
3.4 KiB
TypeScript
Raw Normal View History

2024-01-25 14:49:45 +01:00
//this is a controller for the claimCompletedRecipe route
//it will claim a recipe for the user
import { RequestHandler } from "express";
2024-01-25 14:49:45 +01:00
import { logger } from "@/src/utils/logger";
2024-06-19 10:32:16 +02:00
import { getRecipe } from "@/src/services/itemDataService";
2024-01-25 14:49:45 +01:00
import { IOid } from "@/src/types/commonTypes";
import { getJSONfromString } from "@/src/helpers/stringHelpers";
2024-05-28 13:45:06 +02:00
import { getAccountIdForRequest } from "@/src/services/loginService";
2024-06-19 10:32:16 +02:00
import { getInventory, updateCurrency, addItem, addMiscItems, addRecipes } from "@/src/services/inventoryService";
2024-01-25 14:49:45 +01:00
export interface IClaimCompletedRecipeRequest {
RecipeIds: IOid[];
}
export const claimCompletedRecipeController: RequestHandler = async (req, res) => {
const claimCompletedRecipeRequest = getJSONfromString(String(req.body)) as IClaimCompletedRecipeRequest;
2024-05-28 13:45:06 +02:00
const accountId = await getAccountIdForRequest(req);
2024-01-25 14:49:45 +01:00
if (!accountId) throw new Error("no account id");
const inventory = await getInventory(accountId);
const pendingRecipe = inventory.PendingRecipes.find(
recipe => recipe._id?.toString() === claimCompletedRecipeRequest.RecipeIds[0].$oid
);
if (!pendingRecipe) {
logger.error(`no pending recipe found with id ${claimCompletedRecipeRequest.RecipeIds[0].$oid}`);
throw new Error(`no pending recipe found with id ${claimCompletedRecipeRequest.RecipeIds[0].$oid}`);
}
//check recipe is indeed ready to be completed
// if (pendingRecipe.CompletionDate > new Date()) {
// logger.error(`recipe ${pendingRecipe._id} is not ready to be completed`);
// throw new Error(`recipe ${pendingRecipe._id} is not ready to be completed`);
// }
inventory.PendingRecipes.pull(pendingRecipe._id);
await inventory.save();
2024-01-25 14:49:45 +01:00
2024-06-19 10:32:16 +02:00
const recipe = getRecipe(pendingRecipe.ItemType);
if (!recipe) {
2024-01-25 14:49:45 +01:00
logger.error(`no completed item found for recipe ${pendingRecipe._id}`);
throw new Error(`no completed item found for recipe ${pendingRecipe._id}`);
}
if (req.query.cancel) {
2024-06-19 10:32:16 +02:00
const currencyChanges = await updateCurrency(recipe.buildPrice * -1, false, accountId);
const inventory = await getInventory(accountId);
addMiscItems(inventory, recipe.ingredients);
await inventory.save();
// Not a bug: In the specific case of cancelling a recipe, InventoryChanges are expected to be the root.
res.json({
...currencyChanges,
MiscItems: recipe.ingredients
});
} else {
2024-06-19 10:32:16 +02:00
logger.debug("Claiming Recipe", { recipe, pendingRecipe });
let InventoryChanges = {};
if (recipe.consumeOnUse) {
const recipeChanges = [
{
ItemType: pendingRecipe.ItemType,
ItemCount: -1
}
];
InventoryChanges = { ...InventoryChanges, Recipes: recipeChanges };
const inventory = await getInventory(accountId);
addRecipes(inventory, recipeChanges);
await inventory.save();
}
2024-06-19 10:32:16 +02:00
if (req.query.rush) {
InventoryChanges = {
...InventoryChanges,
...(await updateCurrency(recipe.skipBuildTimePrice, true, accountId))
};
}
res.json({
InventoryChanges: {
2024-06-19 10:32:16 +02:00
...InventoryChanges,
...(await addItem(accountId, recipe.resultType, recipe.num)).InventoryChanges
}
});
}
2024-01-25 14:49:45 +01:00
};