feat(website-extras): implement toggle functionality for website extras and update related tests

This commit is contained in:
2026-07-31 09:47:56 -03:00
parent 45e56c43a6
commit 40cbac17c6
5 changed files with 68 additions and 0 deletions

View File

@@ -42,6 +42,17 @@ class WebsiteExtraController extends Controller
);
}
public function toggle(Request $request, string $websiteExtraCode): WebsiteExtrasResource
{
$tenant = $request->user()->tenant()->firstOrFail();
$this->websiteExtraService->toggleForTenant($tenant, $websiteExtraCode);
return WebsiteExtrasResource::make(
$this->loadTenant($request->user())
);
}
private function loadTenant(User $user): Tenant
{
$tenant = $user->tenant()->firstOrFail();

View File

@@ -33,6 +33,8 @@ class WebsiteExtrasResource extends JsonResource
'nombre' => $definition->nombre,
'descripcion' => $definition->descripcion,
'is_required' => $definition->is_required,
'is_enabled' => $websiteExtras
->get($definition->codigo)?->is_enabled,
'request_rules' => $definition->config_schema['request_rules'] ?? [],
],
]) ?? [],

View File

@@ -166,6 +166,24 @@ class WebsiteExtraService
});
}
public function toggleForTenant(Tenant $tenant, string $extraCode): WebsiteExtra
{
$definition = $this->definitionForTenant($tenant, $extraCode);
return DB::transaction(function () use ($tenant, $definition): WebsiteExtra {
$websiteExtra = $tenant->websiteExtras()
->where('website_type_extra_id', $definition->id)
->lockForUpdate()
->firstOrFail();
$websiteExtra->update([
'is_enabled' => ! $websiteExtra->is_enabled,
]);
return $websiteExtra;
});
}
public function definitionForTenant(Tenant $tenant, string $extraCode): WebsiteTypeExtra
{
return WebsiteTypeExtra::query()

View File

@@ -10,4 +10,6 @@ Route::prefix('v1/adminapp/tenant')
->name('adminapp.tenant.website-extras.show');
Route::put('website-extras/{websiteExtraCode}', [WebsiteExtraController::class, 'update'])
->name('adminapp.tenant.website-extras.update');
Route::patch('website-extras/{websiteExtraCode}/toggle', [WebsiteExtraController::class, 'toggle'])
->name('adminapp.tenant.website-extras.toggle');
});

View File

@@ -79,6 +79,7 @@ class AdminAppWebsiteExtraControllerTest extends TestCase
->assertJsonPath('data.definitions.contactConfig.codigo', 'contactConfig')
->assertJsonPath('data.definitions.contactConfig.nombre', 'Configuración de contacto')
->assertJsonPath('data.definitions.contactConfig.is_required', false)
->assertJsonPath('data.definitions.contactConfig.is_enabled', null)
->assertJsonPath('data.extras.contactConfig.phone', '+54 341 555 0101')
->assertJsonPath('data.resolved_extras.contactConfig.phone', '+54 341 555 0101');
}
@@ -150,6 +151,40 @@ class AdminAppWebsiteExtraControllerTest extends TestCase
->assertNotFound();
}
public function test_adminapp_user_can_toggle_an_existing_website_extra(): void
{
$tenant = $this->createTenant('acme');
$definition = $this->websiteType->extras()->firstOrFail();
$websiteExtra = $tenant->websiteExtras()->create([
'website_type_extra_id' => $definition->id,
'config' => ['phone' => '+54 341 555 0101'],
'is_enabled' => false,
]);
Sanctum::actingAs($this->createAdminAppUser($tenant));
$this->patchJson('/api/v1/adminapp/tenant/website-extras/contactConfig/toggle')
->assertOk()
->assertJsonPath('data.definitions.contactConfig.is_enabled', true);
$this->assertTrue($websiteExtra->refresh()->is_enabled);
$this->patchJson('/api/v1/adminapp/tenant/website-extras/contactConfig/toggle')
->assertOk()
->assertJsonPath('data.definitions.contactConfig.is_enabled', false);
$this->assertFalse($websiteExtra->refresh()->is_enabled);
}
public function test_toggle_returns_not_found_for_an_extra_without_a_tenant_value(): void
{
$tenant = $this->createTenant('acme');
Sanctum::actingAs($this->createAdminAppUser($tenant));
$this->patchJson('/api/v1/adminapp/tenant/website-extras/contactConfig/toggle')
->assertNotFound();
}
private function createTenant(string $code): Tenant
{
return Tenant::query()->create([