From b2e4867bd42893286e3b9f4c88b41788cac25de9 Mon Sep 17 00:00:00 2001 From: "Kyle B. Johnson" Date: Wed, 4 Sep 2024 13:35:39 -0400 Subject: [PATCH] refactor: Update migration closer to data layer --- .../MigrateFormsToCampaignForms.php | 107 ++++++++++++------ 1 file changed, 73 insertions(+), 34 deletions(-) diff --git a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php index 5f8f529cd3..110c7c68ef 100644 --- a/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php +++ b/src/Campaigns/Migrations/MigrateFormsToCampaignForms.php @@ -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 @@ -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'); } /** @@ -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(); } } }