fix(api): fix get sms and sms-batch by id endpoint bugs

This commit is contained in:
isra el
2025-08-21 07:14:49 +03:00
parent 97dd062477
commit 38dd56df26
2 changed files with 9 additions and 39 deletions

View File

@@ -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 };
}
}

View File

@@ -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<any> {
// 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<any> {
// 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<any> {
// 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<any> {
// 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