2024-01-25 14:49:45 +01:00
|
|
|
//this is a controller for the claimCompletedRecipe route
|
|
|
|
//it will claim a recipe for the user
|
|
|
|
|
2024-05-06 15:19:42 +02:00
|
|
|
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-12-31 01:41:29 +01:00
|
|
|
import {
|
|
|
|
getInventory,
|
|
|
|
updateCurrency,
|
|
|
|
addItem,
|
|
|
|
addMiscItems,
|
|
|
|
addRecipes,
|
|
|
|
updateCurrencyByAccountId
|
|
|
|
} from "@/src/services/inventoryService";
|
2024-01-25 14:49:45 +01:00
|
|
|
|
|
|
|
export interface IClaimCompletedRecipeRequest {
|
|
|
|
RecipeIds: IOid[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export const claimCompletedRecipeController: RequestHandler = async (req, res) => {
|
2024-06-24 12:37:28 +02:00
|
|
|
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) {
|
|
|
|
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()) {
|
|
|
|
// throw new Error(`recipe ${pendingRecipe._id} is not ready to be completed`);
|
|
|
|
// }
|
|
|
|
|
2024-06-15 02:50:43 +02:00
|
|
|
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-12-29 06:00:51 +01:00
|
|
|
throw new Error(`no completed item found for recipe ${pendingRecipe._id.toString()}`);
|
2024-01-25 14:49:45 +01:00
|
|
|
}
|
|
|
|
|
2024-06-15 02:50:43 +02:00
|
|
|
if (req.query.cancel) {
|
2024-06-19 10:32:16 +02:00
|
|
|
const inventory = await getInventory(accountId);
|
2024-12-31 01:41:29 +01:00
|
|
|
const currencyChanges = updateCurrency(inventory, recipe.buildPrice * -1, false);
|
2024-06-19 10:32:16 +02:00
|
|
|
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
|
|
|
|
});
|
2024-06-15 02:50:43 +02:00
|
|
|
} 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-17 16:41:02 +02:00
|
|
|
}
|
2024-06-19 10:32:16 +02:00
|
|
|
if (req.query.rush) {
|
|
|
|
InventoryChanges = {
|
|
|
|
...InventoryChanges,
|
2024-12-31 01:41:29 +01:00
|
|
|
...(await updateCurrencyByAccountId(recipe.skipBuildTimePrice, true, accountId))
|
2024-06-19 10:32:16 +02:00
|
|
|
};
|
2024-06-15 02:50:43 +02:00
|
|
|
}
|
2025-01-04 00:25:09 +01:00
|
|
|
const inventory = await getInventory(accountId);
|
|
|
|
InventoryChanges = {
|
|
|
|
...InventoryChanges,
|
|
|
|
...(await addItem(inventory, recipe.resultType, recipe.num)).InventoryChanges
|
|
|
|
};
|
|
|
|
await inventory.save();
|
|
|
|
res.json({ InventoryChanges });
|
2024-06-15 02:50:43 +02:00
|
|
|
}
|
2024-01-25 14:49:45 +01:00
|
|
|
};
|