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
108 changes: 75 additions & 33 deletions src/Campaigns/Migrations/MigrateFormsToCampaignForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

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,7 +29,7 @@ public static function id(): string
*/
public static function timestamp(): int
{
return strtotime('2024-08-21');
return strtotime('2024-08-26 00:00:01');
}

/**
Expand All @@ -38,57 +38,99 @@ public static function timestamp(): int
*/
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
{
return DB::table('posts', 'forms')
->select(
['ID', 'id'],
['post_title', 'title'],
['post_status', 'status'],
['post_date', 'createdAt'],
['meta_value', 'settings']
)
->join(function (JoinQueryBuilder $builder) {
$builder
->leftJoin('give_formmeta', 'formmeta')
->on('formmeta.form_id', 'forms.ID');
})
->where('forms.post_type', 'give_forms')
->where('formmeta.meta_key', 'formBuilderSettings')
->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' => CampaignType::CORE()->getValue(),
kjohnson marked this conversation as resolved.
Show resolved Hide resolved
'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();
return CampaignStatus::PENDING()->getValue();

case DonationFormStatus::DRAFT():
return CampaignStatus::DRAFT();
return CampaignStatus::DRAFT()->getValue();

case DonationFormStatus::TRASH():
return CampaignStatus::INACTIVE();
return CampaignStatus::INACTIVE()->getValue();

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()->getValue();

default: // TODO: How do we handle an unknown form status?
return CampaignStatus::INACTIVE()->getValue();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ 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();

Expand Down
Loading