Skip to content

Commit

Permalink
Fixing a bug with removing scheduling conflicts.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
joshforbes committed Jan 5, 2017
1 parent 529c9b5 commit 7e0c02a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Temporal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -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});
Expand Down
21 changes: 21 additions & 0 deletions tests/TemporalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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...
*/
Expand Down

0 comments on commit 7e0c02a

Please sign in to comment.