Skip to content

Commit

Permalink
feat: Add sendMultipleSms (#484)
Browse files Browse the repository at this point in the history
feat: Add TS logger type
  • Loading branch information
yorch authored Mar 31, 2022
1 parent 49722b2 commit 782af1a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 30 deletions.
63 changes: 33 additions & 30 deletions src/SmsSender.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import twilio from 'twilio';
import Twilio from 'twilio/lib/rest/Twilio';
import { GenericLogger, Message } from './types';

export class SmsSender {
private accountSid: string;
private client: Twilio;
private fromNumber: string;
private logger: any; // TODO: Give appropriate type
private logger: GenericLogger;
private sid: string;
private secret: string;

Expand All @@ -18,7 +19,7 @@ export class SmsSender {
}: {
accountSid: string;
fromNumber: string;
logger: any;
logger: GenericLogger;
sid: string;
secret: string;
}) {
Expand All @@ -36,13 +37,7 @@ export class SmsSender {
});
}

public async sendSms({
body,
recipients
}: {
body: string;
recipients: string[];
}) {
public async sendSms({ body, recipients }: Message) {
if (!body || body.length === 0) {
throw new Error('No body to send SMS');
}
Expand All @@ -51,40 +46,48 @@ export class SmsSender {
throw new Error('No recipients to send SMS');
}

const sendSmsToNumber = (phoneNumber?: string) => {
const sendSmsToNumber = async (phoneNumber?: string) => {
const to = phoneNumber.trim();

if (!to) {
this.logger.error('Not a valid phone number to send SMS');
return;
}

this.logger.info(`Trying to send SMS to number ${phoneNumber}`);
return this.client.messages
.create({

try {
const message = await this.client.messages.create({
body,
to,
from: this.fromNumber
})
.then((message) => {
const { errorCode, errorMessage, status } = message;
if (errorCode) {
this.logger.error(
`There was an error sending SMS to number ${phoneNumber} (${errorCode} - ${errorMessage})`
);
} else {
this.logger.info(
`Sent SMS to number ${phoneNumber} successful (status ${status})`
);
}
return message;
})
.catch((error) => {
});

const { errorCode, errorMessage, status } = message;

if (errorCode) {
this.logger.error(
`Could not send SMS to number '${phoneNumber}'`,
error
`There was an error sending SMS to number ${phoneNumber} (${errorCode} - ${errorMessage})`
);
});
} else {
this.logger.info(
`Sent SMS to number ${phoneNumber} successful (status ${status})`
);
}

return message;
} catch (error) {
this.logger.error(
`Could not send SMS to number '${phoneNumber}'`,
error
);
}
};

return Promise.all(recipients.map(sendSmsToNumber));
}

public async sendMultipleSms(messages: Message[]) {
return Promise.all(messages.map(this.sendSms));
}
}
9 changes: 9 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type Message = {
body: string;
recipients: string[];
};

export type GenericLogger = {
error: (...args: unknown[]) => void;
info: (...args: unknown[]) => void;
};

0 comments on commit 782af1a

Please sign in to comment.