chore: add crude for admin
This commit is contained in:
@@ -113,6 +113,8 @@ export class AuthService {
|
||||
const isAdmin = this.checkUserIsAdmin(user.roles);
|
||||
if (!isAdmin) throw new BadRequestException(AuthMessage.NOT_ADMIN);
|
||||
|
||||
if (user.deletedAt) throw new BadRequestException(AuthMessage.ACCESS_DENIED);
|
||||
|
||||
const tokens = await this.tokensService.generateTokens(user);
|
||||
|
||||
return {
|
||||
@@ -191,6 +193,7 @@ export class AuthService {
|
||||
const isUserAdmin = this.checkUserIsAdmin(user.roles);
|
||||
|
||||
if (!isUserAdmin) throw new BadRequestException(AuthMessage.NOT_ADMIN);
|
||||
if (user.deletedAt) throw new BadRequestException(AuthMessage.ACCESS_DENIED);
|
||||
|
||||
const tokens = await this.tokensService.generateTokens(user);
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import { IsBoolean, IsNotEmpty, IsString, IsUrl, MaxLength } from "class-validat
|
||||
|
||||
import { BlogMessage } from "../../../common/enums/message.enum";
|
||||
|
||||
export class CreateCategoryDto {
|
||||
export class CreateBlogCategoryDto {
|
||||
@IsNotEmpty({ message: BlogMessage.TITLE_REQUIRED })
|
||||
@IsString({ message: BlogMessage.TITLE_STRING })
|
||||
@MaxLength(100, { message: BlogMessage.TITLE_MAX_LENGTH })
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { PartialType } from "@nestjs/swagger";
|
||||
|
||||
import { CreateCategoryDto } from "./create-category.dto";
|
||||
import { CreateBlogCategoryDto } from "./create-category.dto";
|
||||
|
||||
export class UpdateCategoryDto extends PartialType(CreateCategoryDto) {}
|
||||
export class UpdateBlogCategoryDto extends PartialType(CreateBlogCategoryDto) {}
|
||||
|
||||
@@ -11,12 +11,12 @@ import { ParamDto } from "../../../common/DTO/param.dto";
|
||||
import { CategoryListSearchQueryDto } from "../../danak-services/DTO/category-search-query.dto";
|
||||
import { PermissionEnum } from "../../users/enums/permission.enum";
|
||||
import { CreateBlogDto } from "../DTO/create-blog.dto";
|
||||
import { CreateCategoryDto } from "../DTO/create-category.dto";
|
||||
import { CreateBlogCategoryDto } from "../DTO/create-category.dto";
|
||||
import { CreateCommentDto } from "../DTO/create-comment.dto";
|
||||
import { BlogSlugDto } from "../DTO/get-blog-by-slug.dto";
|
||||
import { BlogListSearchQueryDto, BlogSearchQueryDto } from "../DTO/search-blog-query.dto";
|
||||
import { UpdateBlogDto } from "../DTO/update-blog.dto";
|
||||
import { UpdateCategoryDto } from "../DTO/update-category.dto";
|
||||
import { UpdateBlogCategoryDto } from "../DTO/update-category.dto";
|
||||
import { UpdateCommentStatusDto } from "../DTO/update-comment-status.dto";
|
||||
import { BlogsService } from "../providers/blogs.service";
|
||||
|
||||
@@ -165,7 +165,7 @@ export class BlogsController {
|
||||
@Post("categories")
|
||||
@AdminRoute()
|
||||
@PermissionsDec(PermissionEnum.BLOGS)
|
||||
createCategory(@Body() createCategoryDto: CreateCategoryDto) {
|
||||
createCategory(@Body() createCategoryDto: CreateBlogCategoryDto) {
|
||||
return this.blogsService.createCategory(createCategoryDto);
|
||||
}
|
||||
|
||||
@@ -173,7 +173,7 @@ export class BlogsController {
|
||||
@Patch("categories/:id")
|
||||
@AdminRoute()
|
||||
@PermissionsDec(PermissionEnum.BLOGS)
|
||||
updateCategory(@Param() paramDto: ParamDto, @Body() updateCategoryDto: UpdateCategoryDto) {
|
||||
updateCategory(@Param() paramDto: ParamDto, @Body() updateCategoryDto: UpdateBlogCategoryDto) {
|
||||
return this.blogsService.updateCategory(paramDto.id, updateCategoryDto);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,11 +5,11 @@ import { BlogMessage, CommonMessage } from "../../../common/enums/message.enum";
|
||||
import { CategoryListSearchQueryDto } from "../../danak-services/DTO/category-search-query.dto";
|
||||
import { UsersService } from "../../users/providers/users.service";
|
||||
import { CreateBlogDto } from "../DTO/create-blog.dto";
|
||||
import { CreateCategoryDto } from "../DTO/create-category.dto";
|
||||
import { CreateBlogCategoryDto } from "../DTO/create-category.dto";
|
||||
import { CreateCommentDto } from "../DTO/create-comment.dto";
|
||||
import { BlogListSearchQueryDto, BlogSearchQueryDto } from "../DTO/search-blog-query.dto";
|
||||
import { UpdateBlogDto } from "../DTO/update-blog.dto";
|
||||
import { UpdateCategoryDto } from "../DTO/update-category.dto";
|
||||
import { UpdateBlogCategoryDto } from "../DTO/update-category.dto";
|
||||
import { UpdateCommentStatusDto } from "../DTO/update-comment-status.dto";
|
||||
import { CommentStatus } from "../enums/comment-status.enum";
|
||||
import { BlogCategoriesRepository } from "../repositories/blog-categories.repository";
|
||||
@@ -25,7 +25,7 @@ export class BlogsService {
|
||||
) {}
|
||||
|
||||
//*********************************** */
|
||||
async createCategory(createCategoryDto: CreateCategoryDto) {
|
||||
async createCategory(createCategoryDto: CreateBlogCategoryDto) {
|
||||
const existingCategory = await this.blogCategoriesRepository.findOneBy({ title: createCategoryDto.title });
|
||||
if (existingCategory) throw new BadRequestException(BlogMessage.CATEGORY_ALREADY_EXISTS);
|
||||
|
||||
@@ -38,7 +38,7 @@ export class BlogsService {
|
||||
}
|
||||
//*********************************** */
|
||||
|
||||
async updateCategory(id: string, updateCategoryDto: UpdateCategoryDto) {
|
||||
async updateCategory(id: string, updateCategoryDto: UpdateBlogCategoryDto) {
|
||||
const category = await this.findCategoryById(id);
|
||||
|
||||
if (updateCategoryDto.title) {
|
||||
|
||||
@@ -4,7 +4,7 @@ import { IsNull, Repository } from "typeorm";
|
||||
|
||||
import { CategoryListSearchQueryDto } from "../../danak-services/DTO/category-search-query.dto";
|
||||
import { PaginationUtils } from "../../utils/providers/pagination.utils";
|
||||
import { CreateCategoryDto } from "../DTO/create-category.dto";
|
||||
import { CreateBlogCategoryDto } from "../DTO/create-category.dto";
|
||||
import { BlogCategory } from "../entities/blog-category.entity";
|
||||
|
||||
@Injectable()
|
||||
@@ -44,7 +44,7 @@ export class BlogCategoriesRepository extends Repository<BlogCategory> {
|
||||
return query.getManyAndCount();
|
||||
}
|
||||
|
||||
async createCategory(createBlogCategoryDto: CreateCategoryDto) {
|
||||
async createCategory(createBlogCategoryDto: CreateBlogCategoryDto) {
|
||||
const { title, description, iconUrl, isActive } = createBlogCategoryDto;
|
||||
|
||||
const blogCategory = this.create({ title, description, iconUrl, isActive });
|
||||
|
||||
@@ -3,7 +3,7 @@ import { IsBoolean, IsInt, IsNotEmpty, IsOptional, IsString, IsUUID, IsUrl, Leng
|
||||
|
||||
import { CategoryMessage } from "../../../common/enums/message.enum";
|
||||
|
||||
export class CreateCategoryDto {
|
||||
export class CreateDanakServiceCategoryDto {
|
||||
@IsNotEmpty({ message: CategoryMessage.TITLE_REQUIRED })
|
||||
@IsString({ message: CategoryMessage.TITLE_STRING })
|
||||
@Length(3, 50, { message: CategoryMessage.TITLE_LENGTH })
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { PartialType } from "@nestjs/swagger";
|
||||
|
||||
import { CreateCategoryDto } from "./create-category.dto";
|
||||
import { CreateDanakServiceCategoryDto } from "./create-category.dto";
|
||||
|
||||
export class UpdateCategoryDto extends PartialType(CreateCategoryDto) {}
|
||||
export class UpdateDanakServiceCategoryDto extends PartialType(CreateDanakServiceCategoryDto) {}
|
||||
|
||||
@@ -3,11 +3,11 @@ import { ApiOperation, ApiTags } from "@nestjs/swagger";
|
||||
|
||||
import { AddReviewDto } from "./DTO/add-review.dto";
|
||||
import { CategoryListSearchQueryDto, CategorySearchQueryDto } from "./DTO/category-search-query.dto";
|
||||
import { CreateCategoryDto } from "./DTO/create-category.dto";
|
||||
import { CreateDanakServiceCategoryDto } from "./DTO/create-category.dto";
|
||||
import { CreateServiceDto } from "./DTO/create-service.dto";
|
||||
import { DanakServiceReviewQueryDto } from "./DTO/danak-service-review-query.dto";
|
||||
import { DanakServicesGlobalSearchDto, DanakServicesQueryDto, DanakServicesSearchQueryDto } from "./DTO/danak-services-search-query.dto";
|
||||
import { UpdateCategoryDto } from "./DTO/update-category.dto";
|
||||
import { UpdateDanakServiceCategoryDto } from "./DTO/update-category.dto";
|
||||
import { UpdateReviewStatusDto } from "./DTO/update-review-status.dto";
|
||||
import { UpdateServiceDto } from "./DTO/update-service.dto";
|
||||
import { DanakServicesService } from "./providers/danak-services.service";
|
||||
@@ -28,7 +28,7 @@ export class DanakServicesController {
|
||||
@PermissionsDec(PermissionEnum.SERVICES)
|
||||
@ApiOperation({ summary: "Create a new service category => admin route" })
|
||||
@Post("categories")
|
||||
createCategory(@Body() createDto: CreateCategoryDto) {
|
||||
createCategory(@Body() createDto: CreateDanakServiceCategoryDto) {
|
||||
return this.danakServicesService.createCategory(createDto);
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ export class DanakServicesController {
|
||||
@PermissionsDec(PermissionEnum.SERVICES)
|
||||
@ApiOperation({ summary: "update category by id => admin route" })
|
||||
@Patch("categories/:id")
|
||||
updateCategory(@Param() paramDto: ParamDto, @Body() updateDto: UpdateCategoryDto) {
|
||||
updateCategory(@Param() paramDto: ParamDto, @Body() updateDto: UpdateDanakServiceCategoryDto) {
|
||||
return this.danakServicesService.updateCategory(paramDto.id, updateDto);
|
||||
}
|
||||
|
||||
|
||||
@@ -7,11 +7,11 @@ import { SubscriptionStatus } from "../../subscriptions/enums/subscription-statu
|
||||
import { PaginationUtils } from "../../utils/providers/pagination.utils";
|
||||
import { AddReviewDto } from "../DTO/add-review.dto";
|
||||
import { CategoryListSearchQueryDto, CategorySearchQueryDto } from "../DTO/category-search-query.dto";
|
||||
import { CreateCategoryDto } from "../DTO/create-category.dto";
|
||||
import { CreateDanakServiceCategoryDto } from "../DTO/create-category.dto";
|
||||
import { CreateServiceDto } from "../DTO/create-service.dto";
|
||||
import { DanakServiceReviewQueryDto } from "../DTO/danak-service-review-query.dto";
|
||||
import { DanakServicesGlobalSearchDto, DanakServicesQueryDto, DanakServicesSearchQueryDto } from "../DTO/danak-services-search-query.dto";
|
||||
import { UpdateCategoryDto } from "../DTO/update-category.dto";
|
||||
import { UpdateDanakServiceCategoryDto } from "../DTO/update-category.dto";
|
||||
import { UpdateReviewStatusDto } from "../DTO/update-review-status.dto";
|
||||
import { UpdateServiceDto } from "../DTO/update-service.dto";
|
||||
import { DanakServiceCategory } from "../entities/danak-service-category.entity";
|
||||
@@ -34,7 +34,7 @@ export class DanakServicesService {
|
||||
) {}
|
||||
/******************************************** */
|
||||
|
||||
async createCategory(createDto: CreateCategoryDto) {
|
||||
async createCategory(createDto: CreateDanakServiceCategoryDto) {
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
await queryRunner.startTransaction();
|
||||
@@ -98,7 +98,7 @@ export class DanakServicesService {
|
||||
|
||||
/******************************************** */
|
||||
|
||||
async updateCategory(categoryId: string, updateDto: UpdateCategoryDto) {
|
||||
async updateCategory(categoryId: string, updateDto: UpdateDanakServiceCategoryDto) {
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
await queryRunner.startTransaction();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
|
||||
import Decimal from "decimal.js";
|
||||
import { Column, Entity, ManyToOne } from "typeorm";
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
|
||||
import Decimal from "decimal.js";
|
||||
import { Column, Entity, ManyToOne } from "typeorm";
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
|
||||
import Decimal from "decimal.js";
|
||||
import { Column, Entity, ManyToOne, OneToMany } from "typeorm";
|
||||
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import { InjectQueue } from "@nestjs/bullmq";
|
||||
import { BadRequestException, Injectable, Logger } from "@nestjs/common";
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
import { Queue } from "bullmq";
|
||||
import dayjs from "dayjs";
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
import Decimal from "decimal.js";
|
||||
import { Between, DataSource, QueryRunner } from "typeorm";
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { InjectQueue, Processor } from "@nestjs/bullmq";
|
||||
import { Job, Queue } from "bullmq";
|
||||
import dayjs from "dayjs";
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
import Decimal from "decimal.js";
|
||||
import { DataSource, QueryRunner } from "typeorm";
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
|
||||
import Decimal from "decimal.js";
|
||||
import { Column, Entity, ManyToOne, OneToMany } from "typeorm";
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
|
||||
import Decimal from "decimal.js";
|
||||
import { Column, Entity, ManyToOne, OneToMany } from "typeorm";
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
//------------------ process payment -------------------------
|
||||
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
|
||||
import Decimal from "decimal.js";
|
||||
|
||||
export interface IProcessPaymentParams {
|
||||
|
||||
@@ -3,7 +3,6 @@ import { BadRequestException, HttpException, HttpStatus, Injectable, InternalSer
|
||||
import { ConfigService } from "@nestjs/config";
|
||||
import { Queue } from "bullmq";
|
||||
import dayjs from "dayjs";
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
import Decimal from "decimal.js";
|
||||
import { FastifyReply } from "fastify";
|
||||
import { DataSource, Not, QueryRunner } from "typeorm";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
|
||||
import Decimal from "decimal.js";
|
||||
import { Column, Entity, ManyToOne } from "typeorm";
|
||||
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
import { randomBytes } from "node:crypto";
|
||||
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
import Decimal from "decimal.js";
|
||||
import { QueryRunner } from "typeorm";
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
|
||||
import Decimal from "decimal.js";
|
||||
import { Column, DeleteDateColumn, Entity, Index, ManyToOne, OneToMany } from "typeorm";
|
||||
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
import dayjs from "dayjs";
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
import Decimal from "decimal.js";
|
||||
import { DataSource, In, Not, QueryRunner } from "typeorm";
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
import { PartialType } from "@nestjs/swagger";
|
||||
|
||||
import { CreateAdminDto } from "./create-admin.dto";
|
||||
|
||||
export class UpdateAdminDto extends PartialType(CreateAdminDto) {}
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Exclude } from "class-transformer";
|
||||
import { Column, Entity, JoinTable, ManyToMany, OneToMany, OneToOne } from "typeorm";
|
||||
import { Column, DeleteDateColumn, Entity, JoinTable, ManyToMany, OneToMany, OneToOne } from "typeorm";
|
||||
|
||||
import { LegalUser } from "./legal-user.entity";
|
||||
import { RealUser } from "./real-user.entity";
|
||||
@@ -63,6 +63,9 @@ export class User extends BaseEntity {
|
||||
@Column({ type: "enum", enum: FinancialType, nullable: true, default: null })
|
||||
financialType: FinancialType | null;
|
||||
|
||||
@DeleteDateColumn({ nullable: true })
|
||||
deletedAt?: Date;
|
||||
|
||||
//-----------------------------------------
|
||||
|
||||
@ManyToMany(() => Role, (role) => role.users)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -117,7 +117,8 @@ export class UserRepository extends Repository<User> {
|
||||
// .addSelect(["role.id", "role.name"])
|
||||
.where("role.isAdmin = :isAdmin", { isAdmin: true })
|
||||
.leftJoinAndSelect("user.groups", "groups")
|
||||
.leftJoinAndSelect("role.permissions", "permissions");
|
||||
.leftJoinAndSelect("role.permissions", "permissions")
|
||||
.andWhere("user.deletedAt IS NULL");
|
||||
// .where("permissions.name IN (:...adminPermissions)", {
|
||||
// adminPermissions: [PermissionEnum.ADMINS],
|
||||
// });
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Body, Controller, Get, HttpCode, HttpStatus, Param, Patch, Post, Query, Res } from "@nestjs/common";
|
||||
import { Body, Controller, Delete, Get, HttpCode, HttpStatus, Param, Patch, Post, Query, Res } from "@nestjs/common";
|
||||
import { ApiOperation, ApiTags } from "@nestjs/swagger";
|
||||
import { FastifyReply } from "fastify";
|
||||
|
||||
@@ -12,6 +12,7 @@ import { CreateRoleDto } from "./DTO/create-role.dto";
|
||||
import { SearchAdminQueryDto } from "./DTO/search-admins-query.dto";
|
||||
import { SearchCustomersDto } from "./DTO/search-customers.dto";
|
||||
import { SearchRolesQueryDto } from "./DTO/search-roles.dto";
|
||||
import { UpdateAdminDto } from "./DTO/update-admin.dto";
|
||||
import { UpdateCustomerDto } from "./DTO/update-customer.dto";
|
||||
import { UpdateProfileDto } from "./DTO/update-profile.dto";
|
||||
import { CreateUserGroupDto } from "./DTO/user-group.dto";
|
||||
@@ -183,6 +184,30 @@ export class UsersController {
|
||||
return this.adminsService.getAdmins(queryDto);
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "get admin by id ==> admin route" })
|
||||
@AuthGuards()
|
||||
@PermissionsDec(PermissionEnum.ADMINS, PermissionEnum.CUSTOMERS)
|
||||
@Get("admins/:id")
|
||||
getAdminById(@Param() paramDto: ParamDto) {
|
||||
return this.adminsService.getAdminById(paramDto.id);
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "update admin ==> admin route" })
|
||||
@AuthGuards()
|
||||
@PermissionsDec(PermissionEnum.ADMINS, PermissionEnum.CUSTOMERS)
|
||||
@Patch("admins/:id")
|
||||
updateAdmin(@Param() paramDto: ParamDto, @Body() updateDto: UpdateAdminDto) {
|
||||
return this.adminsService.updateAdmin(paramDto.id, updateDto);
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "delete admin ==> admin route" })
|
||||
@AuthGuards()
|
||||
@PermissionsDec(PermissionEnum.ADMINS, PermissionEnum.CUSTOMERS)
|
||||
@Delete("admins/:id")
|
||||
deleteAdmin(@Param() paramDto: ParamDto) {
|
||||
return this.adminsService.deleteAdmin(paramDto.id);
|
||||
}
|
||||
|
||||
@ApiOperation({ summary: "create admin ==> admin route" })
|
||||
@AuthGuards()
|
||||
@PermissionsDec(PermissionEnum.ADMINS, PermissionEnum.CUSTOMERS)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
|
||||
import Decimal from "decimal.js";
|
||||
import { Column, Entity, ManyToOne } from "typeorm";
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
|
||||
import Decimal from "decimal.js";
|
||||
import { Column, Entity, JoinColumn, OneToMany, OneToOne } from "typeorm";
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { BadRequestException, Injectable } from "@nestjs/common";
|
||||
// eslint-disable-next-line import/no-named-as-default
|
||||
import Decimal from "decimal.js";
|
||||
import { QueryRunner } from "typeorm";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user