-
Notifications
You must be signed in to change notification settings - Fork 314
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* variables and mentions * fix lint * add missing changes * fix tests * update quilly, fix bugs * fix lint * apply fixes * apply fixes * Fix MentionParser * Apply Mentions everywhere * Fix MentionParserTest * Small refactoring * Fixing quill import issues * Polished email integration, added customer sender mail * Add missing changes * improve migration command --------- Co-authored-by: Frank <[email protected]> Co-authored-by: Julien Nahum <[email protected]>
- Loading branch information
1 parent
2fdf2a4
commit dad5c82
Showing
50 changed files
with
1,901 additions
and
872 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
api/app/Console/Commands/EmailNotificationMigration.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
<?php | ||
|
||
namespace App\Console\Commands; | ||
|
||
use App\Models\Forms\Form; | ||
use App\Models\Integration\FormIntegration; | ||
use Illuminate\Console\Command; | ||
|
||
class EmailNotificationMigration extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'forms:email-notification-migration'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'One Time Only -- Migrate Email & Submission Notifications to new Email Integration'; | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return int | ||
*/ | ||
public function handle() | ||
{ | ||
if (app()->environment('production')) { | ||
if (!$this->confirm('Are you sure you want to run this migration in production?')) { | ||
$this->info('Migration aborted.'); | ||
return 0; | ||
} | ||
} | ||
$query = FormIntegration::whereIn('integration_id', ['email', 'submission_confirmation']) | ||
->whereHas('form'); | ||
$totalCount = $query->count(); | ||
$progressBar = $this->output->createProgressBar($totalCount); | ||
$progressBar->start(); | ||
|
||
$query->with('form')->chunk(100, function ($integrations) use ($progressBar) { | ||
foreach ($integrations as $integration) { | ||
try { | ||
$this->updateIntegration($integration); | ||
} catch (\Exception $e) { | ||
$this->error('Error updating integration ' . $integration->id . '. Error: ' . $e->getMessage()); | ||
ray($e); | ||
} | ||
$progressBar->advance(); | ||
} | ||
}); | ||
|
||
$progressBar->finish(); | ||
$this->newLine(); | ||
|
||
$this->line('Migration Done'); | ||
} | ||
|
||
public function updateIntegration(FormIntegration $integration) | ||
{ | ||
if (!$integration->form) { | ||
return; | ||
} | ||
$existingData = $integration->data; | ||
if ($integration->integration_id === 'email') { | ||
$integration->data = [ | ||
'send_to' => $existingData->notification_emails ?? null, | ||
'sender_name' => 'OpnForm', | ||
'subject' => 'New form submission', | ||
'email_content' => 'Hello there 👋 <br>New form submission received.', | ||
'include_submission_data' => true, | ||
'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 = [ | ||
'send_to' => $this->getMentionHtml($integration->form), | ||
'sender_name' => $existingData->notification_sender, | ||
'subject' => $existingData->notification_subject, | ||
'email_content' => $existingData->notification_body, | ||
'include_submission_data' => $existingData->notifications_include_submission, | ||
'include_hidden_fields_submission_data' => false, | ||
'reply_to' => $existingData->confirmation_reply_to ?? null | ||
]; | ||
} | ||
return $integration->save(); | ||
} | ||
|
||
private function getMentionHtml(Form $form) | ||
{ | ||
$emailField = $this->getRespondentEmail($form); | ||
return $emailField ? '<span mention-field-id="' . $emailField['id'] . '" mention-field-name="' . $emailField['name'] . '" mention-fallback="" contenteditable="false" mention="true">' . $emailField['name'] . '</span>' : ''; | ||
} | ||
|
||
private function getRespondentEmail(Form $form) | ||
{ | ||
$emailFields = collect($form->properties)->filter(function ($field) { | ||
$hidden = $field['hidden'] ?? false; | ||
return !$hidden && $field['type'] == 'email'; | ||
}); | ||
|
||
return $emailFields->count() > 0 ? $emailFields->first() : null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.