Browse Source

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

pull/121/head
isra el 7 months ago
parent
commit
38dd56df26
  1. 4
      api/src/gateway/gateway.controller.ts
  2. 44
      api/src/gateway/gateway.service.ts

4
api/src/gateway/gateway.controller.ts

@ -170,7 +170,7 @@ export class GatewayController {
@Param('id') deviceId: string, @Param('id') deviceId: string,
@Param('smsId') smsId: string, @Param('smsId') smsId: string,
) { ) {
const data = await this.gatewayService.getSMSById(deviceId, smsId);
const data = await this.gatewayService.getSMSById(smsId);
return { data }; return { data };
} }
@ -181,7 +181,7 @@ export class GatewayController {
@Param('id') deviceId: string, @Param('id') deviceId: string,
@Param('smsBatchId') smsBatchId: string, @Param('smsBatchId') smsBatchId: string,
) { ) {
const data = await this.gatewayService.getSmsBatchById(deviceId, smsBatchId);
const data = await this.gatewayService.getSmsBatchById(smsBatchId);
return { data }; return { data };
} }
} }

44
api/src/gateway/gateway.service.ts

@ -1,7 +1,7 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common' import { HttpException, HttpStatus, Injectable } from '@nestjs/common'
import { InjectModel } from '@nestjs/mongoose' import { InjectModel } from '@nestjs/mongoose'
import { Device, DeviceDocument } from './schemas/device.schema' import { Device, DeviceDocument } from './schemas/device.schema'
import { Model } from 'mongoose'
import { Model, Types } from 'mongoose'
import * as firebaseAdmin from 'firebase-admin' import * as firebaseAdmin from 'firebase-admin'
import { import {
ReceivedSMSDTO, 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) { if (!sms) {
throw new HttpException( throw new HttpException(
@ -860,24 +845,9 @@ export class GatewayService {
return sms; 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) { if (!smsBatch) {
throw new HttpException( throw new HttpException(
@ -891,8 +861,8 @@ export class GatewayService {
// Find all SMS messages that belong to this batch // Find all SMS messages that belong to this batch
const smsMessages = await this.smsModel.find({ 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 // Return both the batch and its SMS messages

Loading…
Cancel
Save