feat: major code refactor, add login and register endpoints with swagger
All checks were successful
Update changelog / changelog (push) Successful in 27s
All checks were successful
Update changelog / changelog (push) Successful in 27s
This commit is contained in:
113
src/services/userService.ts
Normal file
113
src/services/userService.ts
Normal file
@@ -0,0 +1,113 @@
|
||||
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<boolean>} True if an account has been registered successfully.
|
||||
// */
|
||||
// async register(name: string, password: string, role: number | null): Promise<boolean> {
|
||||
//
|
||||
// // 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<number>} User id
|
||||
*/
|
||||
async findIdByCredentials(creds: UserCredentials): Promise<number> {
|
||||
|
||||
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|null>)} User entity
|
||||
*/
|
||||
async findByCredentials(creds: UserCredentials): Promise<User | null> {
|
||||
|
||||
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
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 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>} Number of user entities in DB (0...).
|
||||
*/
|
||||
async countAll(): Promise<number> {
|
||||
return await this.userRepo.count();
|
||||
}
|
||||
|
||||
/**
|
||||
* Simply insert a new user entity.
|
||||
*
|
||||
* @param {User} user The user to insert
|
||||
*/
|
||||
async add(user: User): Promise<any> {
|
||||
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();
|
||||
Reference in New Issue
Block a user