From 7694f3f60a8d809f6126e41e4d54ec4adc6e782a Mon Sep 17 00:00:00 2001 From: isra el Date: Tue, 3 Jun 2025 05:19:47 +0300 Subject: [PATCH] feat(api): create endpoints for getting a specific sms and sms batch by id --- api/src/gateway/gateway.controller.ts | 23 +++++++++ api/src/gateway/gateway.service.ts | 74 +++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/api/src/gateway/gateway.controller.ts b/api/src/gateway/gateway.controller.ts index 3471244..f052519 100644 --- a/api/src/gateway/gateway.controller.ts +++ b/api/src/gateway/gateway.controller.ts @@ -153,6 +153,7 @@ export class GatewayController { @ApiOperation({ summary: 'Update SMS status' }) @UseGuards(AuthGuard, CanModifyDevice) + @HttpCode(HttpStatus.OK) @Patch('/devices/:id/sms-status') async updateSMSStatus( @Param('id') deviceId: string, @@ -161,4 +162,26 @@ export class GatewayController { const data = await this.gatewayService.updateSMSStatus(deviceId, dto); return { data }; } + + @ApiOperation({ summary: 'Get a single SMS by ID' }) + @UseGuards(AuthGuard, CanModifyDevice) + @Get('/devices/:id/sms/:smsId') + async getSMSById( + @Param('id') deviceId: string, + @Param('smsId') smsId: string, + ) { + const data = await this.gatewayService.getSMSById(deviceId, smsId); + return { data }; + } + + @ApiOperation({ summary: 'Get an SMS batch by ID with all its SMS messages' }) + @UseGuards(AuthGuard, CanModifyDevice) + @Get('/devices/:id/sms-batch/:smsBatchId') + async getSmsBatchById( + @Param('id') deviceId: string, + @Param('smsBatchId') smsBatchId: string, + ) { + const data = await this.gatewayService.getSmsBatchById(deviceId, smsBatchId); + return { data }; + } } diff --git a/api/src/gateway/gateway.service.ts b/api/src/gateway/gateway.service.ts index 9e1550b..c73b4aa 100644 --- a/api/src/gateway/gateway.service.ts +++ b/api/src/gateway/gateway.service.ts @@ -821,4 +821,78 @@ export class GatewayService { } others` } } + + async getSMSById(deviceId: string, smsId: string): Promise { + // Check if device exists and is enabled + const device = await this.deviceModel.findById(deviceId); + if (!device) { + throw new HttpException( + { + success: false, + error: 'Device not found', + }, + HttpStatus.NOT_FOUND, + ); + } + + // Find the SMS that belongs to this device + const sms = await this.smsModel.findOne({ + _id: smsId, + device: deviceId + }); + + if (!sms) { + throw new HttpException( + { + success: false, + error: 'SMS not found', + }, + HttpStatus.NOT_FOUND, + ); + } + + return sms; + } + + async getSmsBatchById(deviceId: string, smsBatchId: string): Promise { + // Check if device exists + const device = await this.deviceModel.findById(deviceId); + if (!device) { + throw new HttpException( + { + success: false, + error: 'Device not found', + }, + HttpStatus.NOT_FOUND, + ); + } + + // Find the SMS batch that belongs to this device + const smsBatch = await this.smsBatchModel.findOne({ + _id: smsBatchId, + device: deviceId + }); + + if (!smsBatch) { + throw new HttpException( + { + success: false, + error: 'SMS batch not found', + }, + HttpStatus.NOT_FOUND, + ); + } + + // Find all SMS messages that belong to this batch + const smsMessages = await this.smsModel.find({ + smsBatch: smsBatchId, + device: deviceId + }); + + // Return both the batch and its SMS messages + return { + batch: smsBatch, + messages: smsMessages + }; + } }