diff --git a/api/src/gateway/gateway.controller.ts b/api/src/gateway/gateway.controller.ts index 8ebd198..b5d92f0 100644 --- a/api/src/gateway/gateway.controller.ts +++ b/api/src/gateway/gateway.controller.ts @@ -7,6 +7,7 @@ import { UseGuards, Request, Get, + Delete, } from '@nestjs/common' import { ApiBearerAuth, ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger' import { AuthGuard } from '../auth/guards/auth.guard' @@ -62,6 +63,19 @@ export class GatewayController { return { data } } + @ApiOperation({ summary: 'Delete device' }) + @ApiQuery({ + name: 'apiKey', + required: false, + description: 'Required if jwt bearer token not provided', + }) + @UseGuards(AuthGuard, CanModifyDevice) + @Delete('/devices/:id') + async deleteDevice(@Param('id') deviceId: string) { + const data = await this.gatewayService.deleteDevice(deviceId) + return { data } + } + @ApiOperation({ summary: 'Send SMS to a device' }) @ApiQuery({ name: 'apiKey', diff --git a/api/src/gateway/gateway.service.ts b/api/src/gateway/gateway.service.ts index 6771de3..9f33cd2 100644 --- a/api/src/gateway/gateway.service.ts +++ b/api/src/gateway/gateway.service.ts @@ -58,6 +58,21 @@ export class GatewayService { ) } + async deleteDevice(deviceId: string): Promise { + const device = await this.deviceModel.findById(deviceId) + + if (!device) { + throw new HttpException( + { + error: 'Device not found', + }, + HttpStatus.NOT_FOUND, + ) + } + + return await this.deviceModel.findByIdAndDelete(deviceId) + } + async sendSMS(deviceId: string, smsData: SendSMSInputDTO): Promise { const device = await this.deviceModel.findById(deviceId)