ldc-supportweb/app/Models/Ticket.php

255 lines
7.1 KiB
PHP
Raw Normal View History

2020-11-18 10:23:51 +01:00
<?php
namespace App\Models;
2021-07-23 12:06:08 +02:00
use App\Enums\Role;
2021-04-18 20:31:07 +02:00
use App\Enums\TicketStatus;
use App\Enums\TicketType;
use App\Events\NotifyAdmin;
2021-04-18 20:31:07 +02:00
use App\Events\NotifySubscriptions;
use App\Events\TicketStatusChanged;
2021-07-23 12:06:08 +02:00
use App\Jobs\SendTicketMentionMail;
use App\Mail\TicketMention;
2022-05-31 14:06:07 +02:00
use App\Models\Credit\Account;
use App\Models\Credit\Estimation;
2020-11-18 10:23:51 +01:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
2021-07-23 12:06:08 +02:00
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Collection;
2021-04-18 20:31:07 +02:00
use Kwaadpepper\Enum\Traits\CastsEnums;
2020-11-18 10:23:51 +01:00
class Ticket extends Model
{
use HasFactory;
2021-04-18 20:31:07 +02:00
use CastsEnums;
2020-11-18 10:23:51 +01:00
/**
* The attributes that should be cast to Enums.
*
* @var array
*/
2021-04-18 20:31:07 +02:00
protected $enumCasts = [
'type' => TicketType::class,
'status' => TicketStatus::class
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'is_internal' => 'bool'
];
/**
* The attributes that are mass assignable.
*
* @var array
*/
2021-04-18 20:31:07 +02:00
protected $fillable = [
2021-07-23 11:34:16 +02:00
'site_id',
2021-04-18 20:31:07 +02:00
'type',
'status',
'text',
'is_internal'
2021-04-18 20:31:07 +02:00
];
/**
* Perform any actions required after the model boots.
*
* @return void
*/
2021-04-18 20:31:07 +02:00
protected static function booted()
{
static::creating(function (Ticket $ticket) {
2023-04-11 15:05:29 +00:00
// If bug force the ticket status on new. All the others are estimateTodo.
2023-04-06 08:55:01 +00:00
if ($ticket->type->equals(TicketType::bug())) {
2023-04-11 15:05:29 +00:00
$ticket->status = TicketStatus::new();
2023-04-06 08:55:01 +00:00
} else {
$ticket->status = TicketStatus::estimateTodo();
}
});
2021-04-18 20:31:07 +02:00
static::updating(function (Ticket $ticket) {
if (!$ticket->original['status'] !== $ticket->status->value) {
event(new TicketStatusChanged(
$ticket,
TicketStatus::make($ticket->original['status']),
auth()->user(),
null,
2023-04-17 13:35:33 +00:00
false
));
2021-04-18 20:31:07 +02:00
event(new NotifySubscriptions($ticket, sprintf(
'<b>%s</b> a modifié le statut en <b>%s</b>',
auth()->user()->name,
$ticket->status->label
)));
event(new NotifyAdmin($ticket, sprintf(
'<b>%s</b> a modifié le statut en <b>%s</b>',
auth()->user()->name,
2021-04-18 20:31:07 +02:00
$ticket->status->label
)));
}
});
}
// * RELATIONSHIPS
2022-05-31 14:06:07 +02:00
/**
* HasOne Credit\Estimation relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasOne
*/
public function estimation()
{
return $this->hasOne(Estimation::class);
}
/**
* BelongsTo Site relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2021-07-23 11:34:16 +02:00
public function site()
2021-04-18 20:31:07 +02:00
{
2021-07-23 11:34:16 +02:00
return $this->belongsTo(Site::class);
2021-04-18 20:31:07 +02:00
}
/**
* BelongsTo User relation
* The user that created the ticket
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
2021-04-18 20:31:07 +02:00
public function user()
{
return $this->belongsTo(User::class, 'created_by');
2020-11-18 10:23:51 +01:00
}
2022-05-24 17:23:36 +02:00
/**
* BelongsTo User relation
* The user that is in charge of the ticket
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function assignedUser()
{
return $this->belongsTo(User::class, 'assigned_to');
}
/**
* HasMany Message relation
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
2021-04-18 20:31:07 +02:00
public function messages()
{
return $this->hasMany(Message::class, 'ticket_id');
2020-11-18 10:23:51 +01:00
}
/**
* BelongsToMany User relation
* The Users that are following this ticket
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
2021-04-18 20:31:07 +02:00
public function subscriptions()
{
return $this->belongsToMany(User::class, 'subscriptions');
}
2022-05-26 13:22:50 +02:00
/**
* BelongsToMany Label relation
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function labels()
{
return $this->belongsToMany(Label::class, 'label_ticket');
}
// * METHODS
2021-07-23 12:06:08 +02:00
/**
* Parse message @user tags and send emails to concerned users
* returns and array with to users notified
*
* @param Message $message
* @return Collection
2021-07-23 12:06:08 +02:00
*/
public function sendBeenMentionnedToUser(Message $message): Collection
{
$ticket = $this;
// Find @user + send mail si un seul user est trouvé.
2021-07-23 12:06:08 +02:00
$usersNotified = collect([]);
2022-03-29 16:57:54 +02:00
$matches = [];
2021-07-23 12:06:08 +02:00
$ticket->load('site');
\preg_match_all('/@\w*/', $message->message, $matches);
$matches = collect($matches[0] ?? [])->unique();
foreach ($matches as $pseudo_usertag) {
try {
2022-03-29 16:57:54 +02:00
/** @var \App\Models\User $user */
2021-07-23 12:06:08 +02:00
$user = User::where('name', 'LIKE', '%' . ltrim($pseudo_usertag, '@') . '%')
->where('email', '!=', $ticket->user->email)
->where(function ($q) use ($ticket) {
$q->whereHas('sites', function ($q) use ($ticket) {
$q->where('id', $ticket->site->id);
})->orWhere('role', Role::admin()->value)
->orWhere('role', Role::developper()->value);
})->firstOrFail();
if ($ticket->is_internal) {
if ($user->isAdmin()) {
$usersNotified->add($user);
SendTicketMentionMail::dispatchAfterResponse(
$user,
new TicketMention($ticket, $message->message)
);
}
} else {
$usersNotified->add($user);
SendTicketMentionMail::dispatchAfterResponse(
$user,
new TicketMention($ticket, $message->message)
);
}
2021-07-23 12:06:08 +02:00
} catch (ModelNotFoundException $e) {
continue;
2022-03-29 16:57:54 +02:00
}//end try
}//end foreach
2021-07-23 12:06:08 +02:00
return $usersNotified;
}
2022-05-31 14:06:07 +02:00
/**
* full_name getter
*
* @return string
*/
public function getFullNameAttribute(): string
{
return trans('Ticket n° :ticketId : :ticketSiteName', [
2022-05-31 14:06:07 +02:00
'ticketId' => $this->id,
'ticketSiteName' => $this->site->name
2022-05-31 14:06:07 +02:00
]);
}
/**
* Check if an account has the same brand as this ticket.
*
* @param Account $account
* @return boolean
*/
public function accountIsValidForThisTicket(Account $account): bool
{
/** @var \App\Models\Brand|null */
if (!($siteBrand = optional($this->site)->brand)) {
return false;
}
/** @var \App\Models\Brand */
$accountBrand = $account->brand;
return !\is_null($accountBrand->id) and $accountBrand->id === $siteBrand->id;
}
2020-11-18 10:23:51 +01:00
}