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

Use highest status instead of newest for legacy #509

Merged
merged 3 commits into from
Nov 20, 2024
Merged

Conversation

Andreass2
Copy link
Collaborator

@Andreass2 Andreass2 commented Nov 20, 2024

Description

  • Use highest status instead of newest status for legacy requests

Related Issue(s)

Verification

  • Your code builds clean without any errors or warnings
  • Manual testing done (required)
  • Relevant automated test added (if you find this hard, leave it and we'll help out)
  • All tests run green

Documentation

  • User documentation is updated with a separate linked PR in altinn-studio-docs. (if applicable)

Summary by CodeRabbit

  • New Features

    • Introduced new methods for retrieving the highest status of correspondences, enhancing status evaluation capabilities.
  • Bug Fixes

    • Updated logic for fetching the latest status of correspondences to improve accuracy in status retrieval.
  • Documentation

    • Adjusted method signatures and descriptions to reflect changes in status retrieval logic.

Copy link
Contributor

coderabbitai bot commented Nov 20, 2024

📝 Walkthrough
📝 Walkthrough

Walkthrough

This pull request introduces modifications to the Process methods in two handler classes, LegacyGetCorrespondencesHandler and LegacyGetCorrespondenceOverviewHandler, changing how correspondence statuses are retrieved. The methods now utilize new helper methods, GetHighestStatus and GetHighestStatusWithoutPurged, added to the CorrespondenceStatusExtensions class. Additionally, adjustments are made to the IncludeByStatuses method within QueryExtensions, altering the criteria for filtering statuses. The overall structure and error handling of the methods remain unchanged.

Changes

File Path Change Summary
src/Altinn.Correspondence.Application/GetCorespondences/...Handler.cs Updated Process method to use GetHighestStatusWithoutPurged() for status retrieval.
src/Altinn.Correspondence.Application/GetCorrespondenceOverview/...Handler.cs Updated Process method to use GetHighestStatus() for status retrieval; method signature modified.
src/Altinn.Correspondence.Application/Helpers/...Extensions.cs Added GetHighestStatus and GetHighestStatusWithoutPurged methods to retrieve statuses based on different criteria.
src/Altinn.Correspondence.Persistence/Helpers/...QueryableExtensions.cs Modified IncludeByStatuses method to order statuses by Status instead of StatusChanged.

Possibly related PRs

Suggested labels

kind/feature

Suggested reviewers

  • CelineTrammi

Warning

Rate limit exceeded

@Andreass2 has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 13 minutes and 10 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 8b1a44f and 0ad017a.


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.

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.

@Andreass2 Andreass2 added kind/feature-request New feature or request ignore-for-release pull request wont be included in release notes labels Nov 20, 2024
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: 4

🧹 Outside diff range and nitpick comments (4)
src/Altinn.Correspondence.Application/Helpers/CorrespondenceExtensions.cs (1)

27-33: LGTM! Consider adding XML documentation.

The implementation correctly combines the purge check with the status ordering logic.

Consider adding XML documentation to describe the method's behavior and return value:

+    /// <summary>
+    /// Gets the highest status that is not purged or fetched from the correspondence.
+    /// </summary>
+    /// <param name="correspondence">The correspondence entity.</param>
+    /// <returns>The highest status entity that is not purged or fetched, or null if no such status exists.</returns>
     public static CorrespondenceStatusEntity? GetHighestStatusWithoutPurged(this CorrespondenceEntity correspondence)
src/Altinn.Correspondence.Persistence/Helpers/QueryableExtensions.cs (2)

Line range hint 71-89: Update comments to reflect the new status selection logic

The comments stating "Include correspondences with active status" are now misleading as the method uses highest status rather than most recent status.

Consider updating the comments to:

- if (includeActive) // Include correspondences with active status
+ if (includeActive) // Include correspondences where highest status is active

- if (includeArchived) // Include correspondences with active status
+ if (includeArchived) // Include correspondences where highest status is archived

- if (includePurged) // Include correspondences with active status
+ if (includePurged) // Include correspondences where highest status is purged

Line range hint 64-64: Consider renaming method to reflect new behavior

The method name IncludeByStatuses doesn't indicate whether it uses the newest or highest status for filtering.

Consider renaming to be more explicit:

- public static IQueryable<CorrespondenceEntity> IncludeByStatuses
+ public static IQueryable<CorrespondenceEntity> IncludeByHighestStatuses
src/Altinn.Correspondence.Application/GetCorrespondenceOverview/LegacyGetCorrespondenceOverviewHandler.cs (1)

Line range hint 59-67: Consider documenting status precedence rules

The logic now depends on status hierarchy rather than timestamp. This change in behavior should be documented:

  1. For maintainers: Add code comments explaining the status precedence rules
  2. For users: Update API documentation to reflect this change in status determination
+        // Status precedence: The status hierarchy determines which status is returned,
+        // rather than using the most recent status. This ensures consistent status
+        // representation for legacy requests.
         var latestStatus = correspondence.GetHighestStatus();
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between ef77efa and b2dce8c.

📒 Files selected for processing (4)
  • src/Altinn.Correspondence.Application/GetCorespondences/LegacyGetCorrespondencesHandler.cs (1 hunks)
  • src/Altinn.Correspondence.Application/GetCorrespondenceOverview/LegacyGetCorrespondenceOverviewHandler.cs (1 hunks)
  • src/Altinn.Correspondence.Application/Helpers/CorrespondenceExtensions.cs (1 hunks)
  • src/Altinn.Correspondence.Persistence/Helpers/QueryableExtensions.cs (1 hunks)
🔇 Additional comments (4)
src/Altinn.Correspondence.Application/Helpers/CorrespondenceExtensions.cs (1)

20-26: Verify enum ordering for status hierarchy.

The implementation assumes that higher enum values represent "higher" status levels. We should verify this assumption to ensure correct status ordering.

Consider documenting the status hierarchy in comments or creating a separate method that explicitly defines the status order, making it more maintainable and less dependent on enum values.

Example approach:

private static readonly Dictionary<CorrespondenceStatus, int> StatusHierarchy = new()
{
    { CorrespondenceStatus.Initialized, 1 },
    { CorrespondenceStatus.ReadyForPublish, 2 },
    // ... other statuses
};
✅ Verification successful

The enum values show a clear progression from Initialized (0) -> ReadyForPublish (1) -> Published (2) -> Fetched (3), where higher numeric values represent later stages in the correspondence lifecycle. Let me verify if there are any additional statuses by searching for potential derived or extended enums.


Enum ordering is correct for status hierarchy

The implementation in GetHighestStatus correctly uses enum values to determine the highest status. The CorrespondenceStatus enum shows a clear progression where higher numeric values represent more advanced states in the correspondence lifecycle:

  • Initialized (0)
  • ReadyForPublish (1)
  • Published (2)
  • Fetched (3)

The method correctly excludes Fetched status and uses OrderByDescending to get the highest remaining status, which aligns with the business logic of the correspondence workflow.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the CorrespondenceStatus enum values and their ordering

# Search for the enum definition
ast-grep --pattern 'enum CorrespondenceStatus {
  $$$
}'

