Skip to content

Commit

Permalink
add preorder starts date
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiritin committed Jul 21, 2024
1 parent d3eb340 commit dd96e84
Show file tree
Hide file tree
Showing 21 changed files with 821 additions and 80 deletions.
8 changes: 8 additions & 0 deletions .env.testing
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
DB_DATABASE=testing
DB_HOST=127.0.0.1
DB_CONNECTION=mysql
DB_USERNAME=root
DB_PASSWORD=password
APP_KEY=base64:JzqP44/5fNz4B9cqzDtE6ktvlUmv9q9U0I2o4FjRtUY=
APP_ENV=testing
APP_DEBUG=true
23 changes: 23 additions & 0 deletions app/Console/Commands/BadgesPrintCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Console\Commands;

use App\Models\Badge\Badge;
use App\Models\Badge\States\Printed;
use Illuminate\Console\Command;

class BadgesPrintCommand extends Command
{
protected $signature = 'badges:print';

protected $description = 'Command description';

public function handle(): void
{
// Prints all badges
Badge::whereNull('printed_at')->update([
'status' => Printed::$name,
'printed_at' => now(),
]);
}
}
23 changes: 23 additions & 0 deletions app/Console/Commands/BadgesUnprintCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Console\Commands;

use App\Models\Badge\Badge;
use App\Models\Badge\States\Pending;
use Illuminate\Console\Command;

class BadgesUnprintCommand extends Command
{
protected $signature = 'badges:unprint';

protected $description = 'Command description';

public function handle(): void
{
// Unprint all badges
Badge::whereNotNull('printed_at')->update([
'status' => Pending::$name,
'printed_at' => null,
]);
}
}
72 changes: 72 additions & 0 deletions app/Console/Commands/CreateOrUpdateEventForStateCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\Event;
use Carbon\Carbon;
use App\Enum\EventStateEnum;

class CreateOrUpdateEventForStateCommand extends Command
{
protected $signature = 'event:state {state}';

protected $description = 'Creates or updates an event to match a specific state. Only for local or testing environments.';

public function __construct()
{
parent::__construct();
}

public function handle()
{
if (!app()->environment(['local', 'testing'])) {
$this->error('This command can only be run in local or testing environments.');
return;
}

$state = $this->argument('state');
$event = Event::first();

$fromInput = EventStateEnum::tryFrom($state);

switch ($fromInput) {
case EventStateEnum::COUNTDOWN:
$this->createOrUpdateEvent($event, now()->addDays(11), now()->addDays(20), now()->addDays(21), now()->addDays(30));
break;
case EventStateEnum::PREORDER:
$this->createOrUpdateEvent($event, now()->subDays(1), now()->subDays(10), now()->addDays(11), now()->addDays(20));
break;
case EventStateEnum::LATE:
$this->createOrUpdateEvent($event, now()->subDays(20), now()->subDays(10), now()->subDays(9), now()->addDays(10));
break;
case EventStateEnum::CLOSED:
$this->createOrUpdateEvent($event, now()->subDays(30), now()->subDays(20), now()->subDays(19), now()->subDays(1));
break;
default:
$this->error('Invalid state provided.');
return;
}

$this->info("Event has been created or updated to match the '$state' state.");
}

private function createOrUpdateEvent($event, $startsAt, $preorderStartsAt, $preorderEndsAt, $orderEndsAt)
{
if ($event) {
$event->update([
'starts_at' => $startsAt,
'preorder_starts_at' => $preorderStartsAt,
'preorder_ends_at' => $preorderEndsAt,
'order_ends_at' => $orderEndsAt,
]);
} else {
Event::create([
'starts_at' => $startsAt,
'preorder_starts_at' => $preorderStartsAt,
'preorder_ends_at' => $preorderEndsAt,
'order_ends_at' => $orderEndsAt,
]);
}
}
}
6 changes: 4 additions & 2 deletions app/Http/Controllers/BadgeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ public function index(Request $request)
{
return Inertia::render('Badges/BadgesIndex', [
'badges' => auth()->user()->badges()->with('fursuit.species')->get(),
'canCreate' => Gate::allows('create', Badge::class),
]);
}

public function create(Request $request)
{
$request->user()->can('create', Badge::class);
Gate::authorize('create', Badge::class);
return Inertia::render('Badges/BadgesCreate', [
'species' => Species::has('fursuits', count: 5)->orWhere('checked', true)->get('name'),
'isFree' => auth()->user()->badges()->where('is_free_badge', true)->doesntExist(),
Expand All @@ -38,7 +39,7 @@ public function create(Request $request)

public function store(BadgeCreateRequest $request)
{
$request->user()->can('create', Badge::class);
Gate::authorize('create', Badge::class);
$badge = DB::transaction(function () use ($request) {
// Lock Wallet Balance
$request->user()->balanceInt;
Expand Down Expand Up @@ -130,6 +131,7 @@ public function edit(Badge $badge, Request $request)

public function update(BadgeUpdateRequest $request, Badge $badge)
{
Gate::authorize('update', $badge);
$badge = DB::transaction(function () use ($request, $badge) {
$request->user()->can('update', $badge);
// Lock Wallet Balance
Expand Down
3 changes: 2 additions & 1 deletion app/Models/Badge/Badge.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Bavix\Wallet\Interfaces\Customer;
use Bavix\Wallet\Interfaces\ProductInterface;
use Bavix\Wallet\Traits\HasWalletFloat;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand All @@ -17,7 +18,7 @@

class Badge extends Model implements ProductInterface
{
use HasStates, SoftDeletes, HasWalletFloat, LogsActivity;
use HasStates, SoftDeletes, HasWalletFloat, LogsActivity, HasFactory;

protected $guarded = [];
protected $casts = [
Expand Down
3 changes: 2 additions & 1 deletion app/Models/Fursuit/Fursuit.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use App\Models\Species;
use App\Models\User;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Facades\Storage;
Expand All @@ -17,7 +18,7 @@

class Fursuit extends Model
{
use HasStates, LogsActivity;
use HasStates, LogsActivity, HasFactory;
protected $guarded = [];

protected $casts = [
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Species.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace App\Models;

use App\Models\Fursuit\Fursuit;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Species extends Model
{
use HasFactory;
protected $guarded = [];

public function fursuits()
Expand Down
61 changes: 27 additions & 34 deletions app/Policies/BadgePolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@

namespace App\Policies;

use App\Enum\EventStateEnum;
use App\Models\Badge\Badge;
use App\Models\Badge\States\Pending;
use App\Models\User;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Routing\Route;

class BadgePolicy
{
use HandlesAuthorization;

public function viewAny(User $user): bool
{
return $user->is_admin;
return $user->is_admin && request()->routeIs('filament.*');
}

public function view(User $user, Badge $badge): bool
{
if ($user->is_admin) {
if ($user->is_admin && request()->routeIs('filament.*')) {
return true;
}
return $user->id === $badge->fursuit->user_id;
Expand All @@ -30,11 +33,17 @@ public function create(User $user): bool
if ($event === null) {
return false;
}
if($event->order_ends_at !== null) {
if($event->order_ends_at->isPast()) {
return false;
}

// Admin can override
if ($user->is_admin && request()->routeIs('filament.*')) {
return true;
}

// Safety check if in CLOSED OR LATE return false
if ($event->state === \App\Enum\EventStateEnum::CLOSED || $event->state === \App\Enum\EventStateEnum::COUNTDOWN) {
return false;
}

return true;
}

Expand All @@ -45,58 +54,42 @@ public function update(User $user, Badge $badge): bool
if ($badge->extra_copy_of !== null) {
return false;
}
// Admin can do everything
if ($user->is_admin) {
return true;
}
// Cannot edit when no active event
if ($event === null) {
return false;
}
// Cannot edit a badge that has already been printed
if ($badge->printed_at !== null) {
return false;
}
// Safety check
if($event->order_ends_at !== null) {
if($event->order_ends_at->isPast()) {
return false;
}
}
return $user->id === $badge->fursuit->user_id;
return $this->delete($user, $badge);
}

public function delete(User $user, Badge $badge): bool
{
$event = \App\Models\Event::getActiveEvent();
// Admin can do everything IN FILAMENT

// Admin can do everything
if ($user->is_admin) {
if ($user->is_admin && request()->routeIs('filament.*')) {
return true;
}
// Cannot edit when no active event
if ($event === null) {
return false;
}
// Cannot edit a badge that has already been printed
if ($badge->printed_at !== null) {
if ((string) $badge->status !== Pending::$name) {
return false;
}
// Safety check
if($event->order_ends_at !== null) {
if($event->order_ends_at->isPast()) {
return false;
}

// Safety check if in CLOSED OR LATE return false
if ($event->state === \App\Enum\EventStateEnum::CLOSED || $event->state === \App\Enum\EventStateEnum::COUNTDOWN) {
return false;
}

return $user->id === $badge->fursuit->user_id;
}

public function restore(User $user, Badge $badge): bool
{
return $user->is_admin;
return $user->is_admin && request()->routeIs('filament.*');
}

public function forceDelete(User $user, Badge $badge): bool
{
return $user->is_admin;
return $user->is_admin && request()->routeIs('filament.*');
}
}
43 changes: 43 additions & 0 deletions database/factories/Badge/BadgeFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Database\Factories\Badge;

use App\Models\Badge\Badge;
use App\Models\Badge\States\Pending;
use App\Models\Badge\States\PickedUp;
use App\Models\Badge\States\Printed;
use App\Models\Badge\States\ReadyForPickup;
use App\Models\Fursuit\Fursuit;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Carbon;

class BadgeFactory extends Factory
{
protected $model = Badge::class;

public function definition(): array
{
return [
'is_free_badge' => $this->faker->boolean(),
'extra_copy_of' => null,
'status' => $this->faker->randomElement([
Pending::$name,
Printed::$name,
ReadyForPickup::$name,
PickedUp::$name
]),
'dual_side_print' => $this->faker->boolean(),
'extra_copy' => $this->faker->boolean(),
'apply_late_fee' => $this->faker->boolean(),
'subtotal' => $this->faker->randomNumber(),
'tax_rate' => 19,
'tax' => $this->faker->randomNumber(),
'total' => $this->faker->randomNumber(),
'printed_at' => Carbon::now(),
'pickup_location' => $this->faker->word(),
'ready_for_pickup_at' => Carbon::now(),
'picked_up_at' => Carbon::now(),
'fursuit_id' => Fursuit::factory(),
];
}
}
Loading

0 comments on commit dd96e84

Please sign in to comment.