feat(discount): implement published discounts filtering logic
Added logic to filter published discounts, including those with null start or end dates.
This commit is contained in:
parent
08354b44b6
commit
cfac370e2e
2 changed files with 92 additions and 2 deletions
|
|
@ -54,8 +54,12 @@ class Discount extends Model
|
|||
protected function scopePublished(Builder $query): Builder
|
||||
{
|
||||
return $query
|
||||
->where('date_start', '<=', Date::now())
|
||||
->where('date_end', '>=', Date::now());
|
||||
->where(function (Builder $q): void {
|
||||
$q->whereNull('date_start')->orWhere('date_start', '<=', Date::now());
|
||||
})
|
||||
->where(function (Builder $q): void {
|
||||
$q->whereNull('date_end')->orWhere('date_end', '>=', Date::now());
|
||||
});
|
||||
}
|
||||
|
||||
/** @return \Illuminate\Database\Eloquent\Relations\HasMany<\App\Models\Product,$this> */
|
||||
|
|
|
|||
86
tests/Integration/WithApp/App/Models/DiscountTest.php
Normal file
86
tests/Integration/WithApp/App/Models/DiscountTest.php
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Integration\WithApp\App\Models;
|
||||
|
||||
use App\Models\Discount;
|
||||
use Domain\Enums\DiscountType;
|
||||
use Domain\Models\ValueObjects\DiscountValue;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
describe('Discount model', function (): void {
|
||||
it('filters published discounts including those with null start or end dates', function (): void {
|
||||
// GIVEN
|
||||
Carbon::setTestNow('2026-06-15 12:00:00');
|
||||
|
||||
// 1. Permanent discount (no start, no end) -> published
|
||||
$permanent = new Discount();
|
||||
$permanent->type = DiscountType::PERCENT;
|
||||
$permanent->value = DiscountValue::fromPercentage(10);
|
||||
$permanent->is_old_price_displayed = true;
|
||||
$permanent->date_start = null;
|
||||
$permanent->date_end = null;
|
||||
$permanent->save();
|
||||
|
||||
// 2. Immediate discount with end date in future -> published
|
||||
$immediate = new Discount();
|
||||
$immediate->type = DiscountType::PERCENT;
|
||||
$immediate->value = DiscountValue::fromPercentage(15);
|
||||
$immediate->is_old_price_displayed = true;
|
||||
$immediate->date_start = null;
|
||||
$immediate->date_end = Carbon::parse('2026-06-20 23:59:59');
|
||||
$immediate->save();
|
||||
|
||||
// 3. Started discount with no end date -> published
|
||||
$ongoing = new Discount();
|
||||
$ongoing->type = DiscountType::PERCENT;
|
||||
$ongoing->value = DiscountValue::fromPercentage(20);
|
||||
$ongoing->is_old_price_displayed = true;
|
||||
$ongoing->date_start = Carbon::parse('2026-06-10 00:00:00');
|
||||
$ongoing->date_end = null;
|
||||
$ongoing->save();
|
||||
|
||||
// 4. Bounded active discount -> published
|
||||
$bounded = new Discount();
|
||||
$bounded->type = DiscountType::PERCENT;
|
||||
$bounded->value = DiscountValue::fromPercentage(25);
|
||||
$bounded->is_old_price_displayed = true;
|
||||
$bounded->date_start = Carbon::parse('2026-06-10 00:00:00');
|
||||
$bounded->date_end = Carbon::parse('2026-06-20 23:59:59');
|
||||
$bounded->save();
|
||||
|
||||
// 5. Future discount -> not published
|
||||
$future = new Discount();
|
||||
$future->type = DiscountType::PERCENT;
|
||||
$future->value = DiscountValue::fromPercentage(30);
|
||||
$future->is_old_price_displayed = true;
|
||||
$future->date_start = Carbon::parse('2026-06-20 00:00:00');
|
||||
$future->date_end = Carbon::parse('2026-06-25 23:59:59');
|
||||
$future->save();
|
||||
|
||||
// 6. Expired discount -> not published
|
||||
$expired = new Discount();
|
||||
$expired->type = DiscountType::PERCENT;
|
||||
$expired->value = DiscountValue::fromPercentage(35);
|
||||
$expired->is_old_price_displayed = true;
|
||||
$expired->date_start = Carbon::parse('2026-06-01 00:00:00');
|
||||
$expired->date_end = Carbon::parse('2026-06-10 23:59:59');
|
||||
$expired->save();
|
||||
|
||||
// WHEN
|
||||
$publishedDiscounts = Discount::query()->published()->get();
|
||||
|
||||
// THEN
|
||||
$publishedIds = $publishedDiscounts->pluck('id')->all();
|
||||
|
||||
expect($publishedIds)->toContain($permanent->id)
|
||||
->and($publishedIds)->toContain($immediate->id)
|
||||
->and($publishedIds)->toContain($ongoing->id)
|
||||
->and($publishedIds)->toContain($bounded->id)
|
||||
->and($publishedIds)->not->toContain($future->id)
|
||||
->and($publishedIds)->not->toContain($expired->id);
|
||||
|
||||
Carbon::setTestNow();
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Reference in a new issue