From 38dd56df26657fbe7b25af631daa0cb4a1e2b811 Mon Sep 17 00:00:00 2001 From: isra el Date: Thu, 21 Aug 2025 07:14:49 +0300 Subject: [PATCH] fix(api): fix get sms and sms-batch by id endpoint bugs --- api/src/gateway/gateway.controller.ts | 4 +-- api/src/gateway/gateway.service.ts | 44 +++++---------------------- 2 files changed, 9 insertions(+), 39 deletions(-) diff --git a/api/src/gateway/gateway.controller.ts b/api/src/gateway/gateway.controller.ts index f052519..45aab7d 100644 --- a/api/src/gateway/gateway.controller.ts +++ b/api/src/gateway/gateway.controller.ts @@ -170,7 +170,7 @@ export class GatewayController { @Param('id') deviceId: string, @Param('smsId') smsId: string, ) { - const data = await this.gatewayService.getSMSById(deviceId, smsId); + const data = await this.gatewayService.getSMSById(smsId); return { data }; } @@ -181,7 +181,7 @@ export class GatewayController { @Param('id') deviceId: string, @Param('smsBatchId') smsBatchId: string, ) { - const data = await this.gatewayService.getSmsBatchById(deviceId, smsBatchId); + const data = await this.gatewayService.getSmsBatchById(smsBatchId); return { data }; } } diff --git a/api/src/gateway/gateway.service.ts b/api/src/gateway/gateway.service.ts index 33ec036..73d2bcb 100644 --- a/api/src/gateway/gateway.service.ts +++ b/api/src/gateway/gateway.service.ts @@ -1,7 +1,7 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common' import { InjectModel } from '@nestjs/mongoose' import { Device, DeviceDocument } from './schemas/device.schema' -import { Model } from 'mongoose' +import { Model, Types } from 'mongoose' import * as firebaseAdmin from 'firebase-admin' import { ReceivedSMSDTO, @@ -828,24 +828,9 @@ export class GatewayService { } } - 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, - ); - } + async getSMSById(smsId: string): Promise { - // Find the SMS that belongs to this device - const sms = await this.smsModel.findOne({ - _id: smsId, - device: deviceId - }); + const sms = await this.smsModel.findById(smsId); if (!sms) { throw new HttpException( @@ -860,24 +845,9 @@ export class GatewayService { 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, - ); - } + async getSmsBatchById(smsBatchId: string): Promise { - // Find the SMS batch that belongs to this device - const smsBatch = await this.smsBatchModel.findOne({ - _id: smsBatchId, - device: deviceId - }); + const smsBatch = await this.smsBatchModel.findById(smsBatchId); if (!smsBatch) { throw new HttpException( @@ -891,8 +861,8 @@ export class GatewayService { // Find all SMS messages that belong to this batch const smsMessages = await this.smsModel.find({ - smsBatch: smsBatchId, - device: deviceId + smsBatch: new Types.ObjectId(smsBatchId), + device: smsBatch.device }); // Return both the batch and its SMS messages