SpaceNinjaServer/src/services/loginService.ts
OrdisPrime dd99e8782c
saveLoadout and misc. (#99)
Co-authored-by: Matej Voboril <tobiah@pm.me>
2023-12-14 17:34:15 +01:00

35 lines
1.3 KiB
TypeScript

import { Account } from "@/src/models/loginModel";
import { createInventory } from "@/src/services/inventoryService";
import { IDatabaseAccount } from "@/src/types/loginTypes";
import { createShip } from "./shipService";
import { Types } from "mongoose";
import { LoadoutModel } from "@/src/models/inventoryModels/loadoutModel";
const isCorrectPassword = (requestPassword: string, databasePassword: string): boolean => {
return requestPassword === databasePassword;
};
const createAccount = async (accountData: IDatabaseAccount) => {
const account = new Account(accountData);
try {
await account.save();
const loadoutId = await createLoadout(account._id);
await createInventory(account._id, loadoutId);
await createShip(account._id, loadoutId);
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");
}
};
export { isCorrectPassword, createAccount };
export const createLoadout = async (accountId: Types.ObjectId) => {
const loadout = new LoadoutModel({ loadoutOwnerId: accountId });
const savedLoadout = await loadout.save();
return savedLoadout._id;
};