Skip to content

Conversation

RamilGaripov
Copy link
Contributor

@RamilGaripov RamilGaripov commented Aug 11, 2025

Part of ENG-836

Issues:

Description

  • When an XML is redownloaded, we check whether its size has changed, in which case, we will attempt to remove its rendered versions (HTML and PDF)

Testing

  • Local
    • Use a local script to:
      • upload a file and its renders
      • if same size:
        • only reupload the file
      • if diff size:
        • reupload the file
        • remove existing renders
        • if renders don't exist, make sure it doesn't error
  • Production
    • Make sure an existing patient gets their render deleted after a file redownload

Release Plan

  • Merge this

Summary by CodeRabbit

  • New Features

    • Document render artifacts now include both HTML and PDF variants.
  • Improvements

    • More resilient parsing and broader error handling for document responses.
    • Unified storage-info handling for document downloads and rendering.
    • Downloads now validate file size and trigger cleanup of related render assets when mismatched.
  • Bug Fixes

    • Automatically removes stale rendered files when file size changes or force-download occurs.
  • Refactor

    • Internal helper visibility reduced to limit external access.

Part of ENG-836

Signed-off-by: Ramil Garipov <ramil@metriport.com>
Copy link

linear bot commented Aug 11, 2025

Copy link

coderabbitai bot commented Aug 11, 2025

Caution

Review failed

The pull request is closed.

Walkthrough

Reorganizes imports and error handling, adds MTOM/CID and S3 utilities, parses ITI-39 responses via schema, computes decoded document sizes and, on mismatch with S3, deletes rendered HTML/PDF artifacts. Also removes one exported helper and adds a utility to produce render file paths.

Changes

