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";
|
|
|
|
import { LoadoutModel } from "@/src/models/inventoryModels/loadoutModel";
|
2023-05-19 15:22:48 -03:00
|
|
|
|
|
|
|
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
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
await createInventory(account._id, loadoutId);
|
|
|
|
await createShip(account._id, loadoutId);
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
export { isCorrectPassword, createAccount };
|
2023-12-14 17:34:15 +01:00
|
|
|
|
|
|
|
export const createLoadout = async (accountId: Types.ObjectId) => {
|
|
|
|
const loadout = new LoadoutModel({ loadoutOwnerId: accountId });
|
|
|
|
const savedLoadout = await loadout.save();
|
|
|
|
return savedLoadout._id;
|
|
|
|
};
|