Skip to content

Commit

Permalink
Feature: Add migration to create initial campaigns from existing dona…
Browse files Browse the repository at this point in the history
…tion forms (#7507)
  • Loading branch information
kjohnson authored Aug 28, 2024
1 parent 3f1b23f commit 0b24a19
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 3 deletions.
95 changes: 95 additions & 0 deletions src/Campaigns/Migrations/MigrateFormsToCampaignForms.php
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();
}
}
}
5 changes: 2 additions & 3 deletions src/Campaigns/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Give\Campaigns;

use Give\Campaigns\Actions\DeleteCampaignPage;
use Give\Campaigns\Migrations\MigrateFormsToCampaignForms;
use Give\Campaigns\Migrations\P2P\SetCampaignType;
use Give\Campaigns\Migrations\Tables\CreateCampaignFormsTable;
use Give\Campaigns\Migrations\Tables\CreateCampaignsTable;
Expand Down Expand Up @@ -35,7 +36,6 @@ public function boot(): void
$this->registerMigrations();
}


/**
* @unreleased
*/
Expand All @@ -46,11 +46,11 @@ private function registerMigrations(): void
CreateCampaignsTable::class,
SetCampaignType::class,
CreateCampaignFormsTable::class,
MigrateFormsToCampaignForms::class,
]
);
}


/**
* @unreleased
*/
Expand All @@ -62,7 +62,6 @@ private function registerTableNames(): void
$wpdb->give_campaign_forms = $wpdb->prefix . 'give_campaign_forms';
}


/**
* @unreleased
*/
Expand Down
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);
}
}

0 comments on commit 0b24a19

Please sign in to comment.