feat(event): restore event model and migrate tenant event data
This commit is contained in:
48
app/Domains/Ticketing/Event/Models/Event.php
Normal file
48
app/Domains/Ticketing/Event/Models/Event.php
Normal file
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Ticketing\Event\Models;
|
||||
|
||||
use App\Domains\Core\Tenant\Models\SocialMedia;
|
||||
use App\Domains\Core\Tenant\Models\Tenant;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
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;
|
||||
|
||||
#[Fillable(['tenant_code', 'title', 'subtitle', 'description', 'location', 'date_text', 'published_at'])]
|
||||
class Event extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected function casts(): array
|
||||
{
|
||||
return ['published_at' => 'datetime'];
|
||||
}
|
||||
|
||||
/** @return BelongsTo<Tenant, $this> */
|
||||
public function tenant(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Tenant::class, 'tenant_code', 'codigo');
|
||||
}
|
||||
|
||||
/** @return HasMany<EventDate, $this> */
|
||||
public function dates(): HasMany
|
||||
{
|
||||
return $this->hasMany(EventDate::class)->orderBy('date')->orderBy('time_start');
|
||||
}
|
||||
|
||||
/** @return BelongsToMany<SocialMedia, $this> */
|
||||
public function socialMedia(): BelongsToMany
|
||||
{
|
||||
return $this->belongsToMany(
|
||||
SocialMedia::class,
|
||||
'event_social_media',
|
||||
'event_id',
|
||||
'social_media_code',
|
||||
'id',
|
||||
'code',
|
||||
)->withPivot(['url', 'orden'])->withTimestamps()->orderByPivot('orden');
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ use Illuminate\Support\Carbon;
|
||||
|
||||
#[Fillable([
|
||||
'tenant_code',
|
||||
'event_id',
|
||||
'date',
|
||||
'time_start',
|
||||
'time_end',
|
||||
@@ -37,16 +38,16 @@ class EventDate extends Model
|
||||
protected static function booted(): void
|
||||
{
|
||||
static::creating(fn (self $eventDate) => $eventDate->syncValidityTime());
|
||||
static::created(fn (self $eventDate) => $eventDate->syncTenantDateText());
|
||||
static::created(fn (self $eventDate) => $eventDate->syncEventDateText());
|
||||
static::updated(function (self $eventDate): void {
|
||||
if ($eventDate->wasChanged(['date', 'time_start', 'time_end'])) {
|
||||
$eventDate->syncValidityTime();
|
||||
}
|
||||
|
||||
$eventDate->syncTenantDateText();
|
||||
$eventDate->syncEventDateText();
|
||||
});
|
||||
static::deleted(function (self $eventDate): void {
|
||||
$eventDate->syncTenantDateText();
|
||||
$eventDate->syncEventDateText();
|
||||
ValidityTime::query()
|
||||
->whereKey($eventDate->validity_time_id)
|
||||
->delete();
|
||||
@@ -58,6 +59,7 @@ class EventDate extends Model
|
||||
return [
|
||||
'date' => 'date:Y-m-d',
|
||||
'validity_time_id' => 'integer',
|
||||
'event_id' => 'integer',
|
||||
'rescheduled_to_event_date_id' => 'integer',
|
||||
'suspended_at' => 'datetime',
|
||||
];
|
||||
@@ -69,6 +71,12 @@ class EventDate extends Model
|
||||
return $this->belongsTo(Tenant::class, 'tenant_code', 'codigo');
|
||||
}
|
||||
|
||||
/** @return BelongsTo<Event, $this> */
|
||||
public function event(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(Event::class);
|
||||
}
|
||||
|
||||
/** @return BelongsTo<ValidityTime, $this> */
|
||||
public function validityTime(): BelongsTo
|
||||
{
|
||||
@@ -156,17 +164,17 @@ class EventDate extends Model
|
||||
return EventDateStatus::Completed;
|
||||
}
|
||||
|
||||
private function syncTenantDateText(): void
|
||||
private function syncEventDateText(): void
|
||||
{
|
||||
$tenant = $this->tenant()->first();
|
||||
$event = $this->event()->first();
|
||||
|
||||
if (! $tenant) {
|
||||
if (! $event) {
|
||||
return;
|
||||
}
|
||||
|
||||
$tenant->update([
|
||||
'event_date_text' => app(EventDateTextFormatter::class)->format(
|
||||
$tenant->eventDates()
|
||||
$event->update([
|
||||
'date_text' => app(EventDateTextFormatter::class)->format(
|
||||
$event->dates()
|
||||
->whereNull('rescheduled_to_event_date_id')
|
||||
->whereNull('suspended_at')
|
||||
->pluck('date')
|
||||
|
||||
Reference in New Issue
Block a user