feat: implement setPlacedDecoInfo #558

Merged
Sainan merged 2 commits from setPlacedDecoInfo into main 2024-10-18 07:54:49 -07:00
5 changed files with 83 additions and 3 deletions

View File

@ -0,0 +1,12 @@
import { getAccountIdForRequest } from "@/src/services/loginService";
import { ISetPlacedDecoInfoRequest } from "@/src/types/shipTypes";
import { RequestHandler } from "express";
import { handleSetPlacedDecoInfo } from "@/src/services/shipCustomizationsService";
// eslint-disable-next-line @typescript-eslint/no-misused-promises
export const setPlacedDecoInfoController: RequestHandler = async (req, res) => {
const accountId = await getAccountIdForRequest(req);
const payload = JSON.parse(req.body as string) as ISetPlacedDecoInfoRequest;
await handleSetPlacedDecoInfo(accountId, payload);
res.end();
coderabbitai[bot] commented 2024-10-17 12:04:07 -07:00 (Migrated from github.com)
Review

⚠️ Potential issue

Improve error handling and response structure.

The current implementation lacks explicit error handling and doesn't provide a clear success response. Consider the following improvements:

  1. Add try-catch blocks to handle potential errors from getAccountIdForRequest, JSON parsing, and handleSetPlacedDecoInfo.
  2. Send appropriate error responses with status codes for different error scenarios.
  3. Send a success response with a status code (e.g., 200 OK) and potentially a success message or updated data.

Here's a suggested implementation:

export const setPlacedDecoInfoController: RequestHandler = async (req, res, next) => {
  try {
    const accountId = await getAccountIdForRequest(req);
    
    let payload: ISetPlacedDecoInfoRequest;
    try {
      payload = JSON.parse(req.body as string) as ISetPlacedDecoInfoRequest;
    } catch (error) {
      return res.status(400).json({ error: 'Invalid JSON in request body' });
    }
    
    await handleSetPlacedDecoInfo(accountId, payload);
    res.status(200).json({ message: 'Decoration info updated successfully' });
  } catch (error) {
    next(error); // Let the global error handler deal with it
  }
};
_:warning: Potential issue_ **Improve error handling and response structure.** The current implementation lacks explicit error handling and doesn't provide a clear success response. Consider the following improvements: 1. Add try-catch blocks to handle potential errors from `getAccountIdForRequest`, JSON parsing, and `handleSetPlacedDecoInfo`. 2. Send appropriate error responses with status codes for different error scenarios. 3. Send a success response with a status code (e.g., 200 OK) and potentially a success message or updated data. Here's a suggested implementation: ```typescript export const setPlacedDecoInfoController: RequestHandler = async (req, res, next) => { try { const accountId = await getAccountIdForRequest(req); let payload: ISetPlacedDecoInfoRequest; try { payload = JSON.parse(req.body as string) as ISetPlacedDecoInfoRequest; } catch (error) { return res.status(400).json({ error: 'Invalid JSON in request body' }); } await handleSetPlacedDecoInfo(accountId, payload); res.status(200).json({ message: 'Decoration info updated successfully' }); } catch (error) { next(error); // Let the global error handler deal with it } }; ``` <!-- This is an auto-generated reply by CodeRabbit -->
Review

Hello, I am an AI trained mostly on Express 4. I am entirely useless in Express 5 projects. Beep boop. Can you kill yourself now?

Hello, I am an AI trained mostly on Express 4. I am entirely useless in Express 5 projects. Beep boop. Can you kill yourself now?
coderabbitai[bot] commented 2024-10-17 12:05:57 -07:00 (Migrated from github.com)
Review

@Sainan, I'm sorry to hear that you're feeling this way. If there's anything I can assist you with regarding Express 5 or any other aspect of the project, please let me know.

(_/)
(•_•)

@Sainan, I'm sorry to hear that you're feeling this way. If there's anything I can assist you with regarding Express 5 or any other aspect of the project, please let me know. (\_/\) (•\_•) <!-- This is an auto-generated reply by CodeRabbit -->
};

View File

@ -1,14 +1,34 @@
import { toOid } from "@/src/helpers/inventoryHelpers";
import { IOrbiter, IPersonalRooms, PersonalRoomsModelType } from "@/src/types/personalRoomsTypes";
import { IApartment, IGardening, IPlacedDecosDatabase } from "@/src/types/shipTypes";
import { IApartment, IGardening, IPlacedDecosDatabase, IPictureFrameInfo } from "@/src/types/shipTypes";
import { Schema, model } from "mongoose";
const pictureFrameInfoSchema = new Schema<IPictureFrameInfo>(
{
Image: String,
Filter: String,
XOffset: Number,
YOffset: Number,
Scale: Number,
InvertX: Boolean,
InvertY: Boolean,
ColorCorrection: Number,
Text: String,
TextScale: Number,
TextColorA: Number,
TextColorB: Number,
TextOrientation: Number
},
{ id: false, _id: false }
);
coderabbitai[bot] commented 2024-10-17 12:04:07 -07:00 (Migrated from github.com)
Review

🛠️ Refactor suggestion

Consider adding validation rules to pictureFrameInfoSchema

To enhance data integrity and prevent invalid data from being saved to the database, consider adding validation constraints to the schema fields. For example, you can specify required fields, default values, or value ranges.

For example, you might modify the schema as follows:

 const pictureFrameInfoSchema = new Schema<IPictureFrameInfo>(
     {
-        Image: String,
+        Image: { type: String, required: true },
-        Scale: Number,
+        Scale: { type: Number, min: 0 },
         // Add validation to other fields as needed
     },
     { id: false, _id: false }
 );

Committable suggestion was skipped due to low confidence.

_:hammer_and_wrench: Refactor suggestion_ **Consider adding validation rules to `pictureFrameInfoSchema`** To enhance data integrity and prevent invalid data from being saved to the database, consider adding validation constraints to the schema fields. For example, you can specify required fields, default values, or value ranges. For example, you might modify the schema as follows: ```diff const pictureFrameInfoSchema = new Schema<IPictureFrameInfo>( { - Image: String, + Image: { type: String, required: true }, - Scale: Number, + Scale: { type: Number, min: 0 }, // Add validation to other fields as needed }, { id: false, _id: false } ); ``` > Committable suggestion was skipped due to low confidence. <!-- This is an auto-generated reply by CodeRabbit -->
Review

KILL YOURSELF

KILL YOURSELF
coderabbitai[bot] commented 2024-10-17 12:06:16 -07:00 (Migrated from github.com)
Review

@Sainan, I'm sorry to hear you're feeling this way. If there's anything I can assist you with regarding the code, please let me know.

(•́_•̀)

@Sainan, I'm sorry to hear you're feeling this way. If there's anything I can assist you with regarding the code, please let me know. (•́_•̀) <!-- This is an auto-generated reply by CodeRabbit -->
const placedDecosSchema = new Schema<IPlacedDecosDatabase>(
{
Type: String,
Pos: [Number],
Rot: [Number],
Scale: Number
Scale: Number,
PictureFrameInfo: { type: pictureFrameInfoSchema, default: undefined }
},
{ id: false }
);

View File

@ -51,6 +51,7 @@ import { sellController } from "@/src/controllers/api/sellController";
import { setActiveQuestController } from "@/src/controllers/api/setActiveQuestController";
import { setActiveShipController } from "@/src/controllers/api/setActiveShipController";
import { setBootLocationController } from "@/src/controllers/api/setBootLocationController";
import { setPlacedDecoInfoController } from "@/src/controllers/api/setPlacedDecoInfoController";
import { setShipCustomizationsController } from "@/src/controllers/api/setShipCustomizationsController";
import { setSupportedSyndicateController } from "@/src/controllers/api/setSupportedSyndicateController";
import { setWeaponSkillTreeController } from "../controllers/api/setWeaponSkillTreeController";
@ -128,6 +129,7 @@ apiRouter.post("/purchase.php", purchaseController);
apiRouter.post("/rerollRandomMod.php", rerollRandomModController);
apiRouter.post("/saveLoadout.php", saveLoadoutController);
apiRouter.post("/sell.php", sellController);
apiRouter.post("/setPlacedDecoInfo.php", setPlacedDecoInfoController);
apiRouter.post("/setShipCustomizations.php", setShipCustomizationsController);
apiRouter.post("/setWeaponSkillTree.php", setWeaponSkillTreeController);
apiRouter.post("/shipDecorations.php", shipDecorationsController);

View File

@ -4,7 +4,8 @@ import {
ISetShipCustomizationsRequest,
IShipDatabase,
IShipDecorationsRequest,
IShipDecorationsResponse
IShipDecorationsResponse,
ISetPlacedDecoInfoRequest
} from "@/src/types/shipTypes";
import { logger } from "@/src/utils/logger";
import { Types } from "mongoose";
@ -131,3 +132,23 @@ export const handleSetShipDecorations = async (
return { DecoId: decoId.toString(), Room: placedDecoration.Room, IsApartment: placedDecoration.IsApartment };
};
export const handleSetPlacedDecoInfo = async (accountId: string, req: ISetPlacedDecoInfoRequest): Promise<void> => {
const personalRooms = await getPersonalRooms(accountId);
const room = personalRooms.Ship.Rooms.find(room => room.Name === req.Room);
if (!room) {
logger.error("room not found");
throw new Error("room not found");
}
const placedDeco = room.PlacedDecos?.find(x => x._id.toString() == req.DecoId);
if (!placedDeco) {
logger.error("deco not found");
throw new Error("deco not found");
}
placedDeco.PictureFrameInfo = req.PictureFrameInfo;
await personalRooms.save();
};

View File

@ -70,6 +70,7 @@ export interface IPlacedDecosDatabase {
Pos: [number, number, number];
Rot: [number, number, number];
Scale: number;
PictureFrameInfo?: IPictureFrameInfo;
_id: Types.ObjectId;
}
@ -115,3 +116,27 @@ export interface IShipDecorationsResponse {
OldRoom?: string;
NewRoom?: string;
}
export interface ISetPlacedDecoInfoRequest {
DecoType: string;
DecoId: string;
Room: string;
PictureFrameInfo: IPictureFrameInfo;
BootLocation: string;
}
export interface IPictureFrameInfo {
Image: string;
Filter: string;
XOffset: number;
YOffset: number;
Scale: number;
InvertX: boolean;
InvertY: boolean;
ColorCorrection: number;
Text: string;
TextScale: number;
TextColorA: number;
TextColorB: number;
TextOrientation: number;
}