Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Update Forms to Campaigns migration #7532

Merged
merged 12 commits into from
Sep 12, 2024
127 changes: 88 additions & 39 deletions src/Campaigns/Migrations/MigrateFormsToCampaignForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@

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\Database\Exceptions\DatabaseQueryException;
use Give\Framework\Migrations\Contracts\Migration;
use Give\Framework\Migrations\Exceptions\DatabaseMigrationException;
use Give\Framework\QueryBuilder\JoinQueryBuilder;

/**
* @unreleased
Expand All @@ -29,66 +26,118 @@ public static function id(): string
*/
public static function timestamp(): int
{
return strtotime('2024-08-21');
return strtotime('2024-08-26 00:00:01');
}

/**
* @unreleased
* @inheritDoc
* @throws \Exception
*/
public function run()
kjohnson marked this conversation as resolved.
Show resolved Hide resolved
{
foreach(DonationForm::query()->getAll() ?? [] as $form) {
$this->createParentCampaignForDonationForm($form);
}
DB::transaction(function() {
try {
foreach($this->getFormData() as $formData) {
$this->createParentCampaignForDonationForm($formData);
}
} catch (DatabaseQueryException $exception) {
DB::rollback();
throw new DatabaseMigrationException('An error occurred while creating initial campaigns', 0, $exception);
}
});
}

/**
* @unreleased
*/
protected function getFormData(): array
{
$query = DB::table('posts', 'forms')
->select(
['forms.ID', 'id'],
['forms.post_title', 'title'],
['forms.post_status', 'status'],
['forms.post_date', 'createdAt']
)
->where('forms.post_type', 'give_forms');

$query->select(['formmeta.meta_value', 'settings'])
->join(function (JoinQueryBuilder $builder) {
$builder
->leftJoin('give_formmeta', 'formmeta')
->on('formmeta.form_id', 'forms.ID');
})
->where('formmeta.meta_key', 'formBuilderSettings');

$query->join(function (JoinQueryBuilder $builder) {
$builder
->leftJoin('give_campaigns', 'campaigns')
->on('campaigns.form_id', 'forms.ID');
})
->whereIsNull('campaigns.id');

return $query->getAll();
}

/**
* @unreleased
*/
public function createParentCampaignForDonationForm(DonationForm $form)
public function createParentCampaignForDonationForm($formData): void
{
$campaign = Campaign::create([
'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),
]);
$formId = $formData->id;
$formTitle = $formData->title;
$formStatus = $formData->status;
$formCreatedAt = $formData->createdAt;
$formSettings = json_decode($formData->settings);

$campaignId = DB::table('give_campaigns')
->insert([
'campaign_type' => 'core',
'campaign_title' => $formTitle,
'status' => $this->mapFormToCampaignStatus($formStatus),
'short_desc' => $formSettings->formExcerpt,
'long_desc' => $formSettings->description,
'campaign_logo' => $formSettings->designSettingsLogoUrl,
'campaign_image' => $formSettings->designSettingsImageUrl,
'primary_color' => $formSettings->primaryColor,
'secondary_color' => $formSettings->secondaryColor,
'campaign_goal' => $formSettings->goalAmount,
'start_date' => $formSettings->goalStartDate,
'end_date' => $formSettings->goalEndDate,
'date_created' => $formCreatedAt,
]);

DB::table('give_campaign_forms')
->insert([
'form_id' => $form->id,
'campaign_id' => $campaign->id,
'form_id' => $formId,
'campaign_id' => $campaignId,
]);
}

/**
/**
* @unreleased
*/
public function mapFormStatusToCampaignStatus(DonationFormStatus $status)
public function mapFormToCampaignStatus(string $status): string
{
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 'pending':
return 'pending';

case 'draft':
return 'draft';

case 'trash':
return 'inactive';

case DonationFormStatus::DRAFT():
return CampaignStatus::DRAFT();
case 'publish':
case 'private':
case 'upgraded':
kjohnson marked this conversation as resolved.
Show resolved Hide resolved
return 'active';

case DonationFormStatus::TRASH():
return CampaignStatus::INACTIVE();
default: // TODO: How do we handle an unknown form status?
return 'inactive';
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Give\Campaigns\Migrations\MigrateFormsToCampaignForms;
use Give\Campaigns\Models\Campaign;
use Give\Campaigns\ValueObjects\CampaignType;
use Give\DonationForms\Models\DonationForm;
use Give\Framework\Database\DB;
use Give\Tests\TestCase;
Expand All @@ -24,11 +25,27 @@ public function testCreatesParentCampaignForDonationForm()
$form = DonationForm::factory()->create();
$migration = new MigrateFormsToCampaignForms();

$migration->createParentCampaignForDonationForm($form);
$migration->run();

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

public function testExistingPeerToPeerCampaignFormsAreNotMigrated()
{
$form = DonationForm::factory()->create();
DB::table('give_campaigns')->insert([
'form_id' => $form->id,
]);

$migration = new MigrateFormsToCampaignForms();
$migration->run();

$relationship = DB::table('give_campaign_forms')->where('form_id', $form->id)->get();

$this->assertNull($relationship);
$this->assertEquals(1, DB::table('give_campaigns')->count());
}
}
Loading