import { NotProvidedCredentialsError } from '../../errors'; import { ISMSSender } from '../../interfaces'; import { SendResult } from '../../types'; import { PASSWORD, REGION, USERNAME } from '../../utils/constants'; import { PublishCommand, PublishCommandOutput, SNSClient, } from '@aws-sdk/client-sns'; export default class AwsSMSSenderClient implements ISMSSender { private client: SNSClient; constructor() { const accessKeyId = USERNAME || process.env.AWS_ACCESS_KEY_ID; const secretAccessKey = PASSWORD || process.env.AWS_SECRET_ACCESS_KEY; const region = REGION || process.env.AWS_REGION; if (!(accessKeyId && secretAccessKey)) throw new NotProvidedCredentialsError(); this.client = new SNSClient({ region: region, credentials: { accessKeyId: accessKeyId, secretAccessKey: secretAccessKey, }, }); } public async send( phones: string[] = [], message: string, ): Promise { const result: SendResult = { sent: [], failed: [], }; await Promise.all( phones.map((phone) => this.sendOne(phone, message) .then(() => result.sent.push({ phone })) .catch((error) => result.failed.push({ phone, error: error.message }), ), ), ); return result; } private sendOne = async ( phone: string, message: string, ): Promise => { const command = new PublishCommand({ Message: message, PhoneNumber: phone, }); return await this.client.send(command); }; }