Skip to content

Commit

Permalink
Converted the billing/shipping countries to zones
Browse files Browse the repository at this point in the history
- Replacing the plain json array-based storage
  • Loading branch information
fulopattila122 committed Apr 4, 2024
1 parent e372d62 commit 6f17d58
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/Channel/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
- `language`
- `domain`
- billing fields (merchant data)
- `billing_countries`
- `shipping_countries`
- `billing_zone_id`
- `shipping_zone_id`
- `theme`
- `color`
- Added the `channelables` table for being many-to-many polymorphic relationships with channels and arbitrary models
Expand Down
47 changes: 41 additions & 6 deletions src/Channel/Models/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
use Cviebrock\EloquentSluggable\Sluggable;
use Cviebrock\EloquentSluggable\SluggableScopeHelpers;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Arr;
use Konekt\Address\Contracts\Zone;
use Konekt\Address\Models\ZoneProxy;
use Vanilo\Channel\Contracts\Channel as ChannelContract;
use Vanilo\Contracts\Merchant;
use Vanilo\Contracts\Schematized;
Expand All @@ -44,11 +47,14 @@
* @property ?string $billing_registration_nr
* @property ?string $email
* @property ?string $phone
* @property ?array $billing_countries
* @property ?array $shipping_countries
* @property ?int $billing_zone_id
* @property ?int $shipping_zone_id
* @property string color
* @property ?string theme
*
* @property ?Zone $billingZone
* @property ?Zone $shippingZone
*
* @property ?Carbon $deleted_at
* @property Carbon $created_at
* @property Carbon $updated_at
Expand All @@ -67,8 +73,6 @@ class Channel extends Model implements ChannelContract

protected $casts = [
'configuration' => 'array',
'billing_countries' => 'array',
'shipping_countries' => 'array',
];

public static function findByDomain(string $domain): ?Channel
Expand Down Expand Up @@ -141,12 +145,38 @@ public function getMerchant(): ?Merchant

public function getBillingCountries(): array
{
return $this->billing_countries ?? [];
if (!$this->zoneSupportIsPresent() || null === $this->billingZone) {
return [];
}

return $this->billingZone->getMemberCountryIds();
}

public function getShippingCountries(): array
{
return $this->shipping_countries ?? [];
if (!$this->zoneSupportIsPresent() || null === $this->shippingZone) {
return [];
}

return $this->shippingZone->getMemberCountryIds();
}

public function billingZone(): ?BelongsTo
{
if ($this->zoneSupportIsPresent()) {
return $this->belongsTo(ZoneProxy::modelClass(), 'billing_zone_id', 'id');
}

return null;
}

public function shippingZone(): ?BelongsTo
{
if ($this->zoneSupportIsPresent()) {
return $this->belongsTo(ZoneProxy::modelClass(), 'shipping_zone_id', 'id');
}

return null;
}

public function getConfigurationSchema(): ?Schematized
Expand All @@ -162,4 +192,9 @@ public function sluggable(): array
]
];
}

protected function zoneSupportIsPresent(): bool
{
return class_exists(ZoneProxy::class);
}
}
54 changes: 54 additions & 0 deletions src/Channel/Tests/ChannelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

namespace Vanilo\Channel\Tests;

use Konekt\Address\Models\Zone;
use Konekt\Address\Models\ZoneScope;
use Vanilo\Channel\Models\Channel;
use Vanilo\Contracts\Merchant;

Expand Down Expand Up @@ -110,4 +112,56 @@ public function it_can_return_the_merchant_from_model_data()
$this->assertEquals('IT', $merchant->getAddress()->getCountryCode());
$this->assertEquals('41044', $merchant->getAddress()->getPostalCode());
}

/** @test */
public function it_returns_an_empty_array_of_shipping_and_billing_countries_by_default()
{
$channel = Channel::create(['name' => 'Mobile App']);

$billingCountries = $channel->getBillingCountries();
$shippingCountries = $channel->getShippingCountries();

$this->assertIsArray($billingCountries);
$this->assertEmpty($billingCountries);
$this->assertIsArray($shippingCountries);
$this->assertEmpty($shippingCountries);
}

/** @test */
public function billing_countries_can_be_assigned_via_zones()
{
$zone = Zone::create(['scope' => ZoneScope::BILLING(), 'name' => 'Scandinavia']);
$zone->addCountry('SE');
$zone->addCountry('FI');
$zone->addCountry('DK');
$zone->addCountry('NO');

$channel = Channel::create(['name' => 'Scandinavian Shop', 'billing_zone_id' => $zone->id]);

$billingCountries = $channel->getBillingCountries();

$this->assertCount(4, $billingCountries);
$this->assertContains('SE', $billingCountries);
$this->assertContains('FI', $billingCountries);
$this->assertContains('DK', $billingCountries);
$this->assertContains('NO', $billingCountries);
}

/** @test */
public function shipping_countries_can_be_assigned_via_zones()
{
$zone = Zone::create(['scope' => ZoneScope::SHIPPING(), 'name' => 'Benelux']);
$zone->addCountry('BE');
$zone->addCountry('NL');
$zone->addCountry('LU');

$channel = Channel::create(['name' => 'Benelux Shop', 'shipping_zone_id' => $zone->id]);

$shippingCountries = $channel->getShippingCountries();

$this->assertCount(3, $shippingCountries);
$this->assertContains('BE', $shippingCountries);
$this->assertContains('NL', $shippingCountries);
$this->assertContains('LU', $shippingCountries);
}
}
4 changes: 3 additions & 1 deletion src/Channel/Tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace Vanilo\Channel\Tests;

use Cviebrock\EloquentSluggable\ServiceProvider as SluggableServiceProvider;
use Konekt\Address\Providers\ModuleServiceProvider as AddressModule;
use Konekt\Concord\ConcordServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;
use Vanilo\Channel\Providers\ModuleServiceProvider as ChannelModule;
Expand Down Expand Up @@ -68,7 +69,8 @@ protected function resolveApplicationConfiguration($app)
parent::resolveApplicationConfiguration($app);

$app['config']->set('concord.modules', [
ChannelModule::class
ChannelModule::class,
AddressModule::class,
]);
}
}
8 changes: 6 additions & 2 deletions src/Channel/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@
"php": "^8.2",
"konekt/concord": "^1.13",
"cviebrock/eloquent-sluggable": "^10.0|^11.0",
"laravel/framework": "^10.38.2|^11.0",
"laravel/framework": "^10.43|^11.0",
"vanilo/support": "^4.0"
},
"require-dev": {
"phpunit/phpunit": "^10.0",
"orchestra/testbench": "^8.0|^9.0",
"laravel/legacy-factories": "^1.0"
"laravel/legacy-factories": "^1.0",
"konekt/address": "^3.0"
},
"suggest": {
"konekt/address": "To support billing and shipping zones (^3.0)"
},
"autoload": {
"psr-4": { "Vanilo\\Channel\\": "" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

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

return new class extends Migration {
public function up(): void
{
Schema::table('channels', function (Blueprint $table) {
$table->unsignedBigInteger('billing_zone_id')->nullable();
$table->unsignedBigInteger('shipping_zone_id')->nullable();

if (Schema::hasTable('zones')) {
$table->foreign('billing_zone_id')->references('id')->on('zones');
$table->foreign('shipping_zone_id')->references('id')->on('zones');
}
});

Schema::table('channels', function (Blueprint $table) {
$table->dropColumn('billing_countries');
});
Schema::table('channels', function (Blueprint $table) {
$table->dropColumn('shipping_countries');
});
}

public function down(): void
{
Schema::table('channels', function (Blueprint $table) {
$table->json('billing_countries')->nullable();
$table->json('shipping_countries')->nullable();
});

Schema::table('channels', function (Blueprint $table) {
if (!$this->isSqlite() && Schema::hasIndex('channels', ['billing_zone_id'])) {
$table->dropForeign(['billing_zone_id']);
}
$table->dropColumn('billing_zone_id');
});

Schema::table('channels', function (Blueprint $table) {
if (!$this->isSqlite() && Schema::hasIndex('channels', ['shipping_zone_id'])) {
$table->dropForeign(['shipping_zone_id']);
}
$table->dropColumn('shipping_zone_id');
});
}

private function isSqlite(): bool
{
return 'sqlite' === Schema::connection($this->getConnection())
->getConnection()
->getPdo()
->getAttribute(PDO::ATTR_DRIVER_NAME)
;
}
};

0 comments on commit 6f17d58

Please sign in to comment.