Skip to content

redirection to correct flow after new individual creation#299

Merged
pokhiii merged 4 commits intodevelopfrom
fix/camp-contribution-flow/new-individual
Oct 1, 2024
Merged

redirection to correct flow after new individual creation#299
pokhiii merged 4 commits intodevelopfrom
fix/camp-contribution-flow/new-individual

Conversation

@pokhiii
Copy link
Copy Markdown
Member

@pokhiii pokhiii commented Oct 1, 2024

  • This PR adds a template redirect when a new individual is created.
  • After that it reads the Creation_Flow for the individual and the source to decide what to do: keep it on the same page redirect to something new.

Summary by CodeRabbit

  • New Features

    • Introduced a new action hook for seamless redirection after creating an individual, enhancing user experience based on specific conditions.
  • Bug Fixes

    • Maintained existing functionalities related to user identification and login handling without issues.

@pokhiii pokhiii self-assigned this Oct 1, 2024
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Oct 1, 2024

Warning

Rate limit exceeded

@pokhiii has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 1 minutes and 9 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

Files that changed from the base of the PR and between 17ff7d5 and d18ad34.

Walkthrough

The changes introduce a new action hook, goonj_redirect_after_individual_cration, in the functions.php file. This hook manages redirection after an individual is created, utilizing a specific request parameter to retrieve individual details from the CiviCRM API. The logic determines the redirect URL based on the individual's creation flow and the existence of a collection camp. If no valid redirect path is found, the function exits without redirecting. Other existing functionalities in the file remain unchanged.

Changes

File Change Summary
wp-content/themes/goonj-crm/functions.php Added a new function goonj_redirect_after_individual_cration() to handle post-creation redirection based on specific conditions.

Poem

In the garden where bunnies play,
A new hook hops in to save the day.
With paths that twist and turns that flow,
After creation, off we go!
A seamless leap, a joyful cheer,
For every individual, we hold dear! 🐇✨


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

🧹 Outside diff range and nitpick comments (2)
wp-content/themes/goonj-crm/functions.php (2)

463-463: Typo in comment 'Colllection Camp'

There's a typo in the comment:

// First, we check if the source of Individual is Colllection Camp (or Dropping Center).

The word "Colllection" should be "Collection".

Correct it as follows:

-// First, we check if the source of Individual is Colllection Camp (or Dropping Center).
+// First, we check if the source of Individual is Collection Camp (or Dropping Center).

485-485: Use absolute URL with wp_safe_redirect

When using wp_safe_redirect(), it's recommended to use an absolute URL to ensure the redirection works correctly in all contexts.

Modify the redirect to use home_url():

-wp_safe_redirect( $redirectPath );
+wp_safe_redirect( home_url( $redirectPath ) );
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between e9222ad and 9ab97a9.

📒 Files selected for processing (1)
  • wp-content/themes/goonj-crm/functions.php (1 hunks)
🔇 Additional comments (1)
wp-content/themes/goonj-crm/functions.php (1)

445-449: Possible issues with primary email and phone joins

The query assumes that the individual has both a primary email and a primary phone. If either is missing, the joins might not function as expected, potentially causing the query to fail or return incomplete data.

Consider modifying the query to handle cases where the primary email or phone may not exist, or ensure that these fields are mandatory for an individual.

Automation to verify individuals without primary email or phone:

