chore: add crude for admin
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import slugify from "slugify";
|
||||
import { In } from "typeorm";
|
||||
import { DataSource, In, Not } from "typeorm";
|
||||
|
||||
import { AdminMessage, AdsMessage, UserMessage } from "../../../common/enums/message.enum";
|
||||
import { PaginationUtils } from "../../utils/providers/pagination.utils";
|
||||
@@ -9,6 +9,7 @@ import { CreateAdminDto } from "../DTO/create-admin.dto";
|
||||
import { CreateRoleDto } from "../DTO/create-role.dto";
|
||||
import { SearchAdminQueryDto } from "../DTO/search-admins-query.dto";
|
||||
import { SearchRolesQueryDto } from "../DTO/search-roles.dto";
|
||||
import { UpdateAdminDto } from "../DTO/update-admin.dto";
|
||||
import { PermissionsRepository } from "../repositories/permissions.repository";
|
||||
import { RoleRepository } from "../repositories/roles.repository";
|
||||
import { UserRepository } from "../repositories/users.repository";
|
||||
@@ -20,33 +21,127 @@ export class AdminsService {
|
||||
private readonly rolesRepository: RoleRepository,
|
||||
private readonly permissionsRepository: PermissionsRepository,
|
||||
private readonly passwordService: PasswordService,
|
||||
private readonly dataSource: DataSource,
|
||||
) {}
|
||||
|
||||
async createAdmin(createDto: CreateAdminDto) {
|
||||
const existEmail = await this.userRepository.findOneBy({ email: createDto.email });
|
||||
if (existEmail) throw new BadRequestException(UserMessage.EMAIL_EXIST);
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
|
||||
const existPhone = await this.userRepository.findOneBy({ phone: createDto.phone });
|
||||
if (existPhone) throw new BadRequestException(UserMessage.PHONE_EXIST);
|
||||
try {
|
||||
await queryRunner.connect();
|
||||
await queryRunner.startTransaction();
|
||||
|
||||
const role = await this.rolesRepository.findOne({ where: { id: createDto.roleId }, relations: { permissions: true } });
|
||||
if (!role) throw new BadRequestException(AdsMessage.ROLE_NOT_FOUND);
|
||||
const existEmail = await queryRunner.manager.findOneBy(this.userRepository.target, { email: createDto.email });
|
||||
if (existEmail) throw new BadRequestException(UserMessage.EMAIL_EXIST);
|
||||
|
||||
await this.rolesRepository.save(role);
|
||||
const existPhone = await queryRunner.manager.findOneBy(this.userRepository.target, { phone: createDto.phone });
|
||||
if (existPhone) throw new BadRequestException(UserMessage.PHONE_EXIST);
|
||||
|
||||
if (createDto.password !== createDto.repeatPassword) throw new BadRequestException(AdminMessage.PASSWORD_NOT_MATCH);
|
||||
const role = await queryRunner.manager.findOne(this.rolesRepository.target, {
|
||||
where: { id: createDto.roleId },
|
||||
relations: { permissions: true },
|
||||
});
|
||||
if (!role) throw new BadRequestException(AdsMessage.ROLE_NOT_FOUND);
|
||||
|
||||
const hashedPassword = await this.passwordService.hashPassword(createDto.password);
|
||||
if (createDto.password !== createDto.repeatPassword) throw new BadRequestException(AdminMessage.PASSWORD_NOT_MATCH);
|
||||
|
||||
const userName = slugify(`${createDto.firstName} ${Date.now().toString().slice(-5)}`, { lower: true, trim: true });
|
||||
const hashedPassword = await this.passwordService.hashPassword(createDto.password);
|
||||
const userName = slugify(`${createDto.firstName} ${Date.now().toString().slice(-5)}`, { lower: true, trim: true });
|
||||
|
||||
const adminUser = this.userRepository.create({ ...createDto, roles: [role], password: hashedPassword, userName });
|
||||
await this.userRepository.save(adminUser);
|
||||
await queryRunner.manager.save(this.rolesRepository.target, role);
|
||||
|
||||
const adminUser = queryRunner.manager.create(this.userRepository.target, {
|
||||
...createDto,
|
||||
roles: [role],
|
||||
password: hashedPassword,
|
||||
userName,
|
||||
});
|
||||
|
||||
await queryRunner.manager.save(this.userRepository.target, adminUser);
|
||||
await queryRunner.commitTransaction();
|
||||
|
||||
return {
|
||||
message: AdminMessage.ADMIN_CREATED,
|
||||
adminUser,
|
||||
};
|
||||
} catch (error) {
|
||||
await queryRunner.rollbackTransaction();
|
||||
throw error;
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
/************************************************************ */
|
||||
|
||||
async updateAdmin(id: string, updateDto: UpdateAdminDto) {
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
|
||||
try {
|
||||
await queryRunner.connect();
|
||||
await queryRunner.startTransaction();
|
||||
|
||||
const admin = await queryRunner.manager.findOne(this.userRepository.target, {
|
||||
where: { id, roles: { isAdmin: true } },
|
||||
relations: { roles: true },
|
||||
});
|
||||
if (!admin) throw new BadRequestException(UserMessage.ADMIN_NOT_FOUND);
|
||||
|
||||
if (updateDto.email && updateDto.email !== admin.email) {
|
||||
const existEmail = await queryRunner.manager.findOneBy(this.userRepository.target, { email: updateDto.email, id: Not(id) });
|
||||
if (existEmail) throw new BadRequestException(UserMessage.EMAIL_EXIST);
|
||||
}
|
||||
|
||||
if (updateDto.phone && updateDto.phone !== admin.phone) {
|
||||
const existPhone = await queryRunner.manager.findOneBy(this.userRepository.target, { phone: updateDto.phone, id: Not(id) });
|
||||
if (existPhone) throw new BadRequestException(UserMessage.PHONE_EXIST);
|
||||
}
|
||||
|
||||
if (updateDto.roleId) {
|
||||
const role = await queryRunner.manager.findOne(this.rolesRepository.target, {
|
||||
where: { id: updateDto.roleId },
|
||||
relations: { permissions: true },
|
||||
});
|
||||
if (!role) throw new BadRequestException(AdsMessage.ROLE_NOT_FOUND);
|
||||
|
||||
await queryRunner.manager.save(this.rolesRepository.target, role);
|
||||
|
||||
admin.roles = [role];
|
||||
|
||||
await queryRunner.manager.save(this.userRepository.target, admin);
|
||||
}
|
||||
|
||||
if (updateDto.password) {
|
||||
if (updateDto.password !== updateDto.repeatPassword) throw new BadRequestException(AdminMessage.PASSWORD_NOT_MATCH);
|
||||
|
||||
admin.password = await this.passwordService.hashPassword(updateDto.password);
|
||||
}
|
||||
|
||||
await queryRunner.manager.save(this.userRepository.target, { ...admin, ...updateDto, password: admin.password });
|
||||
|
||||
await queryRunner.commitTransaction();
|
||||
|
||||
return {
|
||||
message: AdminMessage.ADMIN_UPDATED,
|
||||
};
|
||||
} catch (error) {
|
||||
await queryRunner.rollbackTransaction();
|
||||
throw error;
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
/************************************************************ */
|
||||
|
||||
async deleteAdmin(id: string) {
|
||||
const admin = await this.userRepository.findOne({ where: { id, roles: { isAdmin: true } } });
|
||||
if (!admin) throw new BadRequestException(AdminMessage.ADMIN_NOT_FOUND);
|
||||
|
||||
await this.userRepository.softDelete(id);
|
||||
return {
|
||||
message: AdminMessage.ADMIN_CREATED,
|
||||
adminUser,
|
||||
message: AdminMessage.ADMIN_DELETED,
|
||||
};
|
||||
}
|
||||
|
||||
/************************************************************ */
|
||||
|
||||
async createRole(createDto: CreateRoleDto) {
|
||||
@@ -78,6 +173,13 @@ export class AdminsService {
|
||||
|
||||
/************************************************************ */
|
||||
|
||||
async getAdminById(id: string) {
|
||||
const admin = await this.userRepository.findOne({ where: { id, roles: { isAdmin: true } }, relations: { roles: true } });
|
||||
if (!admin) throw new BadRequestException(AdminMessage.ADMIN_NOT_FOUND);
|
||||
return { admin };
|
||||
}
|
||||
/************************************************************ */
|
||||
|
||||
async getPermissions() {
|
||||
const permissions = await this.permissionsRepository.find({ select: { id: true, name: true } });
|
||||
return {
|
||||
|
||||
Reference in New Issue
Block a user