ldc-supportweb/app/Models/Message.php

116 lines
3.7 KiB
PHP
Raw Normal View History

<?php
namespace App\Models;
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;
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
*/
2021-04-18 20:31:07 +02:00
protected $fillable = [
'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
];
/**
* 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) {
$message->load(['user' => function ($q) {
$q->withTrashed();
}]);
/** @var \App\Models\Ticket $ticket */
2021-07-21 09:44:29 +02:00
$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
2021-07-21 09:44:29 +02:00
event(new NotifyTicketCreator(
$ticket,
trans('Nouvelle réponse de :name :<br> :message sur votre ticket', [
'name' => $messageUser->name,
2021-07-21 09:44:29 +02:00
'message' => $message->message
])
));
$usersNotified->push($ticket->user);
2021-07-21 09:44:29 +02:00
}
// ? 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
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
]),
$usersNotified
));
2022-03-29 16:57:54 +02:00
}//end if
2021-07-21 09:44:29 +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');
}
/**
* 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');
}
}