Skip to content

Stop sending receipt when user don't have pan card#495

Merged
tarunnjoshi merged 2 commits intodevelopfrom
stop-receipt-to-send
Nov 25, 2024
Merged

Stop sending receipt when user don't have pan card#495
tarunnjoshi merged 2 commits intodevelopfrom
stop-receipt-to-send

Conversation

@tarunnjoshi
Copy link
Copy Markdown
Member

@tarunnjoshi tarunnjoshi commented Nov 25, 2024

Stop sending receipt when user don't have pan card

Summary by CodeRabbit

  • New Features

    • Introduced functionality to suppress email notifications if a contribution's PAN card number is missing.
    • Added a new event hook to enhance email parameter handling.
  • Bug Fixes

    • Improved error handling for retrieving contribution data related to email notifications.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Nov 25, 2024

Walkthrough

The pull request introduces modifications to the CollectionCampService class within the Civi namespace. Key changes include the addition of a new import statement for the Contribution class and the incorporation of a new method, suppressEmailIfPanCardMissing, which suppresses email notifications if a contribution's PAN card number is missing. Additionally, the getSubscribedEvents method is updated to include a hook for this new functionality, enhancing the email notification system based on specific conditions.

Changes

File Path Change Summary
wp-content/civi-extensions/goonjcustom/Civi/CollectionCampService.php - Added import statement for Contribution class.
- Updated getSubscribedEvents to include &hook_civicrm_alterMailParams.
- Added method suppressEmailIfPanCardMissing for email suppression based on PAN card presence.
- Implemented error handling for contribution data retrieval.

Possibly related PRs

  • bug fixes and refactor #329: Introduces new methods in the CollectionCampService class, enhancing email notification functionalities related to the main PR.
  • Update Email Content #411: Modifies methods within the CollectionCampService class, directly related to the changes in the main PR, focusing on email handling and data management.

Suggested reviewers

  • nishant22029

🎉 In the land of code where emails fly,
A PAN card's absence makes notifications shy.
With a hook and a method, the changes take flight,
Suppressing the emails, making things right!
So let’s celebrate this code that we weave,
For every contribution, we now can believe! 🎊


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.

Copy link
Copy Markdown

@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)
wp-content/civi-extensions/goonjcustom/Civi/CollectionCampService.php (1)

1466-1495: Consider adding tests and documentation.

The email suppression feature would benefit from:

  1. Unit tests to verify the logic for different scenarios (with/without PAN card, invalid contribution ID, etc.)
  2. Documentation explaining the business rule and its implications
  3. Consider moving the PAN card validation logic to a separate service class for better separation of concerns

Would you like me to help create:

  1. Unit tests for the email suppression feature?
  2. Documentation template for the business rule?
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 8248a5e and 7a12ac5.

📒 Files selected for processing (1)
  • wp-content/civi-extensions/goonjcustom/Civi/CollectionCampService.php (3 hunks)
🔇 Additional comments (1)
wp-content/civi-extensions/goonjcustom/Civi/CollectionCampService.php (1)

76-76: LGTM: Hook registration is correct.

The hook registration for mail parameter alteration follows CiviCRM conventions.

*
*/
public function suppressEmailIfPanCardMissing(&$params, $context = NULL) {
if (!$params['valueName'] === 'contribution_online_receipt') {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix the condition syntax.

The condition has two issues:

  1. Using single = for comparison instead of ===
  2. Incorrect order of operands which could lead to accidental assignment

Apply this fix:

-    if (!$params['valueName'] === 'contribution_online_receipt') {
+    if ($params['valueName'] !== 'contribution_online_receipt') {
📝 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
if (!$params['valueName'] === 'contribution_online_receipt') {
if ($params['valueName'] !== 'contribution_online_receipt') {

Comment on lines +1477 to +1494
try {
$contribution = Contribution::get(FALSE)
->addSelect('Contribution_Details.PAN_Card_Number')
->addWhere('id', '=', $contributionId)
->execute()
->single();

$panCard = $contribution['Contribution_Details.PAN_Card_Number'] ?? '';

if (empty($panCard)) {
// Modify params to suppress email (set the toEmail as null, or mark as test)
// Empty out the recipient email address.
$params['toEmail'] = '';
}
}
catch (Exception $e) {
\Civi::log()->error("Error retrieving contribution data for ID $contributionId: " . $e->getMessage());
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve error handling and logging.

The current implementation has several areas for improvement:

  1. The catch block catches all exceptions without specific handling
  2. No logging when emails are suppressed
  3. Redundant comment on line 1489

Consider this improved implementation:

     try {
       $contribution = Contribution::get(FALSE)
         ->addSelect('Contribution_Details.PAN_Card_Number')
         ->addWhere('id', '=', $contributionId)
         ->execute()
         ->single();
 
       $panCard = $contribution['Contribution_Details.PAN_Card_Number'] ?? '';
 
       if (empty($panCard)) {
-        // Modify params to suppress email (set the toEmail as null, or mark as test)
-        // Empty out the recipient email address.
         $params['toEmail'] = '';
+        \Civi::log()->info("Suppressed contribution receipt email for contribution ID $contributionId due to missing PAN card");
       }
     }
-    catch (Exception $e) {
+    catch (\Civi\API4\Exception\ApiException $e) {
       \Civi::log()->error("Error retrieving contribution data for ID $contributionId: " . $e->getMessage());
+    } catch (\Exception $e) {
+      \Civi::log()->error("Unexpected error while processing contribution ID $contributionId: " . $e->getMessage());
     }
📝 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
try {
$contribution = Contribution::get(FALSE)
->addSelect('Contribution_Details.PAN_Card_Number')
->addWhere('id', '=', $contributionId)
->execute()
->single();
$panCard = $contribution['Contribution_Details.PAN_Card_Number'] ?? '';
if (empty($panCard)) {
// Modify params to suppress email (set the toEmail as null, or mark as test)
// Empty out the recipient email address.
$params['toEmail'] = '';
}
}
catch (Exception $e) {
\Civi::log()->error("Error retrieving contribution data for ID $contributionId: " . $e->getMessage());
}
try {
$contribution = Contribution::get(FALSE)
->addSelect('Contribution_Details.PAN_Card_Number')
->addWhere('id', '=', $contributionId)
->execute()
->single();
$panCard = $contribution['Contribution_Details.PAN_Card_Number'] ?? '';
if (empty($panCard)) {
$params['toEmail'] = '';
\Civi::log()->info("Suppressed contribution receipt email for contribution ID $contributionId due to missing PAN card");
}
}
catch (\Civi\API4\Exception\ApiException $e) {
\Civi::log()->error("Error retrieving contribution data for ID $contributionId: " . $e->getMessage());
} catch (\Exception $e) {
\Civi::log()->error("Unexpected error while processing contribution ID $contributionId: " . $e->getMessage());
}

Copy link
Copy Markdown

@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 7a12ac5 and 7f9b124.

📒 Files selected for processing (1)
  • wp-content/civi-extensions/goonjcustom/Civi/CollectionCampService.php (3 hunks)
🔇 Additional comments (2)
wp-content/civi-extensions/goonjcustom/Civi/CollectionCampService.php (2)

11-11: LGTM!

The import statement follows the existing pattern and is correctly placed with other imports.


76-76: LGTM!

The hook registration follows the existing pattern and correctly maps to the new method.

Comment on lines +1466 to +1495
public function suppressEmailIfPanCardMissing(&$params, $context = NULL) {
if ($params['valueName'] !== 'contribution_online_receipt') {
return;
}

$contributionId = $params['tplParams']['contributionID'] ?? NULL;

if (!$contributionId) {
return;
}

try {
$contribution = Contribution::get(FALSE)
->addSelect('Contribution_Details.PAN_Card_Number')
->addWhere('id', '=', $contributionId)
->execute()
->single();

$panCard = $contribution['Contribution_Details.PAN_Card_Number'] ?? '';

if (empty($panCard)) {
// Modify params to suppress email (set the toEmail as null, or mark as test)
// Empty out the recipient email address.
$params['toEmail'] = '';
}
}
catch (Exception $e) {
\Civi::log()->error("Error retrieving contribution data for ID $contributionId: " . $e->getMessage());
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Improve code structure and error handling

The method could benefit from several improvements:

  1. Use early returns to reduce nesting
  2. Add specific error handling for API exceptions
  3. Add logging when email is suppressed
  4. Remove redundant comments

Consider this improved implementation:

 public function suppressEmailIfPanCardMissing(&$params, $context = NULL) {
-    if ($params['valueName'] !== 'contribution_online_receipt') {
-      return;
-    }
+    if ($params['valueName'] !== 'contribution_online_receipt') return;
 
     $contributionId = $params['tplParams']['contributionID'] ?? NULL;
+    if (!$contributionId) return;
 
-    if (!$contributionId) {
-      return;
-    }
-
     try {
       $contribution = Contribution::get(FALSE)
         ->addSelect('Contribution_Details.PAN_Card_Number')
         ->addWhere('id', '=', $contributionId)
         ->execute()
         ->single();
 
       $panCard = $contribution['Contribution_Details.PAN_Card_Number'] ?? '';
 
       if (empty($panCard)) {
-        // Modify params to suppress email (set the toEmail as null, or mark as test)
-        // Empty out the recipient email address.
         $params['toEmail'] = '';
+        \Civi::log()->info("Suppressed contribution receipt email for contribution ID $contributionId due to missing PAN card");
       }
     }
-    catch (Exception $e) {
+    catch (\Civi\API4\Exception\ApiException $e) {
       \Civi::log()->error("Error retrieving contribution data for ID $contributionId: " . $e->getMessage());
+    } catch (\Exception $e) {
+      \Civi::log()->error("Unexpected error while processing contribution ID $contributionId: " . $e->getMessage());
     }
   }
📝 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
public function suppressEmailIfPanCardMissing(&$params, $context = NULL) {
if ($params['valueName'] !== 'contribution_online_receipt') {
return;
}
$contributionId = $params['tplParams']['contributionID'] ?? NULL;
if (!$contributionId) {
return;
}
try {
$contribution = Contribution::get(FALSE)
->addSelect('Contribution_Details.PAN_Card_Number')
->addWhere('id', '=', $contributionId)
->execute()
->single();
$panCard = $contribution['Contribution_Details.PAN_Card_Number'] ?? '';
if (empty($panCard)) {
// Modify params to suppress email (set the toEmail as null, or mark as test)
// Empty out the recipient email address.
$params['toEmail'] = '';
}
}
catch (Exception $e) {
\Civi::log()->error("Error retrieving contribution data for ID $contributionId: " . $e->getMessage());
}
}
public function suppressEmailIfPanCardMissing(&$params, $context = NULL) {
if ($params['valueName'] !== 'contribution_online_receipt') return;
$contributionId = $params['tplParams']['contributionID'] ?? NULL;
if (!$contributionId) return;
try {
$contribution = Contribution::get(FALSE)
->addSelect('Contribution_Details.PAN_Card_Number')
->addWhere('id', '=', $contributionId)
->execute()
->single();
$panCard = $contribution['Contribution_Details.PAN_Card_Number'] ?? '';
if (empty($panCard)) {
$params['toEmail'] = '';
\Civi::log()->info("Suppressed contribution receipt email for contribution ID $contributionId due to missing PAN card");
}
}
catch (\Civi\API4\Exception\ApiException $e) {
\Civi::log()->error("Error retrieving contribution data for ID $contributionId: " . $e->getMessage());
} catch (\Exception $e) {
\Civi::log()->error("Unexpected error while processing contribution ID $contributionId: " . $e->getMessage());
}
}

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.

2 participants