342 lines
10 KiB
PHP
342 lines
10 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Enums\Users\RoleEnum;
|
|
use App\Enums\Tickets\TicketStatusEnum;
|
|
use App\Enums\Tickets\TicketTypeEnum;
|
|
use App\Events\NotifyAdmin;
|
|
use App\Events\NotifySubscriptions;
|
|
use App\Events\TicketStatusChanged;
|
|
use App\Jobs\SendTicketMentionMail;
|
|
use App\Mail\TicketMention;
|
|
use App\Models\Credit\Account;
|
|
use App\Models\Credit\Estimation;
|
|
use App\Models\Ticket\TicketType;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
|
use Illuminate\Support\Str;
|
|
|
|
/**
|
|
* @property string $title The message joined file path.
|
|
* @property string $text Ticket description.
|
|
* @property \App\Models\Site $site_id Site associated.
|
|
* @property \App\Enums\Tickets\TicketTypeEnum $type Ticket type.
|
|
* @property \App\Enums\Tickets\TicketStatusEnum $status Ticket status.
|
|
* @property integer $is_internal Internal ticket or not.
|
|
* @property integer $order Ticket order.
|
|
* @property \Carbon\Carbon $date_mep Production date wished.
|
|
* @property \Carbon\Carbon $last_activity Last activity in the ticket.
|
|
* @property-read \Carbon\Carbon $created_at Created date.
|
|
* @property-read \Carbon\Carbon $updated_at Updated date.
|
|
*/
|
|
class Ticket extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are fillable.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
protected $fillable = [
|
|
'title',
|
|
'text',
|
|
'site_id',
|
|
'type',
|
|
'status',
|
|
'is_internal',
|
|
'order',
|
|
'date_mep',
|
|
'last_activity',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'is_internal' => 'bool',
|
|
'type' => TicketTypeEnum::class,
|
|
'status' => TicketStatusEnum::class,
|
|
'date_mep' => 'date',
|
|
'last_activity' => 'timestamp',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (Ticket $ticket) {
|
|
// If bug force the ticket status on new. All the others are estimateTodo.
|
|
self::setOrder($ticket);
|
|
if ($ticket->type->equals(TicketTypeEnum::bug)) {
|
|
$ticket->status = TicketStatusEnum::new;
|
|
} else {
|
|
$ticket->status = TicketStatusEnum::estimateTodo;
|
|
}
|
|
});
|
|
static::updating(function (Ticket $ticket) {
|
|
if (
|
|
!$ticket->original['status'] !== $ticket->status->value &&
|
|
$ticket->original['order'] === $ticket->order
|
|
) {
|
|
event(new TicketStatusChanged(
|
|
$ticket,
|
|
TicketStatusEnum::make($ticket->original['status']),
|
|
auth()->user(),
|
|
null,
|
|
false
|
|
));
|
|
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,
|
|
$ticket->status->label()
|
|
)));
|
|
}
|
|
if ($ticket->status->equals(TicketStatusEnum::archived)) {
|
|
self::setOrder($ticket, true);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Set order after the last element of the list.
|
|
*
|
|
* @param self $model
|
|
* @param boolean $isArchived
|
|
* @return void
|
|
*/
|
|
private static function setOrder(self $model, bool $isArchived = false): void
|
|
{
|
|
$model->order = \intval(self::query()
|
|
->where('status', $isArchived ? '=' : '!=', TicketStatusEnum::archived)
|
|
->max('order')) + 1;
|
|
}
|
|
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* HasOne Credit\Estimation relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasOne
|
|
*/
|
|
public function estimation(): \Illuminate\Database\Eloquent\Relations\HasOne
|
|
{
|
|
return $this->hasOne(Estimation::class);
|
|
}
|
|
|
|
/**
|
|
* BelongsTo Site relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function site(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(Site::class);
|
|
}
|
|
|
|
/**
|
|
* BelongsTo User relation
|
|
* The user that created the ticket.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function user(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'created_by');
|
|
}
|
|
|
|
/**
|
|
* BelongsTo User relation
|
|
* The user that is in charge of the ticket.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function assignedUser(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'assigned_to');
|
|
}
|
|
|
|
/**
|
|
* HasMany Message relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany
|
|
*/
|
|
public function messages(): \Illuminate\Database\Eloquent\Relations\HasMany
|
|
{
|
|
return $this->hasMany(Message::class, 'ticket_id');
|
|
}
|
|
|
|
/**
|
|
* BelongsToMany User relation
|
|
* The Users that are following this ticket.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function subscriptions(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
{
|
|
return $this->belongsToMany(User::class, 'subscriptions');
|
|
}
|
|
|
|
/**
|
|
* BelongsToMany Label relation.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function labels(): \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Label::class, 'label_ticket');
|
|
}
|
|
|
|
/**
|
|
* BelongsTo ticket_type.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function ticketType(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(TicketType::class, 'type', 'type');
|
|
}
|
|
|
|
/**
|
|
* BelongsToMany Linked Ticket
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
|
|
*/
|
|
public function linkedTickets()
|
|
{
|
|
return $this->belongsToMany(Ticket::class, 'linked_tickets', 'ticket_id', 'linked_ticket_id');
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Parse message @user tags and send emails to concerned users
|
|
* returns and array with to users notified.
|
|
*
|
|
* @param Message $message
|
|
* @return \Illuminate\Support\Collection
|
|
*/
|
|
public function sendBeenMentionnedToUser(Message $message): \Illuminate\Support\Collection
|
|
{
|
|
$ticket = $this;
|
|
// Find @user + send mail si un seul user est trouvé.
|
|
/** @var \Illuminate\Support\Collection<\App\Models\User> */
|
|
$usersNotified = collect([]);
|
|
$matches = [];
|
|
$ticket->load('site');
|
|
\preg_match_all('/@\w*/', $message->message, $matches);
|
|
$matches = collect($matches[0] ?? [])->unique();
|
|
foreach ($matches as $pseudo_usertag) {
|
|
try {
|
|
/** @var \App\Models\User $user */
|
|
$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', RoleEnum::admin)
|
|
->orWhere('role', RoleEnum::developper);
|
|
})->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)
|
|
);
|
|
}
|
|
} catch (ModelNotFoundException $e) {
|
|
continue;
|
|
} //end try
|
|
} //end foreach
|
|
|
|
return $usersNotified;
|
|
}
|
|
|
|
/**
|
|
* full_name getter.
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getFullNameAttribute(): string
|
|
{
|
|
return trans('Ticket n° :ticketId : :ticketSiteName', [
|
|
'ticketId' => $this->id,
|
|
'ticketSiteName' => $this->site->name
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* 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 */
|
|
$siteBrand = optional($this->site)->brand;
|
|
if (!$siteBrand) {
|
|
return false;
|
|
}
|
|
|
|
/** @var \App\Models\Brand */
|
|
$accountBrand = $account->brand;
|
|
|
|
return !\is_null($accountBrand->id) and $accountBrand->id === $siteBrand->id;
|
|
}
|
|
|
|
/**
|
|
* Format the ticket name to contain more information.
|
|
*
|
|
* @param self $ticket
|
|
* @return string
|
|
*/
|
|
public static function formatName(self $ticket): string
|
|
{
|
|
return sprintf(
|
|
'Ticket %d (%s): %s',
|
|
$ticket->id,
|
|
$ticket->user ? $ticket->user->name : trans('Utilisateur supprimé'),
|
|
Str::limit(\preg_replace('/[[:cntrl:]]+/', '', $ticket->text), 35),
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get an array of all files columns of the ticket.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function getFiles(): array
|
|
{
|
|
return [
|
|
$this->file,
|
|
$this->file2,
|
|
$this->file3,
|
|
$this->file4,
|
|
$this->file5,
|
|
];
|
|
}
|
|
}
|