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(transfer-of-machine-ownership): seller cant be buyer #16130

Merged
merged 3 commits into from
Sep 25, 2024

Conversation

sigruntg
Copy link
Member

@sigruntg sigruntg commented Sep 24, 2024

Buyer can't be seller

Attach a link to issue if relevant

What

Stop user if they put same nationalId for buyer and seller.

Why

Specify why you need to achieve this

Screenshots / Gifs

image

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

    • Introduced a validation component to check if the seller and buyer have the same national ID during form submission.
    • Added alert messages in the transfer form to notify users if the seller and buyer share the same national ID.
    • Expanded the transfer form with custom fields for enhanced user interaction.
  • Documentation

    • Updated message entries for buyer alerts to improve user guidance during the ownership transfer process.

@sigruntg sigruntg added the automerge Merge this PR as soon as all checks pass label Sep 24, 2024
@sigruntg sigruntg requested a review from a team as a code owner September 24, 2024 10:26
Copy link
Contributor

coderabbitai bot commented Sep 24, 2024

Walkthrough

The changes introduce a new React component, StopBuyerIfSameAsSeller, which validates whether the buyer and seller share the same national ID before form submission. This validation is integrated into the TransferOfMachineOwnershipForm, which now includes alert messages and custom fields related to this check. Additionally, utility functions for national ID comparison and new message entries for alerts are added to enhance user feedback during the ownership transfer process.

Changes

Files Change Summary
libs/application/templates/aosh/transfer-of-machine-ownership/src/fields/StopBuyerIfSameAsSeller/index.tsx Added a new React functional component StopBuyerIfSameAsSeller that validates national IDs of buyer and seller before form submission.
libs/application/templates/aosh/transfer-of-machine-ownership/src/fields/index.ts Exported the StopBuyerIfSameAsSeller component to make it publicly accessible.
libs/application/templates/aosh/transfer-of-machine-ownership/src/forms/TransferOfMachineOwnershipForm/InformationSection/buyerSubSection.ts Added new fields for alert messages and custom handling in the buyerSubSection, including conditions based on national ID validation.
libs/application/templates/aosh/transfer-of-machine-ownership/src/lib/messages/information.ts Introduced new message entries for alert titles and messages related to the buyer's national ID validation.
libs/application/templates/aosh/transfer-of-machine-ownership/src/utils/doSellerAndBuyerHaveSameNationalId.ts Created a utility function doSellerAndBuyerHaveSameNationalId to compare the national IDs of the buyer and seller.
libs/application/templates/aosh/transfer-of-machine-ownership/src/utils/index.ts Exported the doSellerAndBuyerHaveSameNationalId utility function for external access.

Possibly related PRs

Suggested labels

high priority

Suggested reviewers

  • Toti91
  • helgifr
  • baering

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)
libs/application/templates/aosh/transfer-of-machine-ownership/src/utils/doSellerAndBuyerHaveSameNationalId.ts (2)

4-4: Consider explicitly defining the return type.

While the function signature is generally good, explicitly defining the return type would improve type safety and code readability. This aligns with the TypeScript usage guideline for files in the libs directory.

Consider updating the function signature as follows:

export const doSellerAndBuyerHaveSameNationalId = (answers: FormValue): boolean => {

5-17: LGTM: Implementation is correct, but consider adding input validation.

The function correctly retrieves and compares the national IDs of the buyer and seller. It aligns with the PR objective of preventing a user from being both the buyer and seller. However, consider adding input validation to ensure the national IDs are in the expected format before comparison.

You could enhance the function by adding a validation step:

import { getValueViaPath } from '@island.is/application/core'
import { FormValue } from '@island.is/application/types'

const isValidNationalId = (id: string): boolean => {
  // Implement validation logic for Icelandic national IDs
  // For example, check length, format, or use a regex pattern
  return /^[0-9]{6}-?[0-9]{4}$/.test(id);
}

export const doSellerAndBuyerHaveSameNationalId = (answers: FormValue): boolean => {
  const buyerNationalId = getValueViaPath(answers, 'buyer.nationalId', '') as string
  const sellerNationalId = getValueViaPath(answers, 'seller.nationalId', '') as string

  if (!isValidNationalId(buyerNationalId) || !isValidNationalId(sellerNationalId)) {
    return false; // or throw an error, depending on your error handling strategy
  }

  return buyerNationalId === sellerNationalId
}

This enhancement would improve the robustness of the function and prevent false positives due to invalid input.

libs/application/templates/aosh/transfer-of-machine-ownership/src/fields/StopBuyerIfSameAsSeller/index.tsx (2)

5-9: Component declaration follows TypeScript best practices.

The use of FC with explicit prop types (FieldBaseProps) is excellent. However, consider using the more modern approach of declaring functional components:

export const StopBuyerIfSameAsSeller = ({
  application,
  setBeforeSubmitCallback,
}: React.PropsWithChildren<FieldBaseProps>) => {
  // ...
}

This approach is more explicit about which props are actually used and provides better type inference.


10-17: Logic effectively implements the required validation.

The component successfully prevents the same user from being both buyer and seller, aligning with the PR objective. However, consider the following improvements:

  1. Instead of an empty string, provide a meaningful error message when IDs match:
return [false, 'Buyer and seller cannot be the same person']
  1. Consider adding a comment explaining the return value structure [boolean, string | null] for better maintainability.

  2. The empty fragment return <></> could be replaced with null since no UI is rendered:

return null

These changes would enhance clarity and maintainability without altering the core functionality.

libs/application/templates/aosh/transfer-of-machine-ownership/src/forms/TransferOfMachineOwnershipForm/InformationSection/buyerSubSection.ts (1)

41-48: LGTM: Alert message field effectively addresses the PR objective.

The new alert message field successfully implements the requirement to warn users when the buyer and seller have the same national ID. It uses TypeScript typing and follows good UX practices by displaying the alert conditionally.

Consider extracting the condition function to improve readability:

const isSameNationalId = (answer: FormValue) => doSellerAndBuyerHaveSameNationalId(answer);

buildAlertMessageField({
  // ...other properties
  condition: isSameNationalId,
}),
libs/application/templates/aosh/transfer-of-machine-ownership/src/lib/messages/information.ts (1)

269-279: LGTM! Consider a minor improvement for consistency.

The new messages alertTitle and alertMessage are well-structured and directly address the PR objective of preventing a user from being both the buyer and seller. They follow the existing pattern in the file and are consistent with the coding guidelines.

For improved consistency with other message descriptions in the file, consider updating the description fields to be more specific:

  alertTitle: {
    id: 'aosh.tmo.application:information.labels.buyer.alertTitle',
    defaultMessage: 'Kennitala sú sama og hjá seljanda',
-   description: `Buyer alert title`,
+   description: 'Title for alert when buyer and seller have the same national ID',
  },
  alertMessage: {
    id: 'aosh.tmo.application:information.labels.buyer.alertMessage',
    defaultMessage:
      'Seljandi og kaupandi getur ekki verið sá sami. Vinsamlega skráðu nýja kennitölu.',
-   description: `Buyer alert message`,
+   description: 'Message for alert when buyer and seller have the same national ID',
  },
Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between c5d69fd and a8a5206.

Files selected for processing (6)
  • libs/application/templates/aosh/transfer-of-machine-ownership/src/fields/StopBuyerIfSameAsSeller/index.tsx (1 hunks)
  • libs/application/templates/aosh/transfer-of-machine-ownership/src/fields/index.ts (1 hunks)
  • libs/application/templates/aosh/transfer-of-machine-ownership/src/forms/TransferOfMachineOwnershipForm/InformationSection/buyerSubSection.ts (2 hunks)
  • libs/application/templates/aosh/transfer-of-machine-ownership/src/lib/messages/information.ts (1 hunks)
  • libs/application/templates/aosh/transfer-of-machine-ownership/src/utils/doSellerAndBuyerHaveSameNationalId.ts (1 hunks)
  • libs/application/templates/aosh/transfer-of-machine-ownership/src/utils/index.ts (1 hunks)
Additional context used
Path-based instructions (6)
libs/application/templates/aosh/transfer-of-machine-ownership/src/fields/StopBuyerIfSameAsSeller/index.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/application/templates/aosh/transfer-of-machine-ownership/src/fields/index.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."
libs/application/templates/aosh/transfer-of-machine-ownership/src/forms/TransferOfMachineOwnershipForm/InformationSection/buyerSubSection.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."
libs/application/templates/aosh/transfer-of-machine-ownership/src/lib/messages/information.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."
libs/application/templates/aosh/transfer-of-machine-ownership/src/utils/doSellerAndBuyerHaveSameNationalId.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."
libs/application/templates/aosh/transfer-of-machine-ownership/src/utils/index.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."
Additional comments not posted (10)
libs/application/templates/aosh/transfer-of-machine-ownership/src/fields/index.ts (1)

4-4: LGTM: New export aligns with PR objectives and coding guidelines.

The addition of StopBuyerIfSameAsSeller export is appropriate and aligns well with the PR's objective of preventing a user from being both the buyer and seller in a transaction. This named export also adheres to the coding guidelines for libs/**/* files:

  1. It promotes reusability by making the component available across different NextJS apps.
  2. The naming suggests it's likely a TypeScript component, which is good for type safety.
  3. The named export allows for effective tree-shaking and bundling.
libs/application/templates/aosh/transfer-of-machine-ownership/src/utils/doSellerAndBuyerHaveSameNationalId.ts (2)

1-2: LGTM: Imports are appropriate and follow best practices.

The imports are specific and relevant to the function's requirements. Using named imports facilitates effective tree-shaking, which aligns with the coding guidelines for files in the libs directory.


1-17: Overall assessment: The implementation effectively addresses the PR objective.

This utility function successfully prevents a user from being both the buyer and seller by comparing their national IDs. The implementation adheres to the coding guidelines for the libs directory:

  1. It's reusable across different NextJS apps.
  2. It uses TypeScript for defining props.
  3. The specific imports support effective tree-shaking.

The function achieves its purpose while maintaining good coding practices. With the suggested improvements (explicit return type and input validation), it will be even more robust and type-safe.

libs/application/templates/aosh/transfer-of-machine-ownership/src/fields/StopBuyerIfSameAsSeller/index.tsx (2)

1-3: Imports look good and follow best practices.

The imports are well-structured, using named imports for better tree-shaking. The separation of the utility function doSellerAndBuyerHaveSameNationalId into a separate file promotes modularity and reusability.


1-18: Component adheres to coding guidelines and best practices.

The StopBuyerIfSameAsSeller component is well-structured, reusable, and follows the coding guidelines for libs/**/* files:

  1. It's potentially reusable across different NextJS apps.
  2. TypeScript is effectively used for defining props and types.
  3. The component structure supports effective tree-shaking and bundling.

Great job on creating a focused, single-responsibility component that enhances the application's validation logic.

libs/application/templates/aosh/transfer-of-machine-ownership/src/utils/index.ts (2)

Line range hint 1-18: Overall changes look good, but ensure thorough testing of the new functionality.

The addition of the doSellerAndBuyerHaveSameNationalId export aligns with the PR objective of preventing a user from being both the buyer and seller in a transaction. This change enhances the module's functionality and promotes reusability across the application.

To ensure the new function works as intended and doesn't introduce any regressions:

  1. Verify the implementation of doSellerAndBuyerHaveSameNationalId:
#!/bin/bash
# Description: Check the implementation of the new function

echo "Displaying the content of doSellerAndBuyerHaveSameNationalId.ts:"
cat ./libs/application/templates/aosh/transfer-of-machine-ownership/src/utils/doSellerAndBuyerHaveSameNationalId.ts
  1. Ensure proper test coverage:
#!/bin/bash
# Description: Check for test files related to the new function

echo "Searching for test files related to doSellerAndBuyerHaveSameNationalId:"
fd --type file --extension spec.ts doSellerAndBuyerHaveSameNationalId ./libs/application/templates/aosh/transfer-of-machine-ownership/src/utils/

Please review the implementation and ensure comprehensive test coverage for this new functionality.


18-18: New utility function export looks good, but consider bundling impact.

The addition of the doSellerAndBuyerHaveSameNationalId export aligns well with the existing utility functions in this module. It appears to be a relevant addition for the transfer-of-machine-ownership template.

To ensure this addition doesn't negatively impact tree-shaking and bundling, let's verify its usage:

This will help us understand if the function is being used efficiently and allow for proper tree-shaking.

Verification successful

Function usage verified with no bundling concerns detected.

The doSellerAndBuyerHaveSameNationalId function is properly imported and utilized in the following files:

  • ./libs/application/templates/aosh/transfer-of-machine-ownership/src/fields/StopBuyerIfSameAsSeller/index.tsx
  • ./libs/application/templates/aosh/transfer-of-machine-ownership/src/forms/TransferOfMachineOwnershipForm/InformationSection/buyerSubSection.ts

This affirms that the addition aligns with the module's usage patterns and supports effective tree-shaking.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check usage of the new utility function across the codebase

# Search for imports of the new function
echo "Searching for imports of doSellerAndBuyerHaveSameNationalId:"
rg --type typescript "import.*doSellerAndBuyerHaveSameNationalId.*from.*utils" ./libs/application/templates/aosh/transfer-of-machine-ownership/

# Search for direct usage of the function
echo "Searching for usage of doSellerAndBuyerHaveSameNationalId:"
rg --type typescript "doSellerAndBuyerHaveSameNationalId\(" ./libs/application/templates/aosh/transfer-of-machine-ownership/

Length of output: 802


Script:

#!/bin/bash
# Description: Check usage of the new utility function across the codebase

# Search for imports of the new function
echo "Searching for imports of doSellerAndBuyerHaveSameNationalId:"
rg --type typescript "import.*doSellerAndBuyerHaveSameNationalId.*from.*utils" ./libs/application/templates/aosh/transfer-of-machine-ownership/

# Search for direct usage of the function
echo "Searching for usage of doSellerAndBuyerHaveSameNationalId:"
rg --type typescript "doSellerAndBuyerHaveSameNationalId\(" ./libs/application/templates/aosh/transfer-of-machine-ownership/

Length of output: 600


Script:

#!/bin/bash
# Description: Check usage of the new utility function across the codebase

# Search for imports of the new function
echo "Searching for imports of doSellerAndBuyerHaveSameNationalId:"
rg "import.*doSellerAndBuyerHaveSameNationalId.*from.*utils" ./libs/application/templates/aosh/transfer-of-machine-ownership/ -g "*.ts" -g "*.tsx"

# Search for direct usage of the function
echo "Searching for usage of doSellerAndBuyerHaveSameNationalId:"
rg "doSellerAndBuyerHaveSameNationalId\(" ./libs/application/templates/aosh/transfer-of-machine-ownership/ -g "*.ts" -g "*.tsx"

Length of output: 1296

libs/application/templates/aosh/transfer-of-machine-ownership/src/forms/TransferOfMachineOwnershipForm/InformationSection/buyerSubSection.ts (3)

2-3: LGTM: New imports are appropriate and follow best practices.

The new imports are correctly added and align with the changes made in the file. They follow TypeScript practices by importing types and utilize the application core, which promotes reusability across different NextJS apps.

Also applies to: 11-12


Line range hint 1-57: Confirms compliance with TypeScript and reusability guidelines.

The code effectively uses TypeScript for type safety, as seen in the import statements and function parameters. The use of core components from '@island.is/application/core' and the library structure (libs/) promote reusability across different NextJS apps, aligning with the coding guidelines.


49-53: LGTM: Custom field added to handle buyer-seller validation.

The new custom field with the 'StopBuyerIfSameAsSeller' component appears to address the PR objective of preventing the same user from being both the buyer and seller.

Could you provide more information about the 'StopBuyerIfSameAsSeller' component? It would be helpful to understand its implementation and how it interacts with the form submission process.

Copy link

codecov bot commented Sep 24, 2024

Codecov Report

Attention: Patch coverage is 50.00000% with 3 lines in your changes missing coverage. Please review.

Project coverage is 36.68%. Comparing base (1ea2e20) to head (3b958a6).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...ip/src/utils/doSellerAndBuyerHaveSameNationalId.ts 40.00% 3 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@            Coverage Diff             @@
##             main   #16130      +/-   ##
==========================================
- Coverage   36.71%   36.68%   -0.04%     
==========================================
  Files        6764     6763       -1     
  Lines      139140   139032     -108     
  Branches    39531    39512      -19     
==========================================
- Hits        51092    51003      -89     
+ Misses      88048    88029      -19     
Flag Coverage Δ
api 3.39% <ø> (ø)
application-system-api 41.56% <50.00%> (+<0.01%) ⬆️
application-template-api-modules 23.62% <ø> (+0.03%) ⬆️
application-ui-shell 21.29% <ø> (ø)
judicial-system-backend 54.86% <ø> (+<0.01%) ⬆️
judicial-system-web 28.57% <ø> (ø)

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

Files with missing lines Coverage Δ
...-machine-ownership/src/lib/messages/information.ts 100.00% <ø> (ø)
...h/transfer-of-machine-ownership/src/utils/index.ts 57.89% <100.00%> (+2.33%) ⬆️
...ip/src/utils/doSellerAndBuyerHaveSameNationalId.ts 40.00% <40.00%> (ø)

... and 20 files with indirect coverage changes


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 1ea2e20...3b958a6. Read the comment docs.

@datadog-island-is
Copy link

Datadog Report

All test runs 86ea2db 🔗

4 Total Test Services: 0 Failed, 4 Passed
🔻 Test Sessions change in coverage: 2 decreased, 6 no change

Test Services
Service Name Failed Known Flaky New Flaky Passed Skipped Total Time Code Coverage Change Test Service View
api 0 0 0 4 0 2.78s 1 no change Link
application-system-api 0 0 0 111 2 2m 54.31s 1 decreased (-0.05%) Link
application-template-api-modules 0 0 0 109 0 2m 4.11s 1 decreased (-0.12%) Link
application-ui-shell 0 0 0 74 0 40.17s 1 no change Link

🔻 Code Coverage Decreases vs Default Branch (2)

  • application-template-api-modules - jest 25.57% (-0.12%) - Details
  • application-system-api - jest 37.08% (-0.05%) - Details

@kodiakhq kodiakhq bot merged commit 23d2dbd into main Sep 25, 2024
42 checks passed
@kodiakhq kodiakhq bot deleted the fix/transfer-of-machine-ownership-seller-cant-be-buyer branch September 25, 2024 08:07
thoreyjona pushed a commit that referenced this pull request Oct 2, 2024
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
automerge Merge this PR as soon as all checks pass
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants