Apply previous changes: slack webhooks, date range issue, 12h am/pm format, today preifl, api-keys

This commit is contained in:
JhumanJ
2022-09-21 17:23:37 +02:00
parent 341ee04268
commit 18ed28da2e
19 changed files with 305 additions and 132 deletions

View File

@@ -40,6 +40,7 @@ abstract class UserFormRequest extends \Illuminate\Foundation\Http\FormRequest
'notifications_include_submission' => 'boolean',
'webhook_url' => 'url|nullable',
'use_captcha' => 'boolean',
'slack_webhook_url' => 'url|nullable',
// Customization
'theme' => ['required',Rule::in(Form::THEMES)],
@@ -96,7 +97,9 @@ abstract class UserFormRequest extends \Illuminate\Foundation\Http\FormRequest
// Date field
'properties.*.with_time' => 'boolean|nullable',
'properties.*.use_am_pm' => 'boolean|nullable',
'properties.*.date_range' => 'boolean|nullable',
'properties.*.prefill_today' => 'boolean|nullable',
// Select / Multi Select field
'properties.*.allow_creation' => 'boolean|nullable',

View File

@@ -29,6 +29,7 @@ class FormResource extends JsonResource
'views_count' => $this->when($this->workspace->is_pro, $this->views_count),
'submissions_count' => $this->when($this->workspace->is_pro, $this->submissions_count),
'notifies' => $this->notifies,
'notifies_slack' => $this->notifies_slack,
'send_submission_confirmation' => $this->send_submission_confirmation,
'webhook_url' => $this->webhook_url,
'redirect_url' => $this->redirect_url,
@@ -42,6 +43,7 @@ class FormResource extends JsonResource
'password' => $this->password,
'tags' => $this->tags,
'notification_emails' => $this->notification_emails,
'slack_webhook_url' => $this->slack_webhook_url,
] : [];
$baseData = $this->getFilteredFormData(parent::toArray($request), $userIsFormOwner);

View File

@@ -7,6 +7,8 @@ use App\Notifications\Forms\FormSubmissionNotification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Support\Facades\Notification;
use Spatie\WebhookServer\WebhookCall;
use App\Service\Forms\FormSubmissionFormatter;
class NotifyFormSubmission implements ShouldQueue
{
@@ -20,18 +22,77 @@ class NotifyFormSubmission implements ShouldQueue
*/
public function handle(FormSubmitted $event)
{
if (!$event->form->notifies || !$event->form->is_pro) return;
if (!$event->form->is_pro) return;
$subscribers = collect(preg_split("/\r\n|\n|\r/", $event->form->notification_emails))->filter(function($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
});
\Log::debug('Sending email notification',[
'recipients' => $subscribers->toArray(),
'form_id' => $event->form->id,
'form_slug' => $event->form->slug,
]);
$subscribers->each(function ($subscriber) use ($event) {
Notification::route('mail', $subscriber)->notify(new FormSubmissionNotification($event));
});
if ($event->form->notifies) {
// Send Email Notification
$subscribers = collect(preg_split("/\r\n|\n|\r/", $event->form->notification_emails))->filter(function($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL);
});
\Log::debug('Sending email notification',[
'recipients' => $subscribers->toArray(),
'form_id' => $event->form->id,
'form_slug' => $event->form->slug,
]);
$subscribers->each(function ($subscriber) use ($event) {
Notification::route('mail', $subscriber)->notify(new FormSubmissionNotification($event));
});
}
if ($event->form->notifies_slack) {
// Send Slack Notification
$this->sendSlackNotification($event);
}
}
private function sendSlackNotification(FormSubmitted $event)
{
if($this->validateSlackWebhookUrl($event->form->slack_webhook_url)){
$submissionString = "";
$formatter = (new FormSubmissionFormatter($event->form, $event->data))->outputStringsOnly();
foreach ($formatter->getFieldsWithValue() as $field) {
$tmpVal = is_array($field['value']) ? implode(",", $field['value']) : $field['value'];
$submissionString .= ">*".ucfirst($field['name'])."*: ".$tmpVal." \n";
}
$formURL = url("forms/".$event->form->slug);
$editFormURL = url("forms/".$event->form->slug."/show");
$finalSlackPostData = [
'blocks' => [
[
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => 'New submission for your form *<'.$formURL.'|'.$event->form->title.':>*',
]
],
[
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => $submissionString
]
],
[
'type' => 'section',
'text' => [
'type' => 'mrkdwn',
'text' => '*<'.$formURL.'|🔗 Open Form>* *<'.$editFormURL.'|✍️ Edit Form>*',
]
],
]
];
WebhookCall::create()
->url($event->form->slack_webhook_url)
->doNotSign()
->payload($finalSlackPostData)
->dispatch();
}
}
private function validateSlackWebhookUrl($url)
{
return ($url) ? str_contains($url, 'https://hooks.slack.com/') : false;
}
}

View File

@@ -37,6 +37,7 @@ class Form extends Model
'notification_subject',
'notification_body',
'notifications_include_submission',
'slack_webhook_url',
// integrations
'webhook_url',
@@ -94,6 +95,7 @@ class Form extends Model
protected $hidden = [
'workspace_id',
'notifies',
'slack_webhook_url',
'webhook_url',
'send_submission_confirmation',
'redirect_url',
@@ -223,4 +225,9 @@ class Form extends Model
{
return FormFactory::new();
}
public function getNotifiesSlackAttribute()
{
return !empty($this->slack_webhook_url);
}
}

View File

@@ -4,7 +4,6 @@ namespace App\Models;
use App\Http\Controllers\SubscriptionController;
use App\Models\Forms\Form;
use App\Models\Workspace;
use App\Notifications\ResetPassword;
use App\Notifications\VerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
@@ -13,7 +12,6 @@ use Illuminate\Foundation\Auth\User as Authenticatable;
// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Notifications\Notifiable;
use Laravel\Cashier\Billable;
use Rickycezar\Impersonate\Models\Impersonate;
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Authenticatable implements JWTSubject //, MustVerifyEmail
@@ -205,5 +203,5 @@ class User extends Authenticatable implements JWTSubject //, MustVerifyEmail
});
});
}
}

View File

@@ -38,6 +38,7 @@ class FormCleaner
'theme' => 'default',
'use_captcha' => false,
'password' => null,
'slack_webhook_url' => null,
];
private array $fieldDefaults = [
@@ -68,6 +69,7 @@ class FormCleaner
'logo_picture' => 'The logo was removed.',
'database_fields_update' => 'Form submission will only create new records (no updates).',
'theme' => 'Default theme was applied.',
'slack_webhook_url' => "Slack webhook disabled.",
// For fields
'hide_field_name' => 'Hide field name removed.',