Answer the inbox from the panel, with templates
A reply starts from a template filled in the language the person wrote in, is sent by the application when it has a mailer that delivers or handed to the replier's mail program otherwise, and is recorded on the submission. The same is available through the API. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -12,6 +12,7 @@ use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
|
||||
@@ -59,6 +60,14 @@ class Application extends Model
|
||||
/**
|
||||
* @return BelongsTo<User, $this>
|
||||
*/
|
||||
/**
|
||||
* @return HasMany<ApplicationReply, $this>
|
||||
*/
|
||||
public function replies(): HasMany
|
||||
{
|
||||
return $this->hasMany(ApplicationReply::class)->latest('id');
|
||||
}
|
||||
|
||||
public function handler(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'handled_by');
|
||||
|
||||
31
app/Models/ApplicationReply.php
Normal file
31
app/Models/ApplicationReply.php
Normal file
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* An answer given to a submission, kept so the next person to open it knows
|
||||
* what was already said.
|
||||
*/
|
||||
#[Fillable(['user_id', 'user_name', 'subject', 'body', 'channel'])]
|
||||
class ApplicationReply extends Model
|
||||
{
|
||||
public const UPDATED_AT = null;
|
||||
|
||||
public const MAIL = 'mail';
|
||||
|
||||
public const MAILTO = 'mailto';
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Application, $this>
|
||||
*/
|
||||
public function application(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Application::class);
|
||||
}
|
||||
}
|
||||
64
app/Models/ReplyTemplate.php
Normal file
64
app/Models/ReplyTemplate.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Enums\ApplicationKind;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Spatie\Translatable\HasTranslations;
|
||||
|
||||
/**
|
||||
* An answer the inbox gives often enough to write once: "thanks, we'll be in
|
||||
* touch", "this call is filled", "here is how we work".
|
||||
*
|
||||
* Written in every language, because the answer goes out in the one the
|
||||
* person wrote in. `{name}`, `{subject}` and `{sender}` are filled in.
|
||||
*/
|
||||
#[Fillable(['name', 'kind', 'subject', 'body', 'position'])]
|
||||
class ReplyTemplate extends Model
|
||||
{
|
||||
use HasFactory, HasTranslations;
|
||||
|
||||
public const PLACEHOLDERS = ['{name}', '{subject}', '{sender}'];
|
||||
|
||||
/**
|
||||
* @var array<int, string>
|
||||
*/
|
||||
public array $translatable = ['subject', 'body'];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return ['position' => 'integer'];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Builder<$this> $query
|
||||
* @return Builder<$this>
|
||||
*/
|
||||
public function scopeFor(Builder $query, ApplicationKind $kind): Builder
|
||||
{
|
||||
return $query
|
||||
->where(fn (Builder $q) => $q->whereNull('kind')->orWhere('kind', $kind->value))
|
||||
->orderBy('position')
|
||||
->orderBy('name');
|
||||
}
|
||||
|
||||
/**
|
||||
* Subject and body for one submission, in its language.
|
||||
*
|
||||
* @return array{subject: string, body: string}
|
||||
*/
|
||||
public function renderFor(Application $application, ?User $sender = null): array
|
||||
{
|
||||
$values = [$application->name, $application->subject(), $sender?->name ?? (string) config('app.name')];
|
||||
|
||||
return [
|
||||
'subject' => str_replace(self::PLACEHOLDERS, $values, (string) $this->getTranslation('subject', $application->locale)),
|
||||
'body' => str_replace(self::PLACEHOLDERS, $values, (string) $this->getTranslation('body', $application->locale)),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user