updates to document handling and code editing features

This commit is contained in:
2025-12-03 23:27:08 +01:00
parent 88e526cf6c
commit 7b00887372
29 changed files with 20851 additions and 1114 deletions

View File

@@ -6,6 +6,8 @@ use App\Events\DocumentVersionUpdated;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
class Document extends Model
{
@@ -20,22 +22,63 @@ class Document extends Model
'file_path',
'project_id', // Asegurar que está en fillable
'folder_id',
'user_id',
'issuer',
'status',
'revision',
'version',
'discipline',
'document_type',
'issuer',
'entry_date',
'current_version_id',
'code',
];
public function versions() {
protected static function booted()
{
static::created(function ($document) {
if (request()->hasFile('file')) {
$file = request()->file('file');
$document->createVersion($file);
}
});
}
/**
* Get all versions of the document.
*/
public function versions(): HasMany
{
return $this->hasMany(DocumentVersion::class);
}
/**
* Get the current version of the document.
*/
public function currentVersion(): BelongsTo
{
return $this->belongsTo(DocumentVersion::class, 'current_version_id');
}
/**
* Get the latest version of the document.
*/
public function getLatestVersionAttribute()
{
return $this->versions()->latestFirst()->first();
}
/**
* Create a new version from file content.
*/
public function createVersion(string $content, array $changes = [], ?User $user = null): DocumentVersion
{
$version = DocumentVersion::createFromContent($this, $content, $changes, $user);
// Update current version pointer
$this->update(['current_version_id' => $version->id]);
return $version;
}
public function approvals() {
return $this->hasMany(Approval::class);
@@ -44,17 +87,6 @@ class Document extends Model
public function comments() {
return $this->hasMany(Comment::class)->whereNull('parent_id');
}
public function createVersion($file)
{
return $this->versions()->create([
'file_path' => $file->store("documents/{$this->id}/versions"),
'hash' => hash_file('sha256', $file),
'user_id' => auth()->id(),
'version_number' => $this->versions()->count() + 1
]);
}
public function getCurrentVersionAttribute()
{
@@ -63,7 +95,7 @@ class Document extends Model
public function uploadVersion($file)
{
$this->versions()->create([
$version = $this->versions()->create([
'file_path' => $file->store("projects/{$this->id}/versions"),
'hash' => hash_file('sha256', $file),
'version' => $this->versions()->count() + 1,
@@ -82,4 +114,9 @@ class Document extends Model
->logUnguarded();
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}