Refactor Catalog API: Remove Create Catalog Item Endpoint and Update Related Logic
- Removed the "Create Catalog" endpoint from the Postman collection. - Updated the CatalogController to handle tenant-specific catalog items based on active events. - Refactored CatalogItem model to automatically set event_id based on tenant's active_event_id during creation. - Deleted StoreCatalogItemRequest and CatalogItemResource as they are no longer needed. - Updated various services and controllers to utilize the new tenant-based catalog item retrieval logic. - Added tests to ensure that catalog reads are limited to items associated with the active event.
This commit is contained in:
@@ -52,6 +52,19 @@ class CatalogItem extends Model
|
||||
'group_order' => 0,
|
||||
];
|
||||
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::creating(function (CatalogItem $item): void {
|
||||
if ($item->event_id !== null || $item->tenant_code === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$item->event_id = Tenant::query()
|
||||
->where('codigo', $item->tenant_code)
|
||||
->value('active_event_id');
|
||||
});
|
||||
}
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
@@ -81,6 +94,15 @@ class CatalogItem extends Model
|
||||
return $this->belongsTo(Event::class);
|
||||
}
|
||||
|
||||
/** @param Builder<CatalogItem> $query */
|
||||
public function scopeForTenantCatalog(Builder $query, Tenant $tenant): Builder
|
||||
{
|
||||
return $query
|
||||
->where('catalog_items.tenant_code', $tenant->codigo)
|
||||
->when($tenant->active_event_id !== null, fn (Builder $query): Builder => $query
|
||||
->where('catalog_items.event_id', $tenant->active_event_id));
|
||||
}
|
||||
|
||||
/** @return BelongsTo<Category, $this> */
|
||||
public function category(): BelongsTo
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user