Add Role Module with Role Controller, Service, and DTOs

This commit is contained in:
faris Aljohari
2024-05-06 23:49:09 +03:00
parent 1dcb0a3052
commit a093fd3f72
8 changed files with 145 additions and 0 deletions

View File

@ -12,6 +12,7 @@ import { CommunityModule } from './community/community.module';
import { BuildingModule } from './building/building.module';
import { FloorModule } from './floor/floor.module';
import { UnitModule } from './unit/unit.module';
import { RoleModule } from './role/role.module';
@Module({
imports: [
ConfigModule.forRoot({
@ -19,6 +20,7 @@ import { UnitModule } from './unit/unit.module';
}),
AuthenticationModule,
UserModule,
RoleModule,
CommunityModule,
BuildingModule,
FloorModule,

View File

@ -0,0 +1 @@
export * from './role.controller';

View File

@ -0,0 +1,58 @@
import {
Body,
Controller,
Get,
HttpException,
HttpStatus,
Param,
Put,
UseGuards,
} from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { RoleService } from '../services/role.service';
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
import { UserRoleEditDto } from '../dtos';
@ApiTags('Role Module')
@Controller({
version: '1',
path: 'role',
})
export class RoleController {
constructor(private readonly roleService: RoleService) {}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Get('types')
async fetchRoleTypes() {
try {
const roleTypes = await this.roleService.fetchRoleTypes();
return {
statusCode: HttpStatus.OK,
message: 'Role Types fetched Successfully',
data: roleTypes,
};
} catch (err) {
throw new Error(err);
}
}
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Put('edit/user/:userUuid')
async editUserRoleType(
@Param('userUuid') userUuid: string,
@Body() userRoleEditDto: UserRoleEditDto,
) {
try {
await this.roleService.editUserRoleType(userUuid, userRoleEditDto);
return {
statusCode: HttpStatus.OK,
message: 'User Role Updated Successfully',
};
} catch (error) {
throw new HttpException(
error.message || 'Internal server error',
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
}

1
src/role/dtos/index.ts Normal file
View File

@ -0,0 +1 @@
export * from './role.edit.dto';

View File

@ -0,0 +1,13 @@
import { RoleType } from '@app/common/constants/role.type.enum';
import { ApiProperty } from '@nestjs/swagger';
import { IsEnum } from 'class-validator';
export class UserRoleEditDto {
@ApiProperty({
description: 'role type',
enum: RoleType,
required: true,
})
@IsEnum(RoleType)
roleType: RoleType;
}

25
src/role/role.module.ts Normal file
View File

@ -0,0 +1,25 @@
import { DeviceRepositoryModule } from '@app/common/modules/device';
import { DeviceRepository } from '@app/common/modules/device/repositories';
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { RoleService } from './services/role.service';
import { RoleController } from './controllers/role.controller';
import { DeviceUserPermissionRepository } from '@app/common/modules/device-user-permission/repositories';
import { PermissionTypeRepository } from '@app/common/modules/permission/repositories';
import { RoleTypeRepository } from '@app/common/modules/role-type/repositories';
import { UserRoleRepository } from '@app/common/modules/user-role/repositories';
@Module({
imports: [ConfigModule, DeviceRepositoryModule],
controllers: [RoleController],
providers: [
DeviceUserPermissionRepository,
PermissionTypeRepository,
DeviceRepository,
RoleService,
RoleTypeRepository,
UserRoleRepository,
],
exports: [RoleService],
})
export class RoleModule {}

View File

@ -0,0 +1 @@
export * from './role.service';

View File

@ -0,0 +1,44 @@
import { RoleTypeRepository } from './../../../libs/common/src/modules/role-type/repositories/role.type.repository';
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { UserRoleEditDto } from '../dtos/role.edit.dto';
import { UserRoleRepository } from '@app/common/modules/user-role/repositories';
@Injectable()
export class RoleService {
constructor(
private readonly roleTypeRepository: RoleTypeRepository,
private readonly userRoleRepository: UserRoleRepository,
) {}
async editUserRoleType(userUuid: string, userRoleEditDto: UserRoleEditDto) {
try {
const roleType = await this.fetchRoleByType(userRoleEditDto.roleType);
return await this.userRoleRepository.update(
{ user: { uuid: userUuid } },
{
roleType: {
uuid: roleType.uuid,
},
},
);
} catch (error) {
throw new HttpException(
error.message || 'Internal Server Error',
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
async fetchRoleTypes() {
const roleTypes = await this.roleTypeRepository.find();
return roleTypes;
}
private async fetchRoleByType(roleType: string) {
return await this.roleTypeRepository.findOne({
where: {
type: roleType,
},
});
}
}