-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Et 824 add new elation action (#576)
* refactor createMessageThread * add addMessageToThread
- Loading branch information
1 parent
879a895
commit c129738
Showing
18 changed files
with
545 additions
and
262 deletions.
There are no files selected for viewing
122 changes: 0 additions & 122 deletions
122
extensions/elation/actions/__tests__/createMessageThread.ts
This file was deleted.
Oops, something went wrong.
106 changes: 106 additions & 0 deletions
106
extensions/elation/actions/addMessageToThread/addMessageToThread.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) | ||
}) |
44 changes: 44 additions & 0 deletions
44
extensions/elation/actions/addMessageToThread/addMessageToThread.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}, | ||
}) | ||
}, | ||
} |
8 changes: 8 additions & 0 deletions
8
extensions/elation/actions/addMessageToThread/config/dataPoints.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
37
extensions/elation/actions/addMessageToThread/config/fields.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
2
extensions/elation/actions/addMessageToThread/config/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export { fields, FieldsValidationSchema } from './fields' | ||
export { dataPoints } from './dataPoints' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { addMessageToThread } from './addMessageToThread' |
Oops, something went wrong.