From 6bdce23967a9ef61604b55025258753def773922 Mon Sep 17 00:00:00 2001 From: Chirag Chhatrala <60499540+chiragchhatrala@users.noreply.github.com> Date: Thu, 24 Oct 2024 19:18:04 +0530 Subject: [PATCH] improve email notfication migration (#602) * improve email notfication migration * Fix lint * Added dry run --------- Co-authored-by: Julien Nahum --- .../Commands/EmailNotificationMigration.php | 34 +++++++++++++------ 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/api/app/Console/Commands/EmailNotificationMigration.php b/api/app/Console/Commands/EmailNotificationMigration.php index 15230ebd..bed297ec 100644 --- a/api/app/Console/Commands/EmailNotificationMigration.php +++ b/api/app/Console/Commands/EmailNotificationMigration.php @@ -13,7 +13,7 @@ class EmailNotificationMigration extends Command * * @var string */ - protected $signature = 'forms:email-notification-migration'; + protected $signature = 'forms:email-notification-migration {--dry : Log changes without applying them}'; /** * The console command description. @@ -41,10 +41,12 @@ public function handle() $progressBar = $this->output->createProgressBar($totalCount); $progressBar->start(); - $query->with('form')->chunk(100, function ($integrations) use ($progressBar) { + $isDryRun = $this->option('dry'); + + $query->with('form')->chunk(100, function ($integrations) use ($progressBar, $isDryRun) { foreach ($integrations as $integration) { try { - $this->updateIntegration($integration); + $this->updateIntegration($integration, $isDryRun); } catch (\Exception $e) { $this->error('Error updating integration ' . $integration->id . '. Error: ' . $e->getMessage()); ray($e); @@ -59,14 +61,14 @@ public function handle() $this->line('Migration Done'); } - public function updateIntegration(FormIntegration $integration) + public function updateIntegration(FormIntegration $integration, $isDryRun = false) { if (!$integration->form) { return; } $existingData = $integration->data; - if ($integration->integration_id === 'email') { - $integration->data = [ + if ($integration->integration_id === 'email' && isset($existingData->notification_emails)) { + $newData = [ 'send_to' => $existingData->notification_emails ?? null, 'sender_name' => 'OpnForm', 'subject' => 'New form submission', @@ -75,9 +77,14 @@ public function updateIntegration(FormIntegration $integration) 'include_hidden_fields_submission_data' => false, 'reply_to' => $existingData->notification_reply_to ?? null ]; - } elseif ($integration->integration_id === 'submission_confirmation') { - $integration->integration_id = 'email'; - $integration->data = [ + if ($isDryRun) { + $this->info('Dry run: Would update integration ' . $integration->id . ' with data: ' . json_encode($newData)); + } else { + $integration->data = $newData; + return $integration->save(); + } + } elseif ($integration->integration_id === 'submission_confirmation' && isset($existingData->notification_subject)) { + $newData = [ 'send_to' => $this->getMentionHtml($integration->form), 'sender_name' => $existingData->notification_sender, 'subject' => $existingData->notification_subject, @@ -86,8 +93,15 @@ public function updateIntegration(FormIntegration $integration) 'include_hidden_fields_submission_data' => false, 'reply_to' => $existingData->confirmation_reply_to ?? null ]; + if ($isDryRun) { + $this->info('Dry run: Would update integration ' . $integration->id . ' with data: ' . json_encode($newData)); + } else { + $integration->integration_id = 'email'; + $integration->data = $newData; + return $integration->save(); + } } - return $integration->save(); + return; } private function getMentionHtml(Form $form)