Skip to content

Add Activity tab for goonj activity#512

Merged
tarunnjoshi merged 1 commit intodevelopfrom
add-activity-tab-for-goonj-activity
Nov 29, 2024
Merged

Add Activity tab for goonj activity#512
tarunnjoshi merged 1 commit intodevelopfrom
add-activity-tab-for-goonj-activity

Conversation

@tarunnjoshi
Copy link
Copy Markdown
Member

@tarunnjoshi tarunnjoshi commented Nov 29, 2024

Add Activity tab for goonj activity

Summary by CodeRabbit

  • New Features

    • Introduced a new method to create activities based on the status of Goonj activity collection camps.
    • Enhanced event handling during form submissions to improve activity management.
  • Bug Fixes

    • Adjusted checks for entity types to ensure correct activity creation based on contact status.
  • Documentation

    • Updated comments for clarity and better understanding of the code functionality.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Nov 29, 2024

Walkthrough

The changes in this pull request focus on the GoonjActivitiesService class within the Civi namespace. Key modifications include the addition of new event handling capabilities, specifically the createActivityForGoonjActivityCollectionCamp method, which responds to the &hook_civicrm_pre event. Adjustments were made to existing methods to improve functionality related to entity types and activity creation, while some imports were updated for clarity.

Changes

File Path Change Summary
wp-content/civi-extensions/goonjcustom/Civi/GoonjActivitiesService.php - Added imports for AfformSubmitEvent, Activity, and CoreUtil.
- Removed imports for Email and Utils\CoreUtil.
- Updated getSubscribedEvents to include createActivityForGoonjActivityCollectionCamp.
- Modified setActivitiesVolunteersAddress to check entityType using CoreUtil::isContact.
- Introduced createActivityForGoonjActivityCollectionCamp method for handling activity creation based on collection camp status.

Possibly related PRs

Suggested labels

status : ready for review

Suggested reviewers

  • nishant22029
  • pokhiii

🎉 In the realm of code, where logic takes flight,
GoonjActivitiesService shines ever so bright.
With events now handled, and activities in tow,
The changes unfold, watch the functionality grow!
New methods emerge, like stars in the night,
In the world of Civi, everything feels right! 🌟


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.

@tarunnjoshi tarunnjoshi merged commit 7e8dc38 into develop Nov 29, 2024
@tarunnjoshi tarunnjoshi deleted the add-activity-tab-for-goonj-activity branch November 29, 2024 13:06
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: 3

🧹 Outside diff range and nitpick comments (2)
wp-content/civi-extensions/goonjcustom/Civi/GoonjActivitiesService.php (2)

580-581: Remove redundant comment markers for clarity

The comments have double slashes (// //), which is unnecessary and may reduce readability. Consider cleaning up the comment syntax.

Apply this diff to fix the comments:

-        // // Check for status change.
-        // // Access the id within the decoded data.
+        // Check for status change.
+        // Access the id within the decoded data.

593-605: Refactor handling of 'Other' activity for clarity and maintainability

The current logic for handling the 'Other' activity involves nested conditionals. Consider extracting this into a separate method or simplifying the logic to enhance readability and adhere to the Single Responsibility Principle.

Optionally, refactor the code as follows:

         foreach ($activities as $activityName) {
-            // Check if the activity is 'Others'.
-            if ($activityName == 'Other') {
-                $otherActivity = $objectRef['Goonj_Activities.Other_Activity_Details'] ?? '';
-
-                if ($otherActivity) {
-                    // Use the 'Other_activity' field as the title.
-                    $activityName = $otherActivity;
-                } else {
-                    continue;
-                }
-            }
+            $activityName = self::resolveActivityName($activityName, $objectRef);
+            if (!$activityName) {
+                continue;
+            }

             // Rest of the code...
         }

+    private static function resolveActivityName($activityName, $objectRef)
+    {
+        if ($activityName == 'Other') {
+            $otherActivity = $objectRef['Goonj_Activities.Other_Activity_Details'] ?? '';
+            return $otherActivity ?: null;
+        }
+        return $activityName;
+    }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 3cad555 and 64fbefb.

📒 Files selected for processing (1)
  • wp-content/civi-extensions/goonjcustom/Civi/GoonjActivitiesService.php (13 hunks)

Comment on lines +558 to +623
public static function createActivityForGoonjActivityCollectionCamp(string $op, string $objectName, $objectId, &$objectRef) {
if ($objectName != 'Eck_Collection_Camp' || self::getEntitySubtypeName($objectId) !== self::ENTITY_SUBTYPE_NAME) {
return;
}

$newStatus = $objectRef['Collection_Camp_Core_Details.Status'] ?? '';

if (!$newStatus || !$objectId) {
return;
}

$collectionCamp = EckEntity::get('Collection_Camp', FALSE)
->addSelect('Collection_Camp_Core_Details.Status', 'Collection_Camp_Core_Details.Contact_Id', 'title')
->addWhere('id', '=', $objectId)
->execute()->single();

$currentStatus = $collectionCamp['Collection_Camp_Core_Details.Status'];

if ($currentStatus === $newStatus || $newStatus !== 'authorized') {
return;
}

// // Check for status change.
// // Access the id within the decoded data.
$campId = $objectRef['id'];

if ($campId === NULL) {
return;
}

$activities = $objectRef['Goonj_Activities.How_do_you_want_to_engage_with_Goonj_'];
$startDate = $objectRef['Goonj_Activities.Start_Date'];
$endDate = $objectRef['Goonj_Activities.End_Date'];
$initiator = $objectRef['Collection_Camp_Core_Details.Contact_Id'];

foreach ($activities as $activityName) {
// Check if the activity is 'Others'.
if ($activityName == 'Other') {
$otherActivity = $objectRef['Goonj_Activities.Other_Activity_Details'] ?? '';

if ($otherActivity) {
// Use the 'Other_activity' field as the title.
$activityName = $otherActivity;
}
else {
continue;
}
}

$optionValue = OptionValue::get(TRUE)
->addSelect('value')
->addWhere('option_group_id:name', '=', 'eck_sub_types')
->addWhere('grouping', '=', 'Collection_Camp_Activity')
->addWhere('name', '=', 'Goonj_Activities')
->execute()->single();

$results = EckEntity::create('Collection_Camp_Activity', TRUE)
->addValue('title', $activityName)
->addValue('subtype', $optionValue['value'])
->addValue('Collection_Camp_Activity.Collection_Camp_Id', $campId)
->addValue('Collection_Camp_Activity.Start_Date', $startDate)
->addValue('Collection_Camp_Activity.End_Date', $endDate)
->addValue('Collection_Camp_Activity.Organizing_Person', $initiator)
->execute();
}
}
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

Refactor 'createActivityForGoonjActivityCollectionCamp' method to improve readability

The method is lengthy with multiple nested conditionals and responsibilities. Breaking it into smaller, dedicated methods can enhance readability and maintainability, aligning with best practices like the Single Responsibility Principle.

Consider refactoring the method into smaller helper functions:

  • One function to validate inputs and state.
  • One function to handle 'Other' activities.
  • One function to create the activities.

Comment on lines +499 to 501
if ($objectRef['subtype']) {
!self::isCurrentSubtype($objectRef);
}
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 incorrect usage of the negation operator in conditional statement

The expression !self::isCurrentSubtype($objectRef); doesn't have any effect since it's not part of a conditional check. It seems you intended to check if self::isCurrentSubtype($objectRef) returns false and return early.

Apply this diff to correct the conditional logic:

 if ($objectRef['subtype']) {
-    !self::isCurrentSubtype($objectRef);
+    if (!self::isCurrentSubtype($objectRef)) {
+        return;
+    }
 }
📝 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 ($objectRef['subtype']) {
!self::isCurrentSubtype($objectRef);
}
if ($objectRef['subtype']) {
if (!self::isCurrentSubtype($objectRef)) {
return;
}
}

Comment on lines +588 to +591
$activities = $objectRef['Goonj_Activities.How_do_you_want_to_engage_with_Goonj_'];
$startDate = $objectRef['Goonj_Activities.Start_Date'];
$endDate = $objectRef['Goonj_Activities.End_Date'];
$initiator = $objectRef['Collection_Camp_Core_Details.Contact_Id'];
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

Ensure variables are set before usage to prevent undefined index errors

Before using $activities, $startDate, $endDate, and $initiator, check if these keys exist in $objectRef to avoid potential undefined index notices or errors if the expected data is missing.

Apply this diff to add necessary checks:

+        if (
+            !isset($objectRef['Goonj_Activities.How_do_you_want_to_engage_with_Goonj_']) ||
+            !isset($objectRef['Goonj_Activities.Start_Date']) ||
+            !isset($objectRef['Goonj_Activities.End_Date']) ||
+            !isset($objectRef['Collection_Camp_Core_Details.Contact_Id'])
+        ) {
+            return;
+        }

         $activities = $objectRef['Goonj_Activities.How_do_you_want_to_engage_with_Goonj_'];
         $startDate = $objectRef['Goonj_Activities.Start_Date'];
         $endDate = $objectRef['Goonj_Activities.End_Date'];
         $initiator = $objectRef['Collection_Camp_Core_Details.Contact_Id'];
📝 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
$activities = $objectRef['Goonj_Activities.How_do_you_want_to_engage_with_Goonj_'];
$startDate = $objectRef['Goonj_Activities.Start_Date'];
$endDate = $objectRef['Goonj_Activities.End_Date'];
$initiator = $objectRef['Collection_Camp_Core_Details.Contact_Id'];
if (
!isset($objectRef['Goonj_Activities.How_do_you_want_to_engage_with_Goonj_']) ||
!isset($objectRef['Goonj_Activities.Start_Date']) ||
!isset($objectRef['Goonj_Activities.End_Date']) ||
!isset($objectRef['Collection_Camp_Core_Details.Contact_Id'])
) {
return;
}
$activities = $objectRef['Goonj_Activities.How_do_you_want_to_engage_with_Goonj_'];
$startDate = $objectRef['Goonj_Activities.Start_Date'];
$endDate = $objectRef['Goonj_Activities.End_Date'];
$initiator = $objectRef['Collection_Camp_Core_Details.Contact_Id'];

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