Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Et 824 add new elation action #576

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

sharlotta93
Copy link
Contributor

  • migrate create thread message action to the new format
  • add new action to append a message to the existing thread

Copy link

github-actions bot commented Feb 4, 2025

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 4 🔵🔵🔵🔵⚪
🧪 PR contains tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Possible Error Handling Gap

The addMessageToThread function does not appear to handle API errors explicitly, which could lead to unhandled exceptions if the API call fails. Consider adding a try-catch block to manage potential errors gracefully.

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),
    },
  })
},
Validation Schema Usage

The createMessageThread function uses messageThreadSchema for validation, but it is unclear if all edge cases are covered. Ensure the schema accounts for all possible invalid inputs, especially for optional fields like groupId.

onEvent: async ({ payload, onComplete, onError }): Promise<void> => {
  const {
    patientId,
    senderId,
    practiceId,
    documentDate,
    chartDate,
    messageBody,
    isUrgent,
    groupId,
  } = payload.fields

  const messageThread = messageThreadSchema.parse({
    patient: patientId,
    sender: senderId,
    practice: practiceId,
    document_date: documentDate,
    chart_date: chartDate,
    is_urgent: isUrgent,
    messages: [
      {
        body: messageBody,
        send_date: new Date().toISOString(),
        sender: senderId,
      },
    ],
    members: !isNil(groupId)
      ? [
          {
            group: groupId,
            status: 'Requiring Action',
          },
        ]
      : [],
  })

  const api = makeAPIClient(payload.settings)
  const { id } = await api.createMessageThread(messageThread)

  await onComplete({
    data_points: {
      messageThreadId: String(id),
    },
  })
},
API Endpoint Hardcoding

The addMessageToThread method in ElationDataWrapper hardcodes the endpoint /thread_messages. Consider abstracting this to a configuration or constants file to improve maintainability and flexibility.

public async addMessageToThread(
  obj: AddMessageToThreadInput,
): Promise<MessageThreadResponse> {
  const req = this.Request<MessageThreadResponse>({
    method: 'POST',
    url: `/thread_messages`,
    data: obj,
  })
  const res = await req
  return res

Copy link

github-actions bot commented Feb 4, 2025

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Score
Possible issue
Add error handling for API calls

Add error handling for the api.addMessageToThread call to ensure that any API errors
are caught and appropriately passed to onError to prevent unhandled promise
rejections.

extensions/elation/actions/addMessageToThread/addMessageToThread.ts [31-36]

-const { id } = await api.addMessageToThread({
-  thread: fields.threadId,
-  sender: fields.senderId,
-  body: fields.messageBody,
-  send_date: new Date().toISOString(),
-})
+try {
+  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),
+    },
+  });
+} catch (error) {
+  await onError(error);
+}
Suggestion importance[1-10]: 9

Why: Adding error handling for the api.addMessageToThread call is crucial to ensure that any API errors are caught and appropriately passed to onError. This prevents unhandled promise rejections and improves the robustness of the code.

9
General
Ensure numeric IDs are positive

Validate that threadId and senderId are positive numbers in the
FieldsValidationSchema to prevent invalid or negative IDs from being processed.

extensions/elation/actions/addMessageToThread/config/fields.ts [32-36]

 export const FieldsValidationSchema = z.object({
-  threadId: NumericIdSchema,
-  senderId: NumericIdSchema,
+  threadId: NumericIdSchema.refine((id) => id > 0, { message: "Thread ID must be a positive number" }),
+  senderId: NumericIdSchema.refine((id) => id > 0, { message: "Sender ID must be a positive number" }),
   messageBody: z.string().min(1),
 } satisfies Record<keyof typeof fields, ZodTypeAny>)
Suggestion importance[1-10]: 8

Why: Validating that threadId and senderId are positive numbers enhances data integrity by preventing invalid or negative IDs from being processed. This is a meaningful improvement to the validation schema.

8

@sharlotta93 sharlotta93 enabled auto-merge (squash) February 4, 2025 00:31
@sharlotta93 sharlotta93 requested a review from nckhell February 4, 2025 00:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant