Browse Source

feat(api): add delete device endpoint

pull/4/head
isra el 2 years ago
parent
commit
fbfa07c636
  1. 14
      api/src/gateway/gateway.controller.ts
  2. 15
      api/src/gateway/gateway.service.ts

14
api/src/gateway/gateway.controller.ts

@ -7,6 +7,7 @@ import {
UseGuards, UseGuards,
Request, Request,
Get, Get,
Delete,
} from '@nestjs/common' } from '@nestjs/common'
import { ApiBearerAuth, ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger' import { ApiBearerAuth, ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger'
import { AuthGuard } from '../auth/guards/auth.guard' import { AuthGuard } from '../auth/guards/auth.guard'
@ -62,6 +63,19 @@ export class GatewayController {
return { data } 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' }) @ApiOperation({ summary: 'Send SMS to a device' })
@ApiQuery({ @ApiQuery({
name: 'apiKey', name: 'apiKey',

15
api/src/gateway/gateway.service.ts

@ -58,6 +58,21 @@ export class GatewayService {
) )
} }
async deleteDevice(deviceId: string): Promise<any> {
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<any> { async sendSMS(deviceId: string, smsData: SendSMSInputDTO): Promise<any> {
const device = await this.deviceModel.findById(deviceId) const device = await this.deviceModel.findById(deviceId)

Loading…
Cancel
Save