Skip to content

Commit

Permalink
feat: add active to Offer
Browse files Browse the repository at this point in the history
  • Loading branch information
Yiddishe-Kop committed Jun 29, 2023
1 parent b707c19 commit 4626d68
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

### 2.2.0 (2023-06-29)

- Add `active` column to `Offers` - it's now possible to deactivate Offers. The default value is `true` (for backwards compatibility).

### 2.1.0 (2023-06-25)

- Add `product_ids` column to `Offers` - it's now possible to limit an Offer to specific products
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

return new class extends Migration
{
public function up()
{
Schema::table('offers', function (Blueprint $table) {
$table->boolean('active')->default(true)->after('product_ids');
});
}

public function down()
{
Schema::table('offers', function (Blueprint $table) {
$table->dropColumn('active');
});
}
};
8 changes: 5 additions & 3 deletions src/Models/Offer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@ class Offer extends Model

protected $casts = [
'product_ids' => 'array',
'valid_from' => 'datetime',
'valid_to' => 'datetime',
'active' => 'boolean',
'valid_from' => 'datetime',
'valid_to' => 'datetime',
];

// scopes
public function scopeValid($q)
{
$q->where(function ($q) {
$q->where('active', true)
->where(function ($q) {
$q->where('valid_from', '<', now())
->orWhereNull('valid_from');
})->where(function ($q) {
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/OffersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,19 @@
expect($this->cart->items_total)->toEqual(8500 * 100);
});

test('inactive offer not applied', function () {
Offer::create([
'type' => Offer::TYPE_PERCENTAGE,
'discount' => 10,
'product_type' => Product::class,
'active' => false,
]);

$this->cart->calculateTotals();

expect($this->cart->items_total)->toEqual(9000 * 100);
});

test('Offers can be limited to product_ids', function () {
Offer::create([
'type' => Offer::TYPE_PERCENTAGE,
Expand Down

0 comments on commit 4626d68

Please sign in to comment.