feat: implement setShipFavouriteLoadout.php #662

Merged
Sainan merged 1 commits from setShipFavouriteLoadout into main 2024-12-30 10:48:43 -08:00
2 changed files with 33 additions and 0 deletions

View File

@ -0,0 +1,31 @@
import { getAccountIdForRequest } from "@/src/services/loginService";
import { RequestHandler } from "express";
import { getPersonalRooms } from "@/src/services/personalRoomsService";
import { IOid } from "@/src/types/commonTypes";
import { Types } from "mongoose";
export const setShipFavouriteLoadoutController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req);
const personalRooms = await getPersonalRooms(accountId);
const body = JSON.parse(String(req.body)) as ISetShipFavouriteLoadoutRequest;
if (body.BootLocation != "SHOP") {
throw new Error(`unexpected BootLocation: ${body.BootLocation}`);
}
coderabbitai[bot] commented 2024-12-29 15:50:39 -08:00 (Migrated from github.com)
Review

🛠️ Refactor suggestion

Return a proper response instead of throwing an error.

Throwing an error can break the request pipeline and produce a 500 internal server error. Instead, return a suitable status code (e.g., 400) and problem detail JSON, clarifying why the request was rejected.

- if (body.BootLocation != "SHOP") {
-   throw new Error(`unexpected BootLocation: ${body.BootLocation}`);
- }
+ if (body.BootLocation !== "SHOP") {
+   return res.status(400).json({ error: `unexpected BootLocation: ${body.BootLocation}` });
+ }
📝 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 (body.BootLocation !== "SHOP") {
        return res.status(400).json({ error: `unexpected BootLocation: ${body.BootLocation}` });
    }
_:hammer_and_wrench: Refactor suggestion_ **Return a proper response instead of throwing an error.** Throwing an error can break the request pipeline and produce a 500 internal server error. Instead, return a suitable status code (e.g., 400) and problem detail JSON, clarifying why the request was rejected. ```diff - if (body.BootLocation != "SHOP") { - throw new Error(`unexpected BootLocation: ${body.BootLocation}`); - } + if (body.BootLocation !== "SHOP") { + return res.status(400).json({ error: `unexpected BootLocation: ${body.BootLocation}` }); + } ``` <!-- 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 (body.BootLocation !== "SHOP") { return res.status(400).json({ error: `unexpected BootLocation: ${body.BootLocation}` }); } ````` </details> <!-- suggestion_end --> <!-- This is an auto-generated comment by CodeRabbit -->
const display = personalRooms.TailorShop.FavouriteLoadouts.find(x => x.Tag == body.TagName);
if (display) {
display.LoadoutId = new Types.ObjectId(body.FavouriteLoadoutId.$oid);
} else {
personalRooms.TailorShop.FavouriteLoadouts.push({
Tag: body.TagName,
LoadoutId: new Types.ObjectId(body.FavouriteLoadoutId.$oid)
});
}
await personalRooms.save();
res.json({});
};
interface ISetShipFavouriteLoadoutRequest {
BootLocation: string;
FavouriteLoadoutId: IOid;
TagName: string;
}

View File

@ -54,6 +54,7 @@ import { setActiveShipController } from "@/src/controllers/api/setActiveShipCont
import { setBootLocationController } from "@/src/controllers/api/setBootLocationController"; import { setBootLocationController } from "@/src/controllers/api/setBootLocationController";
import { setPlacedDecoInfoController } from "@/src/controllers/api/setPlacedDecoInfoController"; import { setPlacedDecoInfoController } from "@/src/controllers/api/setPlacedDecoInfoController";
import { setShipCustomizationsController } from "@/src/controllers/api/setShipCustomizationsController"; import { setShipCustomizationsController } from "@/src/controllers/api/setShipCustomizationsController";
import { setShipFavouriteLoadoutController } from "@/src/controllers/api/setShipFavouriteLoadoutController";
import { setSupportedSyndicateController } from "@/src/controllers/api/setSupportedSyndicateController"; import { setSupportedSyndicateController } from "@/src/controllers/api/setSupportedSyndicateController";
import { setWeaponSkillTreeController } from "../controllers/api/setWeaponSkillTreeController"; import { setWeaponSkillTreeController } from "../controllers/api/setWeaponSkillTreeController";
import { shipDecorationsController } from "@/src/controllers/api/shipDecorationsController"; import { shipDecorationsController } from "@/src/controllers/api/shipDecorationsController";
@ -134,6 +135,7 @@ apiRouter.post("/saveLoadout.php", saveLoadoutController);
apiRouter.post("/sell.php", sellController); apiRouter.post("/sell.php", sellController);
apiRouter.post("/setPlacedDecoInfo.php", setPlacedDecoInfoController); apiRouter.post("/setPlacedDecoInfo.php", setPlacedDecoInfoController);
apiRouter.post("/setShipCustomizations.php", setShipCustomizationsController); apiRouter.post("/setShipCustomizations.php", setShipCustomizationsController);
apiRouter.post("/setShipFavouriteLoadout.php", setShipFavouriteLoadoutController);
apiRouter.post("/setWeaponSkillTree.php", setWeaponSkillTreeController); apiRouter.post("/setWeaponSkillTree.php", setWeaponSkillTreeController);
apiRouter.post("/shipDecorations.php", shipDecorationsController); apiRouter.post("/shipDecorations.php", shipDecorationsController);
apiRouter.post("/startDojoRecipe.php", startDojoRecipeController); apiRouter.post("/startDojoRecipe.php", startDojoRecipeController);