Skip to content

Conversation

keshavsaharia
Copy link
Contributor

@keshavsaharia keshavsaharia commented Jun 21, 2025

metriport/metriport-internal#1040

Issues:

Dependencies

  • Upstream: None
  • Downstream: None

Description

I received a report that Surescripts conversion bundles had broken references for MedicationDispense, but I couldn't figure out anywhere that the logic would allow that to happen. I wrote an analysis script to test this, which showed that bundles with MedicationDispense were having their references broken by dangerouslyDeduplicateFhir. This is an issue that likely affects any FHIR bundle with MedicationDispense resources.

Screenshot 2025-06-20 at 7 41 19 PM

After implementing the fix, all patient bundles were deduplicating correctly with correct references.

Screenshot 2025-06-20 at 7 41 40 PM

Testing

Will test by deduplicating some large bundles.

  • Local
    • Use utils script to download large bundles and deduplicate
  • Staging
    • Run deduplication on staging bundles through Surescripts
  • Production
    • Run deduplication on production and monitor

Release Plan

  • Merge this

Summary by CodeRabbit

  • Bug Fixes
    • Ensured that MedicationDispense resources are correctly extracted and included alongside other medication-related data.
    • Extended deduplication and reference replacement processes to cover MedicationDispense resources.

…cationDispense

metriport/metriport-internal#1040

Signed-off-by: Keshav Saharia <keshav@metriport.com>
Copy link

linear bot commented Jun 21, 2025

Copy link

coderabbitai bot commented Jun 21, 2025

Walkthrough

The changes update the handling of the MedicationDispense FHIR resource type. Extraction logic now collects MedicationDispense resources from bundles, and deduplication logic includes MedicationDispense in medication-related resource processing, ensuring consistent treatment with other medication resource types.

Changes

File(s) Change Summary
packages/core/src/external/fhir/bundle/bundle.ts Updated extraction logic to collect and return MedicationDispense resources from FHIR bundles.
packages/core/src/fhir-deduplication/deduplicate-fhir.ts Included MedicationDispense in deduplication and medication reference replacement logic.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant BundleExtractor
    participant Deduplicator

    Client->>BundleExtractor: extractFhirTypesFromBundle(bundle)
    BundleExtractor->>BundleExtractor: Collect MedicationDispense and other resources
    BundleExtractor-->>Client: ExtractedFhirTypes (includes medicationDispenses)

    Client->>Deduplicator: deduplicateFhir(ExtractedFhirTypes)
    Deduplicator->>Deduplicator: Deduplicate medications
    Deduplicator->>Deduplicator: Replace medication references in MedicationDispense and others
    Deduplicator-->>Client: Deduplicated FHIR resources
Loading

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

npm error code ERR_SSL_WRONG_VERSION_NUMBER
npm error errno ERR_SSL_WRONG_VERSION_NUMBER
npm error request to https://10.0.0.28:4873/punycode/-/punycode-2.3.1.tgz failed, reason: C0CC4D52767F0000:error:0A00010B:SSL routines:ssl3_get_record:wrong version number:../deps/openssl/openssl/ssl/record/ssl3_record.c:354:
npm error
npm error A complete log of this run can be found in: /.npm/_logs/2025-06-21T02_55_29_418Z-debug-0.log


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 5522a26 and 12eb5fe.

