ff-admin-server/src/service/userService.ts

130 lines
3.6 KiB
TypeScript
Raw Normal View History

import { dataSource } from "../data-source";
2024-08-27 15:54:59 +00:00
import { role } from "../entity/role";
import { user } from "../entity/user";
import InternalException from "../exceptions/internalException";
export default abstract class UserService {
2024-09-01 12:55:05 +00:00
/**
* @description get users
* @returns {Promise<Array<user>>}
*/
static async getAll(): Promise<Array<user>> {
return await dataSource
.getRepository(user)
.createQueryBuilder("user")
.leftJoinAndSelect("user.roles", "roles")
.leftJoinAndSelect("user.permissions", "permissions")
.leftJoinAndSelect("roles.permissions", "role_permissions")
.getMany()
.then((res) => {
return res;
})
.catch((err) => {
2024-09-06 08:08:19 +00:00
throw new InternalException("users not found", err);
2024-09-01 12:55:05 +00:00
});
}
/**
* @description get user by id
* @param id number
* @returns {Promise<user>}
*/
static async getById(id: number): Promise<user> {
return await dataSource
.getRepository(user)
.createQueryBuilder("user")
2024-09-01 12:55:05 +00:00
.leftJoinAndSelect("user.roles", "roles")
.leftJoinAndSelect("user.permissions", "permissions")
.leftJoinAndSelect("roles.permissions", "role_permissions")
.where("user.id = :id", { id: id })
.getOneOrFail()
.then((res) => {
return res;
})
.catch((err) => {
2024-09-06 08:08:19 +00:00
throw new InternalException("user not found by id", err);
});
}
/**
* @description get user by username
* @param username string
* @returns {Promise<user>}
*/
static async getByUsername(username: string): Promise<user> {
return await dataSource
.getRepository(user)
.createQueryBuilder("user")
.select()
.where("user.username = :username", { username: username })
.getOneOrFail()
.then((res) => {
return res;
})
.catch((err) => {
2024-09-06 08:08:19 +00:00
throw new InternalException("user not found by username", err);
});
}
2024-08-25 11:36:19 +00:00
/**
* @description get users by mail or username
* @param username string
* @param mail string
* @returns {Promise<Array<user>>}
*/
static async getByMailOrUsername(mail?: string, username?: string): Promise<Array<user>> {
return await dataSource
.getRepository(user)
.createQueryBuilder("user")
.select()
.where("user.mail = :mail", { mail: mail })
.orWhere("user.username = :username", { username: username })
.getMany()
.then((res) => {
return res;
})
.catch((err) => {
2024-09-06 08:08:19 +00:00
throw new InternalException("user not found by mail or username", err);
2024-08-25 11:36:19 +00:00
});
}
/**
* @description get count of users
* @returns {Promise<number>}
*/
static async count(): Promise<number> {
return await dataSource
.getRepository(user)
.createQueryBuilder("user")
.select()
.getCount()
.then((res) => {
return res;
})
.catch((err) => {
2024-09-06 08:08:19 +00:00
throw new InternalException("could not count users", err);
2024-08-25 11:36:19 +00:00
});
}
2024-08-27 15:54:59 +00:00
/**
* @description get roles assigned to user
* @param userId number
* @returns {Promise<Array<role>>}
*/
static async getAssignedRolesByUserId(userId: number): Promise<Array<role>> {
return await dataSource
.getRepository(user)
.createQueryBuilder("user")
.leftJoinAndSelect("user.roles", "roles")
2024-09-01 12:55:05 +00:00
.leftJoinAndSelect("roles.permissions", "role_permissions")
2024-08-27 15:54:59 +00:00
.where("user.id = :id", { id: userId })
.getOneOrFail()
.then((res) => {
return res.roles;
})
.catch((err) => {
2024-09-06 08:08:19 +00:00
throw new InternalException("could not get roles for user", err);
2024-08-27 15:54:59 +00:00
});
}
}