Skip to content

Commit

Permalink
#51 - Users can no use same code multiple times, if it is not disposable
Browse files Browse the repository at this point in the history
  • Loading branch information
gabiezur committed Sep 16, 2020
1 parent b7b816e commit 25a8010
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 21 deletions.
4 changes: 2 additions & 2 deletions migrations/2016_05_17_221000_create_promocodes_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ public function up()
});

Schema::create('promocode_user', function (Blueprint $table) {
$table->increments('id');

$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('promocode_id');

$table->timestamp('used_at');

$table->primary(['user_id', 'promocode_id']);

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('promocode_id')->references('id')->on('promocodes')->onDelete('cascade');
});
Expand Down
4 changes: 2 additions & 2 deletions migrations/create_promocodes_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ class CreatePromocodesTable extends Migration
});

Schema::create('promocode_user', function (Blueprint $table) {
$table->increments('id');

$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('promocode_id');

$table->timestamp('used_at');

$table->primary(['user_id', 'promocode_id']);

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('promocode_id')->references('id')->on('promocodes')->onDelete('cascade');
});
Expand Down
4 changes: 2 additions & 2 deletions src/Promocodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -478,8 +478,8 @@ public function check($code)
*/
public function isSecondUsageAttempt(Promocode $promocode)
{
return $promocode->users()->wherePivot(config('promocodes.related_pivot_key', 'user_id'),
auth()->id())->exists();
return $promocode->isDisposable() && $promocode->users()->wherePivot(config('promocodes.related_pivot_key', 'user_id'),
auth()->id())->exists();
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Traits/Rewardable.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function redeemCode($code, $callback = null)
public function applyCode($code, $callback = null)
{
if ($promocode = Promocodes::check($code)) {
if ($promocode->users()->wherePivot(config('promocodes.related_pivot_key'), $this->id)->exists()) {
if ($promocode->isDisposable() && $promocode->users()->wherePivot(config('promocodes.related_pivot_key'), $this->id)->exists()) {
throw new AlreadyUsedException;
}

Expand All @@ -73,6 +73,6 @@ public function applyCode($code, $callback = null)
$callback(null);
}

return null;
return false;
}
}
10 changes: 4 additions & 6 deletions tests/ApplyPromocodeToUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,18 @@ public function it_returns_false_if_promocode_doesnt_exist()
}

/** @test */
public function it_throws_exception_if_user_tries_to_apply_code_twice()
public function it_returns_false_if_user_tries_to_apply_code_twice()
{
$this->expectException(AlreadyUsedException::class);

$user = User::find(1);
$this->actingAs($user);

$promocodes = Promocodes::create();
$promocodes = Promocodes::setDisposable()->create();
$promocode = $promocodes->first();

$this->assertCount(1, $promocodes);

Promocodes::apply($promocode['code']);
Promocodes::apply($promocode['code']);
$this->assertInstanceOf(Promocode::class, Promocodes::apply($promocode['code']));
$this->assertFalse(Promocodes::apply($promocode['code']));
}

/** @test */
Expand Down
23 changes: 16 additions & 7 deletions tests/RewardableTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Promocodes;
use Gabievi\Promocodes\Models\Promocode;
use Gabievi\Promocodes\Tests\Models\User;
use Gabievi\Promocodes\Exceptions\AlreadyUsedException;

class RewardableTraitTest extends TestCase
{
Expand All @@ -22,11 +21,11 @@ public function setUp(): void
}

/** @test */
public function it_returns_null_if_could_not_apply_promocode()
public function it_returns_false_if_could_not_apply_promocode()
{
$applyCode = $this->user->applyCode('INVALID-CODE');

$this->assertNull($applyCode);
$this->assertFalse($applyCode);
}

/** @test */
Expand All @@ -38,10 +37,8 @@ public function it_returns_null_in_callback_if_could_not_apply_promocode()
}

/** @test */
public function it_throws_exception_if_user_already_applied_to_code()
public function it_can_be_user_multiple_times_if_it_is_not_disposable()
{
$this->expectException(AlreadyUsedException::class);

$promocodes = Promocodes::create();
$promocode = $promocodes->first();

Expand All @@ -51,6 +48,18 @@ public function it_throws_exception_if_user_already_applied_to_code()
$this->user->applyCode($promocode['code']);
}

/** @test */
public function it_returns_false_for_second_use_of_disposable_code()
{
$promocodes = Promocodes::setDisposable()->create();
$promocode = $promocodes->first();

$this->assertCount(1, $promocodes);

$this->assertInstanceOf(Promocode::class, $this->user->applyCode($promocode['code']));
$this->assertFalse($this->user->applyCode($promocode['code']));
}

/** @test */
public function it_attaches_current_user_as_applied_to_promocode()
{
Expand Down Expand Up @@ -84,7 +93,7 @@ public function is_returns_promocode_with_user_if_applied_successfuly()
}

/** @test */
public function is_returns_promocode_with_user_in_callback_if_applied_successfuly()
public function it_returns_promocode_with_user_in_callback_if_applied_successfuly()
{
$promocodes = Promocodes::create();
$promocode = $promocodes->first();
Expand Down

0 comments on commit 25a8010

Please sign in to comment.