From 09074f83e2e0465083addb1d7ed59c67f5963c89 Mon Sep 17 00:00:00 2001 From: faris Aljohari <83524184+farisaljohari@users.noreply.github.com> Date: Fri, 28 Jun 2024 20:47:08 +0300 Subject: [PATCH] Add Door Lock Controller with temporary password functionality --- .../controllers/door.lock.controller.ts | 182 ++++++++++++++++++ src/door-lock/controllers/index.ts | 1 + 2 files changed, 183 insertions(+) create mode 100644 src/door-lock/controllers/door.lock.controller.ts create mode 100644 src/door-lock/controllers/index.ts diff --git a/src/door-lock/controllers/door.lock.controller.ts b/src/door-lock/controllers/door.lock.controller.ts new file mode 100644 index 0000000..f3605ef --- /dev/null +++ b/src/door-lock/controllers/door.lock.controller.ts @@ -0,0 +1,182 @@ +import { DoorLockService } from '../services/door.lock.service'; +import { + Body, + Controller, + Post, + Param, + HttpException, + HttpStatus, + Get, + Delete, + UseGuards, +} from '@nestjs/common'; +import { ApiTags, ApiBearerAuth } from '@nestjs/swagger'; +import { AddDoorLockOnlineDto } from '../dtos/add.online-temp.dto'; +import { AddDoorLockOfflineTempDto } from '../dtos/add.offline-temp.dto'; +import { JwtAuthGuard } from '@app/common/guards/jwt.auth.guard'; + +@ApiTags('Door Lock Module') +@Controller({ + version: '1', + path: 'door-lock', +}) +export class DoorLockController { + constructor(private readonly doorLockService: DoorLockService) {} + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Post('temporary-password/online/:doorLockUuid') + async addOnlineTemporaryPassword( + @Body() addDoorLockDto: AddDoorLockOnlineDto, + @Param('doorLockUuid') doorLockUuid: string, + ) { + try { + const temporaryPassword = + await this.doorLockService.addOnlineTemporaryPassword( + addDoorLockDto, + doorLockUuid, + ); + + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'online temporary password added successfully', + data: { + id: temporaryPassword.id, + }, + }; + } catch (error) { + throw new HttpException( + error.message || 'Internal server error', + error.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Post('temporary-password/offline/one-time/:doorLockUuid') + async addOfflineOneTimeTemporaryPassword( + @Body() addDoorLockOfflineTempDto: AddDoorLockOfflineTempDto, + @Param('doorLockUuid') doorLockUuid: string, + ) { + try { + const temporaryPassword = + await this.doorLockService.addOfflineOneTimeTemporaryPassword( + addDoorLockOfflineTempDto, + doorLockUuid, + ); + + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'offline temporary password added successfully', + data: temporaryPassword, + }; + } catch (error) { + throw new HttpException( + error.message || 'Internal server error', + error.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Post('temporary-password/offline/multiple-time/:doorLockUuid') + async addOfflineMultipleTimeTemporaryPassword( + @Body() addDoorLockOfflineTempDto: AddDoorLockOfflineTempDto, + @Param('doorLockUuid') doorLockUuid: string, + ) { + try { + const temporaryPassword = + await this.doorLockService.addOfflineMultipleTimeTemporaryPassword( + addDoorLockOfflineTempDto, + doorLockUuid, + ); + + return { + statusCode: HttpStatus.CREATED, + success: true, + message: 'offline temporary password added successfully', + data: temporaryPassword, + }; + } catch (error) { + throw new HttpException( + error.message || 'Internal server error', + error.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Get('temporary-password/online/:doorLockUuid') + async getOnlineTemporaryPasswords( + @Param('doorLockUuid') doorLockUuid: string, + ) { + try { + return await this.doorLockService.getOnlineTemporaryPasswords( + doorLockUuid, + ); + } catch (error) { + throw new HttpException( + error.message || 'Internal server error', + error.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Get('temporary-password/offline/one-time/:doorLockUuid') + async getOfflineOneTimeTemporaryPasswords( + @Param('doorLockUuid') doorLockUuid: string, + ) { + try { + return await this.doorLockService.getOfflineOneTimeTemporaryPasswords( + doorLockUuid, + ); + } catch (error) { + throw new HttpException( + error.message || 'Internal server error', + error.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Get('temporary-password/offline/multiple-time/:doorLockUuid') + async getOfflineMultipleTimeTemporaryPasswords( + @Param('doorLockUuid') doorLockUuid: string, + ) { + try { + return await this.doorLockService.getOfflineMultipleTimeTemporaryPasswords( + doorLockUuid, + ); + } catch (error) { + throw new HttpException( + error.message || 'Internal server error', + error.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } + @ApiBearerAuth() + @UseGuards(JwtAuthGuard) + @Delete('temporary-password/:doorLockUuid/:passwordId') + async deleteDoorLockPassword( + @Param('doorLockUuid') doorLockUuid: string, + @Param('passwordId') passwordId: string, + ) { + try { + await this.doorLockService.deleteDoorLockPassword( + doorLockUuid, + passwordId, + ); + return { + statusCode: HttpStatus.OK, + message: 'Temporary Password deleted Successfully', + }; + } catch (error) { + throw new HttpException( + error.message || 'Internal server error', + error.status || HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + } +} diff --git a/src/door-lock/controllers/index.ts b/src/door-lock/controllers/index.ts new file mode 100644 index 0000000..d7d3c84 --- /dev/null +++ b/src/door-lock/controllers/index.ts @@ -0,0 +1 @@ +export * from './door.lock.controller';