- Updated `TicketService` to trigger notifications to all administrators upon ticket creation. - Added test to verify administrator notifications when a support ticket is raised. - Minor formatting adjustments in `Ticket` model's imports for consistency.
112 lines
2.5 KiB
PHP
112 lines
2.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
use Illuminate\Database\Eloquent\{Model, SoftDeletes};
|
|
use Illuminate\Database\Eloquent\Relations\{BelongsTo, BelongsToMany, HasMany};
|
|
use Illuminate\Support\Str;
|
|
|
|
#[Fillable([
|
|
'ticket_id',
|
|
'user_id',
|
|
'connected_app_id',
|
|
'issue_category',
|
|
'description',
|
|
'status_id',
|
|
'assigned_user_id',
|
|
])]
|
|
class Ticket extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (Ticket $ticket): void {
|
|
if (empty($ticket->ticket_id)) {
|
|
do {
|
|
$ticketId = 'TKT-'.mb_strtoupper(Str::random(8));
|
|
} while (static::query()->where('ticket_id', $ticketId)->exists());
|
|
|
|
$ticket->ticket_id = $ticketId;
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<User, $this>
|
|
*/
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<ConnectedApp, $this>
|
|
*/
|
|
public function connectedApp(): BelongsTo
|
|
{
|
|
return $this->belongsTo(ConnectedApp::class, 'connected_app_id');
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<TicketStatus, $this>
|
|
*/
|
|
public function status(): BelongsTo
|
|
{
|
|
return $this->belongsTo(TicketStatus::class, 'status_id');
|
|
}
|
|
|
|
/**
|
|
* @return BelongsTo<User, $this>
|
|
*/
|
|
public function assignedUser(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, 'assigned_user_id');
|
|
}
|
|
|
|
/**
|
|
* @return BelongsToMany<Role, $this>
|
|
*/
|
|
public function notifiedRoles(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(
|
|
Role::class,
|
|
'ticket_notified_roles',
|
|
'ticket_id',
|
|
'role_id'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return BelongsToMany<User, $this>
|
|
*/
|
|
public function notifiedUsers(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(
|
|
User::class,
|
|
'ticket_notified_users',
|
|
'ticket_id',
|
|
'user_id'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<TicketAttachment, $this>
|
|
*/
|
|
public function attachments(): HasMany
|
|
{
|
|
return $this->hasMany(TicketAttachment::class, 'ticket_id');
|
|
}
|
|
|
|
/**
|
|
* @return HasMany<TicketReply, $this>
|
|
*/
|
|
public function replies(): HasMany
|
|
{
|
|
return $this->hasMany(TicketReply::class, 'ticket_id');
|
|
}
|
|
}
|