📒 Files selected for processing (2)
  • packages/core/src/external/fhir/bundle/bundle.ts (2 hunks)
  • packages/core/src/fhir-deduplication/deduplicate-fhir.ts (2 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 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/core/src/fhir-deduplication/deduplicate-fhir.ts
  • packages/core/src/external/fhir/bundle/bundle.ts
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: check-pr / lint-build-test
  • GitHub Check: Analyze (javascript)
🔇 Additional comments (6)
packages/core/src/fhir-deduplication/deduplicate-fhir.ts (3)

35-35: LGTM: MedicationDispense added to medication-related types.

This correctly includes MedicationDispense in the medication-related resource types, ensuring consistent handling with other medication resources.


64-64: LGTM: Medication references will be properly replaced.

Adding "medicationDispenses" to the reference replacement logic is correct, as the comment above explains that medication references must be replaced before deduplication.


68-75: ```shell
#!/bin/bash
set -e

1. List all deduplication functions in the fhir-deduplication directory

echo "=== Deduplication functions in fhir-deduplication ==="
rg -n "export function deduplicate" packages/core/src/fhir-deduplication/*.ts

2. Search for any 'Dispense' references in the same directory

echo "=== 'Dispense' references in fhir-deduplication ==="
rg -n "Dispense" packages/core/src/fhir-deduplication/*.ts

3. Show context around medicationDispenses usage in deduplicate-fhir.ts

echo "=== Context around medicationDispenses in deduplicate-fhir.ts ==="
rg -n "medicationDispens" packages/core/src/fhir-deduplication/deduplicate-fhir.ts -A3 -B3


</details>
<details>
<summary>packages/core/src/external/fhir/bundle/bundle.ts (3)</summary>

`384-384`: **Good organization: MedicationDispense array grouped with medication resources.**

Moving the `medicationDispenses` array declaration to be grouped with other medication-related arrays improves code organization and readability.

---

`420-421`: **LGTM: MedicationDispense extraction logic implemented correctly.**

The extraction logic for MedicationDispense resources follows the same pattern as other resource types and correctly adds them to the medicationDispenses array.

---

`502-502`: **LGTM: MedicationDispense included in extraction results.**

Correctly includes the medicationDispenses array in the returned ExtractedFhirTypes object, ensuring the extracted resources are available for downstream processing.

</details>

</blockquote></details>

</details>
<!-- internal state start -->


<!-- DwQgtGAEAqAWCWBnSTIEMB26CuAXA9mAOYCmGJATmriQCaQDG+Ats2bgFyQAOFk+AIwBWJBrngA3EsgEBPRvlqU0AgfFwA6NPEgQAfACgjoCEYDEZyAAUASpETZWaCrKNwSPbABsvkCiQBHbGlcSHFcLzpIACIAM3gADwAKJn8ASi4AMUTIJVpsbi94Bmp4fCwCHgp8bkoveVhMWki/EljKMgZpSFj8PgBZOmLS8oARJFqMRBJo9Fpaf0Rp5EwUJeDIAHdYDrCd3LoCopLxcsgvfCJilAr9gANaTFJq7ER60cPC4ZpMhAo7nrYDBiMpYTZoGT+NAAa3gGCIrXa/mB3V6AyGJ1B40Qk2mrUQ+GwFC6yE26gQWEyAAkAJJ2ARA5rSDQwfa8QSRZhbCGtbh9Gj0OF7DxMDA0BKhfCxSAAZSJ0gYFHg3FwyFFUgoiFBkAZGCZiAANFsdv5ER0UaTdr1GWF8DqPAJqtCyEa0F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finishing_touch_checkbox_start -->

<details open="true">
<summary>✨ Finishing Touches</summary>

- [ ] <!-- {"checkboxId": "7962f53c-55bc-4827-bfbf-6a18da830691"} --> 📝 Generate Docstrings

</details>

<!-- finishing_touch_checkbox_end -->
<!-- tips_start -->

---

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

<details>
<summary>❤️ Share</summary>

- [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai)
- [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai)
- [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai)
- [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)

</details>

<details>
<summary>🪧 Tips</summary>

### Chat

There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai?utm_source=oss&utm_medium=github&utm_campaign=metriport/metriport&utm_content=4076):

- 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](https://www.coderabbit.ai/contact-us/support) 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 docstrings` to [generate docstrings](https://docs.coderabbit.ai/finishing-touches/docstrings) for this 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](https://docs.coderabbit.ai/guides/configure-coderabbit) 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](https://docs.coderabbit.ai) for detailed information on how to use CodeRabbit.
- Join our [Discord Community](http://discord.gg/coderabbit) to get help, request features, and share feedback.
- Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.

</details>

<!-- tips_end -->

@keshavsaharia keshavsaharia marked this pull request as ready for review June 21, 2025 02:54
@keshavsaharia keshavsaharia added this pull request to the merge queue Jun 21, 2025
Merged via the queue into develop with commit 66cfe95 Jun 21, 2025
15 checks passed
@keshavsaharia keshavsaharia deleted the eng-498-medicationdispense-deduplication branch June 21, 2025 19:03
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.

2 participants