hexabot/api/src/user/controllers/role.controller.ts

174 lines
5.2 KiB
TypeScript
Raw Normal View History

2024-09-10 09:50:11 +00:00
/*
* Copyright © 2024 Hexastack. All rights reserved.
*
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms:
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission.
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file).
*/
import {
Body,
Controller,
Delete,
2024-10-29 13:36:46 +00:00
ForbiddenException,
2024-09-10 09:50:11 +00:00
Get,
HttpCode,
NotFoundException,
Param,
Patch,
2024-10-29 13:36:46 +00:00
Post,
2024-09-10 09:50:11 +00:00
Query,
2024-09-23 10:17:19 +00:00
Req,
2024-10-29 13:36:46 +00:00
UseInterceptors,
2024-09-10 09:50:11 +00:00
} from '@nestjs/common';
import { CsrfCheck } from '@tekuconcept/nestjs-csrf';
2024-09-23 10:17:19 +00:00
import { Request } from 'express';
2024-09-10 09:50:11 +00:00
import { CsrfInterceptor } from '@/interceptors/csrf.interceptor';
import { LoggerService } from '@/logger/logger.service';
import { BaseController } from '@/utils/generics/base-controller';
import { PageQueryDto } from '@/utils/pagination/pagination-query.dto';
import { PageQueryPipe } from '@/utils/pagination/pagination-query.pipe';
import { PopulatePipe } from '@/utils/pipes/populate.pipe';
import { SearchFilterPipe } from '@/utils/pipes/search-filter.pipe';
2024-10-29 13:36:46 +00:00
import { TFilterQuery } from '@/utils/types/filter.types';
2024-09-10 09:50:11 +00:00
import { RoleCreateDto, RoleUpdateDto } from '../dto/role.dto';
2024-09-21 11:15:36 +00:00
import { Role, RoleFull, RolePopulate, RoleStub } from '../schemas/role.schema';
2024-09-23 10:17:19 +00:00
import { User } from '../schemas/user.schema';
2024-09-10 09:50:11 +00:00
import { RoleService } from '../services/role.service';
import { UserService } from '../services/user.service';
2024-09-10 09:50:11 +00:00
@UseInterceptors(CsrfInterceptor)
@Controller('role')
2024-09-21 11:15:36 +00:00
export class RoleController extends BaseController<
Role,
RoleStub,
RolePopulate,
RoleFull
> {
2024-09-10 09:50:11 +00:00
constructor(
private readonly roleService: RoleService,
private readonly logger: LoggerService,
private readonly userService: UserService,
2024-09-10 09:50:11 +00:00
) {
super(roleService);
}
/**
* Retrieves a paginated list of roles with optional filtering and population of related entities.
*
* @returns A promise that resolves to the paginated result of roles.
*/
@Get()
async findPage(
@Query(PageQueryPipe) pageQuery: PageQueryDto<Role>,
@Query(PopulatePipe)
populate: string[],
@Query(new SearchFilterPipe<Role>({ allowedFields: ['name'] }))
filters: TFilterQuery<Role>,
) {
2024-09-21 11:15:36 +00:00
return this.canPopulate(populate)
2024-09-10 09:50:11 +00:00
? await this.roleService.findPageAndPopulate(filters, pageQuery)
: await this.roleService.findPage(filters, pageQuery);
}
/**
* Counts the number of roles that match the provided filters.
*
* @returns A promise that resolves to the count of filtered roles.
*/
@Get('count')
async filterCount(
@Query(new SearchFilterPipe<Role>({ allowedFields: ['name'] }))
filters?: TFilterQuery<Role>,
) {
return await this.count(filters);
}
/**
* Retrieves a specific role by its ID. Optionally populates related entities such as permissions and users.
*
* @param id The ID of the role to retrieve.
*
* @returns A promise that resolves to the role object.
*/
@Get(':id')
async findOne(
@Param('id') id: string,
@Query(PopulatePipe)
populate: string[],
) {
2024-09-21 11:15:36 +00:00
const doc = this.canPopulate(populate)
2024-09-10 09:50:11 +00:00
? await this.roleService.findOneAndPopulate(id)
: await this.roleService.findOne(id);
if (!doc) {
this.logger.warn(`Unable to find Role by id ${id}`);
throw new NotFoundException(`Role with ID ${id} not found`);
}
return doc;
}
/**
* Creates a new role in the system.
*
* @param role The role data for creating a new role.
*
* @returns A promise that resolves to the newly created role.
*/
@CsrfCheck(true)
@Post()
async create(@Body() role: RoleCreateDto) {
return await this.roleService.create(role);
}
/**
* Updates an existing role by its ID.
*
* @param id The ID of the role to update.
* @param roleUpdate The updated data for the role.
*
* @returns A promise that resolves to the updated role.
*/
@CsrfCheck(true)
@Patch(':id')
async updateOne(@Param('id') id: string, @Body() roleUpdate: RoleUpdateDto) {
const result = await this.roleService.updateOne(id, roleUpdate);
if (!result) {
this.logger.warn(`Unable to update Role by id ${id}`);
throw new NotFoundException(`Role with ID ${id} not found`);
}
return result;
}
/**
* Deletes a role by its ID.
*
* @param id The ID of the role to delete.
*
* @returns A promise that resolves to the result of the deletion.
*/
@CsrfCheck(true)
@Delete(':id')
@HttpCode(204)
2024-09-23 10:17:19 +00:00
async deleteOne(@Param('id') id: string, @Req() req: Request) {
const userRoles = (req.user as User).roles;
2024-09-23 10:17:19 +00:00
const associatedUser = await this.userService.findOne({
roles: { $in: [id] },
});
if (userRoles.includes(id)) {
throw new ForbiddenException("Your account's role can't be deleted");
2024-09-23 10:17:19 +00:00
} else if (associatedUser) {
throw new ForbiddenException('Role is associated with other users');
} else {
2024-09-23 15:58:19 +00:00
const result = await this.roleService.deleteOne(id);
if (result.deletedCount === 0) {
throw new NotFoundException(`Role with ID ${id} not found`);
}
2024-09-23 15:58:19 +00:00
return result;
2024-09-10 09:50:11 +00:00
}
}
}