Browse Source

feat(api): create endpoints for getting a specific sms and sms batch by id

pull/80/head
isra el 9 months ago
parent
commit
7694f3f60a
  1. 23
      api/src/gateway/gateway.controller.ts
  2. 74
      api/src/gateway/gateway.service.ts

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

74
api/src/gateway/gateway.service.ts

@ -821,4 +821,78 @@ export class GatewayService {
} others`
}
}
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,
);
}
// 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<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,
);
}
// 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
};
}
}
Loading…
Cancel
Save