Browse Source

chore(api): update sms payload fields

pull/8/head
isra el 2 years ago
parent
commit
cdb1a0d73a
  1. 54
      api/src/gateway/gateway.dto.ts
  2. 19
      api/src/gateway/gateway.service.ts

54
api/src/gateway/gateway.dto.ts

@ -39,16 +39,64 @@ export class SMSData {
@ApiProperty({ @ApiProperty({
type: String, type: String,
required: true, required: true,
description: 'SMS text',
description: 'The message to send',
}) })
smsBody: string
message: string
@ApiProperty({ @ApiProperty({
type: Array, type: Array,
required: true, required: true,
description: 'Array of phone numbers',
description: 'List of phone numbers to send the SMS to',
example: ['+2519xxxxxxxx', '+2517xxxxxxxx'], 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[] receivers: string[]
} }
export class SendSMSInputDTO extends SMSData {} 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
}

19
api/src/gateway/gateway.service.ts

@ -6,10 +6,13 @@ import * as firebaseAdmin from 'firebase-admin'
import { RegisterDeviceInputDTO, SendSMSInputDTO } from './gateway.dto' import { RegisterDeviceInputDTO, SendSMSInputDTO } from './gateway.dto'
import { User } from '../users/schemas/user.schema' import { User } from '../users/schemas/user.schema'
import { AuthService } from 'src/auth/auth.service' import { AuthService } from 'src/auth/auth.service'
import { SMS } from './schemas/sms.schema'
@Injectable() @Injectable()
export class GatewayService { export class GatewayService {
constructor( constructor(
@InjectModel(Device.name) private deviceModel: Model<DeviceDocument>, @InjectModel(Device.name) private deviceModel: Model<DeviceDocument>,
@InjectModel(SMS.name) private smsModel: Model<SMS>,
private authService: AuthService, private authService: AuthService,
) {} ) {}
@ -76,6 +79,14 @@ export class GatewayService {
} }
async sendSMS(deviceId: string, smsData: SendSMSInputDTO): Promise<any> { async sendSMS(deviceId: string, smsData: SendSMSInputDTO): Promise<any> {
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) const device = await this.deviceModel.findById(deviceId)
if (!device?.enabled) { if (!device?.enabled) {
@ -88,11 +99,15 @@ export class GatewayService {
) )
} }
const stringifiedSMSData = JSON.stringify(updatedSMSData)
const payload: any = { const payload: any = {
data: { data: {
smsData: JSON.stringify(smsData),
smsData: stringifiedSMSData,
}, },
} }
// TODO: Save SMS and Implement a queue to send the SMS if recipients are too many
try { try {
const response = await firebaseAdmin const response = await firebaseAdmin
.messaging() .messaging()
@ -100,7 +115,7 @@ export class GatewayService {
this.deviceModel this.deviceModel
.findByIdAndUpdate(deviceId, { .findByIdAndUpdate(deviceId, {
$inc: { sentSMSCount: smsData.receivers.length },
$inc: { sentSMSCount: updatedSMSData.recipients.length },
}) })
.exec() .exec()
.catch((e) => { .catch((e) => {

Loading…
Cancel
Save