115 lines
3.7 KiB
PHP
115 lines
3.7 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\MessageType;
|
|
use App\Events\NotifySubscriptions;
|
|
use App\Events\NotifyTicketCreator;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
|
|
|
class Message extends Model
|
|
{
|
|
use HasFactory;
|
|
use CastsEnums;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [
|
|
'message',
|
|
'type'
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast to Enums.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $enumCasts = [
|
|
'type' => MessageType::class
|
|
];
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted()
|
|
{
|
|
static::created(function (Message $message) {
|
|
$message->load(['user' => function ($q) {
|
|
$q->withTrashed();
|
|
}]);
|
|
/** @var \App\Models\Ticket $ticket */
|
|
$ticket = $message->ticket;
|
|
if ($message->user) {
|
|
$ticket->load(['user' => function ($q) {
|
|
$q->withTrashed();
|
|
}]);
|
|
/** @var \App\Models\User $messageUser */
|
|
$messageUser = $message->user()->withTrashed()->firstOrFail();
|
|
// * Send mail to mentionned users
|
|
$usersNotified = $ticket->sendBeenMentionnedToUser($message);
|
|
// * If email has not been sen to ticket author yet.
|
|
if (!$usersNotified->contains($ticket->user) and $ticket->user->id !== $messageUser->id) {
|
|
// * Send mail to ticket author
|
|
event(new NotifyTicketCreator(
|
|
$ticket,
|
|
trans('Nouvelle réponse de :name :<br> :message sur votre ticket', [
|
|
'name' => $messageUser->name,
|
|
'message' => $message->message
|
|
])
|
|
));
|
|
$usersNotified->push($ticket->user);
|
|
}
|
|
// ? Is message author is admin ?
|
|
if ($messageUser->isAdmin()) {
|
|
// TODO: investigate why this make errors on prod,
|
|
// https://supportweb.ldc.fr/ticket/583/message/create
|
|
// SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '583-8' for key 'PRIMARY'
|
|
// $ticket->subscriptions()->attach($messageUser);
|
|
// Add subscription.
|
|
if (!$messageUser->subscriptions()->find($ticket->id)) {
|
|
$messageUser->subscriptions()->attach($ticket->id);
|
|
\session()->flash('info', trans('Vous avez été inscrit.'));
|
|
}
|
|
}
|
|
// * Send mail to other subscribed users
|
|
event(new NotifySubscriptions(
|
|
$message->ticket,
|
|
trans('Nouvelle réponse de :name :<br> :message', [
|
|
'name' => $message->user->name,
|
|
'message' => $message->message
|
|
]),
|
|
$usersNotified
|
|
));
|
|
}//end if
|
|
});
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* BelongsTo Ticket relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function ticket()
|
|
{
|
|
return $this->belongsTo(Ticket::class, 'ticket_id');
|
|
}
|
|
|
|
/**
|
|
* BelongsTo User relation
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
}
|