10000 199/72 canvas/elation/athena contribution plumbing by thomasyopes · Pull Request #3788 · metriport/metriport · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

199/72 canvas/elation/athena contribution plumbing #3788

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

Merged
merged 85 commits into from
May 19, 2025

Conversation

thomasyopes
Copy link
Contributor
@thomasyopes thomasyopes commented May 5, 2025

Issues:

Dependencies

Description

  • Migrates resource diff job to resource-type level computation instead of per individual resource
  • Moves the resource diff job start/get logic to a shared logic
  • Moves fetching of computed bundles pre-signed-urls (EHR and Resource Diff) to shared logic
  • Moves refreshing the EHR bundles to shared logic
  • Moves resource diff job routing to shared routes
  • Split shared file into domain specific elements + added utility files for bundles / jobs
  • Re-typed shared bundle code to use Medplum types, not custom types
  • Moves resource diff utility to fhir-deduplication folder and makes it non-ehr-specific

Testing

  • Local
    • resource type workflow runs (including job updates)
    • canvas level API interactions work on shared commands / jobs / routes (both internal and CX facing)
    • diff utility produces the correct two output bundles.
  • Staging
    • resource type workflow runs (including job updates)
    • canvas level API interactions work on shared commands / jobs / routes (both internal and CX facing)
    • diff utility produces the correct two output bundles.
  • Sandbox
    • N/A
  • Production
    • resource type workflow runs (including job updates)
    • canvas level API interactions work on shared commands / jobs / routes (both internal and CX facing)
    • diff utility produces the correct two output bundles.

Release Plan

  • Before shipping, throttle the start resource diff job lambda to
  • Merge this

Summary by CodeRabbit

  • New Features

    • Added middleware to validate and process EHR source IDs in routes.
    • Introduced new internal API endpoints for managing EHR patient resource diff jobs and bundles.
    • Added utility modules for appointment date ranges, EHR client creation, JWT token handling, external ID parsing, and bundle operations.
    • Introduced classes for cloud and local processing of resource diff bundle computations and EHR bundle refreshes.
    • Added functions to compute resource differences and deduplicate FHIR resources with strict validation.
    • Added new bundle types and resource type validations for improved EHR data handling.
    • Added retry logic to API calls and SQS message sending for robustness.
  • Refactor

    • Unified resource diff bundle creation and refresh logic, removing direction-based handling in favor of explicit bundle types and job IDs.
    • Modularized shared utilities for JWT tokens, bundle operations, and mappings.
    • Replaced direct bundle fetching with asynchronous job-based processing and parallel resource handling.
    • Simplified EHR bundle refresh and resource diff start flows with improved job management and error handling.
    • Updated resource type handling to use standardized supported resource types and schemas.
    • Enhanced API request handling with centralized response validation and retry logic.
    • Consolidated resource diff bundle computation methods into a single streamlined process.
    • Updated import paths and code structure for better modularity and maintainability.
    • Removed obsolete resource diff direction enums and related code.
  • Bug Fixes

    • Improved validation for supported resource types and job status updates.
    • Strengthened error handling and reporting in job processing and bundle operations.
    • Fixed potential runtime errors by safely accessing resource properties and validating inputs.
  • Chores

    • Removed obsolete endpoints, Lambda handlers, and infrastructure related to prior resource diff bundle initiation.
    • Updated import paths and internal wiring to align with new utility modules and improved code structure.
    • Cleaned up unused types, constants, and functions related to removed resource diff direction handling.
    • Adjusted environment variables and permissions in infrastructure stacks to reflect removed components.
  • Documentation

    • Enhanced API route parameter validation and documentation for better clarity and consistency.

Thomas Yopes added 2 commits May 1, 2025 11:13
Ref: ENG-82

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Copy link
coderabbitai bot commented May 5, 2025

Walkthrough

This change set refactors and modularizes the EHR bundle/resource diff job infrastructure, removing legacy Canvas-specific resource diff logic and generalizing the workflow to support multiple EHRs. It introduces new utility modules for bundle and job management, updates API routes and handlers, and replaces direct Lambda invocations with SQS-driven job orchestration. Several files are deleted or replaced, type definitions are updated, and error handling and retry logic are improved throughout the codebase.

Changes

