From 939d3a3ec25037e765d4bcec11a5844781f686f0 Mon Sep 17 00:00:00 2001 From: Sainan <63328889+Sainan@users.noreply.github.com> Date: Fri, 9 May 2025 09:05:14 +0200 Subject: [PATCH] support legacy oid for BrandedSuits --- .../api/claimCompletedRecipeController.ts | 16 +++++++--------- src/controllers/api/inventoryController.ts | 5 +++++ src/helpers/inventoryHelpers.ts | 11 ++++++++++- src/services/inventoryService.ts | 2 +- src/types/inventoryTypes/inventoryTypes.ts | 4 ++-- 5 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/controllers/api/claimCompletedRecipeController.ts b/src/controllers/api/claimCompletedRecipeController.ts index f1fa6f6e..173c7221 100644 --- a/src/controllers/api/claimCompletedRecipeController.ts +++ b/src/controllers/api/claimCompletedRecipeController.ts @@ -4,9 +4,9 @@ import { RequestHandler } from "express"; import { logger } from "@/src/utils/logger"; import { getRecipe } from "@/src/services/itemDataService"; -import { IOid } from "@/src/types/commonTypes"; +import { IOid, IOidWithLegacySupport } from "@/src/types/commonTypes"; import { getJSONfromString } from "@/src/helpers/stringHelpers"; -import { getAccountIdForRequest } from "@/src/services/loginService"; +import { getAccountForRequest } from "@/src/services/loginService"; import { getInventory, updateCurrency, @@ -18,7 +18,7 @@ import { import { IInventoryChanges } from "@/src/types/purchaseTypes"; import { IEquipmentClient } from "@/src/types/inventoryTypes/commonInventoryTypes"; import { InventorySlot } from "@/src/types/inventoryTypes/inventoryTypes"; -import { toOid } from "@/src/helpers/inventoryHelpers"; +import { toOid2 } from "@/src/helpers/inventoryHelpers"; interface IClaimCompletedRecipeRequest { RecipeIds: IOid[]; @@ -26,10 +26,8 @@ interface IClaimCompletedRecipeRequest { export const claimCompletedRecipeController: RequestHandler = async (req, res) => { const claimCompletedRecipeRequest = getJSONfromString(String(req.body)); - const accountId = await getAccountIdForRequest(req); - if (!accountId) throw new Error("no account id"); - - const inventory = await getInventory(accountId); + const account = await getAccountForRequest(req); + const inventory = await getInventory(account._id.toString()); const pendingRecipe = inventory.PendingRecipes.id(claimCompletedRecipeRequest.RecipeIds[0].$oid); if (!pendingRecipe) { throw new Error(`no pending recipe found with id ${claimCompletedRecipeRequest.RecipeIds[0].$oid}`); @@ -81,7 +79,7 @@ export const claimCompletedRecipeController: RequestHandler = async (req, res) = } else { logger.debug("Claiming Recipe", { recipe, pendingRecipe }); - let BrandedSuits: undefined | IOid[]; + let BrandedSuits: undefined | IOidWithLegacySupport[]; if (recipe.secretIngredientAction == "SIA_SPECTRE_LOADOUT_COPY") { inventory.PendingSpectreLoadouts ??= []; inventory.SpectreLoadouts ??= []; @@ -106,7 +104,7 @@ export const claimCompletedRecipeController: RequestHandler = async (req, res) = inventory.BrandedSuits!.findIndex(x => x.equals(pendingRecipe.SuitToUnbrand)), 1 ); - BrandedSuits = [toOid(pendingRecipe.SuitToUnbrand!)]; + BrandedSuits = [toOid2(pendingRecipe.SuitToUnbrand!, account.BuildLabel)]; } let InventoryChanges: IInventoryChanges = {}; diff --git a/src/controllers/api/inventoryController.ts b/src/controllers/api/inventoryController.ts index db94ecaa..a26a73c5 100644 --- a/src/controllers/api/inventoryController.ts +++ b/src/controllers/api/inventoryController.ts @@ -327,6 +327,11 @@ export const getInventoryResponse = async ( for (const upgrade of inventoryResponse.Upgrades) { toLegacyOid(upgrade.ItemId); } + if (inventoryResponse.BrandedSuits) { + for (const id of inventoryResponse.BrandedSuits) { + toLegacyOid(id); + } + } } } } diff --git a/src/helpers/inventoryHelpers.ts b/src/helpers/inventoryHelpers.ts index a860e860..6ced29e9 100644 --- a/src/helpers/inventoryHelpers.ts +++ b/src/helpers/inventoryHelpers.ts @@ -20,9 +20,18 @@ export const version_compare = (a: string, b: string): number => { }; export const toOid = (objectId: Types.ObjectId): IOid => { - return { $oid: objectId.toString() } satisfies IOid; + return { $oid: objectId.toString() }; }; +export function toOid2(objectId: Types.ObjectId, buildLabel: undefined): IOid; +export function toOid2(objectId: Types.ObjectId, buildLabel: string | undefined): IOidWithLegacySupport; +export function toOid2(objectId: Types.ObjectId, buildLabel: string | undefined): IOidWithLegacySupport { + if (buildLabel && version_compare(buildLabel, "2016.12.21.19.13") <= 0) { + return { $id: objectId.toString() }; + } + return { $oid: objectId.toString() }; +} + export const toLegacyOid = (oid: IOidWithLegacySupport): void => { if (!("$id" in oid)) { oid.$id = oid.$oid; diff --git a/src/services/inventoryService.ts b/src/services/inventoryService.ts index 4111209c..2beba2e7 100644 --- a/src/services/inventoryService.ts +++ b/src/services/inventoryService.ts @@ -424,7 +424,7 @@ export const addItem = async ( changes.push({ ItemType: egg.ItemType, ExpirationDate: { $date: { $numberLong: "2000000000000" } }, - ItemId: toOid(egg._id) + ItemId: toOid(egg._id) // TODO: Pass on buildLabel from purchaseService }); } return { diff --git a/src/types/inventoryTypes/inventoryTypes.ts b/src/types/inventoryTypes/inventoryTypes.ts index 061f616d..a32fc796 100644 --- a/src/types/inventoryTypes/inventoryTypes.ts +++ b/src/types/inventoryTypes/inventoryTypes.ts @@ -1,6 +1,6 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { Types } from "mongoose"; -import { IOid, IMongoDate } from "../commonTypes"; +import { IOid, IMongoDate, IOidWithLegacySupport } from "../commonTypes"; import { IColor, IItemConfig, @@ -371,7 +371,7 @@ export interface IInventoryClient extends IDailyAffiliations, InventoryClientEqu EchoesHexConquestCacheScoreMission?: number; EchoesHexConquestActiveFrameVariants?: string[]; EchoesHexConquestActiveStickers?: string[]; - BrandedSuits?: IOid[]; + BrandedSuits?: IOidWithLegacySupport[]; LockedWeaponGroup?: ILockedWeaponGroupClient; HubNpcCustomizations?: IHubNpcCustomization[]; Ship?: IOrbiter; // U22 and below, response only