chore: fetch ticket that only assing to a admin and only super admin can see all ticket

This commit is contained in:
mahyargdz
2025-03-03 12:01:13 +03:30
parent c302ab1c77
commit 1f08514deb
3 changed files with 17 additions and 17 deletions
@@ -243,10 +243,11 @@ export class TicketsService {
return { tickets, count, paginate: true }; return { tickets, count, paginate: true };
} }
//******************************** */ //******************************** */
//TODO: get for admin with group of category async getTicketForAdmin(queryDto: SearchTicketQueryDto, adminId: string) {
async getTicketForAdmin(queryDto: SearchTicketQueryDto) {
const { limit, skip } = PaginationUtils(queryDto); const { limit, skip } = PaginationUtils(queryDto);
const isSuperAdmin = await this.usersService.isSuperAdmin(adminId);
const queryBuilder = this.ticketsRepository const queryBuilder = this.ticketsRepository
.createQueryBuilder("ticket") .createQueryBuilder("ticket")
.leftJoinAndSelect("ticket.category", "category") .leftJoinAndSelect("ticket.category", "category")
@@ -255,6 +256,10 @@ export class TicketsService {
.leftJoinAndSelect("ticket.danakService", "danakService") .leftJoinAndSelect("ticket.danakService", "danakService")
.orderBy("ticket.createdAt", "DESC"); .orderBy("ticket.createdAt", "DESC");
if (!isSuperAdmin) {
queryBuilder.andWhere("ticket.assignedToId = :adminId", { adminId });
}
if (queryDto.status) { if (queryDto.status) {
queryBuilder.andWhere("ticket.status = :status", { status: queryDto.status }); queryBuilder.andWhere("ticket.status = :status", { status: queryDto.status });
} }
+2 -2
View File
@@ -86,8 +86,8 @@ export class TicketsController {
@PermissionsDec(PermissionEnum.TICKETS) @PermissionsDec(PermissionEnum.TICKETS)
@Pagination() @Pagination()
@Get("admin") @Get("admin")
getAllTickets(@Query() queryDto: SearchTicketQueryDto) { getAllTickets(@Query() queryDto: SearchTicketQueryDto, @UserDec("id") adminId: string) {
return this.ticketsService.getTicketForAdmin(queryDto); return this.ticketsService.getTicketForAdmin(queryDto, adminId);
} }
@ApiOperation({ summary: "create ticket messages" }) @ApiOperation({ summary: "create ticket messages" })
+8 -13
View File
@@ -376,12 +376,7 @@ export class UsersService {
///****************************************************** */ ///****************************************************** */
async updateUserPassword(userId: string, newPassword: string) { async updateUserPassword(userId: string, newPassword: string) {
const updatedUser = await this.userRepository.update( const updatedUser = await this.userRepository.update({ id: userId }, { password: newPassword });
{ id: userId },
{
password: newPassword,
},
);
return { return {
updatedUser, updatedUser,
}; };
@@ -396,7 +391,13 @@ export class UsersService {
return { users }; return { users };
} }
/************************************************************ */
async isSuperAdmin(adminId: string) {
const user = await this.userRepository.findOne({ where: { id: adminId }, relations: { roles: true } });
if (!user) throw new BadRequestException(UserMessage.USER_NOT_FOUND);
return user.roles.some((role) => role.name === RoleEnum.SUPER_ADMIN);
}
/************************************************************ */ /************************************************************ */
async getCustomers(queryDto: SearchCustomersDto) { async getCustomers(queryDto: SearchCustomersDto) {
@@ -667,13 +668,7 @@ export class UsersService {
/************************************************************ */ /************************************************************ */
async getCustomersCount() { async getCustomersCount() {
const count = await this.userRepository.count({ const count = await this.userRepository.count({ where: { roles: { name: RoleEnum.USER } } });
where: {
roles: {
name: RoleEnum.USER,
},
},
});
return count; return count;
} }