mirror of
https://github.com/SyncrowIOT/backend.git
synced 2025-07-16 02:36:19 +00:00
190 lines
5.2 KiB
TypeScript
190 lines
5.2 KiB
TypeScript
import { UnitService } from '../services/unit.service';
|
|
import {
|
|
Body,
|
|
Controller,
|
|
Get,
|
|
HttpException,
|
|
HttpStatus,
|
|
Param,
|
|
Post,
|
|
Put,
|
|
Query,
|
|
UseGuards,
|
|
} from '@nestjs/common';
|
|
import { ApiTags, ApiBearerAuth } from '@nestjs/swagger';
|
|
import {
|
|
AddUnitDto,
|
|
AddUserUnitDto,
|
|
AddUserUnitUsingCodeDto,
|
|
} from '../dtos/add.unit.dto';
|
|
import { GetUnitChildDto } from '../dtos/get.unit.dto';
|
|
import { UpdateUnitNameDto } from '../dtos/update.unit.dto';
|
|
import { CheckFloorTypeGuard } from 'src/guards/floor.type.guard';
|
|
import { CheckUserUnitGuard } from 'src/guards/user.unit.guard';
|
|
import { AdminRoleGuard } from 'src/guards/admin.role.guard';
|
|
import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard';
|
|
import { UnitPermissionGuard } from 'src/guards/unit.permission.guard';
|
|
|
|
@ApiTags('Unit Module')
|
|
@Controller({
|
|
version: '1',
|
|
path: 'unit',
|
|
})
|
|
export class UnitController {
|
|
constructor(private readonly unitService: UnitService) {}
|
|
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard, CheckFloorTypeGuard)
|
|
@Post()
|
|
async addUnit(@Body() addUnitDto: AddUnitDto) {
|
|
try {
|
|
const unit = await this.unitService.addUnit(addUnitDto);
|
|
return {
|
|
statusCode: HttpStatus.CREATED,
|
|
success: true,
|
|
message: 'Unit added successfully',
|
|
data: unit,
|
|
};
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard, UnitPermissionGuard)
|
|
@Get(':unitUuid')
|
|
async getUnitByUuid(@Param('unitUuid') unitUuid: string) {
|
|
try {
|
|
const unit = await this.unitService.getUnitByUuid(unitUuid);
|
|
return unit;
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard, UnitPermissionGuard)
|
|
@Get('child/:unitUuid')
|
|
async getUnitChildByUuid(
|
|
@Param('unitUuid') unitUuid: string,
|
|
@Query() query: GetUnitChildDto,
|
|
) {
|
|
try {
|
|
const unit = await this.unitService.getUnitChildByUuid(unitUuid, query);
|
|
return unit;
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard, UnitPermissionGuard)
|
|
@Get('parent/:unitUuid')
|
|
async getUnitParentByUuid(@Param('unitUuid') unitUuid: string) {
|
|
try {
|
|
const unit = await this.unitService.getUnitParentByUuid(unitUuid);
|
|
return unit;
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
@ApiBearerAuth()
|
|
@UseGuards(AdminRoleGuard, CheckUserUnitGuard)
|
|
@Post('user')
|
|
async addUserUnit(@Body() addUserUnitDto: AddUserUnitDto) {
|
|
try {
|
|
await this.unitService.addUserUnit(addUserUnitDto);
|
|
return {
|
|
statusCode: HttpStatus.CREATED,
|
|
success: true,
|
|
message: 'user unit added successfully',
|
|
};
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard)
|
|
@Get('user/:userUuid')
|
|
async getUnitsByUserId(@Param('userUuid') userUuid: string) {
|
|
try {
|
|
return await this.unitService.getUnitsByUserId(userUuid);
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard, UnitPermissionGuard)
|
|
@Put('rename/:unitUuid')
|
|
async renameUnitByUuid(
|
|
@Param('unitUuid') unitUuid: string,
|
|
@Body() updateUnitNameDto: UpdateUnitNameDto,
|
|
) {
|
|
try {
|
|
const unit = await this.unitService.renameUnitByUuid(
|
|
unitUuid,
|
|
updateUnitNameDto,
|
|
);
|
|
return unit;
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard, UnitPermissionGuard)
|
|
@Get(':unitUuid/invitation-code')
|
|
async getUnitInvitationCode(@Param('unitUuid') unitUuid: string) {
|
|
try {
|
|
const unit = await this.unitService.getUnitInvitationCode(unitUuid);
|
|
return unit;
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
|
|
@ApiBearerAuth()
|
|
@UseGuards(JwtAuthGuard)
|
|
@Post('user/verify-code')
|
|
async verifyCodeAndAddUserUnit(
|
|
@Body() addUserUnitUsingCodeDto: AddUserUnitUsingCodeDto,
|
|
) {
|
|
try {
|
|
await this.unitService.verifyCodeAndAddUserUnit(addUserUnitUsingCodeDto);
|
|
return {
|
|
statusCode: HttpStatus.CREATED,
|
|
success: true,
|
|
message: 'user unit added successfully',
|
|
};
|
|
} catch (error) {
|
|
throw new HttpException(
|
|
error.message || 'Internal server error',
|
|
error.status || HttpStatus.INTERNAL_SERVER_ERROR,
|
|
);
|
|
}
|
|
}
|
|
}
|