Compare commits
11 Commits
auth/googl
...
5ada399adf
| Author | SHA1 | Date | |
|---|---|---|---|
| 5ada399adf | |||
| aed818da6e | |||
| 018f3f6c13 | |||
| 217f86f624 | |||
| 25891cefbc | |||
| c66d2019d6 | |||
| 12d496544d | |||
| c31be9c927 | |||
| 67ede1a2b4 | |||
| e778d862ba | |||
| 1438ff66c7 |
@@ -2,8 +2,10 @@
|
||||
|
||||
namespace App\Domains\Catalog\Controllers;
|
||||
|
||||
use App\Domains\Catalog\Enums\GroupLayout;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Models\FeaturedGroup;
|
||||
use App\Domains\Catalog\Models\FeaturedItem;
|
||||
use App\Domains\Catalog\Requests\CatalogItemDetailRequest;
|
||||
use App\Domains\Catalog\Requests\FeaturedGroupPageRequest;
|
||||
use App\Domains\Catalog\Requests\StoreCatalogItemRequest;
|
||||
@@ -14,6 +16,7 @@ use App\Domains\Catalog\Resources\CatalogItemResource;
|
||||
use App\Domains\Catalog\Services\CatalogService;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Pagination\LengthAwarePaginator;
|
||||
|
||||
@@ -81,9 +84,19 @@ class CatalogController extends Controller
|
||||
->setStatusCode(201);
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
/** @return array<array-key, mixed> */
|
||||
private function featuredItemsResponse(FeaturedGroup $featuredGroup, int $page): array
|
||||
{
|
||||
if ($featuredGroup->group_layout !== GroupLayout::Paginated) {
|
||||
$featuredItems = $this->featuredItemsQuery($featuredGroup)->get();
|
||||
|
||||
$featuredItems->each(
|
||||
fn ($featuredItem) => $featuredItem->setRelation('featuredGroup', $featuredGroup)
|
||||
);
|
||||
|
||||
return CatalogFeaturedItemResource::collection($featuredItems)->resolve();
|
||||
}
|
||||
|
||||
$paginator = $this->paginateFeaturedItems($featuredGroup, $page);
|
||||
|
||||
$paginator->getCollection()->each(
|
||||
@@ -99,25 +112,29 @@ class CatalogController extends Controller
|
||||
FeaturedGroup $featuredGroup,
|
||||
int $page,
|
||||
): LengthAwarePaginator {
|
||||
$paginator = $featuredGroup->featuredItems()
|
||||
->with([
|
||||
'catalogItem.inventory',
|
||||
'catalogItem.attachments',
|
||||
'catalogItem.variants.inventory',
|
||||
'catalogItem.variants.attachments',
|
||||
'catalogItem.variants.definitions.itemAttribute.attribute',
|
||||
'catalogItem.bundleComponents.catalogItem',
|
||||
'catalogItem.bundleComponents.variant.catalogItem',
|
||||
])
|
||||
->paginate(
|
||||
perPage: self::ITEMS_PER_PAGE,
|
||||
pageName: 'page',
|
||||
page: $page,
|
||||
);
|
||||
$paginator = $this->featuredItemsQuery($featuredGroup)->paginate(
|
||||
perPage: self::ITEMS_PER_PAGE,
|
||||
pageName: 'page',
|
||||
page: $page,
|
||||
);
|
||||
|
||||
return $paginator->withPath(route('catalog.featured-groups.items.index', [
|
||||
'tenant' => $featuredGroup->tenant_code,
|
||||
'featuredGroup' => $featuredGroup->id,
|
||||
]));
|
||||
}
|
||||
|
||||
/** @return HasMany<FeaturedItem, FeaturedGroup> */
|
||||
private function featuredItemsQuery(FeaturedGroup $featuredGroup): HasMany
|
||||
{
|
||||
return $featuredGroup->featuredItems()->with([
|
||||
'catalogItem.inventory',
|
||||
'catalogItem.attachments',
|
||||
'catalogItem.variants.inventory',
|
||||
'catalogItem.variants.attachments',
|
||||
'catalogItem.variants.definitions.itemAttribute.attribute',
|
||||
'catalogItem.bundleComponents.catalogItem',
|
||||
'catalogItem.bundleComponents.variant.catalogItem',
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
19
app/Domains/Catalog/Enums/GroupLayout.php
Normal file
19
app/Domains/Catalog/Enums/GroupLayout.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Catalog\Enums;
|
||||
|
||||
enum GroupLayout: string
|
||||
{
|
||||
case Paginated = 'paginated';
|
||||
case Simple = 'simple';
|
||||
case SimpleVertical = 'simple_vertical';
|
||||
case Carousel = 'carousel';
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
public static function values(): array
|
||||
{
|
||||
return array_column(self::cases(), 'value');
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Domains\Catalog\Models;
|
||||
|
||||
use App\Domains\Catalog\Enums\GroupLayout;
|
||||
use App\Domains\Catalog\Enums\ProductLayout;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
@@ -13,6 +14,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
#[Fillable([
|
||||
'tenant_code',
|
||||
'product_layout',
|
||||
'group_layout',
|
||||
'group_name',
|
||||
'group_order',
|
||||
])]
|
||||
@@ -28,6 +30,7 @@ class FeaturedGroup extends Model
|
||||
{
|
||||
return [
|
||||
'product_layout' => ProductLayout::class,
|
||||
'group_layout' => GroupLayout::class,
|
||||
'group_order' => 'integer',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -9,8 +9,8 @@ use Illuminate\Http\Resources\Json\JsonResource;
|
||||
/** @mixin FeaturedGroup */
|
||||
class CatalogFeaturedGroupResource extends JsonResource
|
||||
{
|
||||
/** @param array<string, mixed> $itemsPage */
|
||||
public function __construct($resource, private readonly array $itemsPage)
|
||||
/** @param array<array-key, mixed> $items */
|
||||
public function __construct($resource, private readonly array $items)
|
||||
{
|
||||
parent::__construct($resource);
|
||||
}
|
||||
@@ -22,8 +22,9 @@ class CatalogFeaturedGroupResource extends JsonResource
|
||||
'id' => $this->id,
|
||||
'title' => $this->group_name,
|
||||
'layout' => $this->product_layout->value,
|
||||
'group_layout' => $this->group_layout->value,
|
||||
'group_order' => $this->group_order,
|
||||
'items' => $this->itemsPage,
|
||||
'items' => $this->items,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,14 +4,16 @@ namespace App\Domains\Menu\Controllers;
|
||||
|
||||
use App\Domains\Menu\Models\Menu;
|
||||
use App\Http\Controllers\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class MenuController extends Controller
|
||||
{
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
$menues = Menu::all();
|
||||
|
||||
return response()->json($menues);
|
||||
}
|
||||
|
||||
@@ -19,10 +21,22 @@ class MenuController extends Controller
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'code' => 'required|string|unique:menues,code',
|
||||
'parent_menu_code' => [
|
||||
'nullable',
|
||||
'string',
|
||||
Rule::exists('menues', 'code'),
|
||||
'different:code',
|
||||
],
|
||||
'content_type' => [
|
||||
'sometimes',
|
||||
Rule::in([Menu::CONTENT_TYPE_STATIC, Menu::CONTENT_TYPE_DYNAMIC]),
|
||||
],
|
||||
'static_content_schema' => 'required_if:content_type,static|nullable|array',
|
||||
'route' => 'required|string',
|
||||
]);
|
||||
|
||||
$menu = Menu::create($validated);
|
||||
|
||||
return response()->json($menu, 201);
|
||||
}
|
||||
|
||||
@@ -34,17 +48,30 @@ class MenuController extends Controller
|
||||
public function update(Request $request, Menu $menu): JsonResponse
|
||||
{
|
||||
$validated = $request->validate([
|
||||
'code' => 'sometimes|required|string|unique:menues,code,' . $menu->id,
|
||||
'code' => 'sometimes|required|string|unique:menues,code,'.$menu->id,
|
||||
'parent_menu_code' => [
|
||||
'nullable',
|
||||
'string',
|
||||
Rule::exists('menues', 'code'),
|
||||
Rule::notIn([$menu->code]),
|
||||
],
|
||||
'content_type' => [
|
||||
'sometimes',
|
||||
Rule::in([Menu::CONTENT_TYPE_STATIC, Menu::CONTENT_TYPE_DYNAMIC]),
|
||||
],
|
||||
'static_content_schema' => 'required_if:content_type,static|nullable|array',
|
||||
'route' => 'sometimes|required|string',
|
||||
]);
|
||||
|
||||
$menu->update($validated);
|
||||
|
||||
return response()->json($menu);
|
||||
}
|
||||
|
||||
public function destroy(Menu $menu): JsonResponse
|
||||
{
|
||||
$menu->delete();
|
||||
|
||||
return response()->json(null, 204);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,28 +5,76 @@ namespace App\Domains\Menu\Models;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class Menu extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
public const CONTENT_TYPE_DYNAMIC = 'dynamic';
|
||||
|
||||
public const CONTENT_TYPE_STATIC = 'static';
|
||||
|
||||
protected $table = 'menues';
|
||||
|
||||
protected $attributes = [
|
||||
'content_type' => self::CONTENT_TYPE_DYNAMIC,
|
||||
];
|
||||
|
||||
protected $fillable = [
|
||||
'code',
|
||||
'parent_menu_code',
|
||||
'content_type',
|
||||
'static_content_schema',
|
||||
'route',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'static_content_schema' => 'array',
|
||||
];
|
||||
}
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::saving(function (self $menu): void {
|
||||
if (
|
||||
$menu->content_type === self::CONTENT_TYPE_STATIC
|
||||
&& empty($menu->static_content_schema)
|
||||
) {
|
||||
throw ValidationException::withMessages([
|
||||
'static_content_schema' => 'El schema es obligatorio para los menús estáticos.',
|
||||
]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function parent(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(self::class, 'parent_menu_code', 'code');
|
||||
}
|
||||
|
||||
public function children(): HasMany
|
||||
{
|
||||
return $this->hasMany(self::class, 'parent_menu_code', 'code');
|
||||
}
|
||||
|
||||
public function tenants(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
Tenant::class,
|
||||
'tenant_menues',
|
||||
'tenants_menues',
|
||||
'menu_code',
|
||||
'tenant_codigo',
|
||||
'tenant_code',
|
||||
'code',
|
||||
'codigo'
|
||||
)->withTimestamps();
|
||||
)
|
||||
->using(TenantMenu::class)
|
||||
->withPivot('static_content')
|
||||
->withTimestamps();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,36 @@
|
||||
|
||||
namespace App\Domains\Menu\Models;
|
||||
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
use Illuminate\Database\Eloquent\Relations\Pivot;
|
||||
|
||||
class TenantMenu extends Pivot
|
||||
{
|
||||
protected $table = 'tenant_menues';
|
||||
protected $table = 'tenants_menues';
|
||||
|
||||
public $incrementing = true;
|
||||
|
||||
protected $fillable = [
|
||||
'tenant_code',
|
||||
'menu_code',
|
||||
'static_content',
|
||||
];
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'static_content' => 'array',
|
||||
];
|
||||
}
|
||||
|
||||
public function tenant(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Tenant::class, 'tenant_code', 'codigo');
|
||||
}
|
||||
|
||||
public function menu(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Menu::class, 'menu_code', 'code');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,10 @@ class BootstrapTenantController extends Controller
|
||||
$dominio = $request->validated('dominio');
|
||||
|
||||
return TenantResource::make(
|
||||
Tenant::query()->with(['headerLogo', 'footerLogo', 'menues'])->where('dominio', $dominio)->firstOrFail()
|
||||
Tenant::query()
|
||||
->with(['headerLogo', 'footerLogo', 'mainCarouselImages', 'menues', 'socialMedia'])
|
||||
->where('dominio', $dominio)
|
||||
->firstOrFail()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,32 +13,41 @@ use Illuminate\Http\Response;
|
||||
|
||||
class TenantController extends Controller
|
||||
{
|
||||
public function __construct(protected TenantService $tenantService)
|
||||
{
|
||||
}
|
||||
public function __construct(protected TenantService $tenantService) {}
|
||||
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
return TenantResource::collection(Tenant::query()->with(['headerLogo', 'footerLogo'])->latest()->paginateFromRequest())->response();
|
||||
return TenantResource::collection(
|
||||
Tenant::query()
|
||||
->with(['headerLogo', 'footerLogo', 'mainCarouselImages', 'socialMedia'])
|
||||
->latest()
|
||||
->paginateFromRequest()
|
||||
)->response();
|
||||
}
|
||||
|
||||
public function store(StoreTenantRequest $request): JsonResponse
|
||||
{
|
||||
$tenant = $this->tenantService->create($request->validated());
|
||||
|
||||
return TenantResource::make($tenant->loadMissing(['headerLogo', 'footerLogo']))->response()->setStatusCode(201);
|
||||
return TenantResource::make(
|
||||
$tenant->loadMissing(['headerLogo', 'footerLogo', 'mainCarouselImages', 'socialMedia'])
|
||||
)->response()->setStatusCode(201);
|
||||
}
|
||||
|
||||
public function show(Tenant $tenant): TenantResource
|
||||
{
|
||||
return TenantResource::make($tenant->loadMissing(['headerLogo', 'footerLogo']));
|
||||
return TenantResource::make(
|
||||
$tenant->loadMissing(['headerLogo', 'footerLogo', 'mainCarouselImages', 'socialMedia'])
|
||||
);
|
||||
}
|
||||
|
||||
public function update(UpdateTenantRequest $request, Tenant $tenant): TenantResource
|
||||
{
|
||||
$tenant = $this->tenantService->update($tenant, $request->validated());
|
||||
|
||||
return TenantResource::make($tenant->loadMissing(['headerLogo', 'footerLogo']));
|
||||
return TenantResource::make(
|
||||
$tenant->loadMissing(['headerLogo', 'footerLogo', 'mainCarouselImages', 'socialMedia'])
|
||||
);
|
||||
}
|
||||
|
||||
public function destroy(Tenant $tenant): Response
|
||||
|
||||
35
app/Domains/Tenant/Models/SocialMedia.php
Normal file
35
app/Domains/Tenant/Models/SocialMedia.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Tenant\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||
|
||||
class SocialMedia extends Model
|
||||
{
|
||||
protected $table = 'social_media';
|
||||
|
||||
protected $fillable = [
|
||||
'code',
|
||||
'icon',
|
||||
'name',
|
||||
];
|
||||
|
||||
/**
|
||||
* @return BelongsToMany<Tenant, $this>
|
||||
*/
|
||||
public function tenants(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
Tenant::class,
|
||||
'tenant_social_media',
|
||||
'social_media_code',
|
||||
'tenant_code',
|
||||
'code',
|
||||
'codigo'
|
||||
)
|
||||
->withPivot(['url', 'orden'])
|
||||
->withTimestamps()
|
||||
->orderByPivot('orden');
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace App\Domains\Tenant\Models;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Menu\Models\Menu;
|
||||
use App\Domains\Menu\Models\TenantMenu;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
@@ -73,20 +74,57 @@ class Tenant extends Model
|
||||
return $this->belongsTo(Attachment::class, 'hero_bg_image_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsToMany<Attachment, $this>
|
||||
*/
|
||||
public function mainCarouselImages(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
Attachment::class,
|
||||
'tenant_main_carousel_images',
|
||||
'tenant_id',
|
||||
'attachment_id'
|
||||
)
|
||||
->withPivot('orden')
|
||||
->withTimestamps()
|
||||
->orderByPivot('orden');
|
||||
}
|
||||
|
||||
public function catalogItems(): HasMany
|
||||
{
|
||||
return $this->hasMany(CatalogItem::class, 'tenant_code', 'codigo');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsToMany<SocialMedia, $this>
|
||||
*/
|
||||
public function socialMedia(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
SocialMedia::class,
|
||||
'tenant_social_media',
|
||||
'tenant_code',
|
||||
'social_media_code',
|
||||
'codigo',
|
||||
'code'
|
||||
)
|
||||
->withPivot(['url', 'orden'])
|
||||
->withTimestamps()
|
||||
->orderByPivot('orden');
|
||||
}
|
||||
|
||||
public function menues(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
Menu::class,
|
||||
'tenant_menues',
|
||||
'tenant_codigo',
|
||||
'tenants_menues',
|
||||
'tenant_code',
|
||||
'menu_code',
|
||||
'codigo',
|
||||
'code'
|
||||
)->withTimestamps();
|
||||
)
|
||||
->using(TenantMenu::class)
|
||||
->withPivot('static_content')
|
||||
->withTimestamps();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,8 +35,7 @@ class StoreTenantRequest extends FormRequest
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$logoRule = ['required', new ImageOrBase64Rule()];
|
||||
|
||||
$logoRule = ['required', new ImageOrBase64Rule];
|
||||
|
||||
return [
|
||||
'codigo' => ['required', 'string', 'max:255', Rule::unique('tenants', 'codigo')],
|
||||
@@ -61,7 +60,18 @@ class StoreTenantRequest extends FormRequest
|
||||
'footer_bg_color' => ['required', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
|
||||
'header_logo' => $logoRule,
|
||||
'footer_logo' => $logoRule,
|
||||
'hero_bg_image' => ['nullable', new ImageOrBase64Rule()],
|
||||
'hero_bg_image' => ['nullable', new ImageOrBase64Rule],
|
||||
'main_carousel_images' => ['sometimes', 'array'],
|
||||
'main_carousel_images.*' => ['required', 'distinct', new ImageOrBase64Rule],
|
||||
'social_media' => ['sometimes', 'array'],
|
||||
'social_media.*.code' => [
|
||||
'required',
|
||||
'string',
|
||||
'distinct',
|
||||
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'],
|
||||
|
||||
@@ -41,8 +41,7 @@ class UpdateTenantRequest extends FormRequest
|
||||
/** @var Tenant|null $tenant */
|
||||
$tenant = $this->route('tenant');
|
||||
|
||||
$logoRule = ['nullable', new ImageOrBase64Rule()];
|
||||
|
||||
$logoRule = ['nullable', new ImageOrBase64Rule];
|
||||
|
||||
return [
|
||||
'codigo' => [
|
||||
@@ -72,7 +71,18 @@ class UpdateTenantRequest extends FormRequest
|
||||
'footer_bg_color' => ['nullable', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
|
||||
'header_logo' => $logoRule,
|
||||
'footer_logo' => $logoRule,
|
||||
'hero_bg_image' => ['nullable', new ImageOrBase64Rule()],
|
||||
'hero_bg_image' => ['nullable', new ImageOrBase64Rule],
|
||||
'main_carousel_images' => ['sometimes', 'array'],
|
||||
'main_carousel_images.*' => ['required', 'distinct', new ImageOrBase64Rule],
|
||||
'social_media' => ['sometimes', 'array'],
|
||||
'social_media.*.code' => [
|
||||
'required',
|
||||
'string',
|
||||
'distinct',
|
||||
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'],
|
||||
|
||||
@@ -2,11 +2,12 @@
|
||||
|
||||
namespace App\Domains\Tenant\Resources;
|
||||
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
/**
|
||||
* @mixin \App\Domains\Tenant\Models\Tenant
|
||||
* @mixin Tenant
|
||||
*/
|
||||
class TenantResource extends JsonResource
|
||||
{
|
||||
@@ -34,9 +35,26 @@ class TenantResource extends JsonResource
|
||||
'footer_bg_color' => $this->footer_bg_color,
|
||||
// 1 day
|
||||
'header_logo' => $this->headerLogo?->getTemporaryUrl(1440),
|
||||
'footer_logo' => $this->footerLogo?->getTemporaryUrl(1440 ),
|
||||
'footer_logo' => $this->footerLogo?->getTemporaryUrl(1440),
|
||||
'hero_config' => $heroConfig,
|
||||
'event_config' => $this->event_config,
|
||||
'main_carousel_images' => $this->whenLoaded(
|
||||
'mainCarouselImages',
|
||||
fn () => $this->mainCarouselImages
|
||||
->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))
|
||||
->values()
|
||||
),
|
||||
'social_media' => $this->whenLoaded(
|
||||
'socialMedia',
|
||||
fn () => $this->socialMedia
|
||||
->map(fn ($socialMedia) => [
|
||||
'code' => $socialMedia->code,
|
||||
'icon' => $socialMedia->icon,
|
||||
'name' => $socialMedia->name,
|
||||
'url' => $socialMedia->pivot->url,
|
||||
])
|
||||
->values()
|
||||
),
|
||||
'menues' => $this->whenLoaded('menues'),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -2,22 +2,22 @@
|
||||
|
||||
namespace App\Domains\Tenant\Services;
|
||||
|
||||
use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Attachable\Services\AttachmentService;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
|
||||
class TenantService
|
||||
{
|
||||
public function __construct(protected AttachmentService $attachmentService)
|
||||
{
|
||||
}
|
||||
public function __construct(protected AttachmentService $attachmentService) {}
|
||||
|
||||
/**
|
||||
* Create a new tenant and store its logos.
|
||||
*
|
||||
* @param array<string, mixed> $data
|
||||
* @return Tenant
|
||||
*/
|
||||
public function create(array $data): Tenant
|
||||
{
|
||||
@@ -25,13 +25,21 @@ class TenantService
|
||||
$headerLogo = $data['header_logo'] ?? null;
|
||||
$footerLogo = $data['footer_logo'] ?? null;
|
||||
$heroBgImage = $data['hero_bg_image'] ?? null;
|
||||
$mainCarouselImages = $data['main_carousel_images'] ?? [];
|
||||
$socialMedia = $data['social_media'] ?? [];
|
||||
|
||||
unset($data['header_logo'], $data['footer_logo'], $data['hero_bg_image']);
|
||||
unset(
|
||||
$data['header_logo'],
|
||||
$data['footer_logo'],
|
||||
$data['hero_bg_image'],
|
||||
$data['main_carousel_images'],
|
||||
$data['social_media']
|
||||
);
|
||||
|
||||
$headerAttachmentId = null;
|
||||
if ($headerLogo) {
|
||||
$attachment = Str::isUuid($headerLogo)
|
||||
? \App\Domains\Attachable\Models\Attachment::query()->where('key', $headerLogo)->first()
|
||||
? Attachment::query()->where('key', $headerLogo)->first()
|
||||
: $this->attachmentService->store($headerLogo, 'tenants');
|
||||
|
||||
if ($attachment) {
|
||||
@@ -42,7 +50,7 @@ class TenantService
|
||||
$footerAttachmentId = null;
|
||||
if ($footerLogo) {
|
||||
$attachment = Str::isUuid($footerLogo)
|
||||
? \App\Domains\Attachable\Models\Attachment::query()->where('key', $footerLogo)->first()
|
||||
? Attachment::query()->where('key', $footerLogo)->first()
|
||||
: $this->attachmentService->store($footerLogo, 'tenants');
|
||||
|
||||
if ($attachment) {
|
||||
@@ -55,7 +63,7 @@ class TenantService
|
||||
|
||||
if ($heroBgImage) {
|
||||
$attachment = Str::isUuid($heroBgImage)
|
||||
? \App\Domains\Attachable\Models\Attachment::query()->where('key', $heroBgImage)->first()
|
||||
? Attachment::query()->where('key', $heroBgImage)->first()
|
||||
: $this->attachmentService->store($heroBgImage, 'tenants');
|
||||
|
||||
if ($attachment) {
|
||||
@@ -67,6 +75,8 @@ class TenantService
|
||||
|
||||
/** @var Tenant $tenant */
|
||||
$tenant = Tenant::query()->create($data);
|
||||
$this->syncMainCarouselImages($tenant, $mainCarouselImages);
|
||||
$this->syncSocialMedia($tenant, $socialMedia);
|
||||
|
||||
return $tenant;
|
||||
});
|
||||
@@ -75,9 +85,7 @@ class TenantService
|
||||
/**
|
||||
* Update an existing tenant and store new logos if uploaded.
|
||||
*
|
||||
* @param Tenant $tenant
|
||||
* @param array<string, mixed> $data
|
||||
* @return Tenant
|
||||
*/
|
||||
public function update(Tenant $tenant, array $data): Tenant
|
||||
{
|
||||
@@ -85,11 +93,21 @@ class TenantService
|
||||
$hasHeaderLogoKey = array_key_exists('header_logo', $data);
|
||||
$hasFooterLogoKey = array_key_exists('footer_logo', $data);
|
||||
$hasHeroBgImageKey = array_key_exists('hero_bg_image', $data);
|
||||
$hasMainCarouselImagesKey = array_key_exists('main_carousel_images', $data);
|
||||
$hasSocialMediaKey = array_key_exists('social_media', $data);
|
||||
$headerLogo = $data['header_logo'] ?? null;
|
||||
$footerLogo = $data['footer_logo'] ?? null;
|
||||
$heroBgImage = $data['hero_bg_image'] ?? null;
|
||||
$mainCarouselImages = $data['main_carousel_images'] ?? [];
|
||||
$socialMedia = $data['social_media'] ?? [];
|
||||
|
||||
unset($data['header_logo'], $data['footer_logo'], $data['hero_bg_image']);
|
||||
unset(
|
||||
$data['header_logo'],
|
||||
$data['footer_logo'],
|
||||
$data['hero_bg_image'],
|
||||
$data['main_carousel_images'],
|
||||
$data['social_media']
|
||||
);
|
||||
|
||||
$oldHeroBgId = $tenant->hero_bg_image_id;
|
||||
|
||||
@@ -98,7 +116,7 @@ class TenantService
|
||||
if ($hasHeaderLogoKey) {
|
||||
if ($headerLogo) {
|
||||
$attachment = Str::isUuid($headerLogo)
|
||||
? \App\Domains\Attachable\Models\Attachment::query()->where('key', $headerLogo)->first()
|
||||
? Attachment::query()->where('key', $headerLogo)->first()
|
||||
: $this->attachmentService->store($headerLogo, 'tenants');
|
||||
|
||||
if ($attachment) {
|
||||
@@ -114,7 +132,7 @@ class TenantService
|
||||
if ($hasFooterLogoKey) {
|
||||
if ($footerLogo) {
|
||||
$attachment = Str::isUuid($footerLogo)
|
||||
? \App\Domains\Attachable\Models\Attachment::query()->where('key', $footerLogo)->first()
|
||||
? Attachment::query()->where('key', $footerLogo)->first()
|
||||
: $this->attachmentService->store($footerLogo, 'tenants');
|
||||
|
||||
if ($attachment) {
|
||||
@@ -131,7 +149,7 @@ class TenantService
|
||||
if ($hasHeroBgImageKey) {
|
||||
if ($heroBgImage) {
|
||||
$attachment = Str::isUuid($heroBgImage)
|
||||
? \App\Domains\Attachable\Models\Attachment::query()->where('key', $heroBgImage)->first()
|
||||
? Attachment::query()->where('key', $heroBgImage)->first()
|
||||
: $this->attachmentService->store($heroBgImage, 'tenants');
|
||||
|
||||
if ($attachment) {
|
||||
@@ -151,7 +169,70 @@ class TenantService
|
||||
|
||||
$tenant->save();
|
||||
|
||||
if ($hasMainCarouselImagesKey) {
|
||||
$this->syncMainCarouselImages($tenant, $mainCarouselImages);
|
||||
}
|
||||
|
||||
if ($hasSocialMediaKey) {
|
||||
$this->syncSocialMedia($tenant, $socialMedia);
|
||||
}
|
||||
|
||||
return $tenant;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, mixed> $images
|
||||
*/
|
||||
private function syncMainCarouselImages(Tenant $tenant, array $images): void
|
||||
{
|
||||
$attachments = [];
|
||||
|
||||
foreach (array_values($images) as $order => $image) {
|
||||
$attachment = $this->resolveMainCarouselImage($image, $order);
|
||||
$attachments[$attachment->id] = ['orden' => $order];
|
||||
}
|
||||
|
||||
$tenant->mainCarouselImages()->sync($attachments);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, array{code: string, url: string, orden?: int}> $socialMedia
|
||||
*/
|
||||
private function syncSocialMedia(Tenant $tenant, array $socialMedia): void
|
||||
{
|
||||
$associations = [];
|
||||
|
||||
foreach (array_values($socialMedia) as $index => $item) {
|
||||
$associations[$item['code']] = [
|
||||
'url' => $item['url'],
|
||||
'orden' => $item['orden'] ?? $index,
|
||||
];
|
||||
}
|
||||
|
||||
$tenant->socialMedia()->sync($associations);
|
||||
$tenant->unsetRelation('socialMedia');
|
||||
}
|
||||
|
||||
private function resolveMainCarouselImage(mixed $image, int $order): Attachment
|
||||
{
|
||||
if (is_string($image) && Str::isUuid($image)) {
|
||||
$attachment = Attachment::query()
|
||||
->where('key', $image)
|
||||
->where('type', AttachmentType::Image->value)
|
||||
->first();
|
||||
|
||||
if ($attachment === null) {
|
||||
throw ValidationException::withMessages([
|
||||
"main_carousel_images.{$order}" => [
|
||||
'La imagen indicada no existe o no es un attachment de tipo imagen.',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
return $attachment;
|
||||
}
|
||||
|
||||
return $this->attachmentService->store($image, 'tenants/main-carousel');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
use App\Domains\Catalog\Enums\GroupLayout;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('featured_groups', function (Blueprint $table): void {
|
||||
$table->enum('group_layout', GroupLayout::values())
|
||||
->default(GroupLayout::Paginated->value)
|
||||
->after('product_layout');
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('featured_groups', function (Blueprint $table): void {
|
||||
$table->dropColumn('group_layout');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('tenant_main_carousel_images', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('tenant_id')
|
||||
->constrained('tenants')
|
||||
->cascadeOnDelete();
|
||||
$table->foreignId('attachment_id')
|
||||
->constrained('attachments')
|
||||
->cascadeOnDelete();
|
||||
$table->unsignedInteger('orden')->default(0);
|
||||
$table->timestamps();
|
||||
|
||||
$table->unique(['tenant_id', 'attachment_id']);
|
||||
$table->index(['tenant_id', 'orden']);
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('tenant_main_carousel_images');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('social_media', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->string('code')->unique();
|
||||
$table->string('icon');
|
||||
$table->string('name');
|
||||
$table->timestamps();
|
||||
});
|
||||
|
||||
Schema::create('tenant_social_media', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->string('tenant_code');
|
||||
$table->string('social_media_code');
|
||||
$table->string('url');
|
||||
$table->timestamps();
|
||||
|
||||
$table->foreign('tenant_code')
|
||||
->references('codigo')
|
||||
->on('tenants')
|
||||
->cascadeOnDelete();
|
||||
$table->foreign('social_media_code')
|
||||
->references('code')
|
||||
->on('social_media')
|
||||
->cascadeOnDelete();
|
||||
$table->unique(['tenant_code', 'social_media_code']);
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('tenant_social_media');
|
||||
Schema::dropIfExists('social_media');
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('tenant_social_media', function (Blueprint $table): void {
|
||||
$table->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');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('tenant_menues', function (Blueprint $table) {
|
||||
$table->dropForeign(['tenant_codigo']);
|
||||
$table->dropForeign(['menu_code']);
|
||||
});
|
||||
|
||||
Schema::rename('tenant_menues', 'tenants_menues');
|
||||
|
||||
Schema::table('tenants_menues', function (Blueprint $table) {
|
||||
$table->renameColumn('tenant_codigo', 'tenant_code');
|
||||
$table->json('static_content')->nullable();
|
||||
});
|
||||
|
||||
Schema::table('menues', function (Blueprint $table) {
|
||||
$table->string('parent_menu_code')->nullable()->after('code');
|
||||
$table->enum('content_type', ['static', 'dynamic'])
|
||||
->default('dynamic')
|
||||
->after('parent_menu_code');
|
||||
$table->json('static_content_schema')->nullable()->after('content_type');
|
||||
});
|
||||
|
||||
Schema::table('menues', function (Blueprint $table) {
|
||||
$table->foreign('parent_menu_code')
|
||||
->references('code')
|
||||
->on('menues')
|
||||
->nullOnDelete();
|
||||
});
|
||||
|
||||
Schema::table('tenants_menues', function (Blueprint $table) {
|
||||
$table->foreign('tenant_code')
|
||||
->references('codigo')
|
||||
->on('tenants')
|
||||
->cascadeOnDelete();
|
||||
$table->foreign('menu_code')
|
||||
->references('code')
|
||||
->on('menues')
|
||||
->cascadeOnDelete();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('tenants_menues', function (Blueprint $table) {
|
||||
$table->dropForeign(['tenant_code']);
|
||||
$table->dropForeign(['menu_code']);
|
||||
});
|
||||
|
||||
Schema::table('menues', function (Blueprint $table) {
|
||||
$table->dropForeign(['parent_menu_code']);
|
||||
$table->dropColumn([
|
||||
'parent_menu_code',
|
||||
'content_type',
|
||||
'static_content_schema',
|
||||
]);
|
||||
});
|
||||
|
||||
Schema::table('tenants_menues', function (Blueprint $table) {
|
||||
$table->dropColumn('static_content');
|
||||
$table->renameColumn('tenant_code', 'tenant_codigo');
|
||||
});
|
||||
|
||||
Schema::rename('tenants_menues', 'tenant_menues');
|
||||
|
||||
Schema::table('tenant_menues', function (Blueprint $table) {
|
||||
$table->foreign('tenant_codigo')
|
||||
->references('codigo')
|
||||
->on('tenants')
|
||||
->cascadeOnDelete();
|
||||
$table->foreign('menu_code')
|
||||
->references('code')
|
||||
->on('menues')
|
||||
->cascadeOnDelete();
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -23,6 +23,7 @@ class DatabaseSeeder extends Seeder
|
||||
]);
|
||||
|
||||
$this->call([
|
||||
SocialMediaSeeder::class,
|
||||
TenantSeeder::class,
|
||||
AttributeSeeder::class,
|
||||
CategorySeeder::class,
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Domains\Catalog\Enums\CatalogItemType;
|
||||
use App\Domains\Catalog\Enums\GroupLayout;
|
||||
use App\Domains\Catalog\Enums\InventoryPolicy;
|
||||
use App\Domains\Catalog\Enums\ProductLayout;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
@@ -167,6 +168,7 @@ class FiestaFutbolInfantilProductSeeder extends Seeder
|
||||
$featuredGroup = FeaturedGroup::query()->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'product_layout' => ProductLayout::Row,
|
||||
'group_layout' => GroupLayout::Paginated,
|
||||
'group_name' => $groupName,
|
||||
'group_order' => $groupOrder++,
|
||||
]);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Domains\Catalog\Enums\GroupLayout;
|
||||
use App\Domains\Catalog\Enums\InventoryPolicy;
|
||||
use App\Domains\Catalog\Enums\ProductLayout;
|
||||
use App\Domains\Catalog\Models\Attribute;
|
||||
@@ -167,11 +168,12 @@ class ProductCatalogFromImagesSeeder extends Seeder
|
||||
{
|
||||
FeaturedGroup::query()->where('tenant_code', $tenant->codigo)->delete();
|
||||
|
||||
$group = FeaturedGroup::query()->create([
|
||||
$paginatedGroup = FeaturedGroup::query()->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'product_layout' => ProductLayout::ColumnWithImage,
|
||||
'group_layout' => GroupLayout::Paginated,
|
||||
'group_name' => 'Productos',
|
||||
'group_order' => 0,
|
||||
'group_order' => 2,
|
||||
]);
|
||||
|
||||
$items = CatalogItem::query()
|
||||
@@ -179,7 +181,7 @@ class ProductCatalogFromImagesSeeder extends Seeder
|
||||
->orderBy('id')
|
||||
->get('id');
|
||||
|
||||
$group->featuredItems()->createMany(
|
||||
$paginatedGroup->featuredItems()->createMany(
|
||||
$items->values()->map(
|
||||
fn (CatalogItem $item, int $order): array => [
|
||||
'catalog_item_id' => $item->id,
|
||||
@@ -187,6 +189,29 @@ class ProductCatalogFromImagesSeeder extends Seeder
|
||||
]
|
||||
)->all()
|
||||
);
|
||||
|
||||
$carouselGroup = FeaturedGroup::query()->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'product_layout' => ProductLayout::ColumnWithImage,
|
||||
'group_layout' => GroupLayout::Carousel,
|
||||
'group_name' => 'Productos destacados',
|
||||
'group_order' => 1,
|
||||
]);
|
||||
|
||||
$randomItems = CatalogItem::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->inRandomOrder()
|
||||
->limit(5)
|
||||
->get('id');
|
||||
|
||||
$carouselGroup->featuredItems()->createMany(
|
||||
$randomItems->values()->map(
|
||||
fn (CatalogItem $item, int $order): array => [
|
||||
'catalog_item_id' => $item->id,
|
||||
'order' => $order,
|
||||
]
|
||||
)->all()
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
42
database/seeders/SocialMediaSeeder.php
Normal file
42
database/seeders/SocialMediaSeeder.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Domains\Tenant\Models\SocialMedia;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class SocialMediaSeeder extends Seeder
|
||||
{
|
||||
public function run(): void
|
||||
{
|
||||
$socialMedia = [
|
||||
[
|
||||
'code' => 'facebook',
|
||||
'icon' => 'fa-brands fa-facebook',
|
||||
'name' => 'Facebook',
|
||||
],
|
||||
[
|
||||
'code' => 'instagram',
|
||||
'icon' => 'fa-brands fa-instagram',
|
||||
'name' => 'Instagram',
|
||||
],
|
||||
[
|
||||
'code' => 'whatsapp',
|
||||
'icon' => 'fa-brands fa-whatsapp',
|
||||
'name' => 'WhatsApp',
|
||||
],
|
||||
[
|
||||
'code' => 'linkedin',
|
||||
'icon' => 'fa-brands fa-linkedin-in',
|
||||
'name' => 'LinkedIn',
|
||||
],
|
||||
];
|
||||
|
||||
foreach ($socialMedia as $socialMediaData) {
|
||||
SocialMedia::query()->updateOrCreate(
|
||||
['code' => $socialMediaData['code']],
|
||||
$socialMediaData
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Domains\Attachable\Services\AttachmentService;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use App\Domains\Tenant\Services\TenantService;
|
||||
use Illuminate\Database\Seeder;
|
||||
@@ -9,9 +10,39 @@ use Illuminate\Http\UploadedFile;
|
||||
|
||||
class TenantSeeder extends Seeder
|
||||
{
|
||||
public function __construct(protected TenantService $tenantService)
|
||||
{
|
||||
}
|
||||
private const SONDER_MAIN_CAROUSEL_IMAGES = [
|
||||
'01-urban-team.png',
|
||||
'02-running-shoes.png',
|
||||
'03-streetwear.png',
|
||||
'04-football-training.png',
|
||||
'05-activewear-essentials.png',
|
||||
'06-city-runners.png',
|
||||
];
|
||||
|
||||
private const SOCIAL_MEDIA = [
|
||||
[
|
||||
'code' => 'instagram',
|
||||
'url' => 'https://www.instagram.com/sonderargentina/',
|
||||
'orden' => 0,
|
||||
],
|
||||
[
|
||||
'code' => 'facebook',
|
||||
'url' => 'https://www.facebook.com/SonderArgentina/',
|
||||
'orden' => 1,
|
||||
],
|
||||
[
|
||||
'code' => 'linkedin',
|
||||
'url' => 'https://www.linkedin.com/company/35664983/',
|
||||
'orden' => 2,
|
||||
],
|
||||
[
|
||||
'code' => 'whatsapp',
|
||||
'url' => 'https://wa.me/5493412602222',
|
||||
'orden' => 3,
|
||||
],
|
||||
];
|
||||
|
||||
public function __construct(protected TenantService $tenantService) {}
|
||||
|
||||
/**
|
||||
* Run the database seeds.
|
||||
@@ -21,7 +52,7 @@ class TenantSeeder extends Seeder
|
||||
// Check if tenant 'sonder' already exists and delete it to prevent duplicates
|
||||
$existing = Tenant::query()->where('codigo', 'sonder')->first();
|
||||
if ($existing) {
|
||||
$attachmentService = app(\App\Domains\Attachable\Services\AttachmentService::class);
|
||||
$attachmentService = app(AttachmentService::class);
|
||||
if ($existing->headerLogo) {
|
||||
try {
|
||||
$attachmentService->delete($existing->headerLogo);
|
||||
@@ -36,6 +67,13 @@ class TenantSeeder extends Seeder
|
||||
// Ignore exception on cleanup
|
||||
}
|
||||
}
|
||||
foreach ($existing->mainCarouselImages as $mainCarouselImage) {
|
||||
try {
|
||||
$attachmentService->delete($mainCarouselImage);
|
||||
} catch (\Throwable $e) {
|
||||
// Ignore exception on cleanup
|
||||
}
|
||||
}
|
||||
$existing->delete();
|
||||
}
|
||||
|
||||
@@ -72,7 +110,24 @@ class TenantSeeder extends Seeder
|
||||
true
|
||||
);
|
||||
|
||||
$tenant = $this->tenantService->create([
|
||||
$mainCarouselImages = [];
|
||||
foreach (self::SONDER_MAIN_CAROUSEL_IMAGES as $filename) {
|
||||
$imagePath = public_path("images/sonder-main-carousel/{$filename}");
|
||||
|
||||
if (! file_exists($imagePath)) {
|
||||
throw new \RuntimeException("Image not found at path: {$imagePath}");
|
||||
}
|
||||
|
||||
$mainCarouselImages[] = new UploadedFile(
|
||||
$imagePath,
|
||||
$filename,
|
||||
'image/png',
|
||||
null,
|
||||
true
|
||||
);
|
||||
}
|
||||
|
||||
$this->tenantService->create([
|
||||
'codigo' => 'sonder',
|
||||
'nombre' => 'Sonder',
|
||||
'dominio' => 'localhost',
|
||||
@@ -84,26 +139,31 @@ class TenantSeeder extends Seeder
|
||||
'footer_bg_color' => '#313131',
|
||||
'header_logo' => $headerLogo,
|
||||
'footer_logo' => $footerLogo,
|
||||
'main_carousel_images' => $mainCarouselImages,
|
||||
'social_media' => self::SOCIAL_MEDIA,
|
||||
]);
|
||||
|
||||
// Check if tenant 'fiesta_futbol_infantil' already exists
|
||||
$existingFiesta = Tenant::query()->where('codigo', 'fiesta_futbol_infantil')->first();
|
||||
if ($existingFiesta) {
|
||||
$attachmentService = app(\App\Domains\Attachable\Services\AttachmentService::class);
|
||||
$attachmentService = app(AttachmentService::class);
|
||||
if ($existingFiesta->headerLogo) {
|
||||
try {
|
||||
$attachmentService->delete($existingFiesta->headerLogo);
|
||||
} catch (\Throwable $e) {}
|
||||
} catch (\Throwable $e) {
|
||||
}
|
||||
}
|
||||
if ($existingFiesta->footerLogo && $existingFiesta->footer_logo_id !== $existingFiesta->header_logo_id) {
|
||||
try {
|
||||
$attachmentService->delete($existingFiesta->footerLogo);
|
||||
} catch (\Throwable $e) {}
|
||||
} catch (\Throwable $e) {
|
||||
}
|
||||
}
|
||||
if ($existingFiesta->heroBgImage) {
|
||||
try {
|
||||
$attachmentService->delete($existingFiesta->heroBgImage);
|
||||
} catch (\Throwable $e) {}
|
||||
} catch (\Throwable $e) {
|
||||
}
|
||||
}
|
||||
$existingFiesta->delete();
|
||||
}
|
||||
@@ -178,6 +238,7 @@ class TenantSeeder extends Seeder
|
||||
'location' => 'Sunchales, Santa Fe',
|
||||
'dates' => ['2026-10-09', '2026-10-10', '2026-10-11', '2026-10-12'],
|
||||
],
|
||||
'social_media' => self::SOCIAL_MEDIA,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
BIN
public/images/sonder-main-carousel/01-urban-team.png
Normal file
BIN
public/images/sonder-main-carousel/01-urban-team.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.9 MiB |
BIN
public/images/sonder-main-carousel/02-running-shoes.png
Normal file
BIN
public/images/sonder-main-carousel/02-running-shoes.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.1 MiB |
BIN
public/images/sonder-main-carousel/03-streetwear.png
Normal file
BIN
public/images/sonder-main-carousel/03-streetwear.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.9 MiB |
BIN
public/images/sonder-main-carousel/04-football-training.png
Normal file
BIN
public/images/sonder-main-carousel/04-football-training.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.1 MiB |
BIN
public/images/sonder-main-carousel/05-activewear-essentials.png
Normal file
BIN
public/images/sonder-main-carousel/05-activewear-essentials.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.0 MiB |
BIN
public/images/sonder-main-carousel/06-city-runners.png
Normal file
BIN
public/images/sonder-main-carousel/06-city-runners.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 2.0 MiB |
@@ -4,6 +4,7 @@ namespace Tests\Feature\Catalog;
|
||||
|
||||
use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Catalog\Enums\GroupLayout;
|
||||
use App\Domains\Catalog\Enums\ProductLayout;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Models\FeaturedGroup;
|
||||
@@ -21,7 +22,13 @@ class CatalogControllerTest extends TestCase
|
||||
{
|
||||
$tenant = $this->createTenant('catalog-index');
|
||||
$row = $this->createGroup($tenant, ProductLayout::Row, 'Row', 2);
|
||||
$cart = $this->createGroup($tenant, ProductLayout::ColumnWithCart, 'Cart', 1);
|
||||
$cart = $this->createGroup(
|
||||
$tenant,
|
||||
ProductLayout::ColumnWithCart,
|
||||
'Cart',
|
||||
1,
|
||||
GroupLayout::SimpleVertical,
|
||||
);
|
||||
|
||||
$directInventory = Inventory::query()->create([
|
||||
'real_stock' => 10,
|
||||
@@ -50,14 +57,14 @@ class CatalogControllerTest extends TestCase
|
||||
->assertJsonCount(2)
|
||||
->assertJsonPath('0.title', 'Cart')
|
||||
->assertJsonPath('0.layout', ProductLayout::ColumnWithCart->value)
|
||||
->assertJsonPath('0.items.meta.current_page', 1)
|
||||
->assertJsonPath('0.items.data.0.nombre', 'Variants')
|
||||
->assertJsonPath('0.items.data.0.descripcion', 'Variants description')
|
||||
->assertJsonPath('0.items.data.0.precio', '100.00')
|
||||
->assertJsonPath('0.items.data.0.stock_tecnico', 7)
|
||||
->assertJsonCount(2, '0.items.data.0.variants')
|
||||
->assertJsonPath('0.items.data.0.variants.0.stock_tecnico', 4)
|
||||
->assertJsonPath('0.items.data.0.variants.1.stock_tecnico', 3)
|
||||
->assertJsonPath('0.group_layout', GroupLayout::SimpleVertical->value)
|
||||
->assertJsonPath('0.items.0.nombre', 'Variants')
|
||||
->assertJsonPath('0.items.0.descripcion', 'Variants description')
|
||||
->assertJsonPath('0.items.0.precio', '100.00')
|
||||
->assertJsonPath('0.items.0.stock_tecnico', 7)
|
||||
->assertJsonCount(2, '0.items.0.variants')
|
||||
->assertJsonPath('0.items.0.variants.0.stock_tecnico', 4)
|
||||
->assertJsonPath('0.items.0.variants.1.stock_tecnico', 3)
|
||||
->assertJsonPath('1.title', 'Row')
|
||||
->assertJsonPath('1.items.data.0.stock_tecnico', 8)
|
||||
->assertJsonCount(0, '1.items.data.0.variants');
|
||||
@@ -137,15 +144,71 @@ class CatalogControllerTest extends TestCase
|
||||
->assertJsonPath('data.0.nombre', 'Item 13');
|
||||
}
|
||||
|
||||
public function test_non_paginated_group_layouts_return_all_items_as_a_plain_array(): void
|
||||
{
|
||||
$tenant = $this->createTenant('catalog-simple-layouts');
|
||||
$layouts = [
|
||||
GroupLayout::Simple,
|
||||
GroupLayout::SimpleVertical,
|
||||
GroupLayout::Carousel,
|
||||
];
|
||||
|
||||
foreach ($layouts as $order => $groupLayout) {
|
||||
$group = $this->createGroup(
|
||||
$tenant,
|
||||
ProductLayout::Row,
|
||||
$groupLayout->value,
|
||||
$order,
|
||||
$groupLayout,
|
||||
);
|
||||
|
||||
foreach (range(1, 13) as $number) {
|
||||
$item = $this->createItem($tenant, "{$groupLayout->value} Item {$number}");
|
||||
$group->featuredItems()->create([
|
||||
'catalog_item_id' => $item->id,
|
||||
'order' => $number,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$response = $this->getJson("/api/tenants/{$tenant->codigo}/catalog")->assertOk();
|
||||
$groups = $response->json();
|
||||
|
||||
foreach ($layouts as $index => $groupLayout) {
|
||||
$this->assertSame($groupLayout->value, $groups[$index]['group_layout']);
|
||||
$this->assertCount(13, $groups[$index]['items']);
|
||||
$this->assertSame(
|
||||
"{$groupLayout->value} Item 1",
|
||||
$groups[$index]['items'][0]['nombre'],
|
||||
);
|
||||
$this->assertArrayNotHasKey('data', $groups[$index]['items']);
|
||||
$this->assertArrayNotHasKey('meta', $groups[$index]['items']);
|
||||
}
|
||||
|
||||
$carouselGroup = FeaturedGroup::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->where('group_layout', GroupLayout::Carousel)
|
||||
->sole();
|
||||
|
||||
$this->getJson(
|
||||
"/api/tenants/{$tenant->codigo}/catalog/featured-groups/{$carouselGroup->id}/items?page=2"
|
||||
)
|
||||
->assertOk()
|
||||
->assertJsonCount(13)
|
||||
->assertJsonPath('0.nombre', 'carousel Item 1');
|
||||
}
|
||||
|
||||
private function createGroup(
|
||||
Tenant $tenant,
|
||||
ProductLayout $layout,
|
||||
string $name,
|
||||
int $order = 0,
|
||||
GroupLayout $groupLayout = GroupLayout::Paginated,
|
||||
): FeaturedGroup {
|
||||
return FeaturedGroup::query()->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'product_layout' => $layout,
|
||||
'group_layout' => $groupLayout,
|
||||
'group_name' => $name,
|
||||
'group_order' => $order,
|
||||
]);
|
||||
|
||||
@@ -78,6 +78,7 @@ class CatalogSchemaTest extends TestCase
|
||||
'id',
|
||||
'tenant_code',
|
||||
'product_layout',
|
||||
'group_layout',
|
||||
'group_name',
|
||||
'group_order',
|
||||
], Schema::getColumnListing('featured_groups'));
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace Tests\Feature\Seeders;
|
||||
use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Catalog\Enums\CatalogItemType;
|
||||
use App\Domains\Catalog\Enums\GroupLayout;
|
||||
use App\Domains\Catalog\Models\Attribute;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Models\FeaturedGroup;
|
||||
@@ -133,6 +134,10 @@ class FiestaFutbolInfantilProductSeederTest extends TestCase
|
||||
->with('featuredItems.catalogItem')
|
||||
->orderBy('group_order')
|
||||
->get()
|
||||
->each(fn (FeaturedGroup $group) => $this->assertSame(
|
||||
GroupLayout::Paginated,
|
||||
$group->group_layout,
|
||||
))
|
||||
->mapWithKeys(fn (FeaturedGroup $group): array => [
|
||||
$group->group_name => $group->featuredItems->pluck('catalogItem.slug')->all(),
|
||||
])
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Tests\Feature\Seeders;
|
||||
|
||||
use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Catalog\Enums\GroupLayout;
|
||||
use App\Domains\Catalog\Enums\ProductLayout;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Models\FeaturedGroup;
|
||||
@@ -20,7 +21,7 @@ class ProductCatalogFromImagesSeederTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_it_features_every_sonder_product_in_a_single_image_column(): void
|
||||
public function test_it_creates_paginated_and_random_carousel_groups_for_sonder(): void
|
||||
{
|
||||
Storage::fake('s3');
|
||||
|
||||
@@ -51,18 +52,44 @@ class ProductCatalogFromImagesSeederTest extends TestCase
|
||||
ProductCatalogFromImagesSeeder::class,
|
||||
]);
|
||||
|
||||
$group = FeaturedGroup::query()
|
||||
$groups = FeaturedGroup::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->with('featuredItems.catalogItem')
|
||||
->sole();
|
||||
->orderBy('group_order')
|
||||
->get()
|
||||
->keyBy('group_name');
|
||||
|
||||
$this->assertSame('Productos', $group->group_name);
|
||||
$this->assertSame(ProductLayout::ColumnWithImage, $group->product_layout);
|
||||
$this->assertSame(0, $group->group_order);
|
||||
$paginatedGroup = $groups->get('Productos');
|
||||
$carouselGroup = $groups->get('Productos destacados');
|
||||
$catalogItemIds = CatalogItem::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->orderBy('id')
|
||||
->pluck('id');
|
||||
|
||||
$this->assertNotNull($paginatedGroup);
|
||||
$this->assertSame(ProductLayout::ColumnWithImage, $paginatedGroup->product_layout);
|
||||
$this->assertSame(GroupLayout::Paginated, $paginatedGroup->group_layout);
|
||||
$this->assertSame(0, $paginatedGroup->group_order);
|
||||
$this->assertSame(
|
||||
CatalogItem::query()->where('tenant_code', $tenant->codigo)->orderBy('id')->pluck('slug')->all(),
|
||||
$group->featuredItems->pluck('catalogItem.slug')->all(),
|
||||
$paginatedGroup->featuredItems->pluck('catalogItem.slug')->all(),
|
||||
);
|
||||
$this->assertCount(10, $paginatedGroup->featuredItems);
|
||||
|
||||
$this->assertNotNull($carouselGroup);
|
||||
$this->assertSame(ProductLayout::ColumnWithImage, $carouselGroup->product_layout);
|
||||
$this->assertSame(GroupLayout::Carousel, $carouselGroup->group_layout);
|
||||
$this->assertSame(1, $carouselGroup->group_order);
|
||||
$this->assertCount(5, $carouselGroup->featuredItems);
|
||||
$this->assertCount(
|
||||
5,
|
||||
$carouselGroup->featuredItems->pluck('catalog_item_id')->unique(),
|
||||
);
|
||||
$this->assertCount(
|
||||
0,
|
||||
$carouselGroup->featuredItems
|
||||
->pluck('catalog_item_id')
|
||||
->diff($catalogItemIds),
|
||||
);
|
||||
$this->assertCount(10, $group->featuredItems);
|
||||
}
|
||||
}
|
||||
|
||||
52
tests/Feature/Seeders/SocialMediaSeederTest.php
Normal file
52
tests/Feature/Seeders/SocialMediaSeederTest.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Seeders;
|
||||
|
||||
use App\Domains\Tenant\Models\SocialMedia;
|
||||
use Database\Seeders\SocialMediaSeeder;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Tests\TestCase;
|
||||
|
||||
class SocialMediaSeederTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_it_seeds_the_supported_social_media_with_font_awesome_icons(): void
|
||||
{
|
||||
$this->seed(SocialMediaSeeder::class);
|
||||
|
||||
$this->assertSame([
|
||||
[
|
||||
'code' => 'facebook',
|
||||
'icon' => 'fa-brands fa-facebook',
|
||||
'name' => 'Facebook',
|
||||
],
|
||||
[
|
||||
'code' => 'instagram',
|
||||
'icon' => 'fa-brands fa-instagram',
|
||||
'name' => 'Instagram',
|
||||
],
|
||||
[
|
||||
'code' => 'linkedin',
|
||||
'icon' => 'fa-brands fa-linkedin-in',
|
||||
'name' => 'LinkedIn',
|
||||
],
|
||||
[
|
||||
'code' => 'whatsapp',
|
||||
'icon' => 'fa-brands fa-whatsapp',
|
||||
'name' => 'WhatsApp',
|
||||
],
|
||||
], SocialMedia::query()
|
||||
->orderBy('code')
|
||||
->get(['code', 'icon', 'name'])
|
||||
->toArray());
|
||||
}
|
||||
|
||||
public function test_it_can_run_more_than_once_without_creating_duplicates(): void
|
||||
{
|
||||
$this->seed(SocialMediaSeeder::class);
|
||||
$this->seed(SocialMediaSeeder::class);
|
||||
|
||||
$this->assertSame(4, SocialMedia::query()->count());
|
||||
}
|
||||
}
|
||||
72
tests/Feature/Seeders/TenantSeederTest.php
Normal file
72
tests/Feature/Seeders/TenantSeederTest.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Seeders;
|
||||
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Database\Seeders\SocialMediaSeeder;
|
||||
use Database\Seeders\TenantSeeder;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TenantSeederTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_it_loads_the_sonder_main_carousel_images_in_order(): void
|
||||
{
|
||||
Storage::fake('s3');
|
||||
|
||||
$this->seed([
|
||||
SocialMediaSeeder::class,
|
||||
TenantSeeder::class,
|
||||
]);
|
||||
|
||||
$tenant = Tenant::query()->where('codigo', 'sonder')->firstOrFail();
|
||||
$images = $tenant->mainCarouselImages()->get();
|
||||
|
||||
$this->assertSame([
|
||||
'01-urban-team.png',
|
||||
'02-running-shoes.png',
|
||||
'03-streetwear.png',
|
||||
'04-football-training.png',
|
||||
'05-activewear-essentials.png',
|
||||
'06-city-runners.png',
|
||||
], $images->pluck('filename')->all());
|
||||
$this->assertSame([0, 1, 2, 3, 4, 5], $images->pluck('pivot.orden')->all());
|
||||
|
||||
foreach ($images as $image) {
|
||||
Storage::disk('s3')->assertExists($image->path);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_it_assigns_social_media_to_both_tenants_in_order(): void
|
||||
{
|
||||
Storage::fake('s3');
|
||||
|
||||
$this->seed([
|
||||
SocialMediaSeeder::class,
|
||||
TenantSeeder::class,
|
||||
]);
|
||||
|
||||
$expectedCodes = ['instagram', 'facebook', 'linkedin', 'whatsapp'];
|
||||
$expectedUrls = [
|
||||
'https://www.instagram.com/sonderargentina/',
|
||||
'https://www.facebook.com/SonderArgentina/',
|
||||
'https://www.linkedin.com/company/35664983/',
|
||||
'https://wa.me/5493412602222',
|
||||
];
|
||||
|
||||
foreach (['sonder', 'fiesta_futbol_infantil'] as $tenantCode) {
|
||||
$socialMedia = Tenant::query()
|
||||
->where('codigo', $tenantCode)
|
||||
->firstOrFail()
|
||||
->socialMedia()
|
||||
->get();
|
||||
|
||||
$this->assertSame($expectedCodes, $socialMedia->pluck('code')->all());
|
||||
$this->assertSame($expectedUrls, $socialMedia->pluck('pivot.url')->all());
|
||||
$this->assertSame([0, 1, 2, 3], $socialMedia->pluck('pivot.orden')->all());
|
||||
}
|
||||
}
|
||||
}
|
||||
191
tests/Feature/Tenant/TenantMainCarouselImagesTest.php
Normal file
191
tests/Feature/Tenant/TenantMainCarouselImagesTest.php
Normal file
@@ -0,0 +1,191 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Tenant;
|
||||
|
||||
use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TenantMainCarouselImagesTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_a_tenant_has_ordered_main_carousel_images(): void
|
||||
{
|
||||
$tenant = $this->createTenant();
|
||||
$firstAttachment = $this->createAttachment('first.png');
|
||||
$secondAttachment = $this->createAttachment('second.png');
|
||||
|
||||
$tenant->mainCarouselImages()->attach([
|
||||
$secondAttachment->id => ['orden' => 2],
|
||||
$firstAttachment->id => ['orden' => 1],
|
||||
]);
|
||||
|
||||
$attachments = $tenant->mainCarouselImages()->get();
|
||||
|
||||
$this->assertCount(2, $attachments);
|
||||
$this->assertTrue($attachments[0]->is($firstAttachment));
|
||||
$this->assertTrue($attachments[1]->is($secondAttachment));
|
||||
$this->assertSame([1, 2], $attachments->pluck('pivot.orden')->all());
|
||||
}
|
||||
|
||||
public function test_bootstrap_returns_all_main_carousel_image_urls_in_order(): void
|
||||
{
|
||||
$tenant = $this->createTenant();
|
||||
$firstAttachment = $this->createAttachment('first.png');
|
||||
$secondAttachment = $this->createAttachment('second.png');
|
||||
|
||||
$tenant->mainCarouselImages()->attach([
|
||||
$secondAttachment->id => ['orden' => 20],
|
||||
$firstAttachment->id => ['orden' => 10],
|
||||
]);
|
||||
|
||||
$response = $this->getJson('/api/tenants/bootstrap/acme.com');
|
||||
|
||||
$response
|
||||
->assertOk()
|
||||
->assertJsonCount(2, 'data.main_carousel_images');
|
||||
|
||||
$urls = $response->json('data.main_carousel_images');
|
||||
|
||||
$this->assertStringContainsString($firstAttachment->key, $urls[0]);
|
||||
$this->assertStringContainsString($secondAttachment->key, $urls[1]);
|
||||
}
|
||||
|
||||
public function test_show_returns_an_empty_main_carousel_images_array_when_the_tenant_has_no_images(): void
|
||||
{
|
||||
$tenant = $this->createTenant();
|
||||
|
||||
$this->getJson("/api/tenants/{$tenant->codigo}")
|
||||
->assertOk()
|
||||
->assertJsonPath('data.main_carousel_images', []);
|
||||
}
|
||||
|
||||
public function test_store_uploads_and_attaches_main_carousel_images_in_request_order(): void
|
||||
{
|
||||
Storage::fake('s3');
|
||||
|
||||
$headerLogo = $this->createAttachment('header.png');
|
||||
$footerLogo = $this->createAttachment('footer.png');
|
||||
|
||||
$response = $this
|
||||
->withHeader('Accept', 'application/json')
|
||||
->post('/api/tenants', [
|
||||
...$this->tenantPayload(),
|
||||
'header_logo' => $headerLogo->key,
|
||||
'footer_logo' => $footerLogo->key,
|
||||
'main_carousel_images' => [
|
||||
UploadedFile::fake()->image('first-carousel.png', 1200, 300),
|
||||
UploadedFile::fake()->image('second-carousel.png', 1200, 300),
|
||||
],
|
||||
]);
|
||||
|
||||
$response
|
||||
->assertCreated()
|
||||
->assertJsonCount(2, 'data.main_carousel_images');
|
||||
|
||||
$tenant = Tenant::query()->where('codigo', 'new-acme')->firstOrFail();
|
||||
$images = $tenant->mainCarouselImages()->get();
|
||||
|
||||
$this->assertSame(
|
||||
['first-carousel.png', 'second-carousel.png'],
|
||||
$images->pluck('filename')->all()
|
||||
);
|
||||
$this->assertSame([0, 1], $images->pluck('pivot.orden')->all());
|
||||
Storage::disk('s3')->assertExists($images[0]->path);
|
||||
Storage::disk('s3')->assertExists($images[1]->path);
|
||||
}
|
||||
|
||||
public function test_update_replaces_and_can_clear_main_carousel_images(): void
|
||||
{
|
||||
$tenant = $this->createTenant();
|
||||
$oldImage = $this->createAttachment('old.png');
|
||||
$firstImage = $this->createAttachment('first.png');
|
||||
$secondImage = $this->createAttachment('second.png');
|
||||
|
||||
$tenant->mainCarouselImages()->attach($oldImage->id, ['orden' => 0]);
|
||||
|
||||
$this->putJson("/api/tenants/{$tenant->codigo}", [
|
||||
'main_carousel_images' => [$secondImage->key, $firstImage->key],
|
||||
])
|
||||
->assertOk()
|
||||
->assertJsonCount(2, 'data.main_carousel_images');
|
||||
|
||||
$this->assertSame(
|
||||
[$secondImage->id, $firstImage->id],
|
||||
$tenant->mainCarouselImages()->pluck('attachments.id')->all()
|
||||
);
|
||||
$this->assertDatabaseMissing('tenant_main_carousel_images', [
|
||||
'tenant_id' => $tenant->id,
|
||||
'attachment_id' => $oldImage->id,
|
||||
]);
|
||||
|
||||
$this->putJson("/api/tenants/{$tenant->codigo}", [
|
||||
'main_carousel_images' => [],
|
||||
])
|
||||
->assertOk()
|
||||
->assertJsonPath('data.main_carousel_images', []);
|
||||
|
||||
$this->assertDatabaseMissing('tenant_main_carousel_images', [
|
||||
'tenant_id' => $tenant->id,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
private function tenantPayload(): array
|
||||
{
|
||||
return [
|
||||
'codigo' => 'new-acme',
|
||||
'nombre' => 'New Acme',
|
||||
'dominio' => 'new-acme.com',
|
||||
'primary_color' => '#111111',
|
||||
'secondary_color' => '#222222',
|
||||
'danger_color' => '#333333',
|
||||
'success_color' => '#444444',
|
||||
'header_bg_color' => '#ffffff',
|
||||
'footer_bg_color' => '#ffffff',
|
||||
];
|
||||
}
|
||||
|
||||
private function createTenant(): Tenant
|
||||
{
|
||||
$headerLogo = $this->createAttachment('header.png');
|
||||
$footerLogo = $this->createAttachment('footer.png');
|
||||
|
||||
return Tenant::query()->create([
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme',
|
||||
'dominio' => 'acme.com',
|
||||
'primary_color' => '#111111',
|
||||
'secondary_color' => '#222222',
|
||||
'danger_color' => '#333333',
|
||||
'success_color' => '#444444',
|
||||
'header_bg_color' => '#ffffff',
|
||||
'footer_bg_color' => '#ffffff',
|
||||
'header_logo_id' => $headerLogo->id,
|
||||
'footer_logo_id' => $footerLogo->id,
|
||||
]);
|
||||
}
|
||||
|
||||
private function createAttachment(string $filename): Attachment
|
||||
{
|
||||
$key = (string) Str::uuid();
|
||||
|
||||
return Attachment::query()->create([
|
||||
'key' => $key,
|
||||
'path' => "tenants/main-carousel/{$key}.png",
|
||||
'filename' => $filename,
|
||||
'type' => AttachmentType::Image,
|
||||
'mime_type' => 'image/png',
|
||||
'extension' => 'png',
|
||||
'size' => 100,
|
||||
]);
|
||||
}
|
||||
}
|
||||
259
tests/Feature/Tenant/TenantSocialMediaTest.php
Normal file
259
tests/Feature/Tenant/TenantSocialMediaTest.php
Normal file
@@ -0,0 +1,259 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Feature\Tenant;
|
||||
|
||||
use App\Domains\Attachable\Enums\AttachmentType;
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Tenant\Models\SocialMedia;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Tests\TestCase;
|
||||
|
||||
class TenantSocialMediaTest extends TestCase
|
||||
{
|
||||
use RefreshDatabase;
|
||||
|
||||
public function test_social_media_tables_have_the_expected_columns(): void
|
||||
{
|
||||
$this->assertEqualsCanonicalizing([
|
||||
'id',
|
||||
'code',
|
||||
'icon',
|
||||
'name',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
], Schema::getColumnListing('social_media'));
|
||||
|
||||
$this->assertEqualsCanonicalizing([
|
||||
'id',
|
||||
'tenant_code',
|
||||
'social_media_code',
|
||||
'url',
|
||||
'orden',
|
||||
'created_at',
|
||||
'updated_at',
|
||||
], Schema::getColumnListing('tenant_social_media'));
|
||||
}
|
||||
|
||||
public function test_a_tenant_can_have_social_media_with_its_own_url(): void
|
||||
{
|
||||
$tenant = $this->createTenant();
|
||||
$instagram = SocialMedia::query()->create([
|
||||
'code' => 'instagram',
|
||||
'icon' => 'instagram',
|
||||
'name' => 'Instagram',
|
||||
]);
|
||||
|
||||
$tenant->socialMedia()->attach($instagram->code, [
|
||||
'url' => 'https://instagram.com/acme',
|
||||
'orden' => 3,
|
||||
]);
|
||||
|
||||
$this->assertTrue($tenant->socialMedia()->firstOrFail()->is($instagram));
|
||||
$this->assertSame(
|
||||
'https://instagram.com/acme',
|
||||
$tenant->socialMedia()->firstOrFail()->pivot->url
|
||||
);
|
||||
$this->assertSame(3, $tenant->socialMedia()->firstOrFail()->pivot->orden);
|
||||
$this->assertTrue($instagram->tenants()->firstOrFail()->is($tenant));
|
||||
}
|
||||
|
||||
public function test_deleting_a_social_media_deletes_its_tenant_associations(): void
|
||||
{
|
||||
$tenant = $this->createTenant();
|
||||
$instagram = SocialMedia::query()->create([
|
||||
'code' => 'instagram',
|
||||
'icon' => 'instagram',
|
||||
'name' => 'Instagram',
|
||||
]);
|
||||
$tenant->socialMedia()->attach($instagram->code, [
|
||||
'url' => 'https://instagram.com/acme',
|
||||
]);
|
||||
|
||||
$instagram->delete();
|
||||
|
||||
$this->assertDatabaseMissing('tenant_social_media', [
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'social_media_code' => $instagram->code,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_tenant_update_synchronizes_social_media_and_returns_pivot_urls(): void
|
||||
{
|
||||
$tenant = $this->createTenant();
|
||||
$instagram = $this->createSocialMedia('instagram', 'Instagram');
|
||||
$facebook = $this->createSocialMedia('facebook', 'Facebook');
|
||||
|
||||
$tenant->socialMedia()->attach($facebook->code, [
|
||||
'url' => 'https://facebook.com/old-acme',
|
||||
]);
|
||||
|
||||
$this->putJson("/api/tenants/{$tenant->codigo}", [
|
||||
'social_media' => [
|
||||
[
|
||||
'code' => $instagram->code,
|
||||
'url' => 'https://instagram.com/acme',
|
||||
'orden' => 4,
|
||||
],
|
||||
],
|
||||
])
|
||||
->assertOk()
|
||||
->assertJsonPath('data.social_media.0.code', 'instagram')
|
||||
->assertJsonPath('data.social_media.0.icon', 'instagram')
|
||||
->assertJsonPath('data.social_media.0.name', 'Instagram')
|
||||
->assertJsonPath('data.social_media.0.url', 'https://instagram.com/acme')
|
||||
->assertJsonCount(1, 'data.social_media');
|
||||
|
||||
$this->assertDatabaseHas('tenant_social_media', [
|
||||
'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,
|
||||
'social_media_code' => $facebook->code,
|
||||
]);
|
||||
}
|
||||
|
||||
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');
|
||||
$headerLogo = $this->createAttachment('new-header.png');
|
||||
$footerLogo = $this->createAttachment('new-footer.png');
|
||||
|
||||
$this->postJson('/api/tenants', [
|
||||
'codigo' => 'new-acme',
|
||||
'nombre' => 'New Acme',
|
||||
'dominio' => 'new-acme.com',
|
||||
'primary_color' => '#111111',
|
||||
'secondary_color' => '#222222',
|
||||
'danger_color' => '#333333',
|
||||
'success_color' => '#444444',
|
||||
'header_bg_color' => '#ffffff',
|
||||
'footer_bg_color' => '#ffffff',
|
||||
'header_logo' => $headerLogo->key,
|
||||
'footer_logo' => $footerLogo->key,
|
||||
'social_media' => [
|
||||
[
|
||||
'code' => $instagram->code,
|
||||
'url' => 'https://instagram.com/new-acme',
|
||||
],
|
||||
],
|
||||
])
|
||||
->assertCreated()
|
||||
->assertJsonPath('data.social_media.0.code', 'instagram')
|
||||
->assertJsonPath('data.social_media.0.url', 'https://instagram.com/new-acme');
|
||||
|
||||
$this->assertDatabaseHas('tenant_social_media', [
|
||||
'tenant_code' => 'new-acme',
|
||||
'social_media_code' => $instagram->code,
|
||||
'url' => 'https://instagram.com/new-acme',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_tenant_update_can_clear_social_media(): void
|
||||
{
|
||||
$tenant = $this->createTenant();
|
||||
$instagram = $this->createSocialMedia('instagram', 'Instagram');
|
||||
$tenant->socialMedia()->attach($instagram->code, [
|
||||
'url' => 'https://instagram.com/acme',
|
||||
]);
|
||||
|
||||
$this->putJson("/api/tenants/{$tenant->codigo}", [
|
||||
'social_media' => [],
|
||||
])
|
||||
->assertOk()
|
||||
->assertJsonPath('data.social_media', []);
|
||||
|
||||
$this->assertDatabaseMissing('tenant_social_media', [
|
||||
'tenant_code' => $tenant->codigo,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_tenant_update_validates_social_media_codes_and_urls(): void
|
||||
{
|
||||
$tenant = $this->createTenant();
|
||||
|
||||
$this->putJson("/api/tenants/{$tenant->codigo}", [
|
||||
'social_media' => [
|
||||
[
|
||||
'code' => 'unknown',
|
||||
'url' => 'not-a-url',
|
||||
],
|
||||
],
|
||||
])
|
||||
->assertUnprocessable()
|
||||
->assertJsonValidationErrors([
|
||||
'social_media.0.code',
|
||||
'social_media.0.url',
|
||||
]);
|
||||
}
|
||||
|
||||
private function createTenant(): Tenant
|
||||
{
|
||||
$headerLogo = $this->createAttachment('header.png');
|
||||
$footerLogo = $this->createAttachment('footer.png');
|
||||
|
||||
return Tenant::query()->create([
|
||||
'codigo' => 'acme',
|
||||
'nombre' => 'Acme',
|
||||
'dominio' => 'acme.com',
|
||||
'primary_color' => '#111111',
|
||||
'secondary_color' => '#222222',
|
||||
'danger_color' => '#333333',
|
||||
'success_color' => '#444444',
|
||||
'header_bg_color' => '#ffffff',
|
||||
'footer_bg_color' => '#ffffff',
|
||||
'header_logo_id' => $headerLogo->id,
|
||||
'footer_logo_id' => $footerLogo->id,
|
||||
]);
|
||||
}
|
||||
|
||||
private function createAttachment(string $filename): Attachment
|
||||
{
|
||||
return Attachment::query()->create([
|
||||
'path' => "test/{$filename}",
|
||||
'filename' => $filename,
|
||||
'type' => AttachmentType::Image,
|
||||
'mime_type' => 'image/png',
|
||||
]);
|
||||
}
|
||||
|
||||
private function createSocialMedia(string $code, string $name): SocialMedia
|
||||
{
|
||||
return SocialMedia::query()->create([
|
||||
'code' => $code,
|
||||
'icon' => $code,
|
||||
'name' => $name,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ namespace Tests\Unit\Catalog;
|
||||
|
||||
use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Catalog\Enums\CatalogItemType;
|
||||
use App\Domains\Catalog\Enums\GroupLayout;
|
||||
use App\Domains\Catalog\Enums\InventoryPolicy;
|
||||
use App\Domains\Catalog\Enums\ProductLayout;
|
||||
use App\Domains\Catalog\Models\Attribute;
|
||||
@@ -25,6 +26,16 @@ use Tests\TestCase;
|
||||
|
||||
class CatalogModelsTest extends TestCase
|
||||
{
|
||||
public function test_group_layout_has_all_supported_values(): void
|
||||
{
|
||||
$this->assertSame([
|
||||
'paginated',
|
||||
'simple',
|
||||
'simple_vertical',
|
||||
'carousel',
|
||||
], GroupLayout::values());
|
||||
}
|
||||
|
||||
public function test_attribute_maps_its_values_and_relations(): void
|
||||
{
|
||||
$attribute = new Attribute;
|
||||
@@ -83,6 +94,7 @@ class CatalogModelsTest extends TestCase
|
||||
$group = new FeaturedGroup;
|
||||
$group->setRawAttributes([
|
||||
'product_layout' => ProductLayout::ColumnWithImage->value,
|
||||
'group_layout' => GroupLayout::SimpleVertical->value,
|
||||
'group_order' => '2',
|
||||
]);
|
||||
$featuredItem = new FeaturedItem;
|
||||
@@ -95,6 +107,7 @@ class CatalogModelsTest extends TestCase
|
||||
$this->assertSame('featured_groups', $group->getTable());
|
||||
$this->assertFalse($group->usesTimestamps());
|
||||
$this->assertSame(ProductLayout::ColumnWithImage, $group->product_layout);
|
||||
$this->assertSame(GroupLayout::SimpleVertical, $group->group_layout);
|
||||
$this->assertSame(2, $group->group_order);
|
||||
$this->assertInstanceOf(Tenant::class, $group->tenant()->getRelated());
|
||||
$this->assertInstanceOf(FeaturedItem::class, $group->featuredItems()->getRelated());
|
||||
|
||||
Reference in New Issue
Block a user