File(s) / Path(s) Change Summary
packages/api/src/command/mapping/cx.ts, .../patient.ts, .../consolidated-recreate.ts, .../cx-mapping.ts, .../jwt-token.ts, .../athenahealth/command/process-patients-from-appointments.ts, .../athenahealth/command/sync-patient.ts, .../athenahealth/shared.ts, .../canvas/command/process-patients-from-appointments.ts, .../canvas/command/sync-patient.ts, .../canvas/shared.ts, .../elation/command/process-patients-from-appointments.ts, .../elation/command/sync-patient.ts, .../elation/shared.ts, .../healthie/command/process-patients-from-appointments.ts, .../healthie/command/sync-patient.ts, .../healthie/shared.ts, .../jwt-token.ts, .../shared.ts, .../routes/ehr/shared.ts, .../routes/internal/jwt-token/athenahealth.ts, .../canvas.ts, .../elation.ts, .../healthie.ts Updated import paths to new utility modules; no logic changes.
packages/api/src/external/ehr/canvas/command/bundle/fetch-ehr-bundle.ts, .../fetch-resource-diff-bundle-pre-signed-urls.ts, .../job/create-resource-diff-bundles/get-job-payload.ts, .../job/create-resource-diff-bundles/start-job.ts, .../shared.ts, .../resource-diff.ts, .../jwt-token.ts, .../lambdas/src/ehr-start-resource-diff-bundles.ts Deleted legacy Canvas-specific and shared resource diff/bundle/job logic.
packages/api/src/external/ehr/canvas/command/bundle/refresh-ehr-bundles.ts Replaced legacy refresh logic with a new function refreshEhrBundles supporting generalized parameters and single resource type refresh.
packages/api/src/external/ehr/shared/utils/appointment.ts, .../client.ts, .../external-id.ts, .../fhir.ts, .../job.ts, .../jwt-token.ts, .../mappings.ts, .../patient.ts, .../bundle/functions.ts, .../bundle/types.ts Introduced new utility modules for appointment, client, external ID, FHIR, job, JWT token, mappings, patient, and bundle management.
packages/api/src/external/ehr/shared/command/bundle/fetch-bundle-presignd-urls.ts, .../refresh-ehr-bundles.ts Added new generalized functions for fetching bundle pre-signed URLs and refreshing bundles across EHRs.
packages/api/src/external/ehr/shared/job/bundle/create-resource-diff-bundles/get-job-payload.ts, .../start-job.ts, .../start-jobs-across-ehrs.ts Added new generalized job payload retrieval and job start logic for resource diff bundles.
packages/api/src/routes/ehr/canvas/patient.ts, .../routes/internal/ehr/canvas/patient.ts Refactored/removed Canvas-specific resource diff endpoints and logic; migrated to shared, generic job/bundle handling.
packages/api/src/routes/internal/ehr/index.ts, .../patient.ts, .../middleware.ts Added new internal EHR patient resource/job router and middleware for EHR ID validation.
packages/api/src/routes/internal/medical/patient-job.ts Added route for completing patient jobs with no entries; minor doc updates.
packages/core/src/external/ehr/api/api-shared.ts Added validateAndLogResponse utility for consistent API response validation/logging.
packages/core/src/external/ehr/api/bundle/refresh-ehr-bundle.ts, .../job/complete-job.ts, .../job/create-resource-diff-bundles/set-entry-status.ts, .../job/set-entry-status.ts Added/updated functions for refreshing EHR bundles, completing jobs, and setting job entry status with improved error handling and retries.
packages/core/src/external/ehr/api/job/initialize-job.ts, .../update-job-total.ts, .../sync-patient.ts, .../link-patient.ts Updated to use retry logic and centralized response validation.
packages/core/src/external/ehr/bundle/bundle-shared.ts Added/updated type guards and resource type validation for bundle operations.
packages/core/src/external/ehr/bundle/command/create-or-replace-bundle.ts, .../fetch-bundle.ts Improved error handling, resource type validation, and retry logic for S3 operations.
packages/core/src/external/ehr/bundle/create-resource-diff-bundles/steps/compute/ehr-compute-resource-diff-bundles-local.ts, .../ehr-compute-resource-diff-bundles.ts, .../utils.ts Refactored diff computation logic to batch processing, stricter validation, and improved deduplication.
packages/core/src/external/ehr/bundle/create-resource-diff-bundles/steps/compute/ehr-compute-resource-diff-bundles-cloud.ts, .../factory.ts, .../ehr-compute-resource-diff-bundles.ts Added SQS-based cloud handler and updated interfaces for diff bundle computation.
packages/core/src/external/ehr/bundle/create-resource-diff-bundles/steps/start/ehr-start-resource-diff-bundles-local.ts Refactored to remove legacy resource validation, streamline resource type iteration, and manage job totals/completion.
packages/core/src/external/ehr/bundle/job/create-resource-diff-bundles/create-resource-diff-bundle-shared.ts Added shared types/utilities for diff bundle job requests and SQS group IDs.
packages/core/src/external/ehr/bundle/job/create-resource-diff-bundles/steps/refresh/ehr-refresh-ehr-bundles-cloud.ts, .../ehr-refresh-ehr-bundles-factory.ts, .../ehr-refresh-ehr-bundles-local.ts, .../ehr-refresh-ehr-bundles.ts Added/updated cloud/local handlers and interfaces for refreshing EHR bundles.
packages/core/src/external/ehr/bundle/refresh-ehr-bundles/ehr-refresh-ehr-bundles-local.ts Enhanced job lifecycle/error handling and resource type iteration for EHR bundle refresh.
packages/core/src/external/ehr/canvas/index.ts Refactored to generalize resource type/bundle handling, update method signatures, and remove direction-based logic.
packages/core/src/external/ehr/elation/link-patient/elation-link-patient-cloud.ts, .../healthie/link-patient/healthie-link-patient-cloud.ts, .../sync-patient/ehr-sync-patient-cloud.ts Wrapped SQS message sending in retry logic.
packages/core/src/fhir-deduplication/compute-resources-xor.ts Added function for computing XOR of FHIR resource sets with validation and deduplication.
packages/shared/src/interface/external/ehr/fhir-resource.ts Unified resource types with external FHIR types, exported supported resource types, and updated bundle creation signatures.
packages/infra/lib/ehr-nested-stack.ts, .../api-stack.ts, .../api-stack/api-service.ts Removed all references to legacy start resource diff bundles queue/lambda, updated permissions, and environment variables.
packages/lambdas/src/ehr-compute-resource-diff-bundles.ts, .../ehr-refresh-ehr-bundles.ts Updated Lambda handlers to new message formats, resource type handling, and error reporting.