# Alternative: Search using ripgrep
rg -A 20 "enum CorrespondenceStatus"

Length of output: 4121


Script:

#!/bin/bash
# Search for any additional status enums or extensions
rg -g "*.cs" "enum.*Status|Status.*enum" -B 2 -A 20

Length of output: 11156

src/Altinn.Correspondence.Application/GetCorrespondenceOverview/LegacyGetCorrespondenceOverviewHandler.cs (2)

59-59: Verify status hierarchy implementation

The change from GetLatestStatus() to GetHighestStatus() represents a fundamental shift in how correspondence status is determined. While this aligns with the PR objectives, we should ensure the status hierarchy is correctly implemented.

✅ Verification successful

Change from GetLatestStatus to GetHighestStatus is correctly implemented

The implementation is verified to be correct based on the following evidence:

  1. The GetHighestStatus() method in CorrespondenceExtensions.cs is properly implemented:

    • Uses OrderByDescending(s => s.Status) to sort by status enum value
    • Excludes Fetched status from consideration
    • Returns the highest status based on enum value ordering
  2. The status hierarchy is clearly defined in CorrespondenceStatus enum:

    • Starts from Initialized = 0
    • Progresses through ReadyForPublish = 1
    • Higher values represent more advanced states
  3. The change aligns with the PR objectives of using highest status instead of newest for legacy requests, and the implementation correctly switches from timestamp-based ordering (StatusChanged) to enum-value-based ordering (Status).

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the implementation of GetHighestStatus and status hierarchy

# Check the implementation of GetHighestStatus
ast-grep --pattern 'GetHighestStatus() {
  $$$
}'

