Compare commits
2 Commits
45e56c43a6
...
6dd057874a
| Author | SHA1 | Date | |
|---|---|---|---|
| 6dd057874a | |||
| 40cbac17c6 |
@@ -5,6 +5,7 @@ namespace App\Domains\Tenant\Controllers\AdminApp;
|
|||||||
use App\Domains\Auth\Models\User;
|
use App\Domains\Auth\Models\User;
|
||||||
use App\Domains\Tenant\Models\Tenant;
|
use App\Domains\Tenant\Models\Tenant;
|
||||||
use App\Domains\Tenant\Requests\AdminApp\UpdateWebsiteExtraRequest;
|
use App\Domains\Tenant\Requests\AdminApp\UpdateWebsiteExtraRequest;
|
||||||
|
use App\Domains\Tenant\Resources\AdminApp\WebsiteExtraResource;
|
||||||
use App\Domains\Tenant\Resources\AdminApp\WebsiteExtrasResource;
|
use App\Domains\Tenant\Resources\AdminApp\WebsiteExtrasResource;
|
||||||
use App\Domains\Tenant\Services\TenantInformationService;
|
use App\Domains\Tenant\Services\TenantInformationService;
|
||||||
use App\Domains\Tenant\Services\WebsiteExtraService;
|
use App\Domains\Tenant\Services\WebsiteExtraService;
|
||||||
@@ -25,6 +26,20 @@ class WebsiteExtraController extends Controller
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function showExtra(Request $request, string $websiteExtraCode): WebsiteExtraResource
|
||||||
|
{
|
||||||
|
$tenant = $this->loadTenant($request->user());
|
||||||
|
$definition = $this->websiteExtraService->definitionForTenant($tenant, $websiteExtraCode);
|
||||||
|
$websiteExtra = $tenant->websiteExtras
|
||||||
|
->firstWhere('website_type_extra_id', $definition->id);
|
||||||
|
|
||||||
|
if (! $websiteExtra) {
|
||||||
|
abort(404);
|
||||||
|
}
|
||||||
|
|
||||||
|
return WebsiteExtraResource::make($websiteExtra);
|
||||||
|
}
|
||||||
|
|
||||||
public function update(
|
public function update(
|
||||||
UpdateWebsiteExtraRequest $request,
|
UpdateWebsiteExtraRequest $request,
|
||||||
string $websiteExtraCode
|
string $websiteExtraCode
|
||||||
@@ -42,6 +57,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
|
private function loadTenant(User $user): Tenant
|
||||||
{
|
{
|
||||||
$tenant = $user->tenant()->firstOrFail();
|
$tenant = $user->tenant()->firstOrFail();
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Domains\Tenant\Resources\AdminApp;
|
||||||
|
|
||||||
|
use App\Domains\Attachable\Models\Attachment;
|
||||||
|
use App\Domains\Tenant\Models\WebsiteExtra;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @mixin WebsiteExtra
|
||||||
|
*/
|
||||||
|
class WebsiteExtraResource extends JsonResource
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'codigo' => $this->websiteTypeExtra->codigo,
|
||||||
|
'nombre' => $this->websiteTypeExtra->nombre,
|
||||||
|
'descripcion' => $this->websiteTypeExtra->descripcion,
|
||||||
|
'is_required' => $this->websiteTypeExtra->is_required,
|
||||||
|
'is_enabled' => $this->is_enabled,
|
||||||
|
'request_rules' => $this->websiteTypeExtra->config_schema['request_rules'] ?? [],
|
||||||
|
'config' => $this->formatConfig(
|
||||||
|
$this->resolvedConfig(),
|
||||||
|
fn (Attachment $attachment): string => $attachment->key
|
||||||
|
),
|
||||||
|
'resolved_config' => $this->formatConfig(
|
||||||
|
$this->resolvedConfig(),
|
||||||
|
fn (Attachment $attachment): string => $attachment->getTemporaryUrl(1440)
|
||||||
|
),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function formatConfig(mixed $value, callable $formatAttachment): mixed
|
||||||
|
{
|
||||||
|
if ($value instanceof Attachment) {
|
||||||
|
return $formatAttachment($value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! is_array($value)) {
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_map(
|
||||||
|
fn (mixed $item): mixed => $this->formatConfig($item, $formatAttachment),
|
||||||
|
$value
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -33,6 +33,8 @@ class WebsiteExtrasResource extends JsonResource
|
|||||||
'nombre' => $definition->nombre,
|
'nombre' => $definition->nombre,
|
||||||
'descripcion' => $definition->descripcion,
|
'descripcion' => $definition->descripcion,
|
||||||
'is_required' => $definition->is_required,
|
'is_required' => $definition->is_required,
|
||||||
|
'is_enabled' => $websiteExtras
|
||||||
|
->get($definition->codigo)?->is_enabled,
|
||||||
'request_rules' => $definition->config_schema['request_rules'] ?? [],
|
'request_rules' => $definition->config_schema['request_rules'] ?? [],
|
||||||
],
|
],
|
||||||
]) ?? [],
|
]) ?? [],
|
||||||
|
|||||||
@@ -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
|
public function definitionForTenant(Tenant $tenant, string $extraCode): WebsiteTypeExtra
|
||||||
{
|
{
|
||||||
return WebsiteTypeExtra::query()
|
return WebsiteTypeExtra::query()
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ use Illuminate\Support\Facades\Route;
|
|||||||
Route::prefix('v1/adminapp/tenant')
|
Route::prefix('v1/adminapp/tenant')
|
||||||
->middleware(['auth:sanctum', 'adminapp.tenant'])
|
->middleware(['auth:sanctum', 'adminapp.tenant'])
|
||||||
->group(function (): void {
|
->group(function (): void {
|
||||||
Route::get('website-extras', [WebsiteExtraController::class, 'show'])
|
Route::get('website-extras', [WebsiteExtraController::class, 'show']);
|
||||||
->name('adminapp.tenant.website-extras.show');
|
Route::get('website-extras/{websiteExtraCode}', [WebsiteExtraController::class, 'showExtra']);
|
||||||
Route::put('website-extras/{websiteExtraCode}', [WebsiteExtraController::class, 'update'])
|
Route::put('website-extras/{websiteExtraCode}', [WebsiteExtraController::class, 'update']);
|
||||||
->name('adminapp.tenant.website-extras.update');
|
Route::patch('website-extras/{websiteExtraCode}/toggle', [WebsiteExtraController::class, 'toggle']);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -79,10 +79,41 @@ class AdminAppWebsiteExtraControllerTest extends TestCase
|
|||||||
->assertJsonPath('data.definitions.contactConfig.codigo', 'contactConfig')
|
->assertJsonPath('data.definitions.contactConfig.codigo', 'contactConfig')
|
||||||
->assertJsonPath('data.definitions.contactConfig.nombre', 'Configuración de contacto')
|
->assertJsonPath('data.definitions.contactConfig.nombre', 'Configuración de contacto')
|
||||||
->assertJsonPath('data.definitions.contactConfig.is_required', false)
|
->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.extras.contactConfig.phone', '+54 341 555 0101')
|
||||||
->assertJsonPath('data.resolved_extras.contactConfig.phone', '+54 341 555 0101');
|
->assertJsonPath('data.resolved_extras.contactConfig.phone', '+54 341 555 0101');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function test_adminapp_user_can_read_one_website_extra(): void
|
||||||
|
{
|
||||||
|
$tenant = $this->createTenant('acme');
|
||||||
|
$definition = $this->websiteType->extras()->firstOrFail();
|
||||||
|
$tenant->websiteExtras()->create([
|
||||||
|
'website_type_extra_id' => $definition->id,
|
||||||
|
'config' => ['phone' => '+54 341 555 0101'],
|
||||||
|
'is_enabled' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
||||||
|
|
||||||
|
$this->getJson('/api/v1/adminapp/tenant/website-extras/contactConfig')
|
||||||
|
->assertOk()
|
||||||
|
->assertJsonPath('data.codigo', 'contactConfig')
|
||||||
|
->assertJsonPath('data.nombre', 'Configuración de contacto')
|
||||||
|
->assertJsonPath('data.is_enabled', true)
|
||||||
|
->assertJsonPath('data.config.phone', '+54 341 555 0101')
|
||||||
|
->assertJsonPath('data.resolved_config.phone', '+54 341 555 0101');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function test_reading_an_unconfigured_website_extra_returns_not_found(): void
|
||||||
|
{
|
||||||
|
$tenant = $this->createTenant('acme');
|
||||||
|
Sanctum::actingAs($this->createAdminAppUser($tenant));
|
||||||
|
|
||||||
|
$this->getJson('/api/v1/adminapp/tenant/website-extras/contactConfig')
|
||||||
|
->assertNotFound();
|
||||||
|
}
|
||||||
|
|
||||||
public function test_adminapp_user_updates_one_extra_without_touching_other_tenants(): void
|
public function test_adminapp_user_updates_one_extra_without_touching_other_tenants(): void
|
||||||
{
|
{
|
||||||
$tenant = $this->createTenant('acme');
|
$tenant = $this->createTenant('acme');
|
||||||
@@ -150,6 +181,40 @@ class AdminAppWebsiteExtraControllerTest extends TestCase
|
|||||||
->assertNotFound();
|
->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
|
private function createTenant(string $code): Tenant
|
||||||
{
|
{
|
||||||
return Tenant::query()->create([
|
return Tenant::query()->create([
|
||||||
|
|||||||
Reference in New Issue
Block a user