Sequence Diagram(s)

Generalized Resource Diff Bundle Job Flow

sequenceDiagram
    participant API as API Route
    participant JobStarter as startCreateResourceDiffBundlesJob
    participant JobDB as PatientJob Store
    participant BundleRefresher as refreshEhrBundles
    participant SQS as SQS Queue
    participant Lambda as Lambda Worker
    participant S3 as S3 Storage

    API->>JobStarter: POST /internal/ehr/:ehrId/patient/:id/resource/diff (start job)
    JobStarter->>JobDB: Create patient job (type: resource-diff)
    JobStarter->>BundleRefresher: For each supported resource type, refreshEhrBundles(...)
    BundleRefresher->>SQS: Send refresh job message
    SQS->>Lambda: Trigger Lambda for refresh
    Lambda->>S3: Create/replace empty diff bundles (Metriport-only, EHR-only)
    Lambda->>SQS: Send compute diff job message
    SQS->>Lambda: Trigger Lambda for compute
    Lambda->>S3: Fetch resources, compute diff, update bundles
    Lambda->>JobDB: Update job entry status (successful/failed)
Loading

Fetching Resource Diff Job Payload

sequenceDiagram
    participant API as API Route
    participant JobDB as PatientJob Store
    participant BundleFetcher as fetchBundlePreSignedUrls

    API->>JobDB: Fetch patient job by jobId
    alt job.status == "completed"
        API->>BundleFetcher: fetchBundlePreSignedUrls(...)
        BundleFetcher->>API: Return pre-signed URLs
        API->>API: Compose and return job payload with URLs
    else
        API->>API: Return job payload (no URLs)
    end
Loading

Refreshing EHR Bundles

sequenceDiagram
    participant API as API Route
    participant BundleRefresher as refreshEhrBundles
    participant SQS as SQS Queue
    participant Lambda as Lambda Worker
    participant S3 as S3 Storage

    API->>BundleRefresher: POST /internal/ehr/:ehrId/patient/:id/resource/refresh
    BundleRefresher->>SQS: Send refresh job message
    SQS->>Lambda: Trigger Lambda for refresh
    Lambda->>S3: Refresh EHR bundle for resource type
    Lambda->>JobDB: Update job entry status
Loading

JWT Token Management (Generalized)

