import { User } from '../entities/User'; import { AppDataSource } from '../data-source'; export type UserCredentials = { name?: string | undefined; password?: string | undefined; } export class UserService { dataSource = AppDataSource; userRepo = this.dataSource.getRepository(User); // // Phased out in favor of in-controller logic. // // /** // * Register a new user. Checks if a user with provided name exists. // * Returns `true` if user has been registered successfully. // * // * @param {string} name Username // * @param {string} password User's password // * @param {(null|number)} role User's role (0 | null = standard user, 1 = admin) // * @return {Promise} True if an account has been registered successfully. // */ // async register(name: string, password: string, role: number | null): Promise { // // // Check if user by this name already exists. // // If so, return here to not create a new account. // if (await this.userRepo.existsBy({ name: name })) // return false; // // // TODO: insert "dirty" entity... // // await this.userRepo.insert(); // // // TODO: salt the password... // // return true; // } /** * Finds entity id by any credentials passed. * Returns user id (typically number > 0) on success, * -1 on no match, and -2 on multiple matches (if that were to ever occur). * * @param {UserCredentials} creds The creds * @return {Promise} User id */ async findIdByCredentials(creds: UserCredentials): Promise { let users: User[] = await this.userRepo.find({ where: { ...creds } }); if (users.length === 0) return -1; // no match else if (users.length === 1) return users[0].id!; // exact match, return id else return -2; // sus, too many matches } /** * Finds the user entity by any credentials passed. * Returns user on success, otherwise null. * * @param {UserCredentials} creds The creds * @return {(Promise)} User entity */ async findByCredentials(creds: UserCredentials): Promise { let users: User[] = await this.userRepo.find({ where: { ...creds } }); if (users.length !== 1) return null; // no match, or too many matches, sus else return users[0]; // exact match, return user } /** * Finds a User by it's identifier. * * @param {number} id The identifier * @return {(Promise)} User (or null if not found) */ async findById(id: number): Promise { return await this.userRepo.findOneBy({id}); } /** * Counts all the user entities in DB. * Used to tell whether the next created account should be an admin * (No users in DB? That means most likely it's a fresh instance. * Then the next user should be an admin.) * * @return {Promise} Number of user entities in DB (0...). */ async countAll(): Promise { return await this.userRepo.count(); } /** * Simply insert a new user entity. * * @param {User} user The user to insert */ async add(user: User): Promise { await this.userRepo.insert(user); } /** * Simply save user entity to DB. * New user passed? New entity will get created. * Existing entity passed? It'll get updated. * * @param {User} user The user to insert or update */ async save(user: User) { // assert(user.id !== undefined, new Error("Passed user MUST contain an id!")); await this.userRepo.save(user); } } // export default new UserService();