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

fix(service-portal): Change download for pdf docs inbox #16398

Merged
merged 2 commits into from
Oct 15, 2024

Conversation

thordurhhh
Copy link
Member

@thordurhhh thordurhhh commented Oct 15, 2024

What

Change download for pdf docs inbox

Why

Current approach is blocked by some browsers. Use download-service instead when possible.

Checklist:

  • I have performed a self-review of my own code
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • Formatting passes locally with my changes
  • I have rebased against main before asking for a review

Summary by CodeRabbit

  • New Features

    • Enhanced PDF download functionality based on user action (download vs. inline).
    • Improved document download interface with distinct buttons for HTML and PDF documents.
  • Bug Fixes

    • Resolved issues with download links and improved handling of document types.
  • Documentation

    • Updated method signatures to reflect new parameters for better clarity.
  • Style

    • Minor adjustments to the formatting and organization of the code for improved readability.

@thordurhhh thordurhhh added the deprecated:automerge (Disabled) Merge this PR as soon as all checks pass label Oct 15, 2024
@thordurhhh thordurhhh requested a review from a team as a code owner October 15, 2024 11:42
Copy link
Contributor

coderabbitai bot commented Oct 15, 2024

Walkthrough

The changes in this pull request involve updates to the DocumentController class, specifically the getPdf method, which now accepts an additional action parameter from the query string to determine how PDF documents are served. Additionally, modifications were made to the DocumentActionBar component to enhance the download functionality for both HTML and PDF documents. The downloadFile function in the utility file has also been updated to accommodate an optional query parameter, allowing for more flexible URL construction during document downloads.

Changes

File Change Summary
apps/download-service/src/app/modules/documents/document.controller.ts Updated getPdf method to include action parameter from query string; modified Content-Disposition header logic.
libs/service-portal/documents/src/components/DocumentActionBar/DocumentActionBarV2.tsx Refactored document download functionality to separate handling for HTML and PDF documents; enhanced UI with consistent buttons.
libs/service-portal/documents/src/utils/downloadDocumentV2.ts Updated downloadFile function to add optional query parameter for URL construction; modified form action assignment.

Possibly related PRs

Suggested reviewers

  • oddsson

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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

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
Contributor

@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

🧹 Outside diff range and nitpick comments (6)
apps/download-service/src/app/modules/documents/document.controller.ts (2)

43-43: LGTM: Method signature updated correctly

The addition of the @Query('action') action: string parameter to the getPdf method is appropriate for implementing the new download behavior. This change aligns with the PR objectives and maintains type safety.

Consider adding a default value or making the action parameter optional for backward compatibility:

@Query('action') action?: string = 'inline'

This would ensure that existing calls to the endpoint without the action query parameter continue to work as before.


66-68: LGTM: Content-Disposition header set correctly based on action

The conditional setting of the Content-Disposition header based on the action parameter effectively implements the desired download behavior. This change addresses the PR objectives and improves compatibility across different browsers.

For improved readability, consider extracting the ternary operation into a separate constant:

const disposition = action === 'download' ? 'attachment' : 'inline';
res.header(
  'Content-Disposition',
  `${disposition}; filename=${rawDocumentDTO.fileName}.pdf`
);

This minor refactoring would make the code slightly more readable without changing its functionality.

libs/service-portal/documents/src/utils/downloadDocumentV2.ts (1)

40-40: LGTM! Consider adding type annotation for query.

The changes to the downloadFile function effectively implement the new download method for PDF documents as per the PR objectives. The optional query parameter allows for flexible URL construction, maintaining backwards compatibility.

Consider adding a type annotation for the query parameter to improve code clarity:

export const downloadFile = async (
  doc: ActiveDocumentType2,
  userInfo: User,
- query?: string,
+ query?: string | undefined,
) => {

Also applies to: 68-69, 72-72

libs/service-portal/documents/src/components/DocumentActionBar/DocumentActionBarV2.tsx (3)

121-139: LGTM! Consider enhancing accessibility.

The implementation for downloading HTML documents is well-structured and follows React best practices. It adheres to the coding guidelines for reusability and TypeScript usage.

To improve accessibility, consider adding an aria-label to the Button component:

 <Button
   as="span"
   unfocusable
   circle
   icon="download"
   iconType="outline"
   size="medium"
   colorScheme="light"
+  aria-label={formatMessage(m.download)}
 />

This will provide more context for screen readers when focusing on the button.

🧰 Tools
🪛 Biome

[error] 121-121: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)


140-153: LGTM! Consider enhancing consistency.

The implementation for downloading PDF documents aligns well with the PR objectives and follows React best practices. It adheres to the coding guidelines and uses the downloadFile function, which should improve browser compatibility as mentioned in the PR summary.