sequenceDiagram
    participant API as API Route
    participant JWTUtils as checkJwtToken/saveJwtToken
    participant TokenStore as JWT Token Store

    API->>JWTUtils: checkJwtToken({token, source})
    JWTUtils->>TokenStore: getJwtToken({token, source})
    TokenStore-->>JWTUtils: Token info
    JWTUtils-->>API: {active, expired?}

    API->>JWTUtils: saveJwtToken({token, source, exp, data})
    JWTUtils->>TokenStore: findOrCreateJwtToken(...)
    TokenStore-->>JWTUtils: (persisted)
    JWTUtils-->>API: (done)
Loading

SQS-Based Resource Diff Computation

sequenceDiagram
    participant JobStarter as startCreateResourceDiffBundlesJob
    participant SQS as SQS Queue
    participant Lambda as Lambda Worker
    participant S3 as S3 Storage

    JobStarter->>SQS: Send compute diff job message (per resource type)
    SQS->>Lambda: Trigger Lambda for compute
    Lambda->>S3: Fetch resources, compute diff, update bundles
    Lambda->>JobDB: Update job entry status
Loading

These diagrams represent the new modular, EHR-agnostic resource diff and bundle management workflows, reflecting the new control flow and infrastructure introduced by this change set.

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boost—your workflow just got faster.


🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Thomas Yopes added 11 commits May 5, 2025 10:40
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved into /shared/utils/jwt-token.ts

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JWT stuff moved into /shared/utils/jwt-token.ts
Appointments stuff moved into /shared/utils/appointment.ts
External ID stuff moved into /shared/utils/external-id.ts
Client stuff moved into /shared/utils/client.ts

