A free open source IT asset/license management system
Fixed issue where canceled requests don’t get removed from calendar
snipef25075269de9
diff --git a/app/Models/CheckoutRequest.php b/app/Models/CheckoutRequest.phpindex f2b11bdafd..245753b86c 100644--- a/app/Models/CheckoutRequest.php+++ b/app/Models/CheckoutRequest.php@@ -226,6 +226,31 @@ class CheckoutRequest extends Model});}+ /**+ * Flip the row to canceled without firing the full model-event+ * cascade (archive-time cleanup paths on Accessory / Component /+ * Consumable rely on this so a parent-entity update does not+ * trigger a storm of per-request notification observers), but+ * still explicitly re-sync the calendar_events index so the+ * reservation drops off the calendar on cancel. The persistent-+ * calendar-event bug reported against the archive and user-+ * initiated cancel paths traced back to a saveQuietly() /+ * query-builder update() combination that bypassed the+ * HasCalendarEvents observer entirely.+ */+ public function cancelAndSync(): bool+ {+ $this->canceled_at = now();+ $this->state = CheckoutRequestState::Canceled;+ $saved = $this->saveQuietly();++ if ($saved) {+ $this->forceSyncCalendarEvents();+ }++ return $saved;+ }+/*** @return BelongsTo<User, $this>*/diff --git a/app/Models/Component.php b/app/Models/Component.phpindex 7e57492e46..632b798ae1 100644--- a/app/Models/Component.php+++ b/app/Models/Component.php@@ -2,7 +2,6 @@namespace App\Models;-use App\Enums\CheckoutRequestState;use App\Enums\FileStorage;use App\Models\Builders\ComponentQueryBuilder;use App\Models\Contracts\Importable;@@ -188,11 +187,7 @@ class Component extends SnipeModel implements Importablereturn;}- $component->openRequests()->get()->each(function (CheckoutRequest $request): void {- $request->canceled_at = now();- $request->state = CheckoutRequestState::Canceled;- $request->saveQuietly();- });+ $component->openRequests()->get()->each(fn (CheckoutRequest $request) => $request->cancelAndSync());});// On hard-delete, wipe the image file and Files-tab attachments.diff --git a/app/Models/Traits/Requestable.php b/app/Models/Traits/Requestable.phpindex 2857ea478b..0d7930565a 100644--- a/app/Models/Traits/Requestable.php+++ b/app/Models/Traits/Requestable.php@@ -5,7 +5,6 @@ namespace App\Models\Traits;use App\Enums\CheckoutRequestState;use App\Models\CheckoutRequest;use App\Models\User;-use Carbon\Carbon;use Illuminate\Database\Eloquent\Relations\MorphMany;// $asset->requests@@ -125,6 +124,12 @@ trait Requestable* prevents already-terminal rows (fulfilled/canceled) from* getting their canceled_at bumped, and prevents no-op* cancellations from looking like real events downstream.+ *+ * Iterate + save (via cancelAndSync) rather than a query-builder+ * ->update() so the HasCalendarEvents observer fires per row and+ * the matching calendar_events index row gets cleaned up. The+ * cancel count on this path is small (one requester's open+ * requests for one item) so the N+1 shape is deliberate.*/public function cancelRequest($user_id = null): int{@@ -132,13 +137,20 @@ trait Requestable$user_id = auth()->id();}- return $this->requests()+ $pending = $this->requests()->where('user_id', $user_id)->where('state', CheckoutRequestState::Pending->value)- ->update([- 'state' => CheckoutRequestState::Canceled->value,- 'canceled_at' => Carbon::now(),- ]);+ ->get();++ $affected = 0;+ /** @var CheckoutRequest $request */+ foreach ($pending as $request) {+ if ($request->cancelAndSync()) {+ $affected++;+ }+ }++ return $affected;}/**diff --git a/app/Observers/AccessoryObserver.php b/app/Observers/AccessoryObserver.phpindex da21965fb3..c6b4046c5e 100644--- a/app/Observers/AccessoryObserver.php+++ b/app/Observers/AccessoryObserver.php@@ -2,7 +2,6 @@namespace App\Observers;-use App\Enums\CheckoutRequestState;use App\Models\Accessory;use App\Models\Actionlog;use App\Models\CheckoutRequest;@@ -49,11 +48,7 @@ class AccessoryObserver// Archive-time request cleanup.if ($accessory->wasChanged('archived') && $accessory->archived) {- $accessory->openRequests()->get()->each(function (CheckoutRequest $request): void {- $request->canceled_at = now();- $request->state = CheckoutRequestState::Canceled;- $request->saveQuietly();- });+ $accessory->openRequests()->get()->each(fn (CheckoutRequest $request) => $request->cancelAndSync());}}@@ -80,11 +75,7 @@ class AccessoryObserver*/public function deleting(Accessory $accessory){- $accessory->openRequests()->get()->each(function (CheckoutRequest $request): void {- $request->canceled_at = now();- $request->state = CheckoutRequestState::Canceled;- $request->saveQuietly();- });+ $accessory->openRequests()->get()->each(fn (CheckoutRequest $request) => $request->cancelAndSync());$logAction = new Actionlog;$logAction->item_type = Accessory::class;diff --git a/app/Observers/ComponentObserver.php b/app/Observers/ComponentObserver.phpindex eae1be88e8..cff34a4dd5 100644--- a/app/Observers/ComponentObserver.php+++ b/app/Observers/ComponentObserver.php@@ -2,7 +2,6 @@namespace App\Observers;-use App\Enums\CheckoutRequestState;use App\Models\Actionlog;use App\Models\CheckoutRequest;use App\Models\Component;@@ -56,11 +55,7 @@ class ComponentObserver// Archive-time request cleanup.if ($component->wasChanged('archived') && $component->archived) {- $component->openRequests()->get()->each(function (CheckoutRequest $request): void {- $request->canceled_at = now();- $request->state = CheckoutRequestState::Canceled;- $request->saveQuietly();- });+ $component->openRequests()->get()->each(fn (CheckoutRequest $request) => $request->cancelAndSync());}}@@ -87,11 +82,7 @@ class ComponentObserver*/public function deleting(Component $component){- $component->openRequests()->get()->each(function (CheckoutRequest $request): void {- $request->canceled_at = now();- $request->state = CheckoutRequestState::Canceled;- $request->saveQuietly();- });+ $component->openRequests()->get()->each(fn (CheckoutRequest $request) => $request->cancelAndSync());$logAction = new Actionlog;$logAction->item_type = Component::class;diff --git a/app/Observers/ConsumableObserver.php b/app/Observers/ConsumableObserver.phpindex 273ca480de..9943acaee0 100644--- a/app/Observers/ConsumableObserver.php+++ b/app/Observers/ConsumableObserver.php@@ -2,7 +2,6 @@namespace App\Observers;-use App\Enums\CheckoutRequestState;use App\Models\Actionlog;use App\Models\CheckoutAcceptance;use App\Models\CheckoutRequest;@@ -51,11 +50,7 @@ class ConsumableObserver// Archive-time request cleanup.if ($consumable->wasChanged('archived') && $consumable->archived) {- $consumable->openRequests()->get()->each(function (CheckoutRequest $request): void {- $request->canceled_at = now();- $request->state = CheckoutRequestState::Canceled;- $request->saveQuietly();- });+ $consumable->openRequests()->get()->each(fn (CheckoutRequest $request) => $request->cancelAndSync());}}@@ -94,11 +89,7 @@ class ConsumableObserver->where('checkoutable_id', $consumable->id)->delete();- $consumable->openRequests()->get()->each(function (CheckoutRequest $request): void {- $request->canceled_at = now();- $request->state = CheckoutRequestState::Canceled;- $request->saveQuietly();- });+ $consumable->openRequests()->get()->each(fn (CheckoutRequest $request) => $request->cancelAndSync());$logAction = new Actionlog;$logAction->item_type = Consumable::class;diff --git a/database/migrations/2026_10_29_140345_purge_stale_calendar_events_for_terminal_checkout_requests.php b/database/migrations/2026_10_29_140345_purge_stale_calendar_events_for_terminal_checkout_requests.phpnew file mode 100644index 0000000000..62772cbfbc--- /dev/null+++ b/database/migrations/2026_10_29_140345_purge_stale_calendar_events_for_terminal_checkout_requests.php@@ -0,0 +1,47 @@+<?php++use App\Models\CalendarEvent;+use App\Models\CheckoutRequest;+use Illuminate\Database\Migrations\Migration;+use Illuminate\Support\Facades\DB;++/**+ * One-shot cleanup for installs where the user- or archive-initiated+ * cancel paths flipped checkout_requests to a terminal state (canceled+ * or fulfilled) without the HasCalendarEvents observer firing.+ *+ * Delete every calendar_events row whose source CheckoutRequest is+ * no longer pending.+ */+return new class extends Migration+{+ public function up(): void+ {+ $terminalIds = CheckoutRequest::query()+ ->where(function ($q) {+ $q->whereNotNull('canceled_at')+ ->orWhereNotNull('fulfilled_at');+ })+ ->pluck('id')+ ->all();++ if ($terminalIds === []) {+ return;+ }++ // Chunk the IN list so an install with tens of thousands of+ // historically-canceled rows does not blow MySQL's+ // prepared-statement placeholder ceiling on a single DELETE.+ foreach (array_chunk($terminalIds, 1000) as $chunk) {+ DB::table((new CalendarEvent)->getTable())+ ->where('source_type', CheckoutRequest::class)+ ->whereIn('source_id', $chunk)+ ->delete();+ }+ }++ public function down(): void+ {+ // No-op.+ }+};diff --git a/tests/Feature/CalendarEvents/CheckoutRequestCalendarCleanupTest.php b/tests/Feature/CalendarEvents/CheckoutRequestCalendarCleanupTest.phpnew file mode 100644index 0000000000..8424be076e--- /dev/null+++ b/tests/Feature/CalendarEvents/CheckoutRequestCalendarCleanupTest.php@@ -0,0 +1,187 @@+<?php++namespace Tests\Feature\CalendarEvents;++use App\Enums\CheckoutRequestState;+use App\Models\Accessory;+use App\Models\Asset;+use App\Models\CalendarEvent;+use App\Models\CheckoutRequest;+use App\Models\Component;+use App\Models\Consumable;+use App\Models\User;+use Tests\TestCase;++/**+ * A canceled CheckoutRequest must stop showing up on the reservation+ * calendar. The HasCalendarEvents trait keys off the model's updated+ * event, and the previous cancel paths (user-initiated via+ * Requestable::cancelRequest and archive-time via the observer+ * saveQuietly cleanup) both bypassed Eloquent events, leaving the+ * calendar_events row behind with the Pending-state start/end dates.+ */+class CheckoutRequestCalendarCleanupTest extends TestCase+{+ private function assertHasCalendarEvent(CheckoutRequest $request): void+ {+ $this->assertDatabaseHas('calendar_events', [+ 'source_type' => CheckoutRequest::class,+ 'source_id' => $request->id,+ 'source_field' => 'calendar_start',+ ]);+ }++ private function assertMissingCalendarEvent(CheckoutRequest $request): void+ {+ // CalendarEvent uses SoftDeletes, so the trait's sync path+ // soft-deletes the row when calendar_start resolves to null+ // (state != pending). Assert the live row is gone, not the+ // trashed one, matching how CalendarEventsSyncTest checks+ // the soft-delete lifecycle.+ $this->assertDatabaseMissing('calendar_events', [+ 'source_type' => CheckoutRequest::class,+ 'source_id' => $request->id,+ 'source_field' => 'calendar_start',+ 'deleted_at' => null,+ ]);+ }++ public function test_user_initiated_cancel_removes_calendar_event(): void+ {+ $user = User::factory()->create();+ $asset = Asset::factory()->create();+ $request = CheckoutRequest::factory()->create([+ 'user_id' => $user->id,+ 'requestable_id' => $asset->id,+ 'requestable_type' => Asset::class,+ 'start_date' => now()->subDay(),+ 'end_date' => now()->addDays(3),+ ]);++ $this->assertHasCalendarEvent($request);++ $affected = $asset->cancelRequest($user->id);++ $this->assertSame(1, $affected);+ $this->assertSame(CheckoutRequestState::Canceled, $request->fresh()->state);+ $this->assertMissingCalendarEvent($request);+ }++ public function test_cancel_and_sync_on_the_request_removes_calendar_event(): void+ {+ $request = CheckoutRequest::factory()->create([+ 'user_id' => User::factory()->create()->id,+ 'requestable_id' => Asset::factory()->create()->id,+ 'requestable_type' => Asset::class,+ 'start_date' => now(),+ 'end_date' => now()->addDay(),+ ]);++ $this->assertHasCalendarEvent($request);++ $request->cancelAndSync();++ $this->assertSame(CheckoutRequestState::Canceled, $request->fresh()->state);+ $this->assertNotNull($request->fresh()->canceled_at);+ $this->assertMissingCalendarEvent($request);+ }++ public function test_archiving_an_accessory_removes_calendar_events_for_its_requests(): void+ {+ $accessory = Accessory::factory()->create();+ $user = User::factory()->create();++ // Requestable::openRequests reads request rows joined through+ // the accessory's own HasMany to CheckoutRequest, so building+ // the request via factory with a direct morph pointer is enough.+ $request = CheckoutRequest::factory()->create([+ 'user_id' => $user->id,+ 'requestable_id' => $accessory->id,+ 'requestable_type' => Accessory::class,+ 'start_date' => now(),+ 'end_date' => now()->addWeek(),+ ]);++ $this->assertHasCalendarEvent($request);++ $accessory->update(['archived' => 1]);++ $this->assertSame(CheckoutRequestState::Canceled, $request->fresh()->state);+ $this->assertMissingCalendarEvent($request);+ }++ public function test_deleting_a_consumable_removes_calendar_events_for_its_requests(): void+ {+ $consumable = Consumable::factory()->create();+ $user = User::factory()->create();++ $request = CheckoutRequest::factory()->create([+ 'user_id' => $user->id,+ 'requestable_id' => $consumable->id,+ 'requestable_type' => Consumable::class,+ 'start_date' => now(),+ 'end_date' => now()->addWeek(),+ ]);++ $this->assertHasCalendarEvent($request);++ $consumable->delete();++ $this->assertSame(CheckoutRequestState::Canceled, $request->fresh()->state);+ $this->assertMissingCalendarEvent($request);+ }++ public function test_archiving_a_component_removes_calendar_events_for_its_requests(): void+ {+ $component = Component::factory()->create();+ $user = User::factory()->create();++ $request = CheckoutRequest::factory()->create([+ 'user_id' => $user->id,+ 'requestable_id' => $component->id,+ 'requestable_type' => Component::class,+ 'start_date' => now(),+ 'end_date' => now()->addWeek(),+ ]);++ $this->assertHasCalendarEvent($request);++ $component->update(['archived' => 1]);++ $this->assertSame(CheckoutRequestState::Canceled, $request->fresh()->state);+ $this->assertMissingCalendarEvent($request);+ }++ public function test_reconcile_migration_cleans_up_pre_existing_stale_rows(): void+ {+ // Simulate the historical bug: a terminal CheckoutRequest row+ // with a live calendar_events entry still pointing at it.+ $request = CheckoutRequest::factory()->create([+ 'user_id' => User::factory()->create()->id,+ 'requestable_id' => Asset::factory()->create()->id,+ 'requestable_type' => Asset::class,+ 'start_date' => now(),+ 'end_date' => now()->addDay(),+ ]);++ $this->assertHasCalendarEvent($request);++ // Push the request into terminal state via query builder so+ // the HasCalendarEvents observer does not fire, matching the+ // pre-fix cancel path. The calendar_events row survives.+ CheckoutRequest::query()+ ->where('id', $request->id)+ ->update([+ 'state' => CheckoutRequestState::Canceled->value,+ 'canceled_at' => now(),+ ]);++ $this->assertHasCalendarEvent($request);++ // Re-run the one-shot reconcile migration.+ $migration = include database_path('migrations/2026_10_29_140345_purge_stale_calendar_events_for_terminal_checkout_requests.php');+ $migration->up();++ $this->assertMissingCalendarEvent($request);+ }+}