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

[ADVISING-1065]: Insert template into Text Message #327

Merged
merged 9 commits into from
Dec 7, 2023
5,157 changes: 2,655 additions & 2,502 deletions _ide_helper_models.php

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ public function definition(): array
return $sender->getKey();
},
'subject' => fake()->sentence,
'body' => fake()->paragraph,
'body_json' => ['type' => 'doc', 'content' => [['type' => 'paragraph', 'content' => [['type' => 'text', 'text' => fake()->paragraph]]]]],
'body' => ['type' => 'doc', 'content' => [['type' => 'paragraph', 'content' => [['type' => 'text', 'text' => fake()->paragraph]]]]],
'deliver_at' => fake()->dateTimeBetween('-1 year', '-1 day'),
'scheduled' => true,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public function up(): void
$table->string('recipient_id')->nullable();
$table->string('recipient_type')->nullable();
$table->string('subject')->nullable();
$table->longText('body')->nullable();
$table->json('body_json')->nullable();
$table->json('body')->nullable();
$table->boolean('scheduled')->default(true);
$table->timestamp('deliver_at');
$table->timestamps();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public function up(): void
$table->uuid('id')->primary();
$table->string('name');
$table->text('description')->nullable();
$table->text('content');
$table->json('content');
$table->foreignUuid('user_id')->nullable()->constrained()->nullOnDelete();
$table->timestamps();
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ public function handle(): void
'recipient_type' => $record->getMorphClass(),
'subject' => $this->data->subject,
'body' => $this->data->body,
'body_json' => $this->data->bodyJson,
'scheduled' => false,
]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,19 @@ public function deliver(): void
{
$client = new Client(config('services.twilio.account_sid'), config('services.twilio.auth_token'));

$messageContent = [
'from' => config('services.twilio.from_number'),
'body' => $this->deliverable->engagement->getBody(),
];

if (! app()->environment('local')) {
$messageContent['statusCallback'] = route('inbound.webhook.twilio', ['event' => 'status_callback']);
}

try {
$message = $client->messages->create(
! is_null(config('services.twilio.test_to_number')) ? config('services.twilio.test_to_number') : $this->deliverable->engagement->recipient->mobile,
[
'from' => config('services.twilio.from_number'),
'body' => $this->deliverable->engagement->getBody(),
'statusCallback' => route('inbound.webhook.twilio', ['event' => 'status_callback']),
]
$messageContent
);

$this->deliverable->update([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public function __construct(
public Collection $records,
public string $deliveryMethod,
public ?string $subject = null,
public ?string $body = null,
public ?array $bodyJson = null,
public ?array $body = null,
) {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
use Filament\Forms\Components\Select;
use FilamentTiptapEditor\TiptapEditor;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\Textarea;
use Filament\Tables\Actions\BulkAction;
use Filament\Forms\Components\TextInput;
use Illuminate\Database\Eloquent\Builder;
Expand All @@ -55,6 +54,7 @@
use Assist\Engagement\Enums\EngagementDeliveryMethod;
use Assist\Engagement\DataTransferObjects\EngagementBatchCreationData;
use Assist\Engagement\Filament\Actions\Contracts\HasBulkEngagementAction;
use Assist\Engagement\Filament\Resources\EngagementResource\Fields\EngagementSmsBodyField;

class BulkEngagementAction
{
Expand Down Expand Up @@ -84,7 +84,7 @@ public static function make(string $context)
->placeholder(__('Subject'))
->hidden(fn (Get $get): bool => $get('delivery_method') === EngagementDeliveryMethod::Sms->value)
->columnSpanFull(),
TiptapEditor::make('body_json')
TiptapEditor::make('body')
->label('Body')
->mergeTags([
'student full name',
Expand Down Expand Up @@ -146,13 +146,7 @@ public static function make(string $context)
->hidden(fn (Get $get): bool => $get('delivery_method') === EngagementDeliveryMethod::Sms->value)
->helperText('You can insert student information by typing {{ and choosing a tag to insert.')
->columnSpanFull(),
Textarea::make('body')
->placeholder('Body')
->required()
->maxLength(320) // https://www.twilio.com/docs/glossary/what-sms-character-limit#:~:text=Twilio's%20platform%20supports%20long%20messages,best%20deliverability%20and%20user%20experience.
->helperText('The body of your message can be up to 320 characters long.')
->visible(fn (Get $get): bool => $get('delivery_method') === EngagementDeliveryMethod::Sms->value)
->columnSpanFull(),
EngagementSmsBodyField::make(context: 'create'),
]),
])
->action(function (Collection $records, array $data) {
Expand All @@ -162,7 +156,6 @@ public static function make(string $context)
'deliveryMethod' => $data['delivery_method'],
'subject' => $data['subject'] ?? null,
'body' => $data['body'] ?? null,
'bodyJson' => $data['body_json'] ?? null,
]));
})
->modalSubmitActionLabel('Send')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public function __invoke(Model $educatable, array $data): void
$engagement = $educatable->engagements()->create([
'subject' => $data['subject'] ?? null,
'body' => $data['body'] ?? null,
'body_json' => $data['body_json'] ?? null,
'scheduled' => false,
]);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

/*
<COPYRIGHT>

Copyright © 2022-2023, Canyon GBS LLC. All rights reserved.

Advising App™ is licensed under the Elastic License 2.0. For more details,
see https://github.com/canyongbs/advisingapp/blob/main/LICENSE.

Notice:

- You may not provide the software to third parties as a hosted or managed
service, where the service provides users with access to any substantial set of
the features or functionality of the software.
- You may not move, change, disable, or circumvent the license key functionality
in the software, and you may not remove or obscure any functionality in the
software that is protected by the license key.
- You may not alter, remove, or obscure any licensing, copyright, or other notices
of the licensor in the software. Any use of the licensor’s trademarks is subject
to applicable law.
- Canyon GBS LLC respects the intellectual property rights of others and expects the
same in return. Canyon GBS™ and Advising App™ are registered trademarks of
Canyon GBS LLC, and we are committed to enforcing and protecting our trademarks
vigorously.
- The software solution, including services, infrastructure, and code, is offered as a
Software as a Service (SaaS) by Canyon GBS LLC.
- Use of this software implies agreement to the license terms and conditions as stated
in the Elastic License 2.0.

For more information or inquiries please visit our website at
https://www.canyongbs.com or contact us via email at [email protected].

</COPYRIGHT>
*/

namespace Assist\Engagement\Filament\Resources\EngagementResource\Fields;

use Filament\Forms\Get;
use Filament\Forms\Set;
use Filament\Forms\Form;
use Filament\Forms\Components\Field;
use Filament\Forms\Components\Select;
use FilamentTiptapEditor\TiptapEditor;
use Filament\Forms\Components\Checkbox;
use Assist\Engagement\Models\Engagement;
use Assist\Engagement\Models\SmsTemplate;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Expression;
use FilamentTiptapEditor\Enums\TiptapOutput;
use Filament\Forms\Components\Actions\Action;
use Assist\Engagement\Enums\EngagementDeliveryMethod;
use Filament\Resources\RelationManagers\RelationManager;

class EngagementSmsBodyField
{
public static function make(string $context, ?Form $form = null)
{
// TODO Implement length validation (320 characters max)
// https://www.twilio.com/docs/glossary/what-sms-character-limit#:~:text=Twilio's%20platform%20supports%20long%20messages,best%20deliverability%20and%20user%20experience.
return TiptapEditor::make('body')
->label('Body')
->mergeTags([
'student full name',
'student email',
])
->showMergeTagsInBlocksPanel(is_null($form) ? false : ! ($form->getLivewire() instanceof RelationManager))
->profile('sms')
->output(TiptapOutput::Json)
->required()
->hintAction(fn (TiptapEditor $component) => Action::make('loadSmsTemplate')
->form([
Select::make('smsTemplate')
->searchable()
->options(function (Get $get): array {
return SmsTemplate::query()
->when(
$get('onlyMyTemplates'),
fn (Builder $query) => $query->whereBelongsTo(auth()->user())
)
->orderBy('name')
->limit(50)
->pluck('name', 'id')
->toArray();
})
->getSearchResultsUsing(function (Get $get, string $search): array {
return SmsTemplate::query()
->when(
$get('onlyMyTemplates'),
fn (Builder $query) => $query->whereBelongsTo(auth()->user())
)
->when(
$get('onlyMyTeamTemplates'),
fn (Builder $query) => $query->whereIn('user_id', auth()->user()->teams->users->pluck('id'))
)
->where(new Expression('lower(name)'), 'like', "%{$search}%")
->orderBy('name')
->limit(50)
->pluck('name', 'id')
->toArray();
}),
Checkbox::make('onlyMyTemplates')
->label('Only show my templates')
->live()
->afterStateUpdated(fn (Set $set) => $set('smsTemplate', null)),
Checkbox::make('onlyMyTeamTemplates')
->label("Only show my team's templates")
->live()
->afterStateUpdated(fn (Set $set) => $set('smsTemplate', null)),
])
->action(function (array $data) use ($component) {
$template = SmsTemplate::find($data['smsTemplate']);

if (! $template) {
return;
}

$component->state($template->content);
}))
->when($context === 'create', function (Field $field) {
$field->hidden(fn (Get $get): bool => $get('delivery_method') === EngagementDeliveryMethod::Email->value);
})
->when($context === 'edit', function (Field $field) {
$field->visible(fn (Engagement $record): bool => $record->deliverable->channel === EngagementDeliveryMethod::Sms);
})
->helperText('You can insert student information by typing {{ and choosing a tag to insert.')
->columnSpanFull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
use FilamentTiptapEditor\TiptapEditor;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\Fieldset;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Query\Expression;
Expand All @@ -61,6 +60,7 @@
use Filament\Resources\RelationManagers\RelationManager;
use Assist\Engagement\Actions\CreateEngagementDeliverable;
use Assist\Engagement\Filament\Resources\EngagementResource;
use Assist\Engagement\Filament\Resources\EngagementResource\Fields\EngagementSmsBodyField;

class CreateEngagement extends CreateRecord
{
Expand All @@ -84,7 +84,7 @@ public function form(Form $form): Form
->placeholder(__('Subject'))
->hidden(fn (Get $get): bool => $get('delivery_method') === EngagementDeliveryMethod::Sms->value)
->columnSpanFull(),
TiptapEditor::make('body_json')
TiptapEditor::make('body')
->label('Body')
->mergeTags([
'student full name',
Expand Down Expand Up @@ -146,13 +146,7 @@ public function form(Form $form): Form
->hidden(fn (Get $get): bool => $get('delivery_method') === EngagementDeliveryMethod::Sms->value)
->helperText('You can insert student information by typing {{ and choosing a tag to insert.')
->columnSpanFull(),
Textarea::make('body')
->placeholder('Body')
->required()
->maxLength(320) // https://www.twilio.com/docs/glossary/what-sms-character-limit#:~:text=Twilio's%20platform%20supports%20long%20messages,best%20deliverability%20and%20user%20experience.
->helperText('The body of your message can be up to 320 characters long.')
->visible(fn (Get $get): bool => $get('delivery_method') === EngagementDeliveryMethod::Sms->value)
->columnSpanFull(),
EngagementSmsBodyField::make(context: 'create', form: $form),
]),
MorphToSelect::make('recipient')
->label('Recipient')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
use FilamentTiptapEditor\TiptapEditor;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\Fieldset;
use Filament\Forms\Components\Textarea;
use Assist\Engagement\Models\Engagement;
use Filament\Forms\Components\TextInput;
use Filament\Resources\Pages\EditRecord;
Expand All @@ -60,7 +59,9 @@
use FilamentTiptapEditor\Enums\TiptapOutput;
use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Components\DateTimePicker;
use Assist\Engagement\Enums\EngagementDeliveryMethod;
use Assist\Engagement\Filament\Resources\EngagementResource;
use Assist\Engagement\Filament\Resources\EngagementResource\Fields\EngagementSmsBodyField;

class EditEngagement extends EditRecord
{
Expand All @@ -74,8 +75,9 @@ public function form(Form $form): Form
->autofocus()
->required()
->placeholder(__('Subject'))
->columnSpanFull(),
TiptapEditor::make('body_json')
->columnSpanFull()
->visible(fn (Engagement $record): bool => $record->deliverable->channel === EngagementDeliveryMethod::Email),
TiptapEditor::make('body')
->label('Body')
->mergeTags([
'student full name',
Expand Down Expand Up @@ -133,17 +135,11 @@ public function form(Form $form): Form

$component->state($template->content);
}))
->visible(fn (Engagement $record): bool => filled($record->body_json))
->visible(fn (Engagement $record): bool => $record->deliverable->channel === EngagementDeliveryMethod::Email)
->showMergeTagsInBlocksPanel($form->getLivewire() instanceof Page)
->helperText('You can insert student information by typing {{ and choosing a tag to insert.')
->columnSpanFull(),
Textarea::make('body')
->placeholder('Body')
->required()
->maxLength(320) // https://www.twilio.com/docs/glossary/what-sms-character-limit#:~:text=Twilio's%20platform%20supports%20long%20messages,best%20deliverability%20and%20user%20experience.
->helperText('The body of your message can be up to 320 characters long.')
->visible(fn (Engagement $record): bool => blank($record->body_json))
->columnSpanFull(),
EngagementSmsBodyField::make(context: 'edit', form: $form),
MorphToSelect::make('recipient')
->label('Recipient')
->searchable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@

use Filament\Forms\Form;
use Filament\Resources\Resource;
use FilamentTiptapEditor\TiptapEditor;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\RichEditor;
use App\Filament\Pages\EmailConfiguration;
use Filament\Resources\Pages\CreateRecord;
use FilamentTiptapEditor\Enums\TiptapOutput;
use Assist\Engagement\Filament\Resources\SmsTemplateResource;

class CreateSmsTemplate extends CreateRecord
Expand All @@ -68,13 +69,17 @@ public function form(Form $form): Form
->autocomplete(false),
Textarea::make('description')
->string(),
RichEditor::make('content')
->toolbarButtons([
'link',
'redo',
'undo',
// TODO Implement length validation (320 characters max)
// https://www.twilio.com/docs/glossary/what-sms-character-limit#:~:text=Twilio's%20platform%20supports%20long%20messages,best%20deliverability%20and%20user%20experience.
TiptapEditor::make('content')
->mergeTags([
'student full name',
'student email',
])
->maxLength(320) // https://www.twilio.com/docs/glossary/what-sms-character-limit#:~:text=Twilio's%20platform%20supports%20long%20messages,best%20deliverability%20and%20user%20experience.
->profile('sms')
->output(TiptapOutput::Json)
->columnSpanFull()
->extraInputAttributes(['style' => 'min-height: 12rem;'])
->required(),
]);
}
Expand Down
Loading
Loading