-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Update Campaign Donation Query (#7630)
- Loading branch information
Showing
17 changed files
with
350 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
namespace Give\Campaigns\Migrations\Donations; | ||
|
||
use Give\Donations\ValueObjects\DonationMetaKeys; | ||
use Give\Framework\Database\DB; | ||
use Give\Framework\Database\Exceptions\DatabaseQueryException; | ||
use Give\Framework\Migrations\Contracts\Migration; | ||
use Give\Framework\Migrations\Exceptions\DatabaseMigrationException; | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
class AddCampaignId extends Migration | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public static function id(): string | ||
{ | ||
return 'add_campaign_id_to_donations'; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public static function title(): string | ||
{ | ||
return 'Add campaign id to donations'; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public static function timestamp(): string | ||
{ | ||
return strtotime('2024-11-22 00:00:00'); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
* @throws DatabaseMigrationException | ||
*/ | ||
public function run() | ||
{ | ||
$relationships = []; | ||
|
||
try { | ||
$data = DB::table('give_campaign_forms') | ||
->select('campaign_id', 'form_id') | ||
->getAll(); | ||
|
||
foreach ($data as $relationship) { | ||
$relationships[$relationship->campaign_id][] = $relationship->form_id; | ||
} | ||
|
||
$donations = DB::table('posts') | ||
->select('ID') | ||
->attachMeta( | ||
'give_donationmeta', | ||
'ID', | ||
'donation_id', | ||
[DonationMetaKeys::FORM_ID(), 'formId'] | ||
) | ||
->where('post_type', 'give_payment') | ||
->getAll(); | ||
|
||
$donationMeta = []; | ||
|
||
foreach ($donations as $donation) { | ||
foreach ($relationships as $campaignId => $formIds) { | ||
if (in_array($donation->formId, $formIds)) { | ||
$donationMeta[] = [ | ||
'donation_id' => $donation->ID, | ||
'meta_key' => DonationMetaKeys::CAMPAIGN_ID, | ||
'meta_value' => $campaignId, | ||
]; | ||
|
||
break; | ||
} | ||
} | ||
} | ||
|
||
if ( ! empty($donationMeta)) { | ||
DB::table('give_donationmeta') | ||
->insert($donationMeta, ['%d', '%s', '%d']); | ||
} | ||
} catch (DatabaseQueryException $exception) { | ||
throw new DatabaseMigrationException("An error occurred while adding campaign ID to the donation meta table", 0, $exception); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.