For consistency with the HTML download button, consider adding an aria-label to this Button as well:

 <Button
   circle
   icon="download"
   iconType={'outline'}
   onClick={() =>
     downloadFile(activeDocument, userInfo, 'download')
   }
   size="medium"
   colorScheme="light"
+  aria-label={formatMessage(m.download)}
 />

This will ensure both download buttons have consistent accessibility features.


121-154: Great job on restructuring! Consider using optional chaining.

The restructuring of the download functionality using a ternary operator improves code readability and follows React best practices. The separation of HTML and PDF download handling is clear and maintainable.

Consider using optional chaining as suggested by the static analysis tool:

- {activeDocument && activeDocument.document.value ? (
+ {activeDocument?.document.value ? (

This change will make the code more concise and potentially prevent runtime errors if activeDocument is unexpectedly null or undefined.

🧰 Tools
🪛 Biome

[error] 121-121: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 2a7cb5b and 75c2025.

📒 Files selected for processing (3)
  • apps/download-service/src/app/modules/documents/document.controller.ts (3 hunks)
  • libs/service-portal/documents/src/components/DocumentActionBar/DocumentActionBarV2.tsx (1 hunks)
  • libs/service-portal/documents/src/utils/downloadDocumentV2.ts (2 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
apps/download-service/src/app/modules/documents/document.controller.ts (1)

Pattern apps/**/*: "Confirm that the code adheres to the following:

  • NextJS best practices, including file structure, API routes, and static generation methods.
  • Efficient state management and server-side rendering techniques.
  • Optimal use of TypeScript for component and utility type safety."
libs/service-portal/documents/src/components/DocumentActionBar/DocumentActionBarV2.tsx (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
libs/service-portal/documents/src/utils/downloadDocumentV2.ts (1)

Pattern libs/**/*: "Confirm that the code adheres to the following:

  • Reusability of components and hooks across different NextJS apps.
  • TypeScript usage for defining props and exporting types.
  • Effective tree-shaking and bundling practices."
🪛 Biome
libs/service-portal/documents/src/components/DocumentActionBar/DocumentActionBarV2.tsx

[error] 121-121: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

🔇 Additional comments (4)
apps/download-service/src/app/modules/documents/document.controller.ts (2)

11-19: LGTM: Import statement updated correctly

The addition of Query to the import statement from '@nestjs/common' is appropriate for handling the new query parameter in the getPdf method. This change aligns with NestJS best practices for parameter decoration.


Line range hint 1-77: Summary: Changes effectively implement new download behavior

The modifications to the DocumentController class, particularly in the getPdf method, successfully implement the new download behavior for PDF documents. The changes include:

  1. Adding a new action query parameter to control the download behavior.
  2. Conditionally setting the Content-Disposition header based on the action parameter.

These changes align well with the PR objectives and should improve compatibility across different web browsers. The implementation maintains good TypeScript practices and follows NestJS conventions.

To ensure that these changes don't introduce any unintended side effects, please run the following verification script:

This script will help identify any potential areas in the codebase that might need to be updated to work with the new action parameter.

libs/service-portal/documents/src/utils/downloadDocumentV2.ts (1)

Line range hint 1-89: Compliance with coding guidelines confirmed.

The code in this file adheres to the specified guidelines for libs/**/*:

  1. The downloadFile function is exported, promoting reusability across different NextJS apps.
  2. TypeScript is used effectively for defining types and exporting functions.
  3. No issues with tree-shaking or bundling practices are apparent.
libs/service-portal/documents/src/components/DocumentActionBar/DocumentActionBarV2.tsx (1)

Line range hint 1-154: Overall, excellent work on improving the download functionality!

The changes in this file align well with the PR objectives and coding guidelines. The separation of HTML and PDF download handling improves maintainability and browser compatibility. The use of TypeScript and reusable components adheres to the project's standards.

A few minor suggestions have been made to further enhance accessibility and code safety. Great job on this update!

🧰 Tools
🪛 Biome

[error] 121-121: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

Copy link

codecov bot commented Oct 15, 2024

Codecov Report

Attention: Patch coverage is 33.33333% with 2 lines in your changes missing coverage. Please review.

Project coverage is 36.75%. Comparing base (2a7cb5b) to head (c45a7d4).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...e/src/app/modules/documents/document.controller.ts 33.33% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #16398      +/-   ##
==========================================
- Coverage   36.75%   36.75%   -0.01%     
==========================================
  Files        6808     6808              
  Lines      140991   140993       +2     
  Branches    40156    40159       +3     
==========================================
  Hits        51817    51817              
- Misses      89174    89176       +2     
Flag Coverage Δ
air-discount-scheme-backend 54.06% <ø> (ø)
air-discount-scheme-web 0.00% <ø> (ø)
api 3.37% <ø> (ø)
api-domains-air-discount-scheme 36.93% <ø> (ø)
api-domains-assets 26.71% <ø> (ø)
api-domains-auth-admin 48.48% <ø> (ø)
api-domains-communications 39.91% <ø> (ø)
api-domains-criminal-record 48.00% <ø> (ø)
api-domains-driving-license 44.40% <ø> (ø)
api-domains-education 31.51% <ø> (ø)
api-domains-health-insurance 34.77% <ø> (ø)
api-domains-mortgage-certificate 34.95% <ø> (ø)
api-domains-payment-schedule 41.16% <ø> (ø)
application-api-files 58.02% <ø> (ø)
application-core 71.85% <ø> (+0.32%) ⬆️
application-system-api 41.42% <ø> (ø)
application-template-api-modules 27.88% <ø> (-0.02%) ⬇️
application-templates-accident-notification 29.29% <ø> (ø)
application-templates-car-recycling 3.12% <ø> (ø)
application-templates-criminal-record 26.35% <ø> (ø)
application-templates-driving-license 18.29% <ø> (ø)
application-templates-estate 12.31% <ø> (ø)
application-templates-example-payment 25.14% <ø> (ø)
application-templates-financial-aid 14.27% <ø> (ø)
application-templates-general-petition 23.43% <ø> (ø)
application-templates-inheritance-report 6.43% <ø> (ø)
application-templates-marriage-conditions 15.09% <ø> (ø)
application-templates-mortgage-certificate 43.78% <ø> (ø)
application-templates-parental-leave 29.85% <ø> (-0.13%) ⬇️
application-types 6.69% <ø> (ø)
application-ui-components 1.28% <ø> (ø)
application-ui-shell 21.35% <ø> (ø)
clients-charge-fjs-v2 24.11% <ø> (ø)
clients-driving-license 40.67% <ø> (ø)
clients-driving-license-book 43.80% <ø> (ø)
clients-financial-statements-inao 49.32% <ø> (ø)
clients-license-client 1.83% <ø> (ø)
clients-regulations 42.80% <ø> (ø)
clients-rsk-company-registry 29.76% <ø> (ø)
clients-rsk-personal-tax-return 38.00% <ø> (ø)
clients-smartsolutions 12.77% <ø> (ø)
clients-syslumenn 49.44% <ø> (ø)
cms 0.43% <ø> (ø)
cms-translations 39.04% <ø> (ø)
dokobit-signing 63.38% <ø> (ø)
download-service 44.19% <33.33%> (-0.03%) ⬇️
email-service 61.13% <ø> (ø)
feature-flags 91.11% <ø> (ø)
file-storage 53.71% <ø> (ø)
financial-aid-backend 56.40% <ø> (ø)
judicial-system-api 18.39% <ø> (ø)
judicial-system-audit-trail 69.35% <ø> (ø)
judicial-system-backend 55.19% <ø> (ø)
judicial-system-message 67.24% <ø> (ø)
judicial-system-message-handler 48.35% <ø> (ø)
judicial-system-scheduler 69.54% <ø> (ø)
license-api 42.67% <ø> (-0.08%) ⬇️
nest-config 78.44% <ø> (ø)
nest-feature-flags 51.52% <ø> (ø)
nova-sms 62.74% <ø> (ø)
portals-admin-regulations-admin 1.85% <ø> (ø)
portals-core 16.15% <ø> (ø)
services-auth-admin-api 51.89% <ø> (ø)
services-auth-delegation-api 57.32% <ø> (ø)
services-auth-ids-api 51.42% <ø> (ø)
services-auth-personal-representative 45.13% <ø> (-0.03%) ⬇️
services-auth-personal-representative-public 41.28% <ø> (+0.04%) ⬆️
services-auth-public-api 48.91% <ø> (ø)
services-endorsements-api 54.19% <ø> (ø)
services-sessions 65.37% <ø> (ø)
services-university-gateway 48.31% <ø> (ø)
services-user-notification 47.02% <ø> (ø)
services-user-profile 62.10% <ø> (ø)
web 1.82% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...e/src/app/modules/documents/document.controller.ts 48.00% <33.33%> (-4.18%) ⬇️

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2a7cb5b...c45a7d4. Read the comment docs.

@datadog-island-is
Copy link

Datadog Report

Branch report: service-portal/download-inbox-pdf
Commit report: e17d988
Test service: download-service

✅ 0 Failed, 1 Passed, 0 Skipped, 18.38s Total Time
➡️ Test Sessions change in coverage: 1 no change

@kodiakhq kodiakhq bot merged commit a4a05f8 into main Oct 15, 2024
202 checks passed
@kodiakhq kodiakhq bot deleted the service-portal/download-inbox-pdf branch October 15, 2024 12:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deprecated:automerge (Disabled) Merge this PR as soon as all checks pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants