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-15 02:50:43 +02:00
|
|
|
import { getItemByBlueprint } 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-15 02:50:43 +02:00
|
|
|
import { getInventory, updateCurrency, addItem } from "@/src/services/inventoryService";
|
2024-01-25 14:49:45 +01:00
|
|
|
|
|
|
|
export interface IClaimCompletedRecipeRequest {
|
|
|
|
RecipeIds: IOid[];
|
|
|
|
}
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-misused-promises
|
|
|
|
export const claimCompletedRecipeController: RequestHandler = async (req, res) => {
|
|
|
|
const claimCompletedRecipeRequest = getJSONfromString(req.body.toString()) 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`);
|
|
|
|
// }
|
|
|
|
|
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-15 02:50:43 +02:00
|
|
|
const buildable = getItemByBlueprint(pendingRecipe.ItemType);
|
|
|
|
if (!buildable) {
|
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}`);
|
|
|
|
}
|
|
|
|
|
2024-06-15 02:50:43 +02:00
|
|
|
if (req.query.cancel) {
|
|
|
|
// TODO: Refund items
|
|
|
|
res.json({});
|
|
|
|
} else {
|
|
|
|
logger.debug("Claiming Recipe", { buildable, pendingRecipe });
|
2024-06-17 16:41:02 +02:00
|
|
|
if (buildable.consumeOnUse) {
|
|
|
|
// TODO: Remove one instance of this recipe, and include that in InventoryChanges.
|
|
|
|
}
|
2024-06-15 02:50:43 +02:00
|
|
|
let currencyChanges = {};
|
|
|
|
if (req.query.rush && buildable.skipBuildTimePrice) {
|
|
|
|
currencyChanges = await updateCurrency(buildable.skipBuildTimePrice, true, accountId);
|
|
|
|
}
|
|
|
|
res.json({
|
|
|
|
InventoryChanges: {
|
|
|
|
...currencyChanges,
|
2024-06-17 16:41:02 +02:00
|
|
|
...(await addItem(accountId, buildable.resultType, buildable.num)).InventoryChanges
|
2024-06-15 02:50:43 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2024-01-25 14:49:45 +01:00
|
|
|
};
|