import { NotProvidedCredentialsError } from '../../errors'; import { ISMSSender } from '../../interfaces'; import { SendResult } from '../../types'; import { USERNAME, PASSWORD, FROM } from '../../utils/constants'; import { SmsClient } from '@azure/communication-sms'; export default class AzureSMSSenderClient implements ISMSSender { client: SmsClient; constructor() { if ( !(USERNAME && PASSWORD && FROM) && !process.env.COMMUNICATION_SERVICES_CONNECTION_STRING ) throw new NotProvidedCredentialsError(); const connectionString = process.env.COMMUNICATION_SERVICES_CONNECTION_STRING ?? `endpoint=https://${USERNAME}.communication.azure.com/;accesskey=${PASSWORD}`; this.client = new SmsClient(connectionString); } public async send(phones: string[], message: string): Promise { const result: SendResult = { sent: [], failed: [], }; const smsResults = await this.client.send({ from: FROM as string, to: phones, message: message, }); smsResults.forEach((smsResult) => { if (smsResult.errorMessage) result.failed.push({ phone: smsResult.to, error: smsResult.errorMessage, }); else result.sent.push({ phone: smsResult.to }); }); return result; } }