From 3b36abd46dd15495032c396b1f819a78231b32e1 Mon Sep 17 00:00:00 2001 From: ncoronel Date: Mon, 3 Aug 2026 14:02:08 -0300 Subject: [PATCH] feat: Refactor event form functionality by removing social media data from AdminAppBootstrapResource and AdminAppBootstrapService; add EventFormController, EventFormResource, and EventFormService; implement routes and tests for event form --- .../Resources/AdminAppBootstrapResource.php | 11 +-- .../Services/AdminAppBootstrapService.php | 5 +- .../AdminApp/EventFormController.php | 22 ++++++ .../Forms/Resources/EventFormResource.php | 19 +++++ .../Resources/SocialMediaOptionResource.php | 3 +- .../Forms/Services/EventFormService.php | 27 +++++++ app/Domains/Forms/routes/adminapp.php | 10 +++ app/Domains/Forms/routes/api.php | 3 + routes/api.php | 1 + .../Forms/AdminAppEventFormControllerTest.php | 73 +++++++++++++++++++ .../BootstrapAdminAppControllerTest.php | 7 +- .../AdminAppBootstrapResourceTest.php | 17 +---- 12 files changed, 162 insertions(+), 36 deletions(-) create mode 100644 app/Domains/Forms/Controllers/AdminApp/EventFormController.php create mode 100644 app/Domains/Forms/Resources/EventFormResource.php rename app/Domains/{Bootstrap => Forms}/Resources/SocialMediaOptionResource.php (86%) create mode 100644 app/Domains/Forms/Services/EventFormService.php create mode 100644 app/Domains/Forms/routes/adminapp.php create mode 100644 app/Domains/Forms/routes/api.php create mode 100644 tests/Feature/Forms/AdminAppEventFormControllerTest.php diff --git a/app/Domains/Bootstrap/Resources/AdminAppBootstrapResource.php b/app/Domains/Bootstrap/Resources/AdminAppBootstrapResource.php index 0fe9dd1..296e284 100644 --- a/app/Domains/Bootstrap/Resources/AdminAppBootstrapResource.php +++ b/app/Domains/Bootstrap/Resources/AdminAppBootstrapResource.php @@ -3,11 +3,10 @@ namespace App\Domains\Bootstrap\Resources; use App\Domains\Tenant\Models\WebsiteType; -use Illuminate\Database\Eloquent\Collection; use Illuminate\Http\Request; use Illuminate\Http\Resources\Json\JsonResource; -/** @mixin array{website_type: WebsiteType, social_media: Collection} */ +/** @mixin array{website_type: WebsiteType} */ class AdminAppBootstrapResource extends JsonResource { /** @return array */ @@ -31,14 +30,6 @@ class AdminAppBootstrapResource extends JsonResource 'login_header_footer_color' => $websiteType->login_header_footer_color, 'site_logo' => $websiteType->siteLogo?->getTemporaryUrl(1440), 'footer_logo' => $websiteType->footerLogo?->getTemporaryUrl(1440), - 'forms' => [ - [ - 'code' => 'event', - 'social_media' => SocialMediaOptionResource::collection( - $this->resource['social_media'] - ), - ], - ], ]; } } diff --git a/app/Domains/Bootstrap/Services/AdminAppBootstrapService.php b/app/Domains/Bootstrap/Services/AdminAppBootstrapService.php index 71904b3..572579a 100644 --- a/app/Domains/Bootstrap/Services/AdminAppBootstrapService.php +++ b/app/Domains/Bootstrap/Services/AdminAppBootstrapService.php @@ -2,13 +2,11 @@ namespace App\Domains\Bootstrap\Services; -use App\Domains\Tenant\Models\SocialMedia; use App\Domains\Tenant\Models\WebsiteType; -use Illuminate\Database\Eloquent\Collection; class AdminAppBootstrapService { - /** @return array{website_type: WebsiteType, social_media: Collection} */ + /** @return array{website_type: WebsiteType} */ public function get(string $domain): array { return [ @@ -16,7 +14,6 @@ class AdminAppBootstrapService ->with(['siteLogo', 'footerLogo']) ->where('dominio', $domain) ->firstOrFail(), - 'social_media' => SocialMedia::query()->orderBy('id')->get(), ]; } } diff --git a/app/Domains/Forms/Controllers/AdminApp/EventFormController.php b/app/Domains/Forms/Controllers/AdminApp/EventFormController.php new file mode 100644 index 0000000..05a2706 --- /dev/null +++ b/app/Domains/Forms/Controllers/AdminApp/EventFormController.php @@ -0,0 +1,22 @@ +eventFormService->get( + $request->user('sanctum')->tenant()->firstOrFail() + ) + ); + } +} diff --git a/app/Domains/Forms/Resources/EventFormResource.php b/app/Domains/Forms/Resources/EventFormResource.php new file mode 100644 index 0000000..0cfd51d --- /dev/null +++ b/app/Domains/Forms/Resources/EventFormResource.php @@ -0,0 +1,19 @@ + */ + public function toArray(Request $request): array + { + return [ + 'social_media' => SocialMediaOptionResource::collection( + $this->resource['social_media'] + ), + ]; + } +} diff --git a/app/Domains/Bootstrap/Resources/SocialMediaOptionResource.php b/app/Domains/Forms/Resources/SocialMediaOptionResource.php similarity index 86% rename from app/Domains/Bootstrap/Resources/SocialMediaOptionResource.php rename to app/Domains/Forms/Resources/SocialMediaOptionResource.php index 97f3500..c2d5687 100644 --- a/app/Domains/Bootstrap/Resources/SocialMediaOptionResource.php +++ b/app/Domains/Forms/Resources/SocialMediaOptionResource.php @@ -1,6 +1,6 @@ $this->code, 'name' => $this->name, 'icon' => $this->icon, + 'url' => $this->url, ]; } } diff --git a/app/Domains/Forms/Services/EventFormService.php b/app/Domains/Forms/Services/EventFormService.php new file mode 100644 index 0000000..31846de --- /dev/null +++ b/app/Domains/Forms/Services/EventFormService.php @@ -0,0 +1,27 @@ +} */ + public function get(Tenant $tenant): array + { + $urls = $tenant->socialMedia() + ->pluck('tenant_social_media.url', 'social_media.code'); + + return [ + 'social_media' => SocialMedia::query() + ->orderBy('id') + ->get() + ->each(fn (SocialMedia $item) => $item->setAttribute( + 'url', + $urls->get($item->code) + )), + ]; + } +} diff --git a/app/Domains/Forms/routes/adminapp.php b/app/Domains/Forms/routes/adminapp.php new file mode 100644 index 0000000..da1e7e9 --- /dev/null +++ b/app/Domains/Forms/routes/adminapp.php @@ -0,0 +1,10 @@ +middleware(['auth:sanctum', 'adminapp.tenant']) + ->group(function (): void { + Route::get('event', EventFormController::class); + }); diff --git a/app/Domains/Forms/routes/api.php b/app/Domains/Forms/routes/api.php new file mode 100644 index 0000000..062e0fe --- /dev/null +++ b/app/Domains/Forms/routes/api.php @@ -0,0 +1,3 @@ +seed([AuthorizationSeeder::class, SocialMediaSeeder::class]); + WebsiteType::query()->create([ + 'codigo' => 'onticket', + 'nombre' => 'OnTicket', + ]); + } + + public function test_authentication_is_required(): void + { + $this->getJson('/api/v1/adminapp/forms/event')->assertUnauthorized(); + } + + public function test_an_adminapp_user_can_get_the_event_form(): void + { + $tenant = Tenant::query()->create([ + 'codigo' => 'acme', + 'nombre' => 'Acme', + 'dominio' => 'acme.test', + 'website_type_code' => 'onticket', + ]); + $tenant->socialMedia()->attach('instagram', [ + 'url' => 'https://instagram.com/acme', + 'orden' => 1, + ]); + Sanctum::actingAs(User::factory()->create([ + 'rol_codigo' => RoleCode::AdminApp->value, + 'tenant_codigo' => $tenant->codigo, + ])); + + $this->getJson('/api/v1/adminapp/forms/event') + ->assertOk() + ->assertJsonCount(4, 'data.social_media') + ->assertJsonPath('data.social_media.0.code', 'facebook') + ->assertJsonPath('data.social_media.0.name', 'Facebook') + ->assertJsonPath('data.social_media.0.icon', 'fa-brands fa-facebook') + ->assertJsonPath('data.social_media.0.url', null) + ->assertJsonPath('data.social_media.1.code', 'instagram') + ->assertJsonPath('data.social_media.1.url', 'https://instagram.com/acme') + ->assertJsonPath('data.social_media.3.code', 'linkedin'); + } + + public function test_a_customer_cannot_get_the_event_form(): void + { + Sanctum::actingAs(User::factory()->create([ + 'rol_codigo' => RoleCode::User->value, + 'tenant_codigo' => null, + ])); + + $this->getJson('/api/v1/adminapp/forms/event')->assertForbidden(); + } +} diff --git a/tests/Feature/Tenant/BootstrapAdminAppControllerTest.php b/tests/Feature/Tenant/BootstrapAdminAppControllerTest.php index c43dd0b..c64940b 100644 --- a/tests/Feature/Tenant/BootstrapAdminAppControllerTest.php +++ b/tests/Feature/Tenant/BootstrapAdminAppControllerTest.php @@ -4,7 +4,6 @@ namespace Tests\Feature\Tenant; use App\Domains\Attachable\Models\Attachment; use App\Domains\Tenant\Models\WebsiteType; -use Database\Seeders\SocialMediaSeeder; use Illuminate\Foundation\Testing\RefreshDatabase; use Tests\TestCase; @@ -14,7 +13,6 @@ class BootstrapAdminAppControllerTest extends TestCase public function test_it_publicly_bootstraps_the_admin_app_by_domain(): void { - $this->seed(SocialMediaSeeder::class); $footerLogo = Attachment::factory()->create(); WebsiteType::query()->create([ @@ -43,10 +41,7 @@ class BootstrapAdminAppControllerTest extends TestCase ->assertJsonPath('data.login_header_footer_color', '#313131') ->assertJsonPath('data.site_logo', null) ->assertJsonPath('data.footer_logo', $footerLogo->getTemporaryUrl(1440)) - ->assertJsonPath('data.forms.0.code', 'event') - ->assertJsonCount(4, 'data.forms.0.social_media') - ->assertJsonPath('data.forms.0.social_media.0.code', 'facebook') - ->assertJsonPath('data.forms.0.social_media.3.code', 'linkedin') + ->assertJsonMissingPath('data.forms') ->assertJsonMissingPath('data.codigo') ->assertJsonMissingPath('data.nombre') ->assertJsonMissingPath('data.dominio'); diff --git a/tests/Unit/Bootstrap/AdminAppBootstrapResourceTest.php b/tests/Unit/Bootstrap/AdminAppBootstrapResourceTest.php index 9d2a846..eb142f6 100644 --- a/tests/Unit/Bootstrap/AdminAppBootstrapResourceTest.php +++ b/tests/Unit/Bootstrap/AdminAppBootstrapResourceTest.php @@ -3,14 +3,12 @@ namespace Tests\Unit\Bootstrap; use App\Domains\Bootstrap\Resources\AdminAppBootstrapResource; -use App\Domains\Tenant\Models\SocialMedia; use App\Domains\Tenant\Models\WebsiteType; -use Illuminate\Database\Eloquent\Collection; use Tests\TestCase; class AdminAppBootstrapResourceTest extends TestCase { - public function test_it_exposes_the_website_type_and_forms(): void + public function test_it_exposes_the_website_type(): void { $websiteType = new WebsiteType([ 'codigo' => 'shopit', @@ -19,22 +17,11 @@ class AdminAppBootstrapResourceTest extends TestCase ]); $websiteType->setRelation('siteLogo', null); $websiteType->setRelation('footerLogo', null); - $socialMedia = new SocialMedia([ - 'code' => 'instagram', - 'name' => 'Instagram', - 'icon' => 'fa-brands fa-instagram', - ]); - $data = AdminAppBootstrapResource::make([ 'website_type' => $websiteType, - 'social_media' => new Collection([$socialMedia]), ])->resolve(request()); $this->assertSame('shopit', $data['website_type_code']); - $this->assertSame('event', $data['forms'][0]['code']); - $this->assertSame( - 'instagram', - $data['forms'][0]['social_media']->resolve(request())[0]['code'] - ); + $this->assertArrayNotHasKey('forms', $data); } }