feat: implement Attachment domain for file storage and integrate into Tenant service for logo management
This commit is contained in:
@@ -1,44 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Attachable\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
||||
|
||||
#[Fillable([
|
||||
'attachable_type',
|
||||
'attachable_id',
|
||||
'attachment_id',
|
||||
])]
|
||||
class AttachableAttachment extends Model
|
||||
{
|
||||
protected $table = 'attachable_attachments';
|
||||
|
||||
public $timestamps = false;
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'attachable_id' => 'integer',
|
||||
'attachment_id' => 'integer',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return MorphTo<Model, $this>
|
||||
*/
|
||||
public function attachable(): MorphTo
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<Attachment, $this>
|
||||
*/
|
||||
public function attachment(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Attachment::class, 'attachment_id');
|
||||
}
|
||||
}
|
||||
@@ -6,7 +6,6 @@ use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
#[Fillable([
|
||||
@@ -41,14 +40,6 @@ class Attachment extends Model
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return HasMany<AttachableAttachment, $this>
|
||||
*/
|
||||
public function attachables(): HasMany
|
||||
{
|
||||
return $this->hasMany(AttachableAttachment::class, 'attachment_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the pre-signed temporary S3 URL for this attachment.
|
||||
*/
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Attachable\Models\Concerns;
|
||||
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
||||
|
||||
trait HasAttachments
|
||||
{
|
||||
/**
|
||||
* @return MorphToMany<Attachment, Model, $this>
|
||||
*/
|
||||
public function attachments(): MorphToMany
|
||||
{
|
||||
return $this->morphToMany(
|
||||
Attachment::class,
|
||||
'attachable',
|
||||
'attachable_attachments',
|
||||
'attachable_id',
|
||||
'attachment_id',
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
namespace App\Domains\Tenant\Models;
|
||||
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Attachable\Models\Concerns\HasAttachments;
|
||||
use App\Domains\Catalog\Models\Product;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
@@ -24,7 +23,6 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
])]
|
||||
class Tenant extends Model
|
||||
{
|
||||
use HasAttachments;
|
||||
use HasFactory;
|
||||
|
||||
public function getRouteKeyName(): string
|
||||
|
||||
@@ -55,14 +55,6 @@ class TenantService
|
||||
/** @var Tenant $tenant */
|
||||
$tenant = Tenant::query()->create($data);
|
||||
|
||||
if ($headerAttachmentId) {
|
||||
$tenant->attachments()->attach($headerAttachmentId);
|
||||
}
|
||||
|
||||
if ($footerAttachmentId) {
|
||||
$tenant->attachments()->attach($footerAttachmentId);
|
||||
}
|
||||
|
||||
return $tenant;
|
||||
});
|
||||
}
|
||||
@@ -94,7 +86,6 @@ class TenantService
|
||||
|
||||
if ($attachment) {
|
||||
$tenant->header_logo_id = $attachment->id;
|
||||
$tenant->attachments()->attach($attachment->id);
|
||||
} else {
|
||||
$tenant->header_logo_id = null;
|
||||
}
|
||||
@@ -111,7 +102,6 @@ class TenantService
|
||||
|
||||
if ($attachment) {
|
||||
$tenant->footer_logo_id = $attachment->id;
|
||||
$tenant->attachments()->attach($attachment->id);
|
||||
} else {
|
||||
$tenant->footer_logo_id = null;
|
||||
}
|
||||
|
||||
@@ -13,8 +13,6 @@ return new class extends Migration
|
||||
{
|
||||
Schema::create('attachments', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('attachable_type');
|
||||
$table->unsignedBigInteger('attachable_id');
|
||||
$table->uuid('key')->unique();
|
||||
$table->string('path');
|
||||
$table->string('filename');
|
||||
@@ -24,7 +22,6 @@ return new class extends Migration
|
||||
$table->unsignedBigInteger('size')->default(0);
|
||||
$table->timestamps();
|
||||
|
||||
$table->index(['attachable_type', 'attachable_id']);
|
||||
$table->index('type');
|
||||
$table->index('mime_type');
|
||||
});
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('attachments', function (Blueprint $table): void {
|
||||
$table->dropIndex(['attachable_type', 'attachable_id']);
|
||||
$table->dropColumn(['attachable_type', 'attachable_id']);
|
||||
});
|
||||
|
||||
Schema::create('attachable_attachments', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->string('attachable_type');
|
||||
$table->unsignedBigInteger('attachable_id');
|
||||
$table->foreignId('attachment_id')->constrained('attachments')->cascadeOnDelete();
|
||||
|
||||
$table->unique(['attachable_type', 'attachable_id', 'attachment_id'], 'attachable_attachments_unique');
|
||||
$table->index(['attachable_type', 'attachable_id'], 'attachable_attachments_attachable_index');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('attachable_attachments');
|
||||
|
||||
Schema::table('attachments', function (Blueprint $table): void {
|
||||
$table->string('attachable_type');
|
||||
$table->unsignedBigInteger('attachable_id');
|
||||
$table->index(['attachable_type', 'attachable_id']);
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -23,8 +23,8 @@
|
||||
<env name="BCRYPT_ROUNDS" value="4"/>
|
||||
<env name="BROADCAST_CONNECTION" value="null"/>
|
||||
<env name="CACHE_STORE" value="array"/>
|
||||
<env name="DB_CONNECTION" value="sqlite"/>
|
||||
<env name="DB_DATABASE" value=":memory:"/>
|
||||
<env name="DB_CONNECTION" value="mysql"/>
|
||||
<env name="DB_DATABASE" value="shopit_test"/>
|
||||
<env name="DB_URL" value=""/>
|
||||
<env name="MAIL_MAILER" value="array"/>
|
||||
<env name="QUEUE_CONNECTION" value="sync"/>
|
||||
|
||||
@@ -6,7 +6,6 @@ use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use App\Domains\Attachable\Exceptions\AttachmentStorageException;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Attachable\Services\AttachmentService;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
@@ -22,19 +21,12 @@ class AttachmentTest extends TestCase
|
||||
{
|
||||
Storage::fake('s3');
|
||||
|
||||
$tenant = Tenant::query()->create([
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme',
|
||||
'dominio' => 'acme.com',
|
||||
]);
|
||||
|
||||
$file = UploadedFile::fake()->image('logo.png');
|
||||
|
||||
$attachment = app(AttachmentService::class)->store(
|
||||
$file,
|
||||
'attachments/acme/logo.png',
|
||||
);
|
||||
$tenant->attachments()->attach($attachment->getKey());
|
||||
|
||||
$this->assertSame(AttachmentType::Image, $attachment->type);
|
||||
$this->assertTrue(Str::isUuid($attachment->key));
|
||||
@@ -48,26 +40,14 @@ class AttachmentTest extends TestCase
|
||||
'filename' => 'logo.png',
|
||||
'type' => AttachmentType::Image->value,
|
||||
]);
|
||||
$this->assertDatabaseHas('attachable_attachments', [
|
||||
'attachable_type' => $tenant->getMorphClass(),
|
||||
'attachable_id' => $tenant->getKey(),
|
||||
'attachment_id' => $attachment->id,
|
||||
]);
|
||||
|
||||
$freshAttachment = Attachment::query()->findOrFail($attachment->id);
|
||||
|
||||
$this->assertSame(AttachmentType::Image, $freshAttachment->type);
|
||||
$this->assertTrue($tenant->attachments->contains($freshAttachment));
|
||||
}
|
||||
|
||||
public function test_it_does_not_persist_the_attachment_when_the_s3_upload_fails(): void
|
||||
{
|
||||
$tenant = Tenant::query()->create([
|
||||
'codigo' => 'globex',
|
||||
'nombre' => 'Globex',
|
||||
'dominio' => 'globex.com',
|
||||
]);
|
||||
|
||||
$disk = Mockery::mock();
|
||||
Storage::shouldReceive('disk')
|
||||
->once()
|
||||
@@ -94,12 +74,6 @@ class AttachmentTest extends TestCase
|
||||
{
|
||||
Storage::fake('s3');
|
||||
|
||||
$tenant = Tenant::query()->create([
|
||||
'codigo' => 'initech',
|
||||
'nombre' => 'Initech',
|
||||
'dominio' => 'initech.com',
|
||||
]);
|
||||
|
||||
Storage::disk('s3')->put('attachments/initech/spec.pdf', 'spec');
|
||||
|
||||
$attachment = Attachment::query()->create([
|
||||
@@ -111,7 +85,6 @@ class AttachmentTest extends TestCase
|
||||
'extension' => 'pdf',
|
||||
'size' => 512,
|
||||
]);
|
||||
$tenant->attachments()->attach($attachment->getKey());
|
||||
|
||||
app(AttachmentService::class)->delete($attachment);
|
||||
|
||||
@@ -119,19 +92,10 @@ class AttachmentTest extends TestCase
|
||||
$this->assertDatabaseMissing('attachments', [
|
||||
'id' => $attachment->id,
|
||||
]);
|
||||
$this->assertDatabaseMissing('attachable_attachments', [
|
||||
'attachment_id' => $attachment->id,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_keeps_the_database_record_when_the_s3_delete_fails(): void
|
||||
{
|
||||
$tenant = Tenant::query()->create([
|
||||
'codigo' => 'umbrella',
|
||||
'nombre' => 'Umbrella',
|
||||
'dominio' => 'umbrella.com',
|
||||
]);
|
||||
|
||||
$attachment = Attachment::query()->create([
|
||||
'path' => 'attachments/umbrella/audio.mp3',
|
||||
'key' => (string) Str::uuid(),
|
||||
@@ -141,7 +105,6 @@ class AttachmentTest extends TestCase
|
||||
'extension' => 'mp3',
|
||||
'size' => 1024,
|
||||
]);
|
||||
$tenant->attachments()->attach($attachment->getKey());
|
||||
|
||||
$disk = Mockery::mock();
|
||||
Storage::shouldReceive('disk')
|
||||
@@ -161,11 +124,6 @@ class AttachmentTest extends TestCase
|
||||
$this->assertDatabaseHas('attachments', [
|
||||
'id' => $attachment->id,
|
||||
]);
|
||||
$this->assertDatabaseHas('attachable_attachments', [
|
||||
'attachment_id' => $attachment->id,
|
||||
'attachable_type' => $tenant->getMorphClass(),
|
||||
'attachable_id' => $tenant->getKey(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user