fix some ship things. (#16)

This commit is contained in:
OrdisPrime 2023-06-05 00:17:01 +02:00 committed by GitHub
parent 6520284cba
commit a91f969282
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 71 additions and 72 deletions

View File

@ -1,21 +1,14 @@
import { Account } from "@/src/models/loginModel";
import { Ship } from "@/src/models/shipModel"; import { Ship } from "@/src/models/shipModel";
import { createShip } from "@/src/services/shipService";
import { RequestHandler } from "express"; import { RequestHandler } from "express";
// eslint-disable-next-line @typescript-eslint/no-misused-promises // eslint-disable-next-line @typescript-eslint/no-misused-promises
const getShipController: RequestHandler = async (_req, res) => { const getShipController: RequestHandler = async (req, res) => {
const accountId = _req.query.accountId; const accountId = req.query.accountId;
const ship = await Ship.findOne({ ShipOwnerId: accountId }); const ship = await Ship.findOne({ ShipOwnerId: accountId });
if (!ship) { if (!ship) {
const account = await Account.findOne({ _id: accountId }); res.status(500).json({ error: "error finding a corresponding ship" });
if (account) {
await createShip(account._id);
const new_ship = await Ship.findOne({ ShipOwnerId: accountId });
res.json(new_ship);
return; return;
} }
}
res.json(ship); res.json(ship);
}; };

View File

@ -1,46 +1,53 @@
import { Schema, model } from "mongoose"; import { Schema, model } from "mongoose";
import { IShipDatabase } from "../types/shipTypes"; import { IShip } from "../types/shipTypes";
import { Oid } from "../types/commonTypes"; import { Oid } from "../types/commonTypes";
const roomSchema = new Schema({ const roomSchema = new Schema(
{
Name: String, Name: String,
MaxCapacity: Number MaxCapacity: Number
}); },
{ _id: false }
roomSchema.set("toJSON", { );
transform(_document, returnedObject) {
delete returnedObject._id;
}
});
const shipSchema = new Schema({ const shipSchema = new Schema({
Rooms: [roomSchema], Rooms: [roomSchema],
Features: [Schema.Types.Mixed], Features: [String],
ContentUrlSignature: 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({ const apartmentSchema = new Schema({
Rooms: [roomSchema], Rooms: [roomSchema],
FavouriteLoadouts: [Schema.Types.Mixed] FavouriteLoadouts: [Schema.Types.Mixed]
}); });
apartmentSchema.set("toJSON", {
transform(_document, returnedObject) {
delete returnedObject._id;
}
});
const shipDatabaseSchema = new Schema({ const shipDatabaseSchema = new Schema({
ShipOwnerId: String, ShipOwnerId: Schema.Types.ObjectId,
Ship: shipSchema, Ship: shipSchema,
Apartment: apartmentSchema Apartment: apartmentSchema
}); });
shipDatabaseSchema.set("toJSON", { shipDatabaseSchema.set("toJSON", {
transform(_document, returnedObject) { 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._id;
delete returnedObject.Ship._id;
delete returnedObject.Apartment._id;
delete returnedObject.__v; delete returnedObject.__v;
} }
}); });
const Ship = model<IShipDatabase>("Ship", shipDatabaseSchema); const Ship = model<IShip>("Ship", shipDatabaseSchema);
export { Ship }; export { Ship };

View File

@ -1,5 +1,5 @@
import { Ship } from "@/src/models/shipModel"; 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"; import { Types } from "mongoose";
const createShip = async (accountOwnerId: Types.ObjectId) => { const createShip = async (accountOwnerId: Types.ObjectId) => {
@ -8,9 +8,9 @@ const createShip = async (accountOwnerId: Types.ObjectId) => {
await ship.save(); await ship.save();
} catch (error) { } catch (error) {
if (error instanceof 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");
} }
}; };

View File

@ -1,19 +1,19 @@
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
import { Types } from "mongoose"; import { Types } from "mongoose";
import { Oid } from "./inventoryTypes"; import { Oid } from "@/src/types/commonTypes";
export type IShipDatabase = IShipResponse; export interface IShip {
export interface IShipResponse {
ShipOwnerId: Types.ObjectId; ShipOwnerId: Types.ObjectId;
Ship: IShipClass; Ship: IShipClassResponse;
Apartment: IApartmentClass; Apartment: IApartmentClass;
} }
export interface IShipClass { export interface IShipClassResponse extends IShipClassDatabase {
Rooms: IRoomsClass[];
ShipId: Oid; ShipId: Oid;
}
export interface IShipClassDatabase {
Rooms: IRoomsClass[];
Features: string[]; Features: string[];
ContentUrlSignature: string; ContentUrlSignature: string;
} }

View File

@ -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": []
}
}

View File

@ -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": []
}
}