fix: handle standing limits in fishmongerController #795

Merged
Sainan merged 2 commits from fishmonger into main 2025-01-17 05:43:33 -08:00
2 changed files with 14 additions and 12 deletions
Showing only changes of commit e8b2b0a3f1 - Show all commits

View File

@ -3,7 +3,8 @@ import { getAccountIdForRequest } from "@/src/services/loginService";
import { addMiscItems, getInventory, getStandingLimit, updateStandingLimit } from "@/src/services/inventoryService";
import { IMiscItem } from "@/src/types/inventoryTypes/inventoryTypes";
import { IOid } from "@/src/types/commonTypes";
import { ExportSyndicates, ISyndicate } from "warframe-public-export-plus";
import { ExportSyndicates } from "warframe-public-export-plus";
import { getMaxStanding } from "@/src/helpers/syndicateStandingHelper";
export const syndicateStandingBonusController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req);
@ -67,14 +68,3 @@ interface ISyndicateStandingBonusRequest {
};
ModularWeaponId: IOid; // Seems to just be "000000000000000000000000", also note there's a "Category" query field
}
const getMaxStanding = (syndicate: ISyndicate, title: number): number => {
if (!syndicate.titles) {
// LibrarySyndicate
return 125000;
}
if (title == 0) {
return syndicate.titles.find(x => x.level == 1)!.minStanding;
}
return syndicate.titles.find(x => x.level == title)!.maxStanding;
};

View File

@ -0,0 +1,12 @@
import { ISyndicate } from "warframe-public-export-plus";
export const getMaxStanding = (syndicate: ISyndicate, title: number): number => {
if (!syndicate.titles) {
// LibrarySyndicate
return 125000;
}
if (title == 0) {
return syndicate.titles.find(x => x.level == 1)!.minStanding;
}
return syndicate.titles.find(x => x.level == title)!.maxStanding;
coderabbitai[bot] commented 2025-01-17 01:14:52 -08:00 (Migrated from github.com)
Review

⚠️ Potential issue

Handle cases where the title level is not found

Using the non-null assertion operator ! after find assumes that a matching title will always be found. If no matching title exists, this will result in a runtime error.

Modify the code to handle undefined cases:

     if (title == 0) {
-        return syndicate.titles.find(x => x.level == 1)!.minStanding;
+        const firstTitle = syndicate.titles.find(x => x.level == 1);
+        if (!firstTitle) {
+            throw new Error(`Title level 1 not found for syndicate ${syndicate.key}`);
+        }
+        return firstTitle.minStanding;
     }
-    return syndicate.titles.find(x => x.level == title)!.maxStanding;
+    const currentTitle = syndicate.titles.find(x => x.level == title);
+    if (!currentTitle) {
+        throw new Error(`Title level ${title} not found for syndicate ${syndicate.key}`);
+    }
+    return currentTitle.maxStanding;

Alternatively, you can provide a default value or handle the error gracefully.

📝 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.

    if (title == 0) {
        const firstTitle = syndicate.titles.find(x => x.level == 1);
        if (!firstTitle) {
            throw new Error(`Title level 1 not found for syndicate ${syndicate.key}`);
        }
        return firstTitle.minStanding;
    }
    const currentTitle = syndicate.titles.find(x => x.level == title);
    if (!currentTitle) {
        throw new Error(`Title level ${title} not found for syndicate ${syndicate.key}`);
    }
    return currentTitle.maxStanding;
_:warning: Potential issue_ **Handle cases where the title level is not found** Using the non-null assertion operator `!` after `find` assumes that a matching title will always be found. If no matching title exists, this will result in a runtime error. Modify the code to handle undefined cases: ```diff if (title == 0) { - return syndicate.titles.find(x => x.level == 1)!.minStanding; + const firstTitle = syndicate.titles.find(x => x.level == 1); + if (!firstTitle) { + throw new Error(`Title level 1 not found for syndicate ${syndicate.key}`); + } + return firstTitle.minStanding; } - return syndicate.titles.find(x => x.level == title)!.maxStanding; + const currentTitle = syndicate.titles.find(x => x.level == title); + if (!currentTitle) { + throw new Error(`Title level ${title} not found for syndicate ${syndicate.key}`); + } + return currentTitle.maxStanding; ``` Alternatively, you can provide a default value or handle the error gracefully. <!-- 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 if (title == 0) { const firstTitle = syndicate.titles.find(x => x.level == 1); if (!firstTitle) { throw new Error(`Title level 1 not found for syndicate ${syndicate.key}`); } return firstTitle.minStanding; } const currentTitle = syndicate.titles.find(x => x.level == title); if (!currentTitle) { throw new Error(`Title level ${title} not found for syndicate ${syndicate.key}`); } return currentTitle.maxStanding; ````` </details> <!-- suggestion_end --> <!-- This is an auto-generated comment by CodeRabbit -->
};