From 4626d68061d4ebf0452d1bedeaf6799ea0aa542e Mon Sep 17 00:00:00 2001 From: Yehuda Neufeld Date: Thu, 29 Jun 2023 17:29:51 +0300 Subject: [PATCH] feat: add `active` to Offer --- CHANGELOG.md | 4 ++++ ...000000_add_product_ids_to_offers_table.php | 22 +++++++++++++++++++ src/Models/Offer.php | 8 ++++--- tests/Unit/OffersTest.php | 13 +++++++++++ 4 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 database/migrations/2023_06_29_000000_add_product_ids_to_offers_table.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e9be0d..f23c1d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/database/migrations/2023_06_29_000000_add_product_ids_to_offers_table.php b/database/migrations/2023_06_29_000000_add_product_ids_to_offers_table.php new file mode 100644 index 0000000..8c3923c --- /dev/null +++ b/database/migrations/2023_06_29_000000_add_product_ids_to_offers_table.php @@ -0,0 +1,22 @@ +boolean('active')->default(true)->after('product_ids'); + }); + } + + public function down() + { + Schema::table('offers', function (Blueprint $table) { + $table->dropColumn('active'); + }); + } +}; diff --git a/src/Models/Offer.php b/src/Models/Offer.php index 4c01e90..c8e9c8e 100644 --- a/src/Models/Offer.php +++ b/src/Models/Offer.php @@ -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) { diff --git a/tests/Unit/OffersTest.php b/tests/Unit/OffersTest.php index 06ec7ee..eb48c48 100644 --- a/tests/Unit/OffersTest.php +++ b/tests/Unit/OffersTest.php @@ -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,