diff --git a/src/guards/user.building.guard.ts b/src/guards/user.building.guard.ts new file mode 100644 index 0000000..b47124d --- /dev/null +++ b/src/guards/user.building.guard.ts @@ -0,0 +1,70 @@ +import { + CanActivate, + ExecutionContext, + Injectable, + HttpStatus, +} from '@nestjs/common'; +import { SpaceRepository } from '@app/common/modules/space/repositories'; +import { BadRequestException, NotFoundException } from '@nestjs/common'; +import { UserRepository } from '@app/common/modules/user/repositories'; + +@Injectable() +export class CheckUserBuildingGuard implements CanActivate { + constructor( + private readonly spaceRepository: SpaceRepository, + private readonly userRepository: UserRepository, + ) {} + + async canActivate(context: ExecutionContext): Promise { + const req = context.switchToHttp().getRequest(); + + try { + const { userUuid, buildingUuid } = req.body; + + await this.checkUserIsFound(userUuid); + + await this.checkBuildingIsFound(buildingUuid); + + return true; + } catch (error) { + this.handleGuardError(error, context); + return false; + } + } + + private async checkUserIsFound(userUuid: string) { + const userData = await this.userRepository.findOne({ + where: { uuid: userUuid }, + }); + if (!userData) { + throw new NotFoundException('User not found'); + } + } + + private async checkBuildingIsFound(spaceUuid: string) { + const spaceData = await this.spaceRepository.findOne({ + where: { uuid: spaceUuid, spaceType: { type: 'building' } }, + relations: ['spaceType'], + }); + if (!spaceData) { + throw new NotFoundException('Building not found'); + } + } + + private handleGuardError(error: Error, context: ExecutionContext) { + const response = context.switchToHttp().getResponse(); + if ( + error instanceof BadRequestException || + error instanceof NotFoundException + ) { + response + .status(HttpStatus.NOT_FOUND) + .json({ statusCode: HttpStatus.NOT_FOUND, message: error.message }); + } else { + response.status(HttpStatus.BAD_REQUEST).json({ + statusCode: HttpStatus.BAD_REQUEST, + message: 'invalid userUuid or buildingUuid', + }); + } + } +} diff --git a/src/guards/user.community.guard.ts b/src/guards/user.community.guard.ts new file mode 100644 index 0000000..04e08b4 --- /dev/null +++ b/src/guards/user.community.guard.ts @@ -0,0 +1,70 @@ +import { + CanActivate, + ExecutionContext, + Injectable, + HttpStatus, +} from '@nestjs/common'; +import { SpaceRepository } from '@app/common/modules/space/repositories'; +import { BadRequestException, NotFoundException } from '@nestjs/common'; +import { UserRepository } from '@app/common/modules/user/repositories'; + +@Injectable() +export class CheckUserCommunityGuard implements CanActivate { + constructor( + private readonly spaceRepository: SpaceRepository, + private readonly userRepository: UserRepository, + ) {} + + async canActivate(context: ExecutionContext): Promise { + const req = context.switchToHttp().getRequest(); + + try { + const { userUuid, communityUuid } = req.body; + + await this.checkUserIsFound(userUuid); + + await this.checkCommunityIsFound(communityUuid); + + return true; + } catch (error) { + this.handleGuardError(error, context); + return false; + } + } + + private async checkUserIsFound(userUuid: string) { + const userData = await this.userRepository.findOne({ + where: { uuid: userUuid }, + }); + if (!userData) { + throw new NotFoundException('User not found'); + } + } + + private async checkCommunityIsFound(spaceUuid: string) { + const spaceData = await this.spaceRepository.findOne({ + where: { uuid: spaceUuid, spaceType: { type: 'community' } }, + relations: ['spaceType'], + }); + if (!spaceData) { + throw new NotFoundException('Community not found'); + } + } + + private handleGuardError(error: Error, context: ExecutionContext) { + const response = context.switchToHttp().getResponse(); + if ( + error instanceof BadRequestException || + error instanceof NotFoundException + ) { + response + .status(HttpStatus.NOT_FOUND) + .json({ statusCode: HttpStatus.NOT_FOUND, message: error.message }); + } else { + response.status(HttpStatus.BAD_REQUEST).json({ + statusCode: HttpStatus.BAD_REQUEST, + message: 'invalid userUuid or communityUuid', + }); + } + } +} diff --git a/src/guards/user.floor.guard.ts b/src/guards/user.floor.guard.ts new file mode 100644 index 0000000..9235fb8 --- /dev/null +++ b/src/guards/user.floor.guard.ts @@ -0,0 +1,70 @@ +import { + CanActivate, + ExecutionContext, + Injectable, + HttpStatus, +} from '@nestjs/common'; +import { SpaceRepository } from '@app/common/modules/space/repositories'; +import { BadRequestException, NotFoundException } from '@nestjs/common'; +import { UserRepository } from '@app/common/modules/user/repositories'; + +@Injectable() +export class CheckUserFloorGuard implements CanActivate { + constructor( + private readonly spaceRepository: SpaceRepository, + private readonly userRepository: UserRepository, + ) {} + + async canActivate(context: ExecutionContext): Promise { + const req = context.switchToHttp().getRequest(); + + try { + const { userUuid, floorUuid } = req.body; + + await this.checkUserIsFound(userUuid); + + await this.checkFloorIsFound(floorUuid); + + return true; + } catch (error) { + this.handleGuardError(error, context); + return false; + } + } + + private async checkUserIsFound(userUuid: string) { + const userData = await this.userRepository.findOne({ + where: { uuid: userUuid }, + }); + if (!userData) { + throw new NotFoundException('User not found'); + } + } + + private async checkFloorIsFound(spaceUuid: string) { + const spaceData = await this.spaceRepository.findOne({ + where: { uuid: spaceUuid, spaceType: { type: 'floor' } }, + relations: ['spaceType'], + }); + if (!spaceData) { + throw new NotFoundException('Floor not found'); + } + } + + private handleGuardError(error: Error, context: ExecutionContext) { + const response = context.switchToHttp().getResponse(); + if ( + error instanceof BadRequestException || + error instanceof NotFoundException + ) { + response + .status(HttpStatus.NOT_FOUND) + .json({ statusCode: HttpStatus.NOT_FOUND, message: error.message }); + } else { + response.status(HttpStatus.BAD_REQUEST).json({ + statusCode: HttpStatus.BAD_REQUEST, + message: 'invalid userUuid or floorUuid', + }); + } + } +} diff --git a/src/guards/user.room.guard.ts b/src/guards/user.room.guard.ts new file mode 100644 index 0000000..0ecf817 --- /dev/null +++ b/src/guards/user.room.guard.ts @@ -0,0 +1,70 @@ +import { + CanActivate, + ExecutionContext, + Injectable, + HttpStatus, +} from '@nestjs/common'; +import { SpaceRepository } from '@app/common/modules/space/repositories'; +import { BadRequestException, NotFoundException } from '@nestjs/common'; +import { UserRepository } from '@app/common/modules/user/repositories'; + +@Injectable() +export class CheckUserRoomGuard implements CanActivate { + constructor( + private readonly spaceRepository: SpaceRepository, + private readonly userRepository: UserRepository, + ) {} + + async canActivate(context: ExecutionContext): Promise { + const req = context.switchToHttp().getRequest(); + + try { + const { userUuid, roomUuid } = req.body; + + await this.checkUserIsFound(userUuid); + + await this.checkRoomIsFound(roomUuid); + + return true; + } catch (error) { + this.handleGuardError(error, context); + return false; + } + } + + private async checkUserIsFound(userUuid: string) { + const userData = await this.userRepository.findOne({ + where: { uuid: userUuid }, + }); + if (!userData) { + throw new NotFoundException('User not found'); + } + } + + private async checkRoomIsFound(spaceUuid: string) { + const spaceData = await this.spaceRepository.findOne({ + where: { uuid: spaceUuid, spaceType: { type: 'room' } }, + relations: ['spaceType'], + }); + if (!spaceData) { + throw new NotFoundException('Room not found'); + } + } + + private handleGuardError(error: Error, context: ExecutionContext) { + const response = context.switchToHttp().getResponse(); + if ( + error instanceof BadRequestException || + error instanceof NotFoundException + ) { + response + .status(HttpStatus.NOT_FOUND) + .json({ statusCode: HttpStatus.NOT_FOUND, message: error.message }); + } else { + response.status(HttpStatus.BAD_REQUEST).json({ + statusCode: HttpStatus.BAD_REQUEST, + message: 'invalid userUuid or roomUuid', + }); + } + } +} diff --git a/src/guards/user.unit.guard.ts b/src/guards/user.unit.guard.ts new file mode 100644 index 0000000..4d7b1ab --- /dev/null +++ b/src/guards/user.unit.guard.ts @@ -0,0 +1,70 @@ +import { + CanActivate, + ExecutionContext, + Injectable, + HttpStatus, +} from '@nestjs/common'; +import { SpaceRepository } from '@app/common/modules/space/repositories'; +import { BadRequestException, NotFoundException } from '@nestjs/common'; +import { UserRepository } from '@app/common/modules/user/repositories'; + +@Injectable() +export class CheckUserUnitGuard implements CanActivate { + constructor( + private readonly spaceRepository: SpaceRepository, + private readonly userRepository: UserRepository, + ) {} + + async canActivate(context: ExecutionContext): Promise { + const req = context.switchToHttp().getRequest(); + + try { + const { userUuid, unitUuid } = req.body; + + await this.checkUserIsFound(userUuid); + + await this.checkUnitIsFound(unitUuid); + + return true; + } catch (error) { + this.handleGuardError(error, context); + return false; + } + } + + private async checkUserIsFound(userUuid: string) { + const userData = await this.userRepository.findOne({ + where: { uuid: userUuid }, + }); + if (!userData) { + throw new NotFoundException('User not found'); + } + } + + private async checkUnitIsFound(spaceUuid: string) { + const spaceData = await this.spaceRepository.findOne({ + where: { uuid: spaceUuid, spaceType: { type: 'unit' } }, + relations: ['spaceType'], + }); + if (!spaceData) { + throw new NotFoundException('Unit not found'); + } + } + + private handleGuardError(error: Error, context: ExecutionContext) { + const response = context.switchToHttp().getResponse(); + if ( + error instanceof BadRequestException || + error instanceof NotFoundException + ) { + response + .status(HttpStatus.NOT_FOUND) + .json({ statusCode: HttpStatus.NOT_FOUND, message: error.message }); + } else { + response.status(HttpStatus.BAD_REQUEST).json({ + statusCode: HttpStatus.BAD_REQUEST, + message: 'invalid userUuid or unitUuid', + }); + } + } +}