Skip to content

Commit

Permalink
Ensure customer groups are associated when creating resources (#1922)
Browse files Browse the repository at this point in the history
This PR looks to automatically sync customer groups to records that use
the `HasCustomerGroups` trait when they are created. This should
alleviate any confusion or admin burden.

---------

Co-authored-by: alecritson <[email protected]>
Co-authored-by: Glenn Jacobs <[email protected]>
  • Loading branch information
3 people authored Aug 28, 2024
1 parent cd7511c commit 9dc1a7b
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 12 deletions.
24 changes: 24 additions & 0 deletions packages/core/src/Base/Traits/HasCustomerGroups.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use DateTime;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Collection;
use Lunar\Models\CustomerGroup;
Expand All @@ -17,6 +18,29 @@ trait HasCustomerGroups
*/
abstract public function customerGroups(): Relation;

public static function getExtraCustomerGroupPivotValues(CustomerGroup $customerGroup): array
{
return [
];
}

public static function bootHasCustomerGroups(): void
{
static::created(function (Model $model) {
$model->customerGroups()->sync(
CustomerGroup::get()->mapWithKeys(
fn ($customerGroup): array => [$customerGroup->id => [
'enabled' => $customerGroup->default,
'starts_at' => now(),
'ends_at' => null,
'visible' => $customerGroup->default,
...static::getExtraCustomerGroupPivotValues($customerGroup),
]]
)
);
});
}

/**
* Schedule models against customer groups.
*
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,16 @@ public function customerGroups(): BelongsToMany
])->withTimestamps();
}

public static function getExtraCustomerGroupPivotValues(CustomerGroup $customerGroup): array
{
return [
'purchasable' => $customerGroup->default,
];
}

/**
* Return the brand relationship.
*/
public function brand(): BelongsTo
{
return $this->belongsTo(Brand::modelClass());
Expand Down
31 changes: 31 additions & 0 deletions tests/core/Unit/Base/Traits/HasCustomerGroupsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,34 @@

expect(Product::customerGroup($groupA, $startsAt, $endsAt)->get())->toHaveCount(1);
});

test('customer groups are synced on model creation', function () {
$customerGroupA = CustomerGroup::factory()->create([
'default' => true,
]);
$customerGroupB = CustomerGroup::factory()->create([
'default' => false,
]);

Product::factory()->create();

\Pest\Laravel\assertDatabaseHas(
'lunar_customer_group_product',
[
'customer_group_id' => $customerGroupA->id,
'enabled' => 1,
'visible' => 1,
'purchasable' => 1,
],
);

\Pest\Laravel\assertDatabaseHas(
'lunar_customer_group_product',
[
'customer_group_id' => $customerGroupB->id,
'enabled' => 0,
'visible' => 0,
'purchasable' => 0,
],
);
});
6 changes: 1 addition & 5 deletions tests/core/Unit/Models/CustomerGroupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@
test('can return discounts', function () {
$customerGroup = \Lunar\Models\CustomerGroup::factory()->create();

$discount = \Lunar\Models\Discount::factory()->create();

expect($customerGroup->discounts)->toHaveCount(0);

$customerGroup->discounts()->attach($discount->id);
\Lunar\Models\Discount::factory()->create();

expect($customerGroup->refresh()->discounts)->toHaveCount(1);
});
22 changes: 15 additions & 7 deletions tests/core/Unit/Models/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,9 @@
test('customer groups can be enabled', function () {
$product = Product::factory()->create();

expect($product->customerGroups)->toHaveCount(0);

$customerGroup = CustomerGroup::factory()->create();
$customerGroup = CustomerGroup::factory()->create([
'default' => true,
]);

$product->scheduleCustomerGroup($customerGroup);

Expand All @@ -150,7 +150,9 @@
test('customer groups can be scheduled always available', function () {
$product = Product::factory()->create();

$customerGroup = CustomerGroup::factory()->create();
$customerGroup = CustomerGroup::factory()->create([
'default' => true,
]);

$product->scheduleCustomerGroup($customerGroup);

Expand All @@ -170,7 +172,9 @@
test('customer groups can be scheduled with start and end', function () {
$product = Product::factory()->create();

$customerGroup = CustomerGroup::factory()->create();
$customerGroup = CustomerGroup::factory()->create([
'default' => true,
]);

$start = now();
$end = now();
Expand Down Expand Up @@ -207,7 +211,9 @@
test('customer groups can be scheduled with pivot data', function () {
$product = Product::factory()->create();

$customerGroup = CustomerGroup::factory()->create();
$customerGroup = CustomerGroup::factory()->create([
'default' => true,
]);

$start = now();
$end = now();
Expand Down Expand Up @@ -247,7 +253,9 @@
test('customer groups can be unscheduled', function () {
$product = Product::factory()->create();

$customerGroup = CustomerGroup::factory()->create();
$customerGroup = CustomerGroup::factory()->create([
'default' => true,
]);

$start = now();
$end = now();
Expand Down

0 comments on commit 9dc1a7b

Please sign in to comment.