Skip to content

Commit

Permalink
improve email notfication migration (#602)
Browse files Browse the repository at this point in the history
* improve email notfication migration

* Fix lint

* Added dry run

---------

Co-authored-by: Julien Nahum <[email protected]>
  • Loading branch information
chiragchhatrala and JhumanJ authored Oct 24, 2024
1 parent 46359fb commit 6bdce23
Showing 1 changed file with 24 additions and 10 deletions.
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);
} 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

0 comments on commit 6bdce23

Please sign in to comment.