Thomas Yopes added 5 commits May 5, 2025 13:24
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
… 199-elation-contribution

Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
@@ -56,7 +55,6 @@ export async function recreateConsolidated({
createResourceDiffBundles({
cxId: patient.cxId,
patientId: patient.id,
direction: ResourceDiffDirection.METRIPORT_ONLY,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The resource diff job now computes both directions at once.

Thomas Yopes added 2 commits May 6, 2025 11:57
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
@@ -45,7 +48,3 @@ export async function createCanvasClient(
getClient: CanvasApi.create,
});
}

export function getCreateCanvasResourceDiffBundlesJobType(direction: ResourceDiffDirection) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to jobs utils file

Thomas Yopes added 4 commits May 6, 2025 12:52
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Taken from old shared file.

… 199-elation-contribution

Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Copy link
@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (3)
packages/api/src/routes/internal/ehr/__tests__/middleware.test.ts (3)

4-7: Mock objects could be improved with Jest spy functions.

While the dummy mocks work for basic testing, consider using Jest's jest.fn() for the next function to verify it's called in the success case and not called in the error case.

-const dummyNext: NextFunction = function () {
-  return;
-};
+const dummyNext: NextFunction = jest.fn();

9-19: Good test for valid ehrId case, but could verify next() is called.

The test correctly verifies that ehrId is set in query, but doesn't verify that next() is called. This would ensure the middleware is properly continuing the request chain.

  processEhrId(incomingRequest, dummyResp, dummyNext);
  expect(incomingRequest.query.ehrId).toBe("athenahealth");
+  expect(dummyNext).toHaveBeenCalled();

1-29: Consider adding more test cases for edge conditions.

The current tests cover basic valid and invalid cases, but consider adding tests for:

  1. Missing ehrId parameter
  2. Other valid EHR sources (not just "athenahealth")
  3. Edge cases like empty string or null

This would provide more comprehensive test coverage for the middleware.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Lite

📥 Commits

Reviewing files that changed from the base of the PR and between 18c9c63 and cb6a2da.

📒 Files selected for processing (1)
  • packages/api/src/routes/internal/ehr/__tests__/middleware.test.ts (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.ts`: - Use the Onion Pattern to organize a package's code in layers - Try to use immutable code and A3D4 avoid sharing state across different functions, objects, and systems - Try...

**/*.ts: - Use the Onion Pattern to organize a package's code in layers

  • Try to use immutable code and avoid sharing state across different functions, objects, and systems
  • Try to build code that's idempotent whenever possible
  • Prefer functional programming style functions: small, deterministic, 1 input, 1 output
  • Minimize coupling / dependencies
  • Avoid modifying objects received as parameter
  • Only add comments to code to explain why something was done, not how it works
  • Naming
    • classes, enums: PascalCase
    • constants, variables, functions: camelCase
    • file names: kebab-case
    • table and column names: snake_case
    • Use meaningful names, so whoever is reading the code understands what it means
    • Don’t use negative names, like notEnabled, prefer isDisabled
    • For numeric values, if the type doesn’t convey the unit, add the unit to the name
  • Typescript
    • Use types
    • Prefer const instead of let
    • Avoid any and casting from any to other types
    • Type predicates: only applicable to narrow down the type, not to force a complete type conversion
    • Prefer deconstructing parameters for functions instead of multiple parameters that might be of
      the same type
    • Don’t use null inside the app, only on code interacting with external interfaces/services,
      like DB and HTTP; convert to undefined before sending inwards into the code
    • Use async/await instead of .then()
    • Use the strict equality operator ===, don’t use abstract equality operator ==
    • When calling a Promise-returning function asynchronously (i.e., not awaiting), use .catch() to
      handle errors (see processAsyncError and emptyFunction depending on the case)
    • Date and Time
      • Always use buildDayjs() to create dayjs instances
      • Prefer dayjs.duration(...) to create duration consts and keep them as duration
  • Prefer Nullish Coalesce (??) than the OR operator (||) to provide a default value
  • Avoid creating arrow functions
  • Use truthy syntax instead of in - i.e., if (data.link) not if ('link' in data)
  • Error handling
    • Pass the original error as the new one’s cause so the stack trace is persisted
    • Error messages should have a static message - add dynamic data to MetriportError's additionalInfo prop
    • Avoid sending multiple events to Sentry for a single error
  • Global constants and variables
    • Move literals to constants declared after imports when possible (avoid magic numbers)
    • Avoid shared, global objects
  • Avoid using console.log and console.error in packages other than utils, infra and shared,
    and try to use out().log instead
  • Avoid multi-line logs
    • don't send objects as a second parameter to console.log() or out().log()
    • don't create multi-line strings when using JSON.stringify()
  • Use eslint to enforce code style
  • Use prettier to format code
  • max column length is 100 chars
  • multi-line comments use /** */
  • scripts: top-level comments go after the import
  • packages/api/src/routes/internal/ehr/__tests__/middleware.test.ts
🧬 Code Graph Analysis (1)
packages/api/src/routes/internal/ehr/__tests__/middleware.test.ts (1)
packages/api/src/routes/internal/ehr/middleware.ts (1)
  • processEhrId (6-11)
🔇 Additional comments (2)
packages/api/src/routes/internal/ehr/__tests__/middleware.test.ts (2)

1-3: LGTM: Proper imports for Express middleware test.

The imports correctly include the necessary Express types and the middleware function being tested.


20-28: Good test for invalid ehrId case.

The test properly verifies that an error is thrown with the correct message when an invalid ehrId is provided.

Thomas Yopes added 2 commits May 15, 2025 05:55
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Copy link
@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (2)
packages/lambdas/src/ehr-compute-resource-diff-bundles.ts (2)

31-31: Use prefixedLog instead of console.log for consistency

The codebase generally avoids using console.log directly, especially in packages other than utils, infra, and shared. Consider using the prefixedLog helper that's already defined and used elsewhere in this file.

-  console.log(`Running with unparsed body: ${message.body}`);
+  log(`Running with unparsed body: ${message.body}`);

19-21: Consider validating waitTimeInMillis similar to maxAttempts

While the team indicated that environment variables are explicitly set, for consistency consider adding validation for waitTimeInMillis similar to what could be done for maxAttempts to prevent potential NaN issues if the configuration changes.

const waitTimeInMillisRaw = getEnvOrFail("WAIT_TIME_IN_MILLIS");
-const waitTimeInMillis = parseInt(waitTimeInMillisRaw);
+const waitTimeInMillis = parseInt(waitTimeInMillisRaw, 10);
+if (Number.isNaN(waitTimeInMillis) || waitTimeInMillis <= 0) {
+  throw new MetriportError(
+    "`WAIT_TIME_IN_MILLIS` must be a positive integer",
+    undefined,
+    { waitTimeInMillisRaw }
+  );
+}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Lite

📥 Commits

Reviewing files that changed from the base of the PR and between 2b82303 and 09fae9d.

📒 Files selected for processing (3)
  • packages/lambdas/src/ehr-compute-resource-diff-bundles.ts (4 hunks)
  • packages/lambdas/src/ehr-refresh-ehr-bundles.ts (4 hunks)
  • packages/lambdas/src/shared/ehr.ts (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • packages/lambdas/src/shared/ehr.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/lambdas/src/ehr-refresh-ehr-bundles.ts
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.ts`: - Use the Onion Pattern to organize a package's code in layers - Try to use immutable code and avoid sharing state across different functions, objects, and systems - Try...

**/*.ts: - Use the Onion Pattern to organize a package's code in layers

  • Try to use immutable code and avoid sharing state across different functions, objects, and systems
  • Try to build code that's idempotent whenever possible
  • Prefer functional programming style functions: small, deterministic, 1 input, 1 output
  • Minimize coupling / dependencies
  • Avoid modifying objects received as parameter
  • Only add comments to code to explain why something was done, not how it works
  • Naming
    • classes, enums: PascalCase
    • constants, variables, functions: camelCase
    • file names: kebab-case
    • table and column names: snake_case
    • Use meaningful names, so whoever is reading the code understands what it means
    • Don’t use negative names, like notEnabled, prefer isDisabled
    • For numeric values, if the type doesn’t convey the unit, add the unit to the name
  • Typescript
    • Use types
    • Prefer const instead of let
    • Avoid any and casting from any to other types
    • Type predicates: only applicable to narrow down the type, not to force a complete type conversion
    • Prefer deconstructing parameters for functions instead of multiple parameters that might be of
      the same type
    • Don’t use null inside the app, only on code interacting with external interfaces/services,
      like DB and HTTP; convert to undefined before sending inwards into the code
    • Use async/await instead of .then()
    • Use the strict equality operator ===, don’t use abstract equality operator ==
    • When calling a Promise-returning function asynchronously (i.e., not awaiting), use .catch() to
      handle errors (see processAsyncError and emptyFunction depending on the case)
    • Date and Time
      • Always use buildDayjs() to create dayjs instances
      • Prefer dayjs.duration(...) to create duration consts and keep them as duration
  • Prefer Nullish Coalesce (??) than the OR operator (||) to provide a default value
  • Avoid creating arrow functions
  • Use truthy syntax instead of in - i.e., if (data.link) not if ('link' in data)
  • Error handling
    • Pass the original error as the new one’s cause so the stack trace is persisted
    • Error messages should have a static message - add dynamic data to MetriportError's additionalInfo prop
    • Avoid sending multiple events to Sentry for a single error
  • Global constants and variables
    • Move literals to constants declared after imports when possible (avoid magic numbers)
    • Avoid shared, global objects
  • Avoid using console.log and console.error in packages other than utils, infra and shared,
    and try to use out().log instead
  • Avoid multi-line logs
    • don't send objects as a second parameter to console.log() or out().log()
    • don't create multi-line strings when using JSON.stringify()
  • Use eslint to enforce code style
  • Use prettier to format code
  • max column length is 100 chars
  • multi-line comments use /** */
  • scripts: top-level comments go after the import
  • packages/lambdas/src/ehr-compute-resource-diff-bundles.ts
🧠 Learnings (1)
packages/lambdas/src/ehr-compute-resource-diff-bundles.ts (1)
Learnt from: thomasyopes
PR: metriport/metriport#3788
File: packages/lambdas/src/ehr-compute-resource-diff-bundles.ts:42-43
Timestamp: 2025-05-09T18:47:30.626Z
Learning: The AWS SQS message attribute `ApproximateReceiveCount` is controlled by AWS and always contains a valid numeric value, so additional validation isn't required when parsing it with `parseInt`.
🔇 Additional comments (4)
packages/lambdas/src/ehr-compute-resource-diff-bundles.ts (4)

40-42: Code handles receive count appropriately

This is a good implementation of the retry logic using the SQS message's ApproximateReceiveCount attribute to determine when to report errors. As noted in your previous feedback, this value is controlled by AWS and is guaranteed to be a valid numeric value.


36-37: Improved log prefix

The simplified prefixed log addresses the previous feedback about overusing prefixed logs, making the logs more readable while still providing necessary context.


45-45: Updated method call with reportError flag

The updated method call to computeResourceDiffBundles with the additional reportError flag properly implements the new error reporting mechanism based on retry attempts.


59-59: Shared schema usage

Using the imported shared schema instead of a local one is a good improvement that aligns with the PR objectives of centralizing shared logic.

Thomas Yopes added 6 commits May 15, 2025 06:08
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Ref: ENG-199

Ref: #1040
Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
… 199-elation-contribution

Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
Thomas Yopes added 2 commits May 15, 2025 15:45
… 199-elation-contribution

Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
… 199-elation-contribution

Signed-off-by: Thomas Yopes <thomasyopes@Thomass-MBP.attlocal.net>
@thomasyopes thomasyopes added this pull request to the merge queue May 19, 2025
Merged via the queue into develop with commit 741f3bc May 19, 2025
23 checks passed
@thomasyopes thomasyopes deleted the 199-elation-contribution branch May 19, 2025 16:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0