SpaceNinjaServer/src/controllers/api/updateQuestController.ts
Sainan b553097fe4 fix: handle quest completion via missionInventoryUpdate (#1140)
Partial fix for #1126

Reviewed-on: OpenWF/SpaceNinjaServer#1140
Co-authored-by: Sainan <sainan@calamity.inc>
Co-committed-by: Sainan <sainan@calamity.inc>
2025-03-10 16:22:02 -07:00

33 lines
1.5 KiB
TypeScript

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";
import { IInventoryChanges } from "@/src/types/purchaseTypes";
// 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());
// 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);
const updateQuestResponse: { CustomData?: string; InventoryChanges?: IInventoryChanges; MissionRewards: [] } = {
MissionRewards: []
};
updateQuestResponse.InventoryChanges = await updateQuestKey(inventory, updateQuestRequest.QuestKeys);
//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;
}
await inventory.save();
res.send(updateQuestResponse);
};