2020-11-25 16:05:21 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2023-04-12 09:40:01 +00:00
|
|
|
use App\Enums\MessageType;
|
2021-04-18 20:31:07 +02:00
|
|
|
use App\Events\NotifySubscriptions;
|
2021-07-21 09:44:29 +02:00
|
|
|
use App\Events\NotifyTicketCreator;
|
2020-11-25 16:05:21 +01:00
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
2023-04-12 09:40:01 +00:00
|
|
|
use Kwaadpepper\Enum\Traits\CastsEnums;
|
2020-11-25 16:05:21 +01:00
|
|
|
|
2023-04-21 15:45:29 +02:00
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @property string $message The message content.
|
|
|
|
|
* @property \App\Enums\MessageType $type The message is not visible for clients.
|
|
|
|
|
* @property string $file The message joined file path.
|
|
|
|
|
*
|
|
|
|
|
* @package App\Models
|
|
|
|
|
*/
|
2020-11-25 16:05:21 +01:00
|
|
|
class Message extends Model
|
|
|
|
|
{
|
|
|
|
|
use HasFactory;
|
2023-04-12 09:40:01 +00:00
|
|
|
use CastsEnums;
|
2020-11-25 16:05:21 +01:00
|
|
|
|
2022-05-24 12:38:30 +02:00
|
|
|
/**
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
2021-04-18 20:31:07 +02:00
|
|
|
protected $fillable = [
|
2023-04-12 09:40:01 +00:00
|
|
|
'message',
|
|
|
|
|
'type'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The attributes that should be cast to Enums.
|
|
|
|
|
*
|
|
|
|
|
* @var array
|
|
|
|
|
*/
|
|
|
|
|
protected $enumCasts = [
|
|
|
|
|
'type' => MessageType::class
|
2021-04-18 20:31:07 +02:00
|
|
|
];
|
2022-05-24 12:38:30 +02:00
|
|
|
/**
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2021-07-21 09:44:29 +02:00
|
|
|
protected static function booted()
|
|
|
|
|
{
|
|
|
|
|
static::created(function (Message $message) {
|
2022-03-25 17:02:13 +01:00
|
|
|
$message->load(['user' => function ($q) {
|
|
|
|
|
$q->withTrashed();
|
|
|
|
|
}]);
|
2022-02-02 13:51:45 +01:00
|
|
|
/** @var \App\Models\Ticket $ticket */
|
2021-07-21 09:44:29 +02:00
|
|
|
$ticket = $message->ticket;
|
|
|
|
|
if ($message->user) {
|
2022-03-25 17:02:13 +01:00
|
|
|
$ticket->load(['user' => function ($q) {
|
|
|
|
|
$q->withTrashed();
|
|
|
|
|
}]);
|
2022-02-02 13:51:45 +01:00
|
|
|
/** @var \App\Models\User $messageUser */
|
2022-03-25 17:02:13 +01:00
|
|
|
$messageUser = $message->user()->withTrashed()->firstOrFail();
|
2023-04-21 15:45:29 +02:00
|
|
|
// * Send mail to mentionned users\
|
|
|
|
|
/** @var \Illuminate\Support\Collection<\App\Models\User> */
|
|
|
|
|
$dontNotifyUsers = $ticket->sendBeenMentionnedToUser($message);
|
|
|
|
|
|
|
|
|
|
// * Ignorer le cas où le message est privé et l'auteur du ticket n'est pas admin.
|
|
|
|
|
if (!$ticket->user->isAdmin() && $message->type->equals(MessageType::private())) {
|
|
|
|
|
$dontNotifyUsers->push($ticket->user);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// * Filter Users if the message is private.
|
|
|
|
|
if ($message->type->equals(MessageType::private())) {
|
|
|
|
|
$dontNotifyUsers = $dontNotifyUsers->concat(
|
|
|
|
|
// * Add to ignore list all non Admin users.
|
|
|
|
|
$ticket->subscriptions->filter(function (User $suscribedUser) {
|
|
|
|
|
return !$suscribedUser->isAdmin();
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
2022-02-02 13:51:45 +01:00
|
|
|
// * If email has not been sen to ticket author yet.
|
2023-04-21 15:45:29 +02:00
|
|
|
if (
|
|
|
|
|
!$dontNotifyUsers->contains(function (User $dontNotifyUser) use ($ticket) {
|
|
|
|
|
return $dontNotifyUser->id === $ticket->user->id;
|
|
|
|
|
}) and $ticket->user->id !== $messageUser->id
|
|
|
|
|
) {
|
2022-02-02 13:51:45 +01:00
|
|
|
// * Send mail to ticket author
|
2021-07-21 09:44:29 +02:00
|
|
|
event(new NotifyTicketCreator(
|
|
|
|
|
$ticket,
|
|
|
|
|
trans('Nouvelle réponse de :name :<br> :message sur votre ticket', [
|
2022-02-02 13:51:45 +01:00
|
|
|
'name' => $messageUser->name,
|
2021-07-21 09:44:29 +02:00
|
|
|
'message' => $message->message
|
|
|
|
|
])
|
|
|
|
|
));
|
2023-04-21 15:45:29 +02:00
|
|
|
$dontNotifyUsers->push($ticket->user);
|
2021-07-21 09:44:29 +02:00
|
|
|
}
|
2022-02-02 13:51:45 +01:00
|
|
|
// ? Is message author is admin ?
|
|
|
|
|
if ($messageUser->isAdmin()) {
|
2022-02-02 16:47:47 +01:00
|
|
|
// 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);
|
2022-05-24 12:38:30 +02:00
|
|
|
// Add subscription.
|
2022-02-02 16:47:47 +01:00
|
|
|
if (!$messageUser->subscriptions()->find($ticket->id)) {
|
|
|
|
|
$messageUser->subscriptions()->attach($ticket->id);
|
|
|
|
|
\session()->flash('info', trans('Vous avez été inscrit.'));
|
|
|
|
|
}
|
2022-02-02 13:51:45 +01:00
|
|
|
}
|
|
|
|
|
// * Send mail to other subscribed users
|
2021-07-21 09:44:29 +02:00
|
|
|
event(new NotifySubscriptions(
|
|
|
|
|
$message->ticket,
|
|
|
|
|
trans('Nouvelle réponse de :name :<br> :message', [
|
|
|
|
|
'name' => $message->user->name,
|
|
|
|
|
'message' => $message->message
|
|
|
|
|
]),
|
2023-04-21 15:45:29 +02:00
|
|
|
$dontNotifyUsers
|
2021-07-21 09:44:29 +02:00
|
|
|
));
|
2022-03-29 16:57:54 +02:00
|
|
|
}//end if
|
2021-07-21 09:44:29 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 12:38:30 +02:00
|
|
|
// * RELATIONSHIPS
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* BelongsTo Ticket relation
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
|
*/
|
2021-04-18 20:31:07 +02:00
|
|
|
public function ticket()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Ticket::class, 'ticket_id');
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-24 12:38:30 +02:00
|
|
|
/**
|
|
|
|
|
* BelongsTo User relation
|
|
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
|
|
|
*/
|
2021-04-18 20:31:07 +02:00
|
|
|
public function user()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class, 'user_id');
|
2020-11-25 16:05:21 +01:00
|
|
|
}
|
|
|
|
|
}
|