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

improve email notfication migration #602

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions api/app/Console/Commands/EmailNotificationMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Comment on lines +44 to +49
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider adding summary statistics logging.

While error handling is present, it would be helpful to track the overall success/failure rate of the migration.

Add counters and summary logging:

     $isDryRun = $this->option('dry');
+    $successCount = 0;
+    $errorCount = 0;

-    $query->with('form')->chunk(100, function ($integrations) use ($progressBar, $isDryRun) {
+    $query->with('form')->chunk(100, function ($integrations) use ($progressBar, $isDryRun, &$successCount, &$errorCount) {
         foreach ($integrations as $integration) {
             try {
                 $this->updateIntegration($integration, $isDryRun);
+                $successCount++;
             } catch (\Exception $e) {
                 $this->error('Error updating integration ' . $integration->id . '. Error: ' . $e->getMessage());
                 ray($e);
+                $errorCount++;
             }
             $progressBar->advance();
         }
     });

     $progressBar->finish();
     $this->newLine();
+    $this->info("Migration completed with {$successCount} successful updates and {$errorCount} failures");
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
$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);
$isDryRun = $this->option('dry');
$successCount = 0;
$errorCount = 0;
$query->with('form')->chunk(100, function ($integrations) use ($progressBar, $isDryRun, &$successCount, &$errorCount) {
foreach ($integrations as $integration) {
try {
$this->updateIntegration($integration, $isDryRun);
$successCount++;
} catch (\Exception $e) {
$this->error('Error updating integration ' . $integration->id . '. Error: ' . $e->getMessage());
ray($e);
$errorCount++;
}
$progressBar->advance();
}
});
$progressBar->finish();
$this->newLine();
$this->info("Migration completed with {$successCount} successful updates and {$errorCount} failures");

} catch (\Exception $e) {
$this->error('Error updating integration ' . $integration->id . '. Error: ' . $e->getMessage());
ray($e);
Expand All @@ -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',
Expand All @@ -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,
Expand All @@ -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)
Expand Down
Loading