-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from artkonekt/feat-customer-purchases
Added Customer Purchases
- Loading branch information
Showing
10 changed files
with
321 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
# Customer Module Changelog | ||
|
||
## Unreleased | ||
##### 2024-XX-YY | ||
|
||
- Added CustomerPurchases | ||
|
||
## 3.0.0 | ||
##### 2024-04-25 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Konekt\Customer\Contracts; | ||
|
||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Database\Eloquent\Relations\MorphTo; | ||
|
||
interface CustomerPurchase | ||
{ | ||
public function purchasable(): MorphTo; | ||
|
||
public function customer(): BelongsTo; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Konekt\Customer\Models; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Database\Eloquent\Relations\BelongsTo; | ||
use Illuminate\Database\Eloquent\Relations\MorphTo; | ||
use Konekt\Customer\Contracts\CustomerPurchase as CustomerPurchaseContract; | ||
|
||
/** | ||
* @property int $id | ||
* @property int $customer_id | ||
* @property Carbon $purchase_date | ||
* @property float $purchase_value | ||
* @property string $currency | ||
* @property int $purchasable_id | ||
* @property string $purchasable_type | ||
* | ||
* @property-read Model $purchasable | ||
* @property-read Customer $customer | ||
*/ | ||
class CustomerPurchase extends Model implements CustomerPurchaseContract | ||
{ | ||
protected $guarded = ['id', 'created_at', 'updated_at']; | ||
|
||
protected $casts = [ | ||
'purchase_date' => 'date', | ||
]; | ||
|
||
public function purchasable(): MorphTo | ||
{ | ||
return $this->morphTo(); | ||
} | ||
|
||
public function customer(): BelongsTo | ||
{ | ||
return $this->belongsTo(CustomerProxy::modelClass()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Konekt\Customer\Models; | ||
|
||
use Konekt\Concord\Proxies\ModelProxy; | ||
|
||
class CustomerPurchaseProxy extends ModelProxy | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/resources/database/migrations/2024_11_21_102102_create_customer_purchases_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?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::create('customer_purchases', function (Blueprint $table) { | ||
$table->id(); | ||
$table->integer('customer_id')->unsigned(); | ||
$table->date('purchase_date')->nullable(); | ||
$table->decimal('purchase_value', 15, 4)->nullable(); | ||
$table->char('currency', 3)->nullable(); | ||
$table->morphs('purchasable'); | ||
$table->timestamps(); | ||
|
||
$table->foreign('customer_id') | ||
->references('id') | ||
->on('customers') | ||
->onDelete('cascade'); | ||
}); | ||
} | ||
|
||
public function down(): void | ||
{ | ||
Schema::dropIfExists('customer_purchases'); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Konekt\Customer\Models\CustomerProxy; | ||
use Konekt\Customer\Models\CustomerPurchase; | ||
use Konekt\Customer\Models\CustomerPurchaseProxy; | ||
use Konekt\Customer\Tests\Dummies\Order; | ||
use Konekt\Customer\Tests\TestCase; | ||
|
||
class CustomerPurchaseTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function customer_purchase_can_be_created_with_minimal_data(): void | ||
{ | ||
$order = Order::create([ | ||
'number' => '70H-0WJB-2OD6' | ||
]); | ||
|
||
$customer = CustomerProxy::create([])->fresh(); | ||
|
||
$customerPurchase = CustomerPurchaseProxy::create([ | ||
'customer_id' => $customer->id, | ||
'purchasable_id' => $order->id, | ||
'purchasable_type' => Order::class, | ||
])->fresh(); | ||
|
||
$this->assertTrue($order->is($customerPurchase->purchasable)); | ||
$this->assertTrue($customer->is($customerPurchase->customer)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function customer_purchase_can_be_created_with_full_data(): void | ||
{ | ||
$order = Order::create([ | ||
'number' => '70H-0WJB-2OD6' | ||
]); | ||
|
||
$customer = CustomerProxy::create([])->fresh(); | ||
|
||
$customerPurchase = CustomerPurchaseProxy::create([ | ||
'customer_id' => $customer->id, | ||
'purchase_date' => '2024-11-21', | ||
'purchase_value' => 89.62, | ||
'currency' => 'EUR', | ||
'purchasable_id' => $order->id, | ||
'purchasable_type' => Order::class, | ||
])->fresh(); | ||
|
||
$this->assertTrue($order->is($customerPurchase->purchasable)); | ||
$this->assertTrue($customer->is($customerPurchase->customer)); | ||
$this->assertEquals(89.62, $customerPurchase->purchase_value); | ||
$this->assertEquals('EUR', $customerPurchase->currency); | ||
$this->assertEquals('2024-11-21', $customerPurchase->purchase_date->toDateString()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function customer_purchase_can_be_updated_with_full_data(): void | ||
{ | ||
$order = Order::create([ | ||
'number' => '70H-0WJB-2OD6' | ||
]); | ||
|
||
$order2 = Order::create([ | ||
'number' => '50M3-0RD3R-NUMB3R' | ||
]); | ||
|
||
$customer = CustomerProxy::create([])->fresh(); | ||
|
||
$customer2 = CustomerProxy::create([])->fresh(); | ||
|
||
$customerPurchase = CustomerPurchaseProxy::create([ | ||
'customer_id' => $customer->id, | ||
'purchase_date' => '2024-11-21', | ||
'purchase_value' => 89.62, | ||
'currency' => 'EUR', | ||
'purchasable_id' => $order->id, | ||
'purchasable_type' => Order::class, | ||
])->fresh(); | ||
|
||
$customerPurchase->update([ | ||
'customer_id' => $customer2->id, | ||
'purchase_date' => '2001-01-06', | ||
'purchase_value' => 2001.06, | ||
'currency' => 'HUF', | ||
'purchasable_id' => $order2->id, | ||
'purchasable_type' => Order::class, | ||
]); | ||
|
||
$updatedCustomerPurchase = $customerPurchase->fresh(); | ||
|
||
$this->assertFalse($order->is($updatedCustomerPurchase->purchasable)); | ||
$this->assertTrue($order2->is($updatedCustomerPurchase->purchasable)); | ||
|
||
$this->assertFalse($customer->is($updatedCustomerPurchase->customer)); | ||
$this->assertTrue($customer2->is($updatedCustomerPurchase->customer)); | ||
|
||
$this->assertEquals(2001.06, $updatedCustomerPurchase->purchase_value); | ||
$this->assertEquals('HUF', $updatedCustomerPurchase->currency); | ||
$this->assertEquals('2001-01-06', $updatedCustomerPurchase->purchase_date->toDateString()); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function customer_purchase_can_be_deleted(): void | ||
{ | ||
$order = Order::create([ | ||
'number' => '50M3-0RD3R-NUMB3R' | ||
]); | ||
|
||
$customer = CustomerProxy::create([])->fresh(); | ||
|
||
$customerPurchase = CustomerPurchaseProxy::create([ | ||
'customer_id' => $customer->id, | ||
'purchase_date' => '2024-11-21', | ||
'purchase_value' => 89.62, | ||
'currency' => 'EUR', | ||
'purchasable_id' => $order->id, | ||
'purchasable_type' => Order::class, | ||
])->fresh(); | ||
|
||
$customerPurchaseId = $customerPurchase->id; | ||
|
||
$customerPurchase->delete(); | ||
|
||
$this->assertNull(CustomerPurchaseProxy::find($customerPurchaseId)); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function purchases_of_a_customer_can_be_retrieved(): void | ||
{ | ||
$order = Order::create([ | ||
'number' => '50M3-0RD3R-NUMB3R' | ||
]); | ||
|
||
$order2 = Order::create([ | ||
'number' => 'WH47-H4V3-Y0U-B0U6H7' | ||
]); | ||
|
||
$customer = CustomerProxy::create([])->fresh(); | ||
|
||
$customerPurchase1 = CustomerPurchaseProxy::create([ | ||
'customer_id' => $customer->id, | ||
'purchase_date' => '2024-11-21', | ||
'purchase_value' => 89.62, | ||
'currency' => 'EUR', | ||
'purchasable_id' => $order->id, | ||
'purchasable_type' => Order::class, | ||
])->fresh(); | ||
|
||
$customerPurchase2 = CustomerPurchaseProxy::create([ | ||
'customer_id' => $customer->id, | ||
'purchase_date' => '2023-12-09', | ||
'purchase_value' => 10000000.94, | ||
'currency' => 'HUF', | ||
'purchasable_id' => $order2->id, | ||
'purchasable_type' => Order::class, | ||
])->fresh(); | ||
|
||
$this->assertEquals(2, $customer->purchases->count()); | ||
|
||
$this->assertTrue($customer->purchases->every(function ($purchase) { | ||
return $purchase instanceof CustomerPurchase; | ||
})); | ||
|
||
$this->assertTrue($customer->purchases->map->purchasable->every(function ($order) { | ||
return $order instanceof Order; | ||
})); | ||
|
||
$this->assertTrue($customer->purchases->pluck('purchase_value')->contains(89.62)); | ||
$this->assertTrue($customer->purchases->pluck('purchase_value')->contains(10000000.94)); | ||
|
||
$this->assertTrue($customer->purchases->map->purchasable->pluck('number')->contains('50M3-0RD3R-NUMB3R')); | ||
$this->assertTrue($customer->purchases->map->purchasable->pluck('number')->contains('WH47-H4V3-Y0U-B0U6H7')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Konekt\Customer\Tests\Dummies; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Order extends Model | ||
{ | ||
protected $guarded = ['id', 'created_at', 'updated_at']; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters