Skip to content

Commit

Permalink
feat: update factory data for presentation daily and aggregate views
Browse files Browse the repository at this point in the history
  • Loading branch information
alkrauss48 committed Apr 9, 2024
1 parent 1d840da commit 52136bd
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/Models/Presentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Presentation> $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.
*/
Expand Down
55 changes: 55 additions & 0 deletions database/factories/PresentationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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
];
}
}

0 comments on commit 52136bd

Please # to comment.