fix: handle standing limits in fishmongerController #795

Merged
Sainan merged 2 commits from fishmonger into main 2025-01-17 05:43:33 -08:00
Showing only changes of commit 45662c400c - Show all commits

View File

@ -1,9 +1,10 @@
import { getJSONfromString } from "@/src/helpers/stringHelpers";
import { addMiscItems, getInventory } from "@/src/services/inventoryService";
import { getMaxStanding } from "@/src/helpers/syndicateStandingHelper";
import { addMiscItems, getInventory, getStandingLimit, updateStandingLimit } from "@/src/services/inventoryService";
import { getAccountIdForRequest } from "@/src/services/loginService";
import { IMiscItem } from "@/src/types/inventoryTypes/inventoryTypes";
import { RequestHandler } from "express";
import { ExportResources } from "warframe-public-export-plus";
import { ExportResources, ExportSyndicates } from "warframe-public-export-plus";
export const fishmongerController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req);
@ -11,7 +12,7 @@ export const fishmongerController: RequestHandler = async (req, res) => {
const body = getJSONfromString(String(req.body)) as IFishmongerRequest;
const miscItemChanges: IMiscItem[] = [];
let syndicateTag: string | undefined;
let standingChange = 0;
let gainedStanding = 0;
for (const fish of body.Fish) {
const fishData = ExportResources[fish.ItemType];
if (req.query.dissect == "1") {
@ -25,21 +26,29 @@ export const fishmongerController: RequestHandler = async (req, res) => {
}
} else {
syndicateTag = fishData.syndicateTag!;
standingChange += fishData.standingBonus! * fish.ItemCount;
gainedStanding += fishData.standingBonus! * fish.ItemCount;
}
miscItemChanges.push({ ItemType: fish.ItemType, ItemCount: fish.ItemCount * -1 });
}
addMiscItems(inventory, miscItemChanges);
if (standingChange && syndicateTag) {
const syndicate = inventory.Affiliations.find(x => x.Tag == syndicateTag);
if (syndicate !== undefined) {
syndicate.Standing += standingChange;
} else {
inventory.Affiliations.push({
Tag: syndicateTag,
Standing: standingChange
});
if (gainedStanding && syndicateTag) {
let syndicate = inventory.Affiliations.find(x => x.Tag == syndicateTag);
if (!syndicate) {
syndicate = inventory.Affiliations[inventory.Affiliations.push({ Tag: syndicateTag, Standing: 0 }) - 1];
}
const syndicateMeta = ExportSyndicates[syndicateTag];
const max = getMaxStanding(syndicateMeta, syndicate.Title ?? 0);
if (syndicate.Standing + gainedStanding > max) {
gainedStanding = max - syndicate.Standing;
}
if (gainedStanding > getStandingLimit(inventory, syndicateMeta.dailyLimitBin)) {
gainedStanding = getStandingLimit(inventory, syndicateMeta.dailyLimitBin);
}
syndicate.Standing += gainedStanding;
updateStandingLimit(inventory, syndicateMeta.dailyLimitBin, gainedStanding);
}
coderabbitai[bot] commented 2025-01-17 01:14:52 -08:00 (Migrated from github.com)
Review

⚠️ Potential issue

Ensure standingBonus is defined to prevent runtime errors

The use of the non-null assertion operator ! on fishData.standingBonus assumes that this property is always defined. If standingBonus is undefined or null, this could lead to a runtime error. Consider adding a check to ensure standingBonus is defined before using it.

Apply this diff to add a safety check:

            syndicateTag = fishData.syndicateTag!;
-           gainedStanding += fishData.standingBonus! * fish.ItemCount;
+           if (fishData.standingBonus) {
+               gainedStanding += fishData.standingBonus * fish.ItemCount;
+           } else {
+               // Handle undefined standingBonus, e.g., log an error or set a default value
+           }

Committable suggestion skipped: line range outside the PR's diff.

_:warning: Potential issue_ **Ensure `standingBonus` is defined to prevent runtime errors** The use of the non-null assertion operator `!` on `fishData.standingBonus` assumes that this property is always defined. If `standingBonus` is `undefined` or `null`, this could lead to a runtime error. Consider adding a check to ensure `standingBonus` is defined before using it. Apply this diff to add a safety check: ```diff syndicateTag = fishData.syndicateTag!; - gainedStanding += fishData.standingBonus! * fish.ItemCount; + if (fishData.standingBonus) { + gainedStanding += fishData.standingBonus * fish.ItemCount; + } else { + // Handle undefined standingBonus, e.g., log an error or set a default value + } ``` > Committable suggestion skipped: line range outside the PR's diff. <!-- This is an auto-generated comment by CodeRabbit -->
await inventory.save();
res.json({
@ -47,7 +56,7 @@ export const fishmongerController: RequestHandler = async (req, res) => {
MiscItems: miscItemChanges
},
SyndicateTag: syndicateTag,
StandingChange: standingChange
StandingChange: gainedStanding
});
};