2023-05-19 15:22:48 -03:00
|
|
|
import { RequestHandler } from "express";
|
2024-05-28 13:52:27 +02:00
|
|
|
import { getJSONfromString } from "@/src/helpers/stringHelpers";
|
|
|
|
import { getAccountIdForRequest } from "@/src/services/loginService";
|
2025-04-08 03:06:36 -07:00
|
|
|
import { addChallenges, getInventory } from "@/src/services/inventoryService";
|
2025-03-24 11:32:01 -07:00
|
|
|
import { IChallengeProgress, ISeasonChallenge } from "@/src/types/inventoryTypes/inventoryTypes";
|
2025-03-25 06:38:37 -07:00
|
|
|
import { IAffiliationMods } from "@/src/types/purchaseTypes";
|
2024-05-28 13:52:27 +02:00
|
|
|
|
2025-03-24 11:32:01 -07:00
|
|
|
export const updateChallengeProgressController: RequestHandler = async (req, res) => {
|
|
|
|
const challenges = getJSONfromString<IUpdateChallengeProgressRequest>(String(req.body));
|
2024-05-28 13:52:27 +02:00
|
|
|
const accountId = await getAccountIdForRequest(req);
|
2024-06-02 18:35:06 +03:00
|
|
|
|
2025-03-25 06:38:37 -07:00
|
|
|
const inventory = await getInventory(accountId, "ChallengeProgress SeasonChallengeHistory Affiliations");
|
2025-04-08 03:06:36 -07:00
|
|
|
let affiliationMods: IAffiliationMods[] = [];
|
2025-03-25 06:38:37 -07:00
|
|
|
if (challenges.ChallengeProgress) {
|
2025-04-08 03:06:36 -07:00
|
|
|
affiliationMods = addChallenges(inventory, challenges.ChallengeProgress, challenges.SeasonChallengeCompletions);
|
2025-03-25 06:38:37 -07:00
|
|
|
}
|
|
|
|
if (challenges.SeasonChallengeHistory) {
|
2025-04-08 03:06:36 -07:00
|
|
|
challenges.SeasonChallengeHistory.forEach(({ challenge, id }) => {
|
|
|
|
const itemIndex = inventory.SeasonChallengeHistory.findIndex(i => i.challenge === challenge);
|
|
|
|
if (itemIndex !== -1) {
|
|
|
|
inventory.SeasonChallengeHistory[itemIndex].id = id;
|
|
|
|
} else {
|
|
|
|
inventory.SeasonChallengeHistory.push({ challenge, id });
|
2025-03-25 06:38:37 -07:00
|
|
|
}
|
2025-04-08 03:06:36 -07:00
|
|
|
});
|
2025-03-25 06:38:37 -07:00
|
|
|
}
|
2025-03-24 11:32:01 -07:00
|
|
|
await inventory.save();
|
2024-06-02 18:35:06 +03:00
|
|
|
|
2025-03-25 06:38:37 -07:00
|
|
|
res.json({
|
|
|
|
AffiliationMods: affiliationMods
|
|
|
|
});
|
2023-05-19 15:22:48 -03:00
|
|
|
};
|
|
|
|
|
2025-03-24 11:32:01 -07:00
|
|
|
interface IUpdateChallengeProgressRequest {
|
2025-03-25 06:38:37 -07:00
|
|
|
ChallengeProgress?: IChallengeProgress[];
|
|
|
|
SeasonChallengeHistory?: ISeasonChallenge[];
|
|
|
|
SeasonChallengeCompletions?: ISeasonChallenge[];
|
2025-03-24 11:32:01 -07:00
|
|
|
}
|