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

Tester CustomNotificationRecipients #663

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Conversation

mSunberg
Copy link
Collaborator

@mSunberg mSunberg commented Feb 3, 2025

Description

Just a PR for testing functionality around CustomNotificationRecipients

Related Issue(s)

  • #{issue number}

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 properties to customize notification recipients, including options to override recipients and manage multiple custom recipients.
  • Bug Fixes

    • Adjusted the mapping logic for notification recipients, ensuring proper handling of custom recipient data.
  • Documentation

    • Added comments for clarity regarding recipient validation and intended use within the notification system.

Copy link
Contributor

coderabbitai bot commented Feb 3, 2025

📝 Walkthrough

Walkthrough

This pull request modifies the notification mapping process by commenting out the assignment for the CustomNotificationRecipients property in the MapToRequest method of the InitializeCorrespondenceNotificationMapper class. Additionally, two new required properties—RecipientToOverride and Recipients—are introduced in the CustomNotificationRecipientExt class with JSON annotations. A comment is added in the NotificationDetailsExt class, and further comments are included in the ValidateRecipientOverrides method of the InitializeCorrespondenceHelper class without altering the core logic or public API.

Changes

File Change Summary
src/Altinn.Correspondence.API/…/InitializeCorrespondenceNotificationMapper.cs Commented out the assignment of CustomNotificationRecipients in the MapToRequest method.
src/Altinn.Correspondence.API/…/CustomNotificationRecipientExt.cs Added two new required properties: RecipientToOverride and Recipients, both with JsonPropertyName annotations.
src/Altinn.Correspondence.API/…/NotificationDetailsExt.cs Inserted a comment indicating the intended use of a list without altering functionality.
src/Altinn.Correspondence.Application/…/InitializeCorrespondenceHelper.cs Added comments in ValidateRecipientOverrides to clarify the usage of customRecipients while retaining existing logic.

Possibly related PRs

  • add validation for custom recipients with tags #619: The changes in this PR, which involve commenting out the assignment of CustomNotificationRecipients in the MapToRequest method, are related to the modifications in the retrieved PR that enhance validation logic for custom recipients, as both address the handling of custom notification recipients in the correspondence API.
  • Feat: Add support for custom recipients of a notifcation for a correspondence #550: The changes in this PR are directly related to the modifications in the retrieved PR that introduce the WithCustomNotificationRecipients method in the CorrespondenceBuilder class, as both involve handling the CustomNotificationRecipients property.

Suggested labels

kind/documentation, kind/bug, kind/enhancement

Suggested reviewers

  • Ceredron
  • Andreass2

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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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
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

🧹 Nitpick comments (3)
src/Altinn.Correspondence.API/Models/CustomNotificationRecipientExt.cs (2)

18-18: Remove Norwegian comments.

The Norwegian comments "ok, den består av denne" and "og av enda en liste" should be removed or translated to English to maintain consistency and improve code readability.

-    //ok, den består av denne
     [JsonPropertyName("recipientToOverride")]
     public required string RecipientToOverride { get; set; }

     /// <summary>
     /// List of custom recipients to override the default recipients
     /// </summary>
-    //og av enda en liste
     [JsonPropertyName("notificationRecipient")]
     public required List<NotificationRecipientExt> Recipients { get; set; }

Also applies to: 27-27


16-17: Remove unnecessary empty lines.

Remove the extra empty lines between XML comments and property declarations to improve code readability and maintain consistent spacing.

Also applies to: 25-26

src/Altinn.Correspondence.API/Mappers/InitializeCorrespondenceNotificationMapper.cs (1)

37-37: Remove Norwegian comment.

The Norwegian comment "CustomNotificationRecipients blir ikke populert fra API siden" should be removed or translated to English to maintain consistency and improve code readability.

-            //CustomNotificationRecipients blir ikke populert fra API siden
             CustomNotificationRecipients = NotificationMapper.MapExternalRecipientsToRequest(correspondenceNotificationExt.CustomNotificationRecipients)
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between fbf255f and bfcf6b9.