Cohort / File(s) Summary
DR response processing & S3 artifact sync
packages/core/src/external/carequality/ihe-gateway-v2/outbound/xca/process/dr-response.ts
- Reworked imports (individual error handlers, dayjs, IHE types, MTOM/CID utils, S3 helpers).
- Use iti39Schema to parse SOAP/MTOM; coerce documentResponses to array.
- Compute newFileSize from decoded bytes; if it differs from S3 fileInfo.size, log and delete related rendered .html/.pdf via createDocumentRenderFilePaths and S3 utils.
- Added error wrappers (handleEmptyResponse, handleHttpErrorResponse, handleRegistryErrorResponse, handleSchemaErrorResponse) and errorToString.
Document render-path utility
packages/core/src/domain/document/filename.ts
- Added export function createDocumentRenderFilePaths(filePath: string): string[] returning both HTML and PDF render paths; private helper to append .html/.pdf.
CommonWell document download & render cleanup
packages/api/src/external/commonwell/document/document-query.ts
- Added imports for createDocumentRenderFilePaths and S3Utils and instantiated S3Utils for cleanup.
- Removed reliance on getDocToFileFunction and convertToNonExistingS3Info; use getS3Info(validDocs, patient) directly.
- Change download condition to `if (!fileInfo.fileExists
Internalized helper (visibility change)
packages/api/src/command/medical/document/document-query-storage-info.ts
- Removed export from getDocToFileFunction, making it module-internal (no signature or call-site changes within the repo).

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant DRProcessor
  participant IHEGateway
  participant MTOMParser
  participant S3

  Client->>DRProcessor: processDRResponse(request)
  DRProcessor->>IHEGateway: send ITI-39 request
  IHEGateway-->>DRProcessor: SOAP + MTOM response
  DRProcessor->>MTOMParser: parse MTOM & validate (iti39Schema)
  MTOMParser-->>DRProcessor: documentResponses[]
  loop for each document
    DRProcessor->>S3: headObject(document file)
    S3-->>DRProcessor: fileInfo (exists, size)
    DRProcessor->>DRProcessor: decode bytes -> newFileSize
    alt newFileSize != fileInfo.size
      DRProcessor->>S3: deleteFiles(createDocumentRenderFilePaths(fileName))
    end
    DRProcessor->>S3: store/update document file (if needed)
  end
  DRProcessor-->>Client: result or handled error
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~35 minutes

Possibly related PRs


📜 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 b628bc9 and 76acc0e.

📒 Files selected for processing (2)
  • packages/api/src/external/commonwell/document/document-query.ts (5 hunks)
  • packages/core/src/domain/document/filename.ts (2 hunks)
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch eng-836-xml-redownload-needs-to-delete-rendered-files

🪧 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.
  • 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.

Support

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

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 for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for 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.

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: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 385ab9b and a952837.

📒 Files selected for processing (1)
  • packages/core/src/external/carequality/ihe-gateway-v2/outbound/xca/process/dr-response.ts (2 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
**/*.{js,jsx,ts,tsx}

📄 CodeRabbit Inference Engine (.cursorrules)

**/*.{js,jsx,ts,tsx}: 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 const whenever possible
Use async/await instead of .then()
Naming: classes, enums: PascalCase
Naming: constants, variables, functions: camelCase
Naming: file names: kebab-case
Naming: Don’t use negative names, like notEnabled, prefer isDisabled
If possible, use decomposing objects for function parameters
Prefer Nullish Coalesce (??) than the OR operator (||) when you want 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)
While handling errors, keep the stack trace around: if you create a new Error (e.g., MetriportError), make sure to pass the original error as the new one’s cause so the stack trace is available upstream.
max column length is 100 chars
multi-line comments use /** */
top-level comments go after the import (save pre-import to basic file header, like license)
move literals to constants declared after imports when possible

Files:

  • packages/core/src/external/carequality/ihe-gateway-v2/outbound/xca/process/dr-response.ts
**/*.{ts,tsx}

📄 CodeRabbit Inference Engine (.cursorrules)

Use types whenever possible

Files:

  • packages/core/src/external/carequality/ihe-gateway-v2/outbound/xca/process/dr-response.ts
**/*.ts

⚙️ CodeRabbit Configuration File

**/*.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
  • U...

Files:

  • packages/core/src/external/carequality/ihe-gateway-v2/outbound/xca/process/dr-response.ts
🧠 Learnings (3)
📓 Common learnings
Learnt from: RamilGaripov
PR: metriport/metriport#4176
File: packages/fhir-converter/src/lib/handlebars-converter/handlebars-helpers.js:296-320
Timestamp: 2025-07-17T21:24:37.077Z
Learning: RamilGaripov prefers to maintain consistency with existing patterns in a file rather than making isolated fixes to coding guideline violations like var vs const declarations, when the pattern is already established throughout the codebase.
📚 Learning: 2025-05-20T21:26:26.804Z
Learnt from: leite08
PR: metriport/metriport#3814
File: packages/api/src/routes/internal/medical/patient-consolidated.ts:141-174
Timestamp: 2025-05-20T21:26:26.804Z
Learning: The functionality introduced in packages/api/src/routes/internal/medical/patient-consolidated.ts is planned to be refactored in downstream PR #3857, including improvements to error handling and validation.

Applied to files:

  • packages/core/src/external/carequality/ihe-gateway-v2/outbound/xca/process/dr-response.ts
📚 Learning: 2025-06-06T16:45:31.832Z
Learnt from: thomasyopes
PR: metriport/metriport#3970
File: packages/api/src/external/ehr/athenahealth/command/write-back/medication.ts:17-17
Timestamp: 2025-06-06T16:45:31.832Z
Learning: The writeMedicationToChart function in packages/api/src/external/ehr/athenahealth/command/write-back/medication.ts returns a response that is not currently used by any consumers, so changes to its return type are not breaking changes in practice.

Applied to files:

  • packages/core/src/external/carequality/ihe-gateway-v2/outbound/xca/process/dr-response.ts
🧬 Code Graph Analysis (1)
packages/core/src/external/carequality/ihe-gateway-v2/outbound/xca/process/dr-response.ts (2)
packages/core/src/external/sftp/client.ts (1)
  • log (261-263)
packages/mllp-server/src/utils.ts (1)
  • s3Utils (18-18)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (2)
  • GitHub Check: check-pr / lint-build-test
  • GitHub Check: Analyze (javascript)
🔇 Additional comments (1)
packages/core/src/external/carequality/ihe-gateway-v2/outbound/xca/process/dr-response.ts (1)

117-117: deleteFiles handles missing S3 objects gracefully

AWS S3’s deleteObjects API doesn’t error when keys don’t exist, and our deleteFiles wrapper only logs and rethrows on genuine AWS errors. No additional error handling is needed around this call.

Part of ENG-836

Signed-off-by: Ramil Garipov <ramil@metriport.com>
Part of ENG-836

Signed-off-by: Ramil Garipov <ramil@metriport.com>
Part of ENG-836

Signed-off-by: Ramil Garipov <ramil@metriport.com>
Part of ENG-836

Signed-off-by: Ramil Garipov <ramil@metriport.com>
Part of ENG-836

Signed-off-by: Ramil Garipov <ramil@metriport.com>
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: 3

🔭 Outside diff range comments (1)
packages/api/src/command/medical/document/document-query-storage-info.ts (1)

32-48: Make the mapper sync; drop unnecessary async and simplify callers

The returned function does no async work. Making it sync removes Promise churn and lets you simplify getS3Info’s mapping.

-function getDocToFileFunction(patient: Pick<Patient, "cxId" | "id">) {
-  // TODO convert the input from CW Document to a Metriport shape
-  return async (doc: Document): Promise<SimpleFile> => {
+function getDocToFileFunction(patient: Pick<Patient, "cxId" | "id">) {
+  // TODO convert the input from CW Document to a Metriport shape
+  return (doc: Document): SimpleFile => {
     const fileName = createDocumentFilePath(
       patient.cxId,
       patient.id,
       doc.id,
       doc.content?.mimeType
     );
     return {
       docId: doc.id,
       fileName,
       fileLocation: s3BucketName,
       fileContentType: doc.content?.mimeType,
     };
   };
 }

And in getS3Info:

-  const docToFile = getDocToFileFunction(patient);
-  const s3Info = await Promise.allSettled(
-    documents
-      .map(d => docToFile(d))
-      .map(async (filePromise: Promise<SimpleFile>): Promise<S3Info> => {
-        const file = await filePromise;
+  const docToFile = getDocToFileFunction(patient);
+  const s3Info = await Promise.allSettled(
+    documents
+      .map(docToFile)
+      .map(async (file: SimpleFile): Promise<S3Info> => {
         try {
           const {
             exists: fileExists,
             size: fileSize,
             contentType: fileContentType,
           } = await s3Utils.getFileInfoFromS3(file.fileName, file.fileLocation);
           return {
             docId: file.docId,
             fileExists,
             fileSize,
             fileContentType,
             fileName: file.fileName,
             fileLocation: file.fileLocation,
           };
         } catch (error) {
           errors.push({
             error,
             message: String(error),
             docId: file.docId,
           });
           throw error;
         }
       })
   );
🧹 Nitpick comments (2)
packages/api/src/external/commonwell/document/document-query.ts (1)

492-493: Consider reusing a shared S3Utils instance

Minor: instantiate S3Utils at module scope (or reuse a shared instance) to avoid per-call construction and to simplify testing via DI (as done in the Carequality DR flow with setS3UtilsInstance).

packages/core/src/external/carequality/ihe-gateway-v2/outbound/xca/process/dr-response.ts (1)

276-281: Logging: avoid JSON.stringify in logs; use errorToString

Per guidelines, don’t JSON.stringify errors into logs. Use errorToString for a concise, single-line message.

-    log(`Error processing DR response ${JSON.stringify(error)}`);
+    log(`Error processing DR response: ${errorToString(error)}`);
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between a952837 and b628bc9.

📒 Files selected for processing (4)
  • packages/api/src/command/medical/document/document-query-storage-info.ts (1 hunks)
  • packages/api/src/external/commonwell/document/document-query.ts (6 hunks)
  • packages/core/src/domain/document/filename.ts (1 hunks)
  • packages/core/src/external/carequality/ihe-gateway-v2/outbound/xca/process/dr-response.ts (2 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.ts

⚙️ CodeRabbit Configuration File

**/*.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
  • U...

Files:

  • packages/core/src/domain/document/filename.ts
  • packages/api/src/command/medical/document/document-query-storage-info.ts
  • packages/api/src/external/commonwell/document/document-query.ts
  • packages/core/src/external/carequality/ihe-gateway-v2/outbound/xca/process/dr-response.ts
🧬 Code Graph Analysis (2)
packages/api/src/external/commonwell/document/document-query.ts (4)
packages/mllp-server/src/utils.ts (1)
  • s3Utils (18-18)
packages/core/src/external/aws/s3.ts (1)
  • S3Utils (140-570)
packages/api/src/command/medical/document/document-query-storage-info.ts (1)
  • getS3Info (62-109)
packages/core/src/domain/document/filename.ts (1)
  • createDocumentRenderFilePaths (24-28)
packages/core/src/external/carequality/ihe-gateway-v2/outbound/xca/process/dr-response.ts (2)
packages/core/src/domain/document/filename.ts (1)
  • createDocumentRenderFilePaths (24-28)
packages/mllp-server/src/utils.ts (1)
  • s3Utils (18-18)
🔇 Additional comments (1)
packages/api/src/command/medical/document/document-query-storage-info.ts (1)

32-32: No external references to getDocToFileFunction remain
Verified via rg -n --glob '!*node_modules*' '\bgetDocToFileFunction\b' | sed '/document-query-storage-info\.ts/d'—no matches found outside document-query-storage-info.ts.

@RamilGaripov RamilGaripov added this pull request to the merge queue Aug 11, 2025
@RamilGaripov RamilGaripov removed this pull request from the merge queue due to a manual request Aug 11, 2025
Part of ENG-836

Signed-off-by: Ramil Garipov <ramil@metriport.com>
Part of ENG-836

Signed-off-by: Ramil Garipov <ramil@metriport.com>
@RamilGaripov RamilGaripov added this pull request to the merge queue Aug 11, 2025
Merged via the queue into develop with commit d13ee7b Aug 11, 2025
14 of 15 checks passed
@RamilGaripov RamilGaripov deleted the eng-836-xml-redownload-needs-to-delete-rendered-files branch August 11, 2025 23:24
@RamilGaripov RamilGaripov mentioned this pull request Aug 11, 2025
1 task
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