2025-01-05 14:14:00 +01:00
|
|
|
import { dataSource } from "../../data-source";
|
|
|
|
import { role } from "../../entity/user/role";
|
|
|
|
import { user } from "../../entity/user/user";
|
2025-01-29 09:42:22 +01:00
|
|
|
import DatabaseActionException from "../../exceptions/databaseActionException";
|
2025-01-05 14:14:00 +01:00
|
|
|
import InternalException from "../../exceptions/internalException";
|
2024-08-22 11:40:31 +02:00
|
|
|
|
|
|
|
export default abstract class UserService {
|
2024-09-01 14:55:05 +02: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")
|
2025-01-12 18:17:54 +01:00
|
|
|
.orderBy("firstname", "ASC")
|
|
|
|
.addOrderBy("lastname", "ASC")
|
2024-09-01 14:55:05 +02:00
|
|
|
.getMany()
|
|
|
|
.then((res) => {
|
|
|
|
return res;
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2025-01-29 09:42:22 +01:00
|
|
|
throw new DatabaseActionException("SELECT", "user", err);
|
2024-09-01 14:55:05 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-08-22 11:40:31 +02:00
|
|
|
/**
|
|
|
|
* @description get user by id
|
2025-01-29 08:53:49 +01:00
|
|
|
* @param id string
|
2024-08-22 11:40:31 +02:00
|
|
|
* @returns {Promise<user>}
|
|
|
|
*/
|
2025-01-29 08:53:49 +01:00
|
|
|
static async getById(id: string): Promise<user> {
|
2024-08-22 11:40:31 +02:00
|
|
|
return await dataSource
|
|
|
|
.getRepository(user)
|
|
|
|
.createQueryBuilder("user")
|
2024-09-01 14:55:05 +02:00
|
|
|
.leftJoinAndSelect("user.roles", "roles")
|
|
|
|
.leftJoinAndSelect("user.permissions", "permissions")
|
|
|
|
.leftJoinAndSelect("roles.permissions", "role_permissions")
|
2024-08-22 11:40:31 +02:00
|
|
|
.where("user.id = :id", { id: id })
|
|
|
|
.getOneOrFail()
|
|
|
|
.then((res) => {
|
|
|
|
return res;
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2025-01-29 09:42:22 +01:00
|
|
|
throw new DatabaseActionException("SELECT", "user", err);
|
2024-08-22 11:40:31 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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) => {
|
2025-01-29 09:42:22 +01:00
|
|
|
throw new DatabaseActionException("SELECT", "user", err);
|
2024-08-22 11:40:31 +02:00
|
|
|
});
|
|
|
|
}
|
2024-08-25 13:36:19 +02: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) => {
|
2025-01-29 09:42:22 +01:00
|
|
|
throw new DatabaseActionException("SELECT", "user", err);
|
2024-08-25 13:36:19 +02: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) => {
|
2025-01-29 09:42:22 +01:00
|
|
|
throw new DatabaseActionException("COUNT", "users", err);
|
2024-08-25 13:36:19 +02:00
|
|
|
});
|
|
|
|
}
|
2024-08-27 17:54:59 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @description get roles assigned to user
|
2025-01-29 08:53:49 +01:00
|
|
|
* @param userId string
|
2024-08-27 17:54:59 +02:00
|
|
|
* @returns {Promise<Array<role>>}
|
|
|
|
*/
|
2025-01-29 08:53:49 +01:00
|
|
|
static async getAssignedRolesByUserId(userId: string): Promise<Array<role>> {
|
2024-08-27 17:54:59 +02:00
|
|
|
return await dataSource
|
|
|
|
.getRepository(user)
|
|
|
|
.createQueryBuilder("user")
|
|
|
|
.leftJoinAndSelect("user.roles", "roles")
|
2024-09-01 14:55:05 +02:00
|
|
|
.leftJoinAndSelect("roles.permissions", "role_permissions")
|
2024-08-27 17:54:59 +02:00
|
|
|
.where("user.id = :id", { id: userId })
|
|
|
|
.getOneOrFail()
|
|
|
|
.then((res) => {
|
|
|
|
return res.roles;
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
2025-01-29 09:42:22 +01:00
|
|
|
throw new DatabaseActionException("SELECT", "userRoles", err);
|
2024-08-27 17:54:59 +02:00
|
|
|
});
|
|
|
|
}
|
2024-08-22 11:40:31 +02:00
|
|
|
}
|