Update Contributor And Contribution Count#522
Conversation
WalkthroughThe pull request introduces enhancements to the Changes
Possibly related PRs
Suggested labels
Suggested reviewers
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Actionable comments posted: 3
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (2)
wp-content/civi-extensions/goonjcustom/Civi/InstitutionCollectionCampService.php(2 hunks)wp-content/civi-extensions/goonjcustom/api/v3/Goonjcustom/InstitutionCollectionCampCron.php(1 hunks)
🔇 Additional comments (2)
wp-content/civi-extensions/goonjcustom/api/v3/Goonjcustom/InstitutionCollectionCampCron.php (1)
77-78: LGTM! The new method calls are well-placed.
The addition of updateContributorCount and updateContributionCount calls is appropriate here, as they're protected by the existing try-catch block and executed after sending the logistics email.
wp-content/civi-extensions/goonjcustom/Civi/InstitutionCollectionCampService.php (1)
684-684: Fix spelling in field name
The field name 'Monitory_Contribution' appears to be a spelling error. It should probably be 'Monetary_Contribution'.
| $contributions = Contribution::get(FALSE) | ||
| ->addSelect('total_amount') | ||
| ->addWhere('Contribution_Details.Source', '=', $collectionCamp['id']) | ||
| ->addWhere('is_test', 'IS NOT NULL') |
There was a problem hiding this comment.
Fix the is_test condition in the WHERE clause
The current condition is_test IS NOT NULL will include test contributions. You probably want to exclude test contributions.
- ->addWhere('is_test', 'IS NOT NULL')
+ ->addWhere('is_test', '=', 0)📝 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.
| ->addWhere('is_test', 'IS NOT NULL') | |
| ->addWhere('is_test', '=', 0) |
| public static function updateContributorCount($collectionCamp) { | ||
| $activities = Activity::get(FALSE) | ||
| ->addSelect('id') | ||
| ->addWhere('Material_Contribution.Institution_Collection_Camp', '=', $collectionCamp['id']) | ||
| ->execute(); | ||
|
|
||
| $contributorCount = count($activities); | ||
|
|
||
| EckEntity::update('Collection_Camp', FALSE) | ||
| ->addValue('Camp_Outcome.Number_of_Contributors', $contributorCount) | ||
| ->addWhere('id', '=', $collectionCamp['id']) | ||
| ->execute(); | ||
| } |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Add input parameter validation
The method should validate the $collectionCamp parameter before use. Consider checking if it's an array and if it has an 'id' key.
public static function updateContributorCount($collectionCamp) {
+ if (!is_array($collectionCamp) || !isset($collectionCamp['id'])) {
+ throw new \InvalidArgumentException('Invalid collection camp parameter');
+ }
+
$activities = Activity::get(FALSE)📝 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.
| public static function updateContributorCount($collectionCamp) { | |
| $activities = Activity::get(FALSE) | |
| ->addSelect('id') | |
| ->addWhere('Material_Contribution.Institution_Collection_Camp', '=', $collectionCamp['id']) | |
| ->execute(); | |
| $contributorCount = count($activities); | |
| EckEntity::update('Collection_Camp', FALSE) | |
| ->addValue('Camp_Outcome.Number_of_Contributors', $contributorCount) | |
| ->addWhere('id', '=', $collectionCamp['id']) | |
| ->execute(); | |
| } | |
| public static function updateContributorCount($collectionCamp) { | |
| if (!is_array($collectionCamp) || !isset($collectionCamp['id'])) { | |
| throw new \InvalidArgumentException('Invalid collection camp parameter'); | |
| } | |
| $activities = Activity::get(FALSE) | |
| ->addSelect('id') | |
| ->addWhere('Material_Contribution.Institution_Collection_Camp', '=', $collectionCamp['id']) | |
| ->execute(); | |
| $contributorCount = count($activities); | |
| EckEntity::update('Collection_Camp', FALSE) | |
| ->addValue('Camp_Outcome.Number_of_Contributors', $contributorCount) | |
| ->addWhere('id', '=', $collectionCamp['id']) | |
| ->execute(); | |
| } |
| public static function updateContributionCount($collectionCamp) { | ||
| $contributions = Contribution::get(FALSE) | ||
| ->addSelect('total_amount') | ||
| ->addWhere('Contribution_Details.Source', '=', $collectionCamp['id']) | ||
| ->addWhere('is_test', 'IS NOT NULL') | ||
| ->execute(); | ||
|
|
||
| // Initialize sum variable. | ||
| $totalSum = 0; | ||
|
|
||
| // Iterate through the results and sum the total_amount. | ||
| foreach ($contributions as $contribution) { | ||
| $totalSum += $contribution['total_amount']; | ||
| } | ||
|
|
||
| EckEntity::update('Collection_Camp', FALSE) | ||
| ->addValue('Camp_Outcome.Monitory_Contribution', $totalSum) | ||
| ->addWhere('id', '=', $collectionCamp['id']) | ||
| ->execute(); | ||
| } |
There was a problem hiding this comment.
🛠️ Refactor suggestion
Add input validation and type safety
The method needs input validation and should ensure numeric type safety for calculations.
public static function updateContributionCount($collectionCamp) {
+ if (!is_array($collectionCamp) || !isset($collectionCamp['id'])) {
+ throw new \InvalidArgumentException('Invalid collection camp parameter');
+ }
+
$contributions = Contribution::get(FALSE)
->addSelect('total_amount')
->addWhere('Contribution_Details.Source', '=', $collectionCamp['id'])
- ->addWhere('is_test', 'IS NOT NULL')
+ ->addWhere('is_test', '=', 0)
->execute();
// Initialize sum variable.
$totalSum = 0;
// Iterate through the results and sum the total_amount.
foreach ($contributions as $contribution) {
- $totalSum += $contribution['total_amount'];
+ $totalSum += (float)($contribution['total_amount'] ?? 0);
}
EckEntity::update('Collection_Camp', FALSE)
->addValue('Camp_Outcome.Monitory_Contribution', $totalSum)
->addWhere('id', '=', $collectionCamp['id'])
->execute();
}📝 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.
| public static function updateContributionCount($collectionCamp) { | |
| $contributions = Contribution::get(FALSE) | |
| ->addSelect('total_amount') | |
| ->addWhere('Contribution_Details.Source', '=', $collectionCamp['id']) | |
| ->addWhere('is_test', 'IS NOT NULL') | |
| ->execute(); | |
| // Initialize sum variable. | |
| $totalSum = 0; | |
| // Iterate through the results and sum the total_amount. | |
| foreach ($contributions as $contribution) { | |
| $totalSum += $contribution['total_amount']; | |
| } | |
| EckEntity::update('Collection_Camp', FALSE) | |
| ->addValue('Camp_Outcome.Monitory_Contribution', $totalSum) | |
| ->addWhere('id', '=', $collectionCamp['id']) | |
| ->execute(); | |
| } | |
| public static function updateContributionCount($collectionCamp) { | |
| if (!is_array($collectionCamp) || !isset($collectionCamp['id'])) { | |
| throw new \InvalidArgumentException('Invalid collection camp parameter'); | |
| } | |
| $contributions = Contribution::get(FALSE) | |
| ->addSelect('total_amount') | |
| ->addWhere('Contribution_Details.Source', '=', $collectionCamp['id']) | |
| ->addWhere('is_test', '=', 0) | |
| ->execute(); | |
| // Initialize sum variable. | |
| $totalSum = 0; | |
| // Iterate through the results and sum the total_amount. | |
| foreach ($contributions as $contribution) { | |
| $totalSum += (float)($contribution['total_amount'] ?? 0); | |
| } | |
| EckEntity::update('Collection_Camp', FALSE) | |
| ->addValue('Camp_Outcome.Monitory_Contribution', $totalSum) | |
| ->addWhere('id', '=', $collectionCamp['id']) | |
| ->execute(); | |
| } |
The functions update contributor and contribution counts for a collection camp. updateContributorCount counts and updates the number of contributors, while updateContributionCount sums and updates the total monetary contributions.