Comment on lines +431 to +432
add_action( 'template_redirect', 'goonj_redirect_after_individual_cration' );
function goonj_redirect_after_individual_cration() {
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

Typo in function name 'goonj_redirect_after_individual_cration'

The function name goonj_redirect_after_individual_cration contains a typo in the word "cration". It should be "creation" to reflect correct spelling and improve code readability.

Apply this diff to correct the typo:

-add_action( 'template_redirect', 'goonj_redirect_after_individual_cration' );
+add_action( 'template_redirect', 'goonj_redirect_after_individual_creation' );

-function goonj_redirect_after_individual_cration() {
+function goonj_redirect_after_individual_creation() {
📝 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
add_action( 'template_redirect', 'goonj_redirect_after_individual_cration' );
function goonj_redirect_after_individual_cration() {
add_action( 'template_redirect', 'goonj_redirect_after_individual_creation' );
function goonj_redirect_after_individual_creation() {

Comment on lines +434 to +438
! isset( $_REQUEST['goonjAction'] ) ||
$_REQUEST['goonjAction'] !== 'individualCreated' ||
! isset( $_REQUEST['individualId'] )
) {
return;
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

Security Concern: Use of $_REQUEST Superglobal

Using $_REQUEST can be insecure because it includes data from $_GET, $_POST, and $_COOKIE. This might allow unexpected data sources to affect the logic.

It's safer to specify the expected superglobal. For example, if you expect the data to come from a GET request, use $_GET:

-	if (
-		! isset( $_REQUEST['goonjAction'] ) ||
-		$_REQUEST['goonjAction'] !== 'individualCreated' ||
-		! isset( $_REQUEST['individualId'] )
-	) {
+	if (
+		! isset( $_GET['goonjAction'] ) ||
+		$_GET['goonjAction'] !== 'individualCreated' ||
+		! isset( $_GET['individualId'] )
+	) {

Ensure that the rest of the code is updated accordingly.

📝 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
! isset( $_REQUEST['goonjAction'] ) ||
$_REQUEST['goonjAction'] !== 'individualCreated' ||
! isset( $_REQUEST['individualId'] )
) {
return;
! isset( $_GET['goonjAction'] ) ||
$_GET['goonjAction'] !== 'individualCreated' ||
! isset( $_GET['individualId'] )
) {
return;

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

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 9ab97a9 and 17ff7d5.

📒 Files selected for processing (1)
  • wp-content/themes/goonj-crm/functions.php (2 hunks)

Comment on lines +434 to +487
if (
! isset( $_REQUEST['goonjAction'] ) ||
$_REQUEST['goonjAction'] !== 'individualCreated' ||
! isset( $_REQUEST['individualId'] )
) {
return;
}

$individual = \Civi\Api4\Contact::get( false )
->addSelect( 'source', 'Individual_fields.Creation_Flow', 'email.email', 'phone.phone' )
->addJoin( 'Email AS email', 'LEFT' )
->addJoin( 'Phone AS phone', 'LEFT' )
->addWhere( 'email.is_primary', '=', true )
->addWhere( 'phone.is_primary', '=', true )
->addWhere( 'id', '=', absint( $_REQUEST['individualId'] ) )
->setLimit( 1 )
->execute()->single();

$creationFlow = $individual['Individual_fields.Creation_Flow'];
$source = $individual['source'];

if ( ! $source ) {
return;
}

switch ( $creationFlow ) {
case 'material-contribution':
// If the individual was created while in the process of material contribution,
// then we need to find out from WHERE was she trying to contribute.

// First, we check if the source of Individual is Colllection Camp (or Dropping Center).
$collectionCamp = \Civi\Api4\EckEntity::get( 'Collection_Camp', false )
->addWhere( 'title', '=', $source )
->setLimit( 1 )
->execute()->first();

if ( ! empty( $collectionCamp['id'] ) ) {
$redirectPath = sprintf(
'/material-contribution/#?email=%s&phone=%s&Material_Contribution.Collection_Camp=%s&source_contact_id=%s',
$individual['email.email'],
$individual['phone.phone'],
$collectionCamp['id'],
$individual['id']
);
break;
}
}

if ( empty( $redirectPath ) ) {
return;
}

wp_safe_redirect( $redirectPath );
}
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

Consider refactoring for improved modularity

The function goonj_redirect_after_individual_cration is quite long and handles multiple responsibilities. This can make it harder to maintain and test.

Consider breaking down the function into smaller, more focused functions:

  1. A function to validate the request parameters
  2. A function to retrieve individual data
  3. Separate functions for each creation flow

Example refactoring:

function goonj_redirect_after_individual_creation() {
    if (!goonj_validate_individual_creation_request()) {
        return;
    }

    $individual = goonj_get_individual_data($_REQUEST['individualId']);
    if (empty($individual)) {
        return;
    }

    $redirectPath = goonj_get_redirect_path($individual);
    if (!empty($redirectPath)) {
        wp_safe_redirect($redirectPath);
        exit;
    }
}

function goonj_validate_individual_creation_request() {
    return isset($_REQUEST['goonjAction']) 
        && $_REQUEST['goonjAction'] === 'individualCreated'
        && isset($_REQUEST['individualId']);
}

function goonj_get_individual_data($individualId) {
    // API call to retrieve individual data
}

function goonj_get_redirect_path($individual) {
    $creationFlow = $individual['Individual_fields.Creation_Flow'];
    $source = $individual['source'];

    if (!$source) {
        return '';
    }

    switch ($creationFlow) {
        case 'material-contribution':
            return goonj_get_material_contribution_redirect_path($individual, $source);
        // Add other cases here
        default:
            error_log("Unhandled creation flow: " . $creationFlow);
            return '';
    }
}

function goonj_get_material_contribution_redirect_path($individual, $source) {
    // Existing logic for material contribution redirect
}

This refactoring improves modularity, making the code easier to understand, maintain, and test.

Comment on lines +442 to +450
$individual = \Civi\Api4\Contact::get( false )
->addSelect( 'source', 'Individual_fields.Creation_Flow', 'email.email', 'phone.phone' )
->addJoin( 'Email AS email', 'LEFT' )
->addJoin( 'Phone AS phone', 'LEFT' )
->addWhere( 'email.is_primary', '=', true )
->addWhere( 'phone.is_primary', '=', true )
->addWhere( 'id', '=', absint( $_REQUEST['individualId'] ) )
->setLimit( 1 )
->execute()->single();
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

Add error handling for CiviCRM API call

The CiviCRM API call lacks error handling. If the API call fails, it may lead to unexpected behavior or errors.

Consider wrapping the API call in a try-catch block and handle potential exceptions:

-	$individual = \Civi\Api4\Contact::get( false )
-		->addSelect( 'source', 'Individual_fields.Creation_Flow', 'email.email', 'phone.phone' )
-		->addJoin( 'Email AS email', 'LEFT' )
-		->addJoin( 'Phone AS phone', 'LEFT' )
-		->addWhere( 'email.is_primary', '=', true )
-		->addWhere( 'phone.is_primary', '=', true )
-		->addWhere( 'id', '=', absint( $_REQUEST['individualId'] ) )
-		->setLimit( 1 )
-		->execute()->single();
+	try {
+		$individual = \Civi\Api4\Contact::get( false )
+			->addSelect( 'source', 'Individual_fields.Creation_Flow', 'email.email', 'phone.phone' )
+			->addJoin( 'Email AS email', 'LEFT' )
+			->addJoin( 'Phone AS phone', 'LEFT' )
+			->addWhere( 'email.is_primary', '=', true )
+			->addWhere( 'phone.is_primary', '=', true )
+			->addWhere( 'id', '=', absint( $_REQUEST['individualId'] ) )
+			->setLimit( 1 )
+			->execute()->single();
+	} catch (\Exception $e) {
+		error_log('Error retrieving individual data: ' . $e->getMessage());
+		return;
+	}
+
+	if (empty($individual)) {
+		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
$individual = \Civi\Api4\Contact::get( false )
->addSelect( 'source', 'Individual_fields.Creation_Flow', 'email.email', 'phone.phone' )
->addJoin( 'Email AS email', 'LEFT' )
->addJoin( 'Phone AS phone', 'LEFT' )
->addWhere( 'email.is_primary', '=', true )
->addWhere( 'phone.is_primary', '=', true )
->addWhere( 'id', '=', absint( $_REQUEST['individualId'] ) )
->setLimit( 1 )
->execute()->single();
try {
$individual = \Civi\Api4\Contact::get( false )
->addSelect( 'source', 'Individual_fields.Creation_Flow', 'email.email', 'phone.phone' )
->addJoin( 'Email AS email', 'LEFT' )
->addJoin( 'Phone AS phone', 'LEFT' )
->addWhere( 'email.is_primary', '=', true )
->addWhere( 'phone.is_primary', '=', true )
->addWhere( 'id', '=', absint( $_REQUEST['individualId'] ) )
->setLimit( 1 )
->execute()->single();
} catch (\Exception $e) {
error_log('Error retrieving individual data: ' . $e->getMessage());
return;
}
if (empty($individual)) {
return;
}

Comment thread wp-content/themes/goonj-crm/functions.php
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