mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-11-26 11:34:53 +00:00
Add Role Module with Role Controller, Service, and DTOs
This commit is contained in:
@ -12,6 +12,7 @@ import { CommunityModule } from './community/community.module';
|
|||||||
import { BuildingModule } from './building/building.module';
|
import { BuildingModule } from './building/building.module';
|
||||||
import { FloorModule } from './floor/floor.module';
|
import { FloorModule } from './floor/floor.module';
|
||||||
import { UnitModule } from './unit/unit.module';
|
import { UnitModule } from './unit/unit.module';
|
||||||
|
import { RoleModule } from './role/role.module';
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
ConfigModule.forRoot({
|
ConfigModule.forRoot({
|
||||||
@ -19,6 +20,7 @@ import { UnitModule } from './unit/unit.module';
|
|||||||
}),
|
}),
|
||||||
AuthenticationModule,
|
AuthenticationModule,
|
||||||
UserModule,
|
UserModule,
|
||||||
|
RoleModule,
|
||||||
CommunityModule,
|
CommunityModule,
|
||||||
BuildingModule,
|
BuildingModule,
|
||||||
FloorModule,
|
FloorModule,
|
||||||
|
|||||||
1
src/role/controllers/index.ts
Normal file
1
src/role/controllers/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './role.controller';
|
||||||
58
src/role/controllers/role.controller.ts
Normal file
58
src/role/controllers/role.controller.ts
Normal 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
1
src/role/dtos/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './role.edit.dto';
|
||||||
13
src/role/dtos/role.edit.dto.ts
Normal file
13
src/role/dtos/role.edit.dto.ts
Normal 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
25
src/role/role.module.ts
Normal 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 {}
|
||||||
1
src/role/services/index.ts
Normal file
1
src/role/services/index.ts
Normal file
@ -0,0 +1 @@
|
|||||||
|
export * from './role.service';
|
||||||
44
src/role/services/role.service.ts
Normal file
44
src/role/services/role.service.ts
Normal 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,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user