📒 Files selected for processing (4)
  • src/Altinn.Correspondence.API/Mappers/InitializeCorrespondenceNotificationMapper.cs (1 hunks)
  • src/Altinn.Correspondence.API/Models/CustomNotificationRecipientExt.cs (1 hunks)
  • src/Altinn.Correspondence.API/Models/NotificationDetailsExt.cs (1 hunks)
  • src/Altinn.Correspondence.Application/Helpers/InitializeCorrespondenceHelper.cs (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • src/Altinn.Correspondence.API/Models/NotificationDetailsExt.cs
  • src/Altinn.Correspondence.Application/Helpers/InitializeCorrespondenceHelper.cs
🧰 Additional context used
📓 Learnings (2)
📓 Common learnings
Learnt from: CelineTrammi
PR: Altinn/altinn-correspondence#550
File: src/Altinn.Correspondence.API/Mappers/InitializeCorrespondenceNotificationMapper.cs:36-37
Timestamp: 2024-12-10T10:51:48.001Z
Learning: Null checking of `CustomNotificationRecipients` is performed within the `MapExternalRecipientsToRequest` method in the `NotificationMapper` class, so additional checks in the `InitializeCorrespondenceNotificationMapper` class are unnecessary.
src/Altinn.Correspondence.API/Mappers/InitializeCorrespondenceNotificationMapper.cs (1)
Learnt from: CelineTrammi
PR: Altinn/altinn-correspondence#550
File: src/Altinn.Correspondence.API/Mappers/InitializeCorrespondenceNotificationMapper.cs:36-37
Timestamp: 2024-12-10T10:51:48.001Z
Learning: Null checking of `CustomNotificationRecipients` is performed within the `MapExternalRecipientsToRequest` method in the `NotificationMapper` class, so additional checks in the `InitializeCorrespondenceNotificationMapper` class are unnecessary.
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Analyze (csharp)
🔇 Additional comments (1)
src/Altinn.Correspondence.API/Mappers/InitializeCorrespondenceNotificationMapper.cs (1)

38-38: LGTM! Null checking is handled correctly.

Based on the retrieved learnings, null checking of CustomNotificationRecipients is performed within the MapExternalRecipientsToRequest method in the NotificationMapper class, so this implementation is correct.

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

🧹 Nitpick comments (2)
src/Altinn.Correspondence.Application/Helpers/InitializeCorrespondenceHelper.cs (2)

174-180: Consider adding null check for RecipientToOverride.

The code assumes RecipientToOverride is not null. Consider adding a null check to prevent potential null reference exceptions.

Apply this diff to add the null check:

-            foreach (var recipientToOverride in customRecipients)
-            {
-                if (!recipients.Any(recipient => recipient.WithoutPrefix() == recipientToOverride.RecipientToOverride.WithoutPrefix()))
-                {
-                    return NotificationErrors.CouldNotFindRecipientToOverride(recipientToOverride.RecipientToOverride);
-                }
-            }
+            foreach (var recipientToOverride in customRecipients)
+            {
+                if (recipientToOverride.RecipientToOverride == null)
+                {
+                    return NotificationErrors.InvalidRecipientOverride;
+                }
+                if (!recipients.Any(recipient => recipient.WithoutPrefix() == recipientToOverride.RecipientToOverride.WithoutPrefix()))
+                {
+                    return NotificationErrors.CouldNotFindRecipientToOverride(recipientToOverride.RecipientToOverride);
+                }
+            }
🧰 Tools
🪛 GitHub Check: Test application

[failure] 174-174:
The name 'customRecipients' does not exist in the current context


[failure] 174-174:
The name 'customRecipients' does not exist in the current context


182-197: Consider adding null check for Recipients collection.

The code assumes Recipients collection is not null. Consider adding a null check to prevent potential null reference exceptions.

Apply this diff to add the null check:

             foreach (var recipientOverride in customRecipients)
             {
+                if (recipientOverride.Recipients == null)
+                {
+                    return NotificationErrors.InvalidRecipientOverride;
+                }
                 foreach (var recipient in recipientOverride.Recipients)
                 {
🧰 Tools
🪛 GitHub Check: Test application

[failure] 182-182:
The name 'customRecipients' does not exist in the current context


[failure] 182-182:
The name 'customRecipients' does not exist in the current context

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 598934f and d16dcee.

📒 Files selected for processing (1)
  • src/Altinn.Correspondence.Application/Helpers/InitializeCorrespondenceHelper.cs (1 hunks)
🧰 Additional context used
📓 Learnings (1)
src/Altinn.Correspondence.Application/Helpers/InitializeCorrespondenceHelper.cs (2)
Learnt from: CelineTrammi
PR: Altinn/altinn-correspondence#550
File: src/Altinn.Correspondence.API/Mappers/InitializeCorrespondenceNotificationMapper.cs:36-37
Timestamp: 2024-12-10T10:51:48.001Z
Learning: Null checking of `CustomNotificationRecipients` is performed within the `MapExternalRecipientsToRequest` method in the `NotificationMapper` class, so additional checks in the `InitializeCorrespondenceNotificationMapper` class are unnecessary.
Learnt from: CelineTrammi
PR: Altinn/altinn-correspondence#550
File: src/Altinn.Correspondence.Application/Helpers/InitializeCorrespondenceHelper.cs:195-213
Timestamp: 2024-12-10T10:51:58.965Z
Learning: In `src/Altinn.Correspondence.Application/Helpers/InitializeCorrespondenceHelper.cs`, within the `ValidateNotificationChannelForRecipientOverrides` method, there are four options for `notificationChannel`: `Email`, `EmailPreferred`, `Sms`, and `SmsPreferred`. `Email` and `Sms` explicitly state that the other won't be used. Therefore, using `!=` with `||` in the conditions is intentional and correct to validate the notification channels appropriately.
🪛 GitHub Check: Test application
src/Altinn.Correspondence.Application/Helpers/InitializeCorrespondenceHelper.cs

[failure] 160-160:
The name 'rOrEmail' does not exist in the current context


[failure] 160-160:
The name 'customRecipients' does not exist in the current context


[failure] 160-160:
The name 'rOrEmail' does not exist in the current context


[failure] 160-160:
The name 'customRecipients' does not exist in the current context

🪛 GitHub Actions: Test application
src/Altinn.Correspondence.Application/Helpers/InitializeCorrespondenceHelper.cs

[error] 160-160: The name 'rOrEmail' does not exist in the current context.

⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Analyze (csharp)

Comment on lines +158 to +160
//denne brukes
//CustomNotificationRecipients er tom
rOrEmail = customRecipients.Where(recipient => recipient.Recipients.Any(r => r.EmailAddress != null || r.MobileNumber != null)).ToList();
Copy link
Contributor

@coderabbitai coderabbitai bot Feb 3, 2025

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix compilation errors in recipient validation.

The code has critical compilation errors that need to be addressed:

  1. The variable customRecipients is not declared or initialized.
  2. The variable name rOrEmail is incorrect.
  3. The line appears to be truncated.

Apply this diff to fix the compilation errors:

-            //denne brukes
-            //CustomNotificationRecipients er tom
-rOrEmail = customRecipients.Where(recipient => recipient.Recipients.Any(r => r.EmailAddress != null || r.MobileNumber != null)).ToList();
+            var customRecipients = notification.CustomNotificationRecipients ?? [];
+            var recipientWithNumberOrEmail = customRecipients.Where(recipient => 
+                recipient.Recipients.Any(r => r.EmailAddress != null || r.MobileNumber != null))
+                .ToList();
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
//denne brukes
//CustomNotificationRecipients er tom
rOrEmail = customRecipients.Where(recipient => recipient.Recipients.Any(r => r.EmailAddress != null || r.MobileNumber != null)).ToList();
var customRecipients = notification.CustomNotificationRecipients ?? [];
var recipientWithNumberOrEmail = customRecipients.Where(recipient =>
recipient.Recipients.Any(r => r.EmailAddress != null || r.MobileNumber != null))
.ToList();
🧰 Tools
🪛 GitHub Check: Test application

[failure] 160-160:
The name 'rOrEmail' does not exist in the current context


[failure] 160-160:
The name 'customRecipients' does not exist in the current context


[failure] 160-160:
The name 'rOrEmail' does not exist in the current context


[failure] 160-160:
The name 'customRecipients' does not exist in the current context

🪛 GitHub Actions: Test application

[error] 160-160: The name 'rOrEmail' does not exist in the current context.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can I chat whit you without creating a bug on purpose?

Copy link
Contributor

Choose a reason for hiding this comment

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

@mSunberg Absolutely! You can chat with me through PR comments without creating bugs. I can help you with various tasks like:

  • Code review and analysis
  • Generating tests
  • Explaining code behavior
  • Providing implementation suggestions
  • Answering questions about the codebase
  • Creating diagrams
  • Gathering codebase statistics

Just let me know what you'd like to discuss or what kind of assistance you need!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Do I have to refer to you in the comment in order for you to answer?

Copy link
Contributor

Choose a reason for hiding this comment

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

@mSunberg No, you don't need to refer to me specifically in your comments. I automatically monitor and respond to all comments made in pull requests where I'm enabled. Feel free to write your comments naturally, and I'll be here to help!

Copy link
Contributor

Choose a reason for hiding this comment

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

@mSunberg The form field path should be:

correspondence.notification.customNotificationRecipients[0].recipientToOverride

The issue is that you're using recipients in the path, but the model shows that the property is named customNotificationRecipients. Try using this updated path and let me know if you still have any issues.

🔗 Analysis chain


🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for CustomNotificationRecipients usage in tests and examples
rg -l "CustomNotificationRecipients" --type cs

# Search for test files containing "notification" and "recipient"
rg -l "notification.*recipient" --type cs test/

Length of output: 742


Script:

#!/bin/bash
# Check test files for CustomNotificationRecipients usage
ast-grep --pattern 'class CorrespondenceNotificationTests {
  $$$
  $test_method($$$) {
    $$$
    CustomNotificationRecipients
    $$$
  }
  $$$
}'

# Get the model structure
cat src/Altinn.Correspondence.API/Models/InitializeCorrespondenceNotificationExt.cs

Length of output: 3902


Script:

#!/bin/bash
# Get the correspondence model structure
cat src/Altinn.Correspondence.API/Models/InitializeCorrespondenceExt.cs

# Search for any example usages in tests
rg -A 5 "CustomNotificationRecipients" Test/Altinn.Correspondence.Tests/TestingController/Correspondence/CorrespondenceNotificationTests.cs

Length of output: 4714


Script:

#!/bin/bash
# Get the builder class content
cat Test/Altinn.Correspondence.Tests/Factories/CorrespondenceBuilder.cs

# Also check the request model
rg -l "InitializeCorrespondencesRequest" --type cs

Length of output: 10383


✏️ Learnings added
Learnt from: mSunberg
PR: Altinn/altinn-correspondence#663
File: src/Altinn.Correspondence.Application/Helpers/InitializeCorrespondenceHelper.cs:158-160
Timestamp: 2025-02-03T09:12:16.185Z
Learning: In the Altinn Correspondence API, the form field path for accessing CustomNotificationRecipients should use `customNotificationRecipients` (not `recipients`) in the path, following the model structure: `correspondence.notification.customNotificationRecipients[index].recipientToOverride`

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

thanks, what about:
correspondence.notification.customNotificationRecipients[0].emailAddress

Copy link
Contributor

Choose a reason for hiding this comment

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

@mSunberg The correct form field path should be:

correspondence.notification.customNotificationRecipients[0].recipients[0].emailAddress

This is because:

  1. customNotificationRecipients contains a list of CustomNotificationRecipientExt
  2. Each CustomNotificationRecipientExt has a recipients property which is a list of NotificationRecipientExt
  3. emailAddress is a property of NotificationRecipientExt

🐰 thoughtfully taking notes about the model structure

🔗 Analysis chain
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Get the model structure for CustomNotificationRecipientExt
cat src/Altinn.Correspondence.API/Models/CustomNotificationRecipientExt.cs

# Get the model structure for NotificationRecipientExt
cat src/Altinn.Correspondence.API/Models/NotificationRecipientExt.cs

# Search for any example usages in tests
rg -A 5 "EmailAddress.*Recipients" --type cs test/

Length of output: 2247


✏️ Learnings added
Learnt from: mSunberg
PR: Altinn/altinn-correspondence#663
File: src/Altinn.Correspondence.Application/Helpers/InitializeCorrespondenceHelper.cs:158-160
Timestamp: 2025-02-03T09:16:22.006Z
Learning: In the Altinn Correspondence API, the form field path for accessing email address in CustomNotificationRecipients should follow the nested structure: `correspondence.notification.customNotificationRecipients[index].recipients[index].emailAddress` as EmailAddress is a property of NotificationRecipientExt which is contained in the Recipients list of CustomNotificationRecipientExt.

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Recipient overrides with email or mobile number are not allowed when using a custom notification template because of name lookup

Copy link
Contributor

Choose a reason for hiding this comment

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

@mSunberg Thank you for the clarification. I understand now - when using a custom notification template, recipient overrides cannot include email or mobile number details due to the name lookup requirements.

🐰 thoughtfully taking notes about the validation rules


✏️ Learnings added
Learnt from: mSunberg
PR: Altinn/altinn-correspondence#663
File: src/Altinn.Correspondence.Application/Helpers/InitializeCorrespondenceHelper.cs:158-160
Timestamp: 2025-02-03T09:20:02.031Z
Learning: In the Altinn Correspondence API, recipient overrides containing email addresses or mobile numbers are not allowed when using NotificationTemplate.CustomMessage due to name lookup requirements. This is validated in InitializeCorrespondenceHelper.ValidateRecipientOverrides().

Note: Learnings are effective only in the context of similar code segments. To apply general coding guidelines, please configure review instructions. You can manage existing learnings in the app.

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.

1 participant