Skip to content

Commit

Permalink
Twilio 'From' action field (#151)
Browse files Browse the repository at this point in the history
* feat: add `from` action field to Send SMS action

Co-authored-by: Nick Hellemans <[email protected]>
  • Loading branch information
radoslawstepinski and nckhell authored Jun 1, 2023
1 parent a0d46e4 commit ad518e0
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 5 deletions.
15 changes: 14 additions & 1 deletion extensions/twilio/v2/actions/sendSms/config/fields.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { z, type ZodTypeAny } from 'zod'
import { E164PhoneValidationSchema } from '@awell-health/extensions-core'
import {
E164PhoneValidationSchema,
E164PhoneValidationOptionalSchema,
} from '@awell-health/extensions-core'
import {
type Field,
FieldType,
Expand All @@ -8,6 +11,15 @@ import {
import { MessageValidationSchema } from '../../../../common/validation'

export const fields = {
from: {
label: '"From" number',
id: 'from',
type: FieldType.STRING,
stringType: StringType.PHONE,
required: false,
description:
'The phone number that will send the text messages, it must be a Twilio phone number that you own. When left blank, the "From" number from the extension settings will be used.',
},
recipient: {
id: 'recipient',
label: '"To" phone number',
Expand All @@ -27,5 +39,6 @@ export const fields = {

export const FieldsValidationSchema = z.object({
recipient: E164PhoneValidationSchema,
from: E164PhoneValidationOptionalSchema,
message: MessageValidationSchema,
} satisfies Record<keyof typeof fields, ZodTypeAny>)
59 changes: 59 additions & 0 deletions extensions/twilio/v2/actions/sendSms/sendSms.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { sendSms } from './sendSms'
import twilioSdk from '../../../common/sdk/twilio'

describe('Send SMS action', () => {
const onComplete = jest.fn()
const onError = jest.fn()
const getLastTwilioClient = (): any => (twilioSdk as any as jest.Mock<typeof twilioSdk>).mock.results.at(-1)?.value

beforeEach(() => {
onComplete.mockClear()
Expand All @@ -23,6 +25,7 @@ describe('Send SMS action', () => {
fields: {
message: 'Message content',
recipient: '+32494000000',
from: '',
},
settings: {
accountSid: 'AC-accountSid',
Expand Down Expand Up @@ -51,6 +54,7 @@ describe('Send SMS action', () => {
fields: {
message: 'Message content',
recipient: '',
from: '',
},
settings: {
accountSid: 'AC-accountSid',
Expand Down Expand Up @@ -79,6 +83,7 @@ describe('Send SMS action', () => {
fields: {
message: '',
recipient: '+19144542596',
from: '',
},
settings: {
accountSid: 'AC-accountSid',
Expand All @@ -92,4 +97,58 @@ describe('Send SMS action', () => {
expect(onComplete).not.toHaveBeenCalled()
expect(onError).toHaveBeenCalled()
})

describe("'From' number", () => {
const basePayload = {
pathway: {
id: 'pathway-id',
definition_id: 'pathway-definition-id',
},
activity: {
id: 'activity-id',
},
patient: { id: 'test-patient' },
fields: {
message: 'Message content',
recipient: '+32494000000',
from: '+32494000000',
},
settings: {
accountSid: 'AC-accountSid',
authToken: 'authToken',
fromNumber: '+19144542596',
},
}

test('Should use one provided in action fields', async () => {
await sendSms.onActivityCreated(
basePayload,
onComplete,
onError
)
expect(getLastTwilioClient().messages.create.mock.calls.at(-1)[0].from).toEqual(basePayload.fields.from)
expect(onComplete).toHaveBeenCalled()
expect(onError).not.toHaveBeenCalled()
})

test('Should fallback to settings if no number is provided', async () => {
const payloadWithoutFrom = {
...basePayload,
fields: {
message: 'Message content',
recipient: '+32494000000',
from: '',
},
}

await sendSms.onActivityCreated(
payloadWithoutFrom,
onComplete,
onError
)
expect(getLastTwilioClient().messages.create.mock.calls.at(-1)[0].from).toEqual(payloadWithoutFrom.settings.fromNumber)
expect(onComplete).toHaveBeenCalled()
expect(onError).not.toHaveBeenCalled()
})
})
})
8 changes: 4 additions & 4 deletions extensions/twilio/v2/actions/sendSms/sendSms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { fromZodError } from 'zod-validation-error'
import twilioSdk from '../../../common/sdk/twilio'
import { type Action } from '@awell-health/extensions-core'
import { type settings } from '../../../settings'
import { Category , validate } from '@awell-health/extensions-core'
import { Category, validate } from '@awell-health/extensions-core'
import { SettingsValidationSchema } from '../../../settings'
import { FieldsValidationSchema, fields } from './config'

Expand All @@ -17,8 +17,8 @@ export const sendSms: Action<typeof fields, typeof settings> = {
onActivityCreated: async (payload, onComplete, onError) => {
try {
const {
settings: { accountSid, authToken, fromNumber },
fields: { recipient, message },
settings: { accountSid, authToken, fromNumber: defaultFromNumber },
fields: { recipient, message, from },
} = validate({
schema: z.object({
settings: SettingsValidationSchema,
Expand All @@ -34,7 +34,7 @@ export const sendSms: Action<typeof fields, typeof settings> = {

await client.messages.create({
body: message,
from: fromNumber,
from: from ?? defaultFromNumber,
to: recipient,
})

Expand Down

0 comments on commit ad518e0

Please sign in to comment.