first
This commit is contained in:
46
app/Models/AuditLog.php
Normal file
46
app/Models/AuditLog.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class AuditLog extends Model
|
||||
{
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'actor_user_id',
|
||||
'actor_role',
|
||||
'action',
|
||||
'subject_type',
|
||||
'subject_id',
|
||||
'target_user_id',
|
||||
'project_id',
|
||||
'old_values',
|
||||
'new_values',
|
||||
'justification',
|
||||
'ip_address',
|
||||
'user_agent',
|
||||
'created_at',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'old_values' => 'array',
|
||||
'new_values' => 'array',
|
||||
'created_at' => 'datetime',
|
||||
];
|
||||
}
|
||||
|
||||
public function actor(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'actor_user_id');
|
||||
}
|
||||
|
||||
public function targetUser(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'target_user_id');
|
||||
}
|
||||
}
|
||||
24
app/Models/Department.php
Normal file
24
app/Models/Department.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
|
||||
class Department extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = ['name', 'code', 'is_active'];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return ['is_active' => 'boolean'];
|
||||
}
|
||||
|
||||
public function users(): HasMany
|
||||
{
|
||||
return $this->hasMany(User::class);
|
||||
}
|
||||
}
|
||||
26
app/Models/ProjectCollaborator.php
Normal file
26
app/Models/ProjectCollaborator.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class ProjectCollaborator extends Model
|
||||
{
|
||||
protected $fillable = ['project_id', 'user_id', 'role', 'added_by'];
|
||||
|
||||
public function project(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(TranscriptionProject::class, 'project_id');
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
|
||||
public function addedByUser(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'added_by');
|
||||
}
|
||||
}
|
||||
24
app/Models/ProjectComment.php
Normal file
24
app/Models/ProjectComment.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
|
||||
class ProjectComment extends Model
|
||||
{
|
||||
use SoftDeletes;
|
||||
|
||||
protected $fillable = ['project_id', 'user_id', 'message'];
|
||||
|
||||
public function project(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(TranscriptionProject::class, 'project_id');
|
||||
}
|
||||
|
||||
public function user(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class);
|
||||
}
|
||||
}
|
||||
36
app/Models/TranscriptVersion.php
Normal file
36
app/Models/TranscriptVersion.php
Normal file
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
|
||||
class TranscriptVersion extends Model
|
||||
{
|
||||
public $timestamps = false;
|
||||
|
||||
protected $fillable = [
|
||||
'project_id',
|
||||
'edited_by',
|
||||
'version_number',
|
||||
'old_text',
|
||||
'new_text',
|
||||
'change_summary',
|
||||
'created_at',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return ['created_at' => 'datetime'];
|
||||
}
|
||||
|
||||
public function project(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(TranscriptionProject::class, 'project_id');
|
||||
}
|
||||
|
||||
public function editor(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'edited_by');
|
||||
}
|
||||
}
|
||||
153
app/Models/TranscriptionProject.php
Normal file
153
app/Models/TranscriptionProject.php
Normal file
@@ -0,0 +1,153 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
class TranscriptionProject extends Model
|
||||
{
|
||||
use HasFactory, SoftDeletes;
|
||||
|
||||
protected $fillable = [
|
||||
'uuid',
|
||||
'title',
|
||||
'description',
|
||||
'owner_user_id',
|
||||
'original_filename',
|
||||
'stored_audio_path',
|
||||
'mime_type',
|
||||
'file_size',
|
||||
'duration_seconds',
|
||||
'language',
|
||||
'transcription_status',
|
||||
'transcription_engine',
|
||||
'transcript_text',
|
||||
'transcript_confidence',
|
||||
'error_message',
|
||||
'processed_at',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'transcript_text' => 'encrypted', // enkripsi kandungan sensitif
|
||||
'transcript_confidence' => 'float',
|
||||
'processed_at' => 'datetime',
|
||||
'file_size' => 'integer',
|
||||
'duration_seconds' => 'integer',
|
||||
];
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::creating(function (self $project) {
|
||||
if (empty($project->uuid)) {
|
||||
$project->uuid = (string) Str::uuid();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Route model binding gunakan uuid
|
||||
public function getRouteKeyName(): string
|
||||
{
|
||||
return 'uuid';
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Relationships
|
||||
// -------------------------------------------------------
|
||||
|
||||
public function owner(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'owner_user_id');
|
||||
}
|
||||
|
||||
public function collaborators(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(User::class, 'project_collaborators', 'project_id', 'user_id')
|
||||
->withPivot('role', 'added_by')
|
||||
->withTimestamps();
|
||||
}
|
||||
|
||||
public function projectCollaborators(): HasMany
|
||||
{
|
||||
return $this->hasMany(ProjectCollaborator::class, 'project_id');
|
||||
}
|
||||
|
||||
public function transcriptVersions(): HasMany
|
||||
{
|
||||
return $this->hasMany(TranscriptVersion::class, 'project_id')->orderByDesc('version_number');
|
||||
}
|
||||
|
||||
public function comments(): HasMany
|
||||
{
|
||||
return $this->hasMany(ProjectComment::class, 'project_id')->latest();
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Helpers
|
||||
// -------------------------------------------------------
|
||||
|
||||
public function isOwnedBy(User $user): bool
|
||||
{
|
||||
return $this->owner_user_id === $user->id;
|
||||
}
|
||||
|
||||
public function hasCollaborator(User $user): bool
|
||||
{
|
||||
return $this->collaborators()->where('user_id', $user->id)->exists();
|
||||
}
|
||||
|
||||
public function isAccessibleBy(User $user): bool
|
||||
{
|
||||
return $this->isOwnedBy($user) || $this->hasCollaborator($user);
|
||||
}
|
||||
|
||||
public function isPending(): bool
|
||||
{
|
||||
return $this->transcription_status === 'pending';
|
||||
}
|
||||
|
||||
public function isProcessing(): bool
|
||||
{
|
||||
return $this->transcription_status === 'processing';
|
||||
}
|
||||
|
||||
public function isCompleted(): bool
|
||||
{
|
||||
return $this->transcription_status === 'completed';
|
||||
}
|
||||
|
||||
public function isFailed(): bool
|
||||
{
|
||||
return $this->transcription_status === 'failed';
|
||||
}
|
||||
|
||||
public function fileSizeForHumans(): string
|
||||
{
|
||||
$bytes = $this->file_size;
|
||||
if ($bytes >= 1073741824) {
|
||||
return number_format($bytes / 1073741824, 2) . ' GB';
|
||||
}
|
||||
if ($bytes >= 1048576) {
|
||||
return number_format($bytes / 1048576, 2) . ' MB';
|
||||
}
|
||||
return number_format($bytes / 1024, 2) . ' KB';
|
||||
}
|
||||
|
||||
public function durationForHumans(): ?string
|
||||
{
|
||||
if (! $this->duration_seconds) {
|
||||
return null;
|
||||
}
|
||||
$m = intdiv($this->duration_seconds, 60);
|
||||
$s = $this->duration_seconds % 60;
|
||||
return sprintf('%d:%02d', $m, $s);
|
||||
}
|
||||
}
|
||||
106
app/Models/User.php
Normal file
106
app/Models/User.php
Normal file
@@ -0,0 +1,106 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
|
||||
class User extends Authenticatable
|
||||
{
|
||||
use HasFactory, Notifiable, SoftDeletes;
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'email',
|
||||
'password',
|
||||
'role',
|
||||
'department_id',
|
||||
'is_active',
|
||||
'last_login_at',
|
||||
'email_verified_at',
|
||||
];
|
||||
|
||||
protected $hidden = [
|
||||
'password',
|
||||
'remember_token',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'email_verified_at' => 'datetime',
|
||||
'last_login_at' => 'datetime',
|
||||
'password' => 'hashed',
|
||||
'is_active' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Role helpers
|
||||
// -------------------------------------------------------
|
||||
|
||||
public function isAdmin(): bool
|
||||
{
|
||||
return $this->role === 'admin';
|
||||
}
|
||||
|
||||
public function isUser(): bool
|
||||
{
|
||||
return $this->role === 'user';
|
||||
}
|
||||
|
||||
public function isActive(): bool
|
||||
{
|
||||
return (bool) $this->is_active;
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Relationships
|
||||
// -------------------------------------------------------
|
||||
|
||||
public function department(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Department::class);
|
||||
}
|
||||
|
||||
public function ownedProjects(): HasMany
|
||||
{
|
||||
return $this->hasMany(TranscriptionProject::class, 'owner_user_id');
|
||||
}
|
||||
|
||||
public function collaboratingProjects(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
TranscriptionProject::class,
|
||||
'project_collaborators',
|
||||
'user_id',
|
||||
'project_id'
|
||||
)->withPivot('role', 'added_by')->withTimestamps();
|
||||
}
|
||||
|
||||
public function auditActionsPerformed(): HasMany
|
||||
{
|
||||
return $this->hasMany(AuditLog::class, 'actor_user_id');
|
||||
}
|
||||
|
||||
public function auditActionsTargeted(): HasMany
|
||||
{
|
||||
return $this->hasMany(AuditLog::class, 'target_user_id');
|
||||
}
|
||||
|
||||
// -------------------------------------------------------
|
||||
// Usage check (for safe delete)
|
||||
// -------------------------------------------------------
|
||||
|
||||
public function hasUsage(): bool
|
||||
{
|
||||
return $this->ownedProjects()->withTrashed()->exists()
|
||||
|| $this->collaboratingProjects()->exists()
|
||||
|| AuditLog::where('actor_user_id', $this->id)->exists();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user