Skip to content

Commit

Permalink
refactor: Update migration closer to data layer
Browse files Browse the repository at this point in the history
  • Loading branch information
kjohnson committed Sep 4, 2024
1 parent f12b328 commit b2e4867
Showing 1 changed file with 73 additions and 34 deletions.
107 changes: 73 additions & 34 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,96 @@ public static function timestamp(): int
*/
public function run()
{
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'],
['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'];
$formSettings = json_decode($formData['settings']);

$campaignId = DB::table('give_campaigns')
->insert([
'type' => CampaignType::CORE(),
'title' => $formTitle,
'status' => $this->mapFormToCampaignStatus($formStatus),
'shortDescription' => $formSettings->formExcerpt,
'longDescription' => $formSettings->description,
'logo' => $formSettings->designSettingsLogoUrl,
'image' => $formSettings->designSettingsImageUrl,
'primaryColor' => $formSettings->primaryColor,
'secondaryColor' => $formSettings->secondaryColor,
'goal' => $formSettings->goalAmount,
'startDate' => $formSettings->goalStartDate,
'endDate' => $formSettings->goalEndDate,
]);

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();
switch (new DonationFormStatus($status)) {

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

0 comments on commit b2e4867

Please sign in to comment.