diff --git a/api/src/gateway/gateway.dto.ts b/api/src/gateway/gateway.dto.ts index 07baf66..cb405b0 100644 --- a/api/src/gateway/gateway.dto.ts +++ b/api/src/gateway/gateway.dto.ts @@ -39,16 +39,64 @@ export class SMSData { @ApiProperty({ type: String, required: true, - description: 'SMS text', + description: 'The message to send', }) - smsBody: string + message: string @ApiProperty({ type: Array, required: true, - description: 'Array of phone numbers', + description: 'List of phone numbers to send the SMS to', example: ['+2519xxxxxxxx', '+2517xxxxxxxx'], }) + recipients: string[] + + // TODO: restructure the Payload such that it contains bactchId, smsId, recipients and message in an optimized way + // message: string + // bactchId: string + // list: { + // smsId: string + // recipient: string + // } + + // Legacy fields to be removed in the future + // @ApiProperty({ + // type: String, + // required: true, + // description: '(Legacy) Will be Replace with `message` field in the future', + // }) + smsBody: string + + // @ApiProperty({ + // type: Array, + // required: false, + // description: + // '(Legacy) Will be Replace with `recipients` field in the future', + // example: ['+2519xxxxxxxx', '+2517xxxxxxxx'], + // }) receivers: string[] } export class SendSMSInputDTO extends SMSData {} + +export class ReceivedSMSDTO { + @ApiProperty({ + type: String, + required: true, + description: 'The message received', + }) + message: string + + @ApiProperty({ + type: String, + required: true, + description: 'The phone number of the sender', + }) + sender: string + + @ApiProperty({ + type: Date, + required: true, + description: 'The time the message was received', + }) + receivedAt: Date +} diff --git a/api/src/gateway/gateway.service.ts b/api/src/gateway/gateway.service.ts index 5080743..f995008 100644 --- a/api/src/gateway/gateway.service.ts +++ b/api/src/gateway/gateway.service.ts @@ -6,10 +6,13 @@ import * as firebaseAdmin from 'firebase-admin' import { RegisterDeviceInputDTO, SendSMSInputDTO } from './gateway.dto' import { User } from '../users/schemas/user.schema' import { AuthService } from 'src/auth/auth.service' +import { SMS } from './schemas/sms.schema' + @Injectable() export class GatewayService { constructor( @InjectModel(Device.name) private deviceModel: Model, + @InjectModel(SMS.name) private smsModel: Model, private authService: AuthService, ) {} @@ -76,6 +79,14 @@ export class GatewayService { } async sendSMS(deviceId: string, smsData: SendSMSInputDTO): Promise { + const updatedSMSData = { + message: smsData.message || smsData.smsBody, + recipients: smsData.recipients || smsData.receivers, + + // Legacy fields to be removed in the future + smsBody: smsData.message || smsData.smsBody, + receivers: smsData.recipients || smsData.receivers, + } const device = await this.deviceModel.findById(deviceId) if (!device?.enabled) { @@ -88,11 +99,15 @@ export class GatewayService { ) } + const stringifiedSMSData = JSON.stringify(updatedSMSData) const payload: any = { data: { - smsData: JSON.stringify(smsData), + smsData: stringifiedSMSData, }, } + + // TODO: Save SMS and Implement a queue to send the SMS if recipients are too many + try { const response = await firebaseAdmin .messaging() @@ -100,7 +115,7 @@ export class GatewayService { this.deviceModel .findByIdAndUpdate(deviceId, { - $inc: { sentSMSCount: smsData.receivers.length }, + $inc: { sentSMSCount: updatedSMSData.recipients.length }, }) .exec() .catch((e) => {