Skip to content

Commit

Permalink
Et 824 add new elation action (#576)
Browse files Browse the repository at this point in the history
* refactor createMessageThread
* add addMessageToThread
  • Loading branch information
sharlotta93 authored Feb 4, 2025
1 parent 879a895 commit c129738
Show file tree
Hide file tree
Showing 18 changed files with 545 additions and 262 deletions.
122 changes: 0 additions & 122 deletions extensions/elation/actions/__tests__/createMessageThread.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import { TestHelpers } from '@awell-health/extensions-core'

import { addMessageToThread as action } from './addMessageToThread'
import { ZodError } from 'zod'
import { makeAPIClient } from '../../client'

jest.mock('../../client')

describe('Elation - Add message to thread', () => {
const {
extensionAction: addMessageToThread,
onComplete,
onError,
helpers,
clearMocks,
} = TestHelpers.fromAction(action)

const mockAddMessageToThread = jest.fn()

const messagExample = {
senderId: 12345,
threadId: 999,
messageBody: 'Hello world',
}

const settings = {
auth_url: 'authurl',
base_url: 'baseurl',
client_id: 'client_id',
client_secret: 'client_secret',
username: 'username',
password: 'password',
}

beforeEach(() => {
clearMocks()
})

describe('Validation', () => {
test('Should call onError when required fields are missing', async () => {
const resp = addMessageToThread.onEvent({
payload: {
fields: {
...messagExample,
messageBody: undefined,
},
settings,
} as any,
onComplete,
onError,
helpers,
})

await expect(resp).rejects.toThrow(ZodError)
expect(onComplete).not.toHaveBeenCalled()
})

test('Should call onError when no threadId is invalid format', async () => {
const resp = addMessageToThread.onEvent({
payload: {
fields: {
...messagExample,
threadId: 'some text',
},
settings,
} as any,
onComplete,
onError,
helpers,
})

await expect(resp).rejects.toThrow(ZodError)
expect(onComplete).not.toHaveBeenCalled()
})
})

describe('Successful cases', () => {
beforeAll(() => {
const mockAPIClient = makeAPIClient as jest.Mock
mockAPIClient.mockImplementation(() => ({
addMessageToThread: mockAddMessageToThread.mockResolvedValue({
id: 1,
}),
}))
})

test('Should call onComplete when successful', async () => {
await addMessageToThread.onEvent({
payload: {
fields: messagExample,
settings,
} as any,
onComplete,
onError,
helpers,
})

expect(onComplete).toHaveBeenCalledWith({
data_points: {
messageId: '1',
},
})
expect(onError).not.toHaveBeenCalled()
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { z } from 'zod'
import { type Action, Category, validate } from '@awell-health/extensions-core'
import { SettingsValidationSchema, type settings } from '../../settings'
import { makeAPIClient } from '../../client'
import { fields, FieldsValidationSchema, dataPoints } from './config'

export const addMessageToThread: Action<
typeof fields,
typeof settings,
keyof typeof dataPoints
> = {
key: 'addMessageToThread',
category: Category.EHR_INTEGRATIONS,
title: 'Add Message to an existing thread',
description:
'Adds a message to an existing thread in Elation',
fields,
previewable: true,
dataPoints,
onEvent: async ({ payload, onComplete, onError }): Promise<void> => {
const { fields, settings } = validate({
schema: z.object({
fields: FieldsValidationSchema,
settings: SettingsValidationSchema,
}),
payload,
})

const api = makeAPIClient(settings)

const { id } = await api.addMessageToThread({
thread: fields.threadId,
sender: fields.senderId,
body: fields.messageBody,
send_date: new Date().toISOString(),
})

await onComplete({
data_points: {
messageId: String(id),
},
})
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { type DataPointDefinition } from '@awell-health/extensions-core'

export const dataPoints = {
messageId: {
key: 'messageId',
valueType: 'number',
},
} satisfies Record<string, DataPointDefinition>
37 changes: 37 additions & 0 deletions extensions/elation/actions/addMessageToThread/config/fields.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
FieldType,
NumericIdSchema,
type Field,
} from '@awell-health/extensions-core'
import z, { type ZodTypeAny } from 'zod'

export const fields = {
threadId: {
id: 'threadId',
label: 'Thread ID',
description:
'The ID of the message thread to which the message will be added',
type: FieldType.NUMERIC,
required: true,
},
senderId: {
id: 'senderId',
label: 'Sender ID',
description: 'The ID of the user that adds the message to the thread',
type: FieldType.NUMERIC,
required: true,
},
messageBody: {
id: 'messageBody',
label: 'Message Body',
description: 'The content of the message to be added to the thread',
type: FieldType.STRING,
required: true,
},
} satisfies Record<string, Field>

export const FieldsValidationSchema = z.object({
threadId: NumericIdSchema,
senderId: NumericIdSchema,
messageBody: z.string().min(1),
} satisfies Record<keyof typeof fields, ZodTypeAny>)
2 changes: 2 additions & 0 deletions extensions/elation/actions/addMessageToThread/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { fields, FieldsValidationSchema } from './fields'
export { dataPoints } from './dataPoints'
1 change: 1 addition & 0 deletions extensions/elation/actions/addMessageToThread/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { addMessageToThread } from './addMessageToThread'
Loading

0 comments on commit c129738

Please sign in to comment.