diff --git a/app/Models/Presentation.php b/app/Models/Presentation.php index 65150d6..63690b1 100644 --- a/app/Models/Presentation.php +++ b/app/Models/Presentation.php @@ -2,6 +2,7 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -29,6 +30,22 @@ class Presentation extends Model implements HasMedia 'deleted_at', ]; + /** + * Scope a query to only include presentations for the authenticated user. + * + * @param Builder $query + */ + public function scopeForUser(Builder $query): void + { + if (auth()->user()->isAdministrator()) { + return; + } + + $presentationIds = auth()->user()->presentations()->pluck('id'); + + $query->whereIn('id', $presentationIds); + } + /** * Get the options for generating the slug. */ diff --git a/database/factories/PresentationFactory.php b/database/factories/PresentationFactory.php index cd33318..bef0552 100644 --- a/database/factories/PresentationFactory.php +++ b/database/factories/PresentationFactory.php @@ -2,14 +2,68 @@ namespace Database\Factories; +use App\Models\AggregateView; +use App\Models\DailyView; +use App\Models\Presentation; use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; +use Illuminate\Database\Eloquent\Factories\Sequence; +use Illuminate\Support\Facades\App; /** * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Presentation> */ class PresentationFactory extends Factory { + /** + * Configure the model factory. + */ + public function configure(): static + { + if (App::environment('testing')) { + return $this; + } + + return $this->afterCreating(function (Presentation $presentation) { + // Create some fake Daily Views + DailyView::factory() + ->count(rand(0, 9)) + ->state(new Sequence( + ['session_id' => uniqid()], + ['session_id' => uniqid()], + ['session_id' => uniqid()], + )) + ->create([ + 'presentation_id' => $presentation->id, + 'adhoc_slug' => null, + ]); + + $daysSincePresentationCreated = now()->diffInDays($presentation->created_at); + + if ($daysSincePresentationCreated === 0) { + return; + } + + // Create some fake Aggregate Views + for ($i = 1; $i <= $daysSincePresentationCreated; $i++) { + if (rand(0, 9) < 3) { + // 30% chance of no views on a given day + continue; + } + + $totalCount = rand(1, 9); + + AggregateView::create([ + 'presentation_id' => $presentation->id, + 'adhoc_slug' => null, + 'total_count' => $totalCount, + 'unique_count' => rand(1, $totalCount), + 'created_at' => now()->subDays($i), + ]); + } + }); + } + /** * Define the model's default state. * @@ -23,6 +77,7 @@ public function definition(): array 'is_published' => fake()->boolean(), 'content' => "# My Presentation\n\n**Slide 1**\n\n*Slide 2*\n\nSlide 3", 'user_id' => User::factory(), + 'created_at' => now()->subDays(rand(0, 21)), // Sometime over the last 3 weeks ]; } }