-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Add migration to create initial campaigns from existing dona…
…tion forms (#7507)
- Loading branch information
Showing
3 changed files
with
131 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
<?php | ||
|
||
namespace Give\Campaigns\Migrations; | ||
|
||
use DateTime; | ||
use Give\Campaigns\Models\Campaign; | ||
use Give\Campaigns\ValueObjects\CampaignStatus; | ||
use Give\Campaigns\ValueObjects\CampaignType; | ||
use Give\DonationForms\Models\DonationForm; | ||
use Give\DonationForms\ValueObjects\DonationFormStatus; | ||
use Give\Framework\Database\DB; | ||
use Give\Framework\Migrations\Contracts\Migration; | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
class MigrateFormsToCampaignForms extends Migration | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
public static function id(): string | ||
{ | ||
return 'migrate_forms_to_campaign_forms'; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public static function timestamp(): int | ||
{ | ||
return strtotime('2024-08-21'); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
* @inheritDoc | ||
*/ | ||
public function run() | ||
{ | ||
foreach(DonationForm::query()->getAll() ?? [] as $form) { | ||
$this->createParentCampaignForDonationForm($form); | ||
} | ||
} | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public function createParentCampaignForDonationForm(DonationForm $form) | ||
{ | ||
$campaign = Campaign::create([ | ||
'pageId' => 0, | ||
'type' => CampaignType::CORE(), | ||
'title' => $form->title, | ||
'shortDescription' => $form->settings->formExcerpt, | ||
'longDescription' => $form->settings->description, | ||
'logo' => $form->settings->designSettingsLogoUrl, | ||
'image' => $form->settings->designSettingsImageUrl, | ||
'primaryColor' => $form->settings->primaryColor, | ||
'secondaryColor' => $form->settings->secondaryColor, | ||
'goal' => (int) $form->settings->goalAmount, | ||
'status' => $this->mapFormStatusToCampaignStatus($form->status), | ||
'startDate' => new DateTime($form->settings->goalStartDate), | ||
'endDate' => new DateTime($form->settings->goalEndDate), | ||
]); | ||
|
||
DB::table('give_campaign_forms') | ||
->insert([ | ||
'form_id' => $form->id, | ||
'campaign_id' => $campaign->id, | ||
]); | ||
} | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public function mapFormStatusToCampaignStatus(DonationFormStatus $status) | ||
{ | ||
switch ($status) { | ||
case DonationFormStatus::PUBLISHED(): | ||
case DonationFormStatus::UPGRADED(): // TODO: How do we handle upgraded, non-upgraded forms? | ||
case DonationFormStatus::PRIVATE(): // TODO: How do we handle Private forms? | ||
return CampaignStatus::ACTIVE(); | ||
|
||
case DonationFormStatus::PENDING(): | ||
return CampaignStatus::PENDING(); | ||
|
||
case DonationFormStatus::DRAFT(): | ||
return CampaignStatus::DRAFT(); | ||
|
||
case DonationFormStatus::TRASH(): | ||
return CampaignStatus::INACTIVE(); | ||
} | ||
} | ||
} |
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
34 changes: 34 additions & 0 deletions
34
tests/Unit/Campaigns/Migrations/MigrateFormsToCampaignFormsTest.php
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,34 @@ | ||
<?php | ||
|
||
namespace Give\Tests\Unit\Campaigns\Migrations; | ||
|
||
use Give\Campaigns\Migrations\MigrateFormsToCampaignForms; | ||
use Give\Campaigns\Models\Campaign; | ||
use Give\DonationForms\Models\DonationForm; | ||
use Give\Framework\Database\DB; | ||
use Give\Tests\TestCase; | ||
use Give\Tests\TestTraits\RefreshDatabase; | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
final class MigrateFormsToCampaignFormsTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
/** | ||
* @unreleased | ||
*/ | ||
public function testCreatesParentCampaignForDonationForm() | ||
{ | ||
$form = DonationForm::factory()->create(); | ||
$migration = new MigrateFormsToCampaignForms(); | ||
|
||
$migration->createParentCampaignForDonationForm($form); | ||
|
||
$relationship = DB::table('give_campaign_forms')->where('form_id', $form->id)->get(); | ||
|
||
$this->assertNotNull(Campaign::find($relationship->campaign_id)); | ||
$this->assertEquals($form->id, $relationship->form_id); | ||
} | ||
} |