From 7e0c02a5a34a630555a77e4a5bc72f9d85539475 Mon Sep 17 00:00:00 2001 From: Josh Forbes Date: Thu, 5 Jan 2017 08:46:26 -0500 Subject: [PATCH] Fixing a bug with removing scheduling conflicts. The method was indiscriminately removing any temporal models that were scheduling after the start of the new one. Now the method actually checks if a scheduling conflict exists before removing on those entries. --- src/Temporal.php | 12 ++++++++++-- tests/TemporalTest.php | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/src/Temporal.php b/src/Temporal.php index 4ca9204..174ed40 100644 --- a/src/Temporal.php +++ b/src/Temporal.php @@ -99,7 +99,13 @@ protected function removeSchedulingConflicts() return $this->getQuery()->where('valid_start', '>', Carbon::now())->delete(); } - $this->getQuery()->where('valid_start', '>', $this->valid_end)->delete(); + $this->getQuery() + ->where('valid_start', '<', $this->valid_end) + ->where(function ($query) { + $query->whereNull('valid_end') + ->orWhere('valid_end', '>', $this->valid_start); + }) + ->delete(); } /** @@ -123,7 +129,9 @@ protected function endCurrent() */ private function getQuery() { - $query = $this->where($this->temporalParentColumn, $this->{$this->temporalParentColumn}); + $query = $this + ->where($this->primaryKey, '!=', $this->{$this->primaryKey}) + ->where($this->temporalParentColumn, $this->{$this->temporalParentColumn}); if ($this->temporalPolymorphicTypeColumn) { $query->where($this->temporalPolymorphicTypeColumn, $this->{$this->temporalPolymorphicTypeColumn}); diff --git a/tests/TemporalTest.php b/tests/TemporalTest.php index 61f8b38..6d8dea8 100644 --- a/tests/TemporalTest.php +++ b/tests/TemporalTest.php @@ -164,6 +164,27 @@ public function testItRemovesAScheduledCommissionWhenANewOneIsCreated() $this->assertNull($scheduledCommission->fresh()); } + /** + * Tests... + */ + public function testItOnlyRemovesAScheduledCommissionWhenThereIsAConflict() + { + $scheduledCommission = TemporalTestCommission::create([ + 'id' => 2, + 'agent_id' => 1, + 'valid_start' => Carbon::now()->addDays(20), + 'valid_end' => null + ]); + TemporalTestCommission::create([ + 'id' => 3, + 'agent_id' => 1, + 'valid_start' => Carbon::now(), + 'valid_end' => Carbon::now()->addDays(19) + ]); + + $this->assertNotNull($scheduledCommission->fresh()); + } + /** * Tests... */