# Check for any status enum or hierarchy definition
rg -A 10 'enum CorrespondenceStatus'

# Look for tests validating the status hierarchy
fd -e cs -e csproj --exec rg -l 'GetHighestStatus|CorrespondenceStatus.*Test'

Length of output: 2591


Script:

#!/bin/bash
# Let's check the implementation of GetHighestStatus in CorrespondenceExtensions
rg -A 20 "GetHighestStatus" ./src/Altinn.Correspondence.Application/Helpers/CorrespondenceExtensions.cs

# Let's also check if there's any ordering or comparison logic for statuses
rg -A 10 "CorrespondenceStatus.*>|CompareTo|OrderBy.*Status" ./src/Altinn.Correspondence.Application/

# Check for any test files that might validate the status hierarchy
fd "test" -e cs --exec rg -l "CorrespondenceStatus|GetHighestStatus"

Length of output: 34270


Line range hint 59-146: Verify impact on status-dependent logic

The change to GetHighestStatus() affects downstream logic that depends on the status value. The code uses this status for:

  1. Availability check (IsAvailableForRecipient())
  2. Response construction
  3. Status-based field population (Archived, Confirmed dates)

We should ensure these dependencies work correctly with the new status determination method.

✅ Verification successful

Let me analyze the results from the shell script execution to verify the impact of GetHighestStatus() on status-dependent logic.

The search results show several key findings:

  1. The IsAvailableForRecipient() method is well-defined in CorrespondenceExtensions.cs and has a clear list of valid statuses:
[Published, Fetched, Read, Replied, Confirmed, Archived, Reserved]
  1. The status transitions and dependencies are consistently enforced across handlers:
  • Read requires prior Fetched status
  • Confirmed requires prior Fetched status
  • Archived requires prior Confirmed status if confirmation is needed
  1. The GetHighestStatus() implementation in CorrespondenceExtensions.cs excludes Fetched status and orders by status value:
.Where(s => s.Status != CorrespondenceStatus.Fetched)
.OrderByDescending(s => s.Status)
  1. The status value is used consistently for:
  • Availability checks via IsAvailableForRecipient()
  • Response construction with proper status text
  • Status-based field population (Archived, Confirmed dates)

Let me generate the final response:

GetHighestStatus() implementation maintains status-dependent logic integrity

The new status determination method correctly preserves the status transition rules and dependencies by:

  1. Excluding Fetched status which is transient and shouldn't affect the highest status
  2. Using status value ordering that aligns with the valid status progression
  3. Maintaining compatibility with IsAvailableForRecipient() checks
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for status-dependent logic across the codebase

# Look for methods that depend on correspondence status
rg -A 5 'IsAvailableForRecipient|CorrespondenceStatus\.'

# Check for tests covering status-dependent scenarios
fd -e cs --exec rg -l 'IsAvailableForRecipient.*Test|CorrespondenceStatus.*Test'

Length of output: 54973

src/Altinn.Correspondence.Application/GetCorespondences/LegacyGetCorrespondencesHandler.cs (1)

135-135: Verify business impact of switching from newest to highest status

The change from GetLatestStatusWithoutPurged() to GetHighestStatusWithoutPurged() could impact existing consumers expecting the newest status. Please ensure this aligns with business requirements.

✅ Verification successful

Let me analyze the findings from the shell script results:

  1. The codebase shows a clear pattern of using two different status retrieval methods:

    • GetLatestStatus() - Used in most handlers for regular operations
    • GetHighestStatus() - Used specifically in legacy handlers
  2. The change in LegacyGetCorrespondencesHandler.cs from GetLatestStatusWithoutPurged() to GetHighestStatusWithoutPurged() is consistent with:

    • LegacyGetCorrespondenceOverviewHandler.cs which uses GetHighestStatus()
    • Other legacy handlers that are designed to maintain backward compatibility
  3. The implementation in CorrespondenceExtensions.cs shows:

    • GetLatestStatus() orders by timestamp (newest)
    • GetHighestStatus() orders by status value (highest)
    • Both methods exclude Fetched status
    • Both have variants that exclude purged status
  4. This change appears to be part of a deliberate pattern to handle legacy requests differently from regular requests, maintaining consistency across legacy handlers.

Change is consistent with legacy handler pattern and safe to proceed

The switch from GetLatestStatusWithoutPurged() to GetHighestStatusWithoutPurged() in LegacyGetCorrespondencesHandler aligns with the established pattern of using highest status for legacy handlers, as evidenced by similar implementation in LegacyGetCorrespondenceOverviewHandler. Regular handlers continue to use latest status, maintaining the intended separation between legacy and current implementations.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for other status-related implementations and tests
# to ensure consistency and identify potential impacts

# Check for other status-related methods
rg -A 3 "GetLatestStatus|GetHighestStatus" --type cs

# Look for related test cases
rg -A 5 "test.*Status" --type cs

Length of output: 44656

Copy link
Collaborator

@CelineTrammi CelineTrammi left a comment

Choose a reason for hiding this comment

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

Looks good.
But I think there should be added a warning or some type of documentation internally the status enum order having an impact on the logic in the code.

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

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between b2dce8c and 8b1a44f.

📒 Files selected for processing (2)
  • src/Altinn.Correspondence.Application/Helpers/CorrespondenceExtensions.cs (1 hunks)
  • src/Altinn.Correspondence.Persistence/Helpers/QueryableExtensions.cs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/Altinn.Correspondence.Persistence/Helpers/QueryableExtensions.cs
🔇 Additional comments (1)
src/Altinn.Correspondence.Application/Helpers/CorrespondenceExtensions.cs (1)

20-32: Verify CorrespondenceStatus enum ordering

The implementation looks clean and consistent. However, since MaxBy(s => s.Status) relies on the enum values' ordering, we should verify that the CorrespondenceStatus enum values are ordered correctly to ensure the "highest" status is determined as intended.

✅ Verification successful

CorrespondenceStatus enum ordering is correct for MaxBy operation

The enum values are properly ordered with increasing severity/progression:

  • Basic states: Initialized(0) → ReadyForPublish(1) → Published(2) → Fetched(3) → Read(4) → Replied(5) → Confirmed(6)
  • Terminal states: PurgedByRecipient(7), PurgedByAltinn(8), Archived(9)

The MaxBy(s => s.Status) operation will correctly identify the highest status since:

  1. The enum values follow a logical progression
  2. Terminal states (Purged/Archived) have the highest values
  3. The IsPurged() extension method correctly filters both purge states when needed
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify the ordering of CorrespondenceStatus enum values

# Search for the CorrespondenceStatus enum definition
ast-grep --pattern 'enum CorrespondenceStatus {
  $$$
}'

Length of output: 58


Script:

#!/bin/bash
# Let's try searching for enum definition using ripgrep instead
rg "enum CorrespondenceStatus" -A 15 --type cs

Length of output: 3164


Script:

#!/bin/bash
# Let's get the complete enum definition to see all status values
rg "enum CorrespondenceStatus" -A 30 --type cs src/Altinn.Correspondence.Core/Models/Enums/CorrespondenceStatus.cs

Length of output: 893


Script:

#!/bin/bash
# Let's check if there are any more status values and also look for IsPurged extension method
rg "enum CorrespondenceStatus" -A 40 --type cs src/Altinn.Correspondence.Core/Models/Enums/CorrespondenceStatus.cs

# Search for IsPurged extension method
rg "IsPurged" -A 5 --type cs

Length of output: 6038


Script:

#!/bin/bash
# Let's get the complete enum to see if there are any more status values after PurgedByRecipient
rg "PurgedByRecipient = 7" -A 10 --type cs src/Altinn.Correspondence.Core/Models/Enums/CorrespondenceStatus.cs

Length of output: 370

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

🧹 Outside diff range and nitpick comments (1)
src/Altinn.Correspondence.Application/Helpers/CorrespondenceExtensions.cs (1)

20-33: Document the behavioral difference between Latest and Highest status

Consider adding XML documentation to explain the semantic difference between Latest (time-based) and Highest (enum value-based) status selection. This will help developers choose the appropriate method for their use case.

Example documentation:

/// <summary>
/// Gets the status with the highest enum value, excluding Fetched status.
/// Unlike GetLatestStatus which uses timestamp, this method prioritizes by status severity.
/// </summary>
public static CorrespondenceStatusEntity? GetHighestStatus(...)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 8b1a44f and 0ad017a.

📒 Files selected for processing (2)
  • src/Altinn.Correspondence.Application/Helpers/CorrespondenceExtensions.cs (1 hunks)
  • src/Altinn.Correspondence.Persistence/Helpers/QueryableExtensions.cs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • src/Altinn.Correspondence.Persistence/Helpers/QueryableExtensions.cs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ignore-for-release pull request wont be included in release notes kind/feature-request New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants