SpaceNinjaServer/src/controllers/api/updateQuestController.ts

33 lines
1.5 KiB
TypeScript
Raw Normal View History

2025-01-24 14:13:21 +01:00
import { RequestHandler } from "express";
import { parseString } from "@/src/helpers/general";
import { getJSONfromString } from "@/src/helpers/stringHelpers";
import { updateQuestKey, IUpdateQuestRequest } from "@/src/services/questService";
import { getInventory } from "@/src/services/inventoryService";
2025-01-31 17:24:42 +01:00
import { IInventoryChanges } from "@/src/types/purchaseTypes";
2025-01-24 14:13:21 +01:00
// eslint-disable-next-line @typescript-eslint/no-misused-promises
export const updateQuestController: RequestHandler = async (req, res) => {
const accountId = parseString(req.query.accountId);
const updateQuestRequest = getJSONfromString<IUpdateQuestRequest>((req.body as string).toString());
2025-01-24 14:13:21 +01:00
// updates should be made only to one quest key per request
if (updateQuestRequest.QuestKeys.length > 1) {
throw new Error(`quest keys array should only have 1 item, but has ${updateQuestRequest.QuestKeys.length}`);
}
const inventory = await getInventory(accountId);
2025-01-31 17:24:42 +01:00
const updateQuestResponse: { CustomData?: string; InventoryChanges?: IInventoryChanges; MissionRewards: [] } = {
MissionRewards: []
};
updateQuestResponse.InventoryChanges = await updateQuestKey(inventory, updateQuestRequest.QuestKeys);
2025-01-31 17:24:42 +01:00
//TODO: might need to parse the custom data and add the associated items to inventory
if (updateQuestRequest.QuestKeys[0].CustomData) {
updateQuestResponse.CustomData = updateQuestRequest.QuestKeys[0].CustomData;
2025-01-24 14:13:21 +01:00
}
await inventory.save();
2025-01-31 17:24:42 +01:00
res.send(updateQuestResponse);
2025-01-24 14:13:21 +01:00
};