fix: purchasing flawed mods from iron wake #802

Merged
Sainan merged 3 commits from vendor-fix into main 2025-01-18 02:11:52 -08:00
Showing only changes of commit 332389ccd8 - Show all commits

View File

@ -50,23 +50,24 @@ export const handlePurchase = async (
const inventoryChanges: IInventoryChanges = {};
if (purchaseRequest.PurchaseParams.Source == 7) {
const manifest = getVendorManifestByOid(purchaseRequest.PurchaseParams.SourceId!);
if (!manifest) {
throw new Error(`unknown vendor id: ${purchaseRequest.PurchaseParams.SourceId!}`);
if (manifest) {
const ItemId = (JSON.parse(purchaseRequest.PurchaseParams.ExtraPurchaseInfoJson!) as { ItemId: string }).ItemId;
const offer = manifest.VendorInfo.ItemManifest.find(x => x.Id.$oid == ItemId);
if (!offer) {
throw new Error(`unknown vendor offer: ${ItemId}`);
}
if (offer.ItemPrices) {
await handleItemPrices(
accountId,
offer.ItemPrices,
purchaseRequest.PurchaseParams.Quantity,
inventoryChanges
);
}
purchaseRequest.PurchaseParams.Quantity *= offer.QuantityMultiplier;
} else if (!ExportVendors[purchaseRequest.PurchaseParams.SourceId!]) {
throw new Error(`unknown vendor: ${purchaseRequest.PurchaseParams.SourceId!}`);
}
coderabbitai[bot] commented 2025-01-17 22:10:49 -08:00 (Migrated from github.com)
Review

🛠️ Refactor suggestion

Add error handling for JSON parsing.

The JSON.parse operation could throw if ExtraPurchaseInfoJson is malformed. Consider adding try-catch block to handle parsing errors gracefully.

-            const ItemId = (JSON.parse(purchaseRequest.PurchaseParams.ExtraPurchaseInfoJson!) as { ItemId: string })
-                .ItemId;
+            let ItemId: string;
+            try {
+                const parsed = JSON.parse(purchaseRequest.PurchaseParams.ExtraPurchaseInfoJson!) as { ItemId: string };
+                if (typeof parsed.ItemId !== 'string') {
+                    throw new Error('Invalid ItemId type');
+                }
+                ItemId = parsed.ItemId;
+            } catch (error) {
+                throw new Error(`Failed to parse vendor purchase info: ${error.message}`);
+            }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

            let ItemId: string;
            try {
                const parsed = JSON.parse(purchaseRequest.PurchaseParams.ExtraPurchaseInfoJson!) as { ItemId: string };
                if (typeof parsed.ItemId !== 'string') {
                    throw new Error('Invalid ItemId type');
                }
                ItemId = parsed.ItemId;
            } catch (error) {
                throw new Error(`Failed to parse vendor purchase info: ${error.message}`);
            }
_:hammer_and_wrench: Refactor suggestion_ **Add error handling for JSON parsing.** The JSON.parse operation could throw if `ExtraPurchaseInfoJson` is malformed. Consider adding try-catch block to handle parsing errors gracefully. ```diff - const ItemId = (JSON.parse(purchaseRequest.PurchaseParams.ExtraPurchaseInfoJson!) as { ItemId: string }) - .ItemId; + let ItemId: string; + try { + const parsed = JSON.parse(purchaseRequest.PurchaseParams.ExtraPurchaseInfoJson!) as { ItemId: string }; + if (typeof parsed.ItemId !== 'string') { + throw new Error('Invalid ItemId type'); + } + ItemId = parsed.ItemId; + } catch (error) { + throw new Error(`Failed to parse vendor purchase info: ${error.message}`); + } ``` <!-- suggestion_start --> <details> <summary>📝 Committable suggestion</summary> > ‼️ **IMPORTANT** > Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements. `````suggestion let ItemId: string; try { const parsed = JSON.parse(purchaseRequest.PurchaseParams.ExtraPurchaseInfoJson!) as { ItemId: string }; if (typeof parsed.ItemId !== 'string') { throw new Error('Invalid ItemId type'); } ItemId = parsed.ItemId; } catch (error) { throw new Error(`Failed to parse vendor purchase info: ${error.message}`); } ````` </details> <!-- suggestion_end --> <!-- This is an auto-generated comment by CodeRabbit -->
const ItemId = (JSON.parse(purchaseRequest.PurchaseParams.ExtraPurchaseInfoJson!) as { ItemId: string }).ItemId;
const offer = manifest.VendorInfo.ItemManifest.find(x => x.Id.$oid == ItemId);
if (!offer) {
throw new Error(`unknown vendor offer: ${ItemId}`);
}
if (offer.ItemPrices) {
await handleItemPrices(
accountId,
offer.ItemPrices,
purchaseRequest.PurchaseParams.Quantity,
inventoryChanges
);
}
purchaseRequest.PurchaseParams.Quantity *= offer.QuantityMultiplier;
}
const purchaseResponse = await handleStoreItemAcquisition(