From 217f86f624ffa42a6573f3247241efb9726117e3 Mon Sep 17 00:00:00 2001 From: ncoronel Date: Thu, 23 Jul 2026 15:19:17 -0300 Subject: [PATCH] feat(social-media): add 'orden' field to social media pivot table and update related functionality --- app/Domains/Tenant/Models/SocialMedia.php | 5 +-- app/Domains/Tenant/Models/Tenant.php | 5 +-- .../Tenant/Requests/StoreTenantRequest.php | 1 + .../Tenant/Requests/UpdateTenantRequest.php | 1 + app/Domains/Tenant/Services/TenantService.php | 9 ++++-- ...add_orden_to_tenant_social_media_table.php | 24 ++++++++++++++ .../Feature/Tenant/TenantSocialMediaTest.php | 32 +++++++++++++++++++ 7 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 database/migrations/2026_07_23_000300_add_orden_to_tenant_social_media_table.php diff --git a/app/Domains/Tenant/Models/SocialMedia.php b/app/Domains/Tenant/Models/SocialMedia.php index b308a24..616a543 100644 --- a/app/Domains/Tenant/Models/SocialMedia.php +++ b/app/Domains/Tenant/Models/SocialMedia.php @@ -28,7 +28,8 @@ class SocialMedia extends Model 'code', 'codigo' ) - ->withPivot('url') - ->withTimestamps(); + ->withPivot(['url', 'orden']) + ->withTimestamps() + ->orderByPivot('orden'); } } diff --git a/app/Domains/Tenant/Models/Tenant.php b/app/Domains/Tenant/Models/Tenant.php index 133d785..43a1570 100644 --- a/app/Domains/Tenant/Models/Tenant.php +++ b/app/Domains/Tenant/Models/Tenant.php @@ -107,8 +107,9 @@ class Tenant extends Model 'codigo', 'code' ) - ->withPivot('url') - ->withTimestamps(); + ->withPivot(['url', 'orden']) + ->withTimestamps() + ->orderByPivot('orden'); } public function menues(): BelongsToMany diff --git a/app/Domains/Tenant/Requests/StoreTenantRequest.php b/app/Domains/Tenant/Requests/StoreTenantRequest.php index 60c3b03..9092d03 100644 --- a/app/Domains/Tenant/Requests/StoreTenantRequest.php +++ b/app/Domains/Tenant/Requests/StoreTenantRequest.php @@ -71,6 +71,7 @@ class StoreTenantRequest extends FormRequest Rule::exists('social_media', 'code'), ], 'social_media.*.url' => ['required', 'url', 'max:2048'], + 'social_media.*.orden' => ['sometimes', 'integer', 'min:0', 'distinct'], 'hero_config' => ['nullable', 'array'], 'hero_config.title_html' => ['nullable', 'string'], 'hero_config.description_html' => ['nullable', 'string'], diff --git a/app/Domains/Tenant/Requests/UpdateTenantRequest.php b/app/Domains/Tenant/Requests/UpdateTenantRequest.php index 7efd48c..7594531 100644 --- a/app/Domains/Tenant/Requests/UpdateTenantRequest.php +++ b/app/Domains/Tenant/Requests/UpdateTenantRequest.php @@ -82,6 +82,7 @@ class UpdateTenantRequest extends FormRequest Rule::exists('social_media', 'code'), ], 'social_media.*.url' => ['required', 'url', 'max:2048'], + 'social_media.*.orden' => ['sometimes', 'integer', 'min:0', 'distinct'], 'hero_config' => ['nullable', 'array'], 'hero_config.title_html' => ['nullable', 'string'], 'hero_config.description_html' => ['nullable', 'string'], diff --git a/app/Domains/Tenant/Services/TenantService.php b/app/Domains/Tenant/Services/TenantService.php index 7c8d1f1..9cfc5b4 100644 --- a/app/Domains/Tenant/Services/TenantService.php +++ b/app/Domains/Tenant/Services/TenantService.php @@ -197,14 +197,17 @@ class TenantService } /** - * @param array $socialMedia + * @param array $socialMedia */ private function syncSocialMedia(Tenant $tenant, array $socialMedia): void { $associations = []; - foreach ($socialMedia as $item) { - $associations[$item['code']] = ['url' => $item['url']]; + foreach (array_values($socialMedia) as $index => $item) { + $associations[$item['code']] = [ + 'url' => $item['url'], + 'orden' => $item['orden'] ?? $index, + ]; } $tenant->socialMedia()->sync($associations); diff --git a/database/migrations/2026_07_23_000300_add_orden_to_tenant_social_media_table.php b/database/migrations/2026_07_23_000300_add_orden_to_tenant_social_media_table.php new file mode 100644 index 0000000..bfa3191 --- /dev/null +++ b/database/migrations/2026_07_23_000300_add_orden_to_tenant_social_media_table.php @@ -0,0 +1,24 @@ +unsignedInteger('orden')->default(0)->after('url'); + $table->index(['tenant_code', 'orden']); + }); + } + + public function down(): void + { + Schema::table('tenant_social_media', function (Blueprint $table): void { + $table->dropIndex(['tenant_code', 'orden']); + $table->dropColumn('orden'); + }); + } +}; diff --git a/tests/Feature/Tenant/TenantSocialMediaTest.php b/tests/Feature/Tenant/TenantSocialMediaTest.php index d7c7679..0901eb9 100644 --- a/tests/Feature/Tenant/TenantSocialMediaTest.php +++ b/tests/Feature/Tenant/TenantSocialMediaTest.php @@ -30,6 +30,7 @@ class TenantSocialMediaTest extends TestCase 'tenant_code', 'social_media_code', 'url', + 'orden', 'created_at', 'updated_at', ], Schema::getColumnListing('tenant_social_media')); @@ -46,6 +47,7 @@ class TenantSocialMediaTest extends TestCase $tenant->socialMedia()->attach($instagram->code, [ 'url' => 'https://instagram.com/acme', + 'orden' => 3, ]); $this->assertTrue($tenant->socialMedia()->firstOrFail()->is($instagram)); @@ -53,6 +55,7 @@ class TenantSocialMediaTest extends TestCase 'https://instagram.com/acme', $tenant->socialMedia()->firstOrFail()->pivot->url ); + $this->assertSame(3, $tenant->socialMedia()->firstOrFail()->pivot->orden); $this->assertTrue($instagram->tenants()->firstOrFail()->is($tenant)); } @@ -91,6 +94,7 @@ class TenantSocialMediaTest extends TestCase [ 'code' => $instagram->code, 'url' => 'https://instagram.com/acme', + 'orden' => 4, ], ], ]) @@ -105,6 +109,7 @@ class TenantSocialMediaTest extends TestCase 'tenant_code' => $tenant->codigo, 'social_media_code' => $instagram->code, 'url' => 'https://instagram.com/acme', + 'orden' => 4, ]); $this->assertDatabaseMissing('tenant_social_media', [ 'tenant_code' => $tenant->codigo, @@ -112,6 +117,33 @@ class TenantSocialMediaTest extends TestCase ]); } + public function test_tenant_social_media_are_returned_in_configured_order(): void + { + $tenant = $this->createTenant(); + $instagram = $this->createSocialMedia('instagram', 'Instagram'); + $facebook = $this->createSocialMedia('facebook', 'Facebook'); + + $this->putJson("/api/tenants/{$tenant->codigo}", [ + 'social_media' => [ + [ + 'code' => $instagram->code, + 'url' => 'https://instagram.com/acme', + 'orden' => 20, + ], + [ + 'code' => $facebook->code, + 'url' => 'https://facebook.com/acme', + 'orden' => 10, + ], + ], + ]) + ->assertOk() + ->assertJsonPath('data.social_media.0.code', 'facebook') + ->assertJsonPath('data.social_media.1.code', 'instagram') + ->assertJsonMissingPath('data.social_media.0.orden') + ->assertJsonMissingPath('data.social_media.1.orden'); + } + public function test_tenant_store_synchronizes_social_media(): void { $instagram = $this->createSocialMedia('instagram', 'Instagram');