From a91f9692821aa45c6cd693c029974d382a777640 Mon Sep 17 00:00:00 2001 From: OrdisPrime <134585663+OrdisPrime@users.noreply.github.com> Date: Mon, 5 Jun 2023 00:17:01 +0200 Subject: [PATCH] fix some ship things. (#16) --- src/controllers/api/getShipController.ts | 15 +++------ src/models/shipModel.ts | 43 ++++++++++++++---------- src/services/shipService.ts | 6 ++-- src/types/shipTypes.ts | 14 ++++---- static/fixed_responses/postShip.json | 33 ------------------ static/fixed_responses/ship.json | 32 ++++++++++++++++++ 6 files changed, 71 insertions(+), 72 deletions(-) delete mode 100644 static/fixed_responses/postShip.json create mode 100644 static/fixed_responses/ship.json diff --git a/src/controllers/api/getShipController.ts b/src/controllers/api/getShipController.ts index 2da6f8b2..7439acf1 100644 --- a/src/controllers/api/getShipController.ts +++ b/src/controllers/api/getShipController.ts @@ -1,20 +1,13 @@ -import { Account } from "@/src/models/loginModel"; import { Ship } from "@/src/models/shipModel"; -import { createShip } from "@/src/services/shipService"; import { RequestHandler } from "express"; // eslint-disable-next-line @typescript-eslint/no-misused-promises -const getShipController: RequestHandler = async (_req, res) => { - const accountId = _req.query.accountId; +const getShipController: RequestHandler = async (req, res) => { + const accountId = req.query.accountId; const ship = await Ship.findOne({ ShipOwnerId: accountId }); if (!ship) { - const account = await Account.findOne({ _id: accountId }); - if (account) { - await createShip(account._id); - const new_ship = await Ship.findOne({ ShipOwnerId: accountId }); - res.json(new_ship); - return; - } + res.status(500).json({ error: "error finding a corresponding ship" }); + return; } res.json(ship); }; diff --git a/src/models/shipModel.ts b/src/models/shipModel.ts index d782d917..463a95c2 100644 --- a/src/models/shipModel.ts +++ b/src/models/shipModel.ts @@ -1,46 +1,53 @@ import { Schema, model } from "mongoose"; -import { IShipDatabase } from "../types/shipTypes"; +import { IShip } from "../types/shipTypes"; import { Oid } from "../types/commonTypes"; -const roomSchema = new Schema({ - Name: String, - MaxCapacity: Number -}); - -roomSchema.set("toJSON", { - transform(_document, returnedObject) { - delete returnedObject._id; - } -}); +const roomSchema = new Schema( + { + Name: String, + MaxCapacity: Number + }, + { _id: false } +); const shipSchema = new Schema({ Rooms: [roomSchema], - Features: [Schema.Types.Mixed], + Features: [String], ContentUrlSignature: String }); +shipSchema.set("toJSON", { + transform(_document, returnedObject) { + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call + returnedObject.ShipId = { $oid: returnedObject._id.toString() } satisfies Oid; + delete returnedObject._id; + } +}); + const apartmentSchema = new Schema({ Rooms: [roomSchema], FavouriteLoadouts: [Schema.Types.Mixed] }); +apartmentSchema.set("toJSON", { + transform(_document, returnedObject) { + delete returnedObject._id; + } +}); + const shipDatabaseSchema = new Schema({ - ShipOwnerId: String, + ShipOwnerId: Schema.Types.ObjectId, Ship: shipSchema, Apartment: apartmentSchema }); shipDatabaseSchema.set("toJSON", { transform(_document, returnedObject) { - // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call - returnedObject.Ship.ShipId = { $oid: returnedObject._id.toString() } satisfies Oid; delete returnedObject._id; - delete returnedObject.Ship._id; - delete returnedObject.Apartment._id; delete returnedObject.__v; } }); -const Ship = model("Ship", shipDatabaseSchema); +const Ship = model("Ship", shipDatabaseSchema); export { Ship }; diff --git a/src/services/shipService.ts b/src/services/shipService.ts index 8d131f1a..bce95805 100644 --- a/src/services/shipService.ts +++ b/src/services/shipService.ts @@ -1,5 +1,5 @@ import { Ship } from "@/src/models/shipModel"; -import new_ship from "@/static/fixed_responses/postShip.json"; +import new_ship from "@/static/fixed_responses/ship.json"; import { Types } from "mongoose"; const createShip = async (accountOwnerId: Types.ObjectId) => { @@ -8,9 +8,9 @@ const createShip = async (accountOwnerId: Types.ObjectId) => { await ship.save(); } catch (error) { if (error instanceof Error) { - throw new Error(`error creating inventory" ${error.message}`); + throw new Error(`error creating ship" ${error.message}`); } - throw new Error("error creating inventory that is not of instance Error"); + throw new Error("error creating ship that is not of instance Error"); } }; diff --git a/src/types/shipTypes.ts b/src/types/shipTypes.ts index 7a180521..1c7a1d58 100644 --- a/src/types/shipTypes.ts +++ b/src/types/shipTypes.ts @@ -1,19 +1,19 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ import { Types } from "mongoose"; -import { Oid } from "./inventoryTypes"; +import { Oid } from "@/src/types/commonTypes"; -export type IShipDatabase = IShipResponse; - -export interface IShipResponse { +export interface IShip { ShipOwnerId: Types.ObjectId; - Ship: IShipClass; + Ship: IShipClassResponse; Apartment: IApartmentClass; } -export interface IShipClass { - Rooms: IRoomsClass[]; +export interface IShipClassResponse extends IShipClassDatabase { ShipId: Oid; +} +export interface IShipClassDatabase { + Rooms: IRoomsClass[]; Features: string[]; ContentUrlSignature: string; } diff --git a/static/fixed_responses/postShip.json b/static/fixed_responses/postShip.json deleted file mode 100644 index aeb53b93..00000000 --- a/static/fixed_responses/postShip.json +++ /dev/null @@ -1,33 +0,0 @@ -{ - "ShipOwnerId": "removed", - "Ship": { - "Features": [ - "/Lotus/Types/Items/ShipFeatureItems/EarthNavigationFeatureItem", - "/Lotus/Types/Items/ShipFeatureItems/ArsenalFeatureItem", - "/Lotus/Types/Items/ShipFeatureItems/SocialMenuFeatureItem", - "/Lotus/Types/Items/ShipFeatureItems/ModsFeatureItem", - "/Lotus/Types/Items/ShipFeatureItems/FoundryFeatureItem", - "/Lotus/Types/Items/ShipFeatureItems/MercuryNavigationFeatureItem" - ], - "ShipId": { "$oid": "removed" }, - "Rooms": [ - { "Name": "AlchemyRoom", "MaxCapacity": 1600 }, - { "Name": "BridgeRoom", "MaxCapacity": 1600 }, - { "Name": "LisetRoom", "MaxCapacity": 1000 }, - { "Name": "OperatorChamberRoom", "MaxCapacity": 1600 }, - { "Name": "OutsideRoom", "MaxCapacity": 1600 }, - { "Name": "PersonalQuartersRoom", "MaxCapacity": 1600 } - ], - "ContentUrlSignature": "removed" - }, - "Apartment": { - "Rooms": [ - { "Name": "ElevatorLanding", "MaxCapacity": 1600 }, - { "Name": "ApartmentRoomA", "MaxCapacity": 1000 }, - { "Name": "ApartmentRoomB", "MaxCapacity": 1600 }, - { "Name": "ApartmentRoomC", "MaxCapacity": 1600 }, - { "Name": "DuviriHallway", "MaxCapacity": 1600 } - ], - "FavouriteLoadouts": [] - } -} diff --git a/static/fixed_responses/ship.json b/static/fixed_responses/ship.json new file mode 100644 index 00000000..8ed911c1 --- /dev/null +++ b/static/fixed_responses/ship.json @@ -0,0 +1,32 @@ +{ + "Ship": { + "Features": [ + "/Lotus/Types/Items/ShipFeatureItems/EarthNavigationFeatureItem", + "/Lotus/Types/Items/ShipFeatureItems/ArsenalFeatureItem", + "/Lotus/Types/Items/ShipFeatureItems/SocialMenuFeatureItem", + "/Lotus/Types/Items/ShipFeatureItems/ModsFeatureItem", + "/Lotus/Types/Items/ShipFeatureItems/FoundryFeatureItem", + "/Lotus/Types/Items/ShipFeatureItems/MercuryNavigationFeatureItem" + ], + "ShipId": { "$oid": "removed" }, + "Rooms": [ + { "Name": "AlchemyRoom", "MaxCapacity": 1600 }, + { "Name": "BridgeRoom", "MaxCapacity": 1600 }, + { "Name": "LisetRoom", "MaxCapacity": 1000 }, + { "Name": "OperatorChamberRoom", "MaxCapacity": 1600 }, + { "Name": "OutsideRoom", "MaxCapacity": 1600 }, + { "Name": "PersonalQuartersRoom", "MaxCapacity": 1600 } + ], + "ContentUrlSignature": "removed" + }, + "Apartment": { + "Rooms": [ + { "Name": "ElevatorLanding", "MaxCapacity": 1600 }, + { "Name": "ApartmentRoomA", "MaxCapacity": 1000 }, + { "Name": "ApartmentRoomB", "MaxCapacity": 1600 }, + { "Name": "ApartmentRoomC", "MaxCapacity": 1600 }, + { "Name": "DuviriHallway", "MaxCapacity": 1600 } + ], + "FavouriteLoadouts": [] + } +}