2023-06-02 00:20:49 -03:00
|
|
|
import { Account } from "@/src/models/loginModel";
|
2023-06-04 03:06:22 +02:00
|
|
|
import { createInventory } from "@/src/services/inventoryService";
|
2023-06-02 00:20:49 -03:00
|
|
|
import { IDatabaseAccount } from "@/src/types/loginTypes";
|
2023-06-05 04:16:49 +08:00
|
|
|
import { createShip } from "./shipService";
|
2023-12-14 17:34:15 +01:00
|
|
|
import { Types } from "mongoose";
|
2024-02-18 13:58:43 +01:00
|
|
|
import { Loadout } from "@/src/models/inventoryModels/loadoutModel";
|
|
|
|
import { PersonalRooms } from "@/src/models/personalRoomsModel";
|
|
|
|
import new_personal_rooms from "@/static/fixed_responses/personalRooms.json";
|
2024-05-28 13:45:06 +02:00
|
|
|
import { Request } from "express";
|
2023-05-19 15:22:48 -03:00
|
|
|
|
2024-05-28 13:45:06 +02:00
|
|
|
export const isCorrectPassword = (requestPassword: string, databasePassword: string): boolean => {
|
2023-05-23 20:42:06 -04:00
|
|
|
return requestPassword === databasePassword;
|
2023-05-19 15:22:48 -03:00
|
|
|
};
|
|
|
|
|
2024-05-28 13:45:06 +02:00
|
|
|
export const createAccount = async (accountData: IDatabaseAccount) => {
|
2023-05-23 20:42:06 -04:00
|
|
|
const account = new Account(accountData);
|
|
|
|
try {
|
|
|
|
await account.save();
|
2023-12-14 17:34:15 +01:00
|
|
|
const loadoutId = await createLoadout(account._id);
|
2024-02-18 13:58:43 +01:00
|
|
|
const shipId = await createShip(account._id);
|
|
|
|
await createInventory(account._id, { loadOutPresetId: loadoutId, ship: shipId });
|
|
|
|
await createPersonalRooms(account._id, shipId);
|
2023-05-23 20:42:06 -04:00
|
|
|
return account.toJSON();
|
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof Error) {
|
|
|
|
throw new Error(error.message);
|
|
|
|
}
|
|
|
|
throw new Error("error creating account that is not of instance Error");
|
2023-05-19 15:22:48 -03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-12-14 17:34:15 +01:00
|
|
|
export const createLoadout = async (accountId: Types.ObjectId) => {
|
2024-02-18 13:58:43 +01:00
|
|
|
const loadout = new Loadout({ loadoutOwnerId: accountId });
|
2023-12-14 17:34:15 +01:00
|
|
|
const savedLoadout = await loadout.save();
|
|
|
|
return savedLoadout._id;
|
|
|
|
};
|
2024-02-18 13:58:43 +01:00
|
|
|
|
|
|
|
export const createPersonalRooms = async (accountId: Types.ObjectId, shipId: Types.ObjectId) => {
|
|
|
|
const personalRooms = new PersonalRooms({
|
|
|
|
...new_personal_rooms,
|
|
|
|
personalRoomsOwnerId: accountId,
|
|
|
|
activeShipId: shipId
|
|
|
|
});
|
|
|
|
await personalRooms.save();
|
|
|
|
};
|
2024-05-28 13:45:06 +02:00
|
|
|
|
2024-12-22 00:44:49 +01:00
|
|
|
export const getAccountForRequest = async (req: Request) => {
|
2024-05-28 13:45:06 +02:00
|
|
|
if (!req.query.accountId) {
|
|
|
|
throw new Error("Request is missing accountId parameter");
|
|
|
|
}
|
|
|
|
if (!req.query.nonce || parseInt(req.query.nonce as string) === 0) {
|
|
|
|
throw new Error("Request is missing nonce parameter");
|
|
|
|
}
|
|
|
|
const account = await Account.findOne(
|
|
|
|
{
|
|
|
|
_id: req.query.accountId,
|
|
|
|
Nonce: req.query.nonce
|
|
|
|
},
|
|
|
|
"_id"
|
|
|
|
);
|
|
|
|
if (!account) {
|
|
|
|
throw new Error("Invalid accountId-nonce pair");
|
|
|
|
}
|
2024-12-22 00:44:49 +01:00
|
|
|
return account;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getAccountIdForRequest = async (req: Request): Promise<string> => {
|
|
|
|
return (await getAccountForRequest(req))._id.toString();
|
2024-05-28 13:45:06 +02:00
|
|
|
};
|