Skip to content

Commit e5629e6

Browse files
committed
Release 2.0-beta7 (#79)
1 parent a6171e8 commit e5629e6

9 files changed

+162
-45
lines changed

CHANGELOG.md

+30-25
Original file line numberDiff line numberDiff line change
@@ -3,70 +3,75 @@ All notable changes to this project will be documented in this file.
33

44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
55

6-
## [Unreleased]
6+
## 2.0-beta7 - 2022-01-19
77

88
### Fixed
99

10-
- When translating an attribute, if we can't find a translation we default to whatever the FieldType gives back instead of erroring.
11-
- `TranslatedText` fieldtype now implements `JsonSerializable` interface ([#50](https://github.com/getcandy/getcandy/issues/50))
12-
- When translating an attribute, if we can't find a translation we default to whatever the FieldType gives back instead of erroring.
10+
- When translating an attribute, if we can't find a translation, we default to whatever the FieldType gives back instead of erroring.
11+
- `TranslatedText` fieldtype now implements `JsonSerializable` interface ([#50](https://github.com/getcandy/getcandy/issues/50)).
1312

14-
### Added
13+
### Changed
1514

16-
- Created the `AttributeManifest` class so dev's can add their own attributable classes.
17-
- Created the `FieldTypeManifest` class so dev's can add custom FieldTypes to the store.
18-
- Added a new `default` column to the `tax_classes` table.
15+
- Models that have channels now implement `start_at` and `end_at` columns. This replaces the previous `published_at` column.
16+
- Laravel UI modal components removed.
1917

2018
### Added
2119

20+
- Added a new `default` column to the `tax_classes` table.
21+
- Added `customer_id` to orders so an order has a 1:1 relation to a customer. ([#73](https://github.com/getcandy/getcandy/issues/73)).
2222
- Created the `AttributeManifest` class so dev's can add their own attributable classes.
2323
- Created the `FieldTypeManifest` class so dev's can add custom FieldTypes to the store.
24+
- Added `$table->userForeignKey()` macro for migrations that create foreign keys which reference a user id.
2425

25-
### Changed
26-
27-
- Models that have channels now implement `start_at` and `end_at` columns. This replaces the previous `published_at` column.
26+
[View Changes](https://github.com/getcandy/core/compare/2.0-beta5...2.0-beta7)
2827

2928
## 2.0-beta5 - 2022-01-10
3029

31-
### Fixes
30+
### Fixed
3231

33-
- Added check on customers for when using MySQL search driver to prevent undefined columns [#40](https://github.com/getcandy/getcandy/issues/40)
32+
- Added check on customers for when using MySQL search driver to prevent undefined columns [#40](https://github.com/getcandy/getcandy/issues/40).
3433

3534
### Changed
3635

3736
- Changed `https` to `http` on country import due to issues with local environment CA installations.
3837

3938
## 2.0-beta4 - 2022-01-07
4039

41-
### Added
42-
- Made customers searchable via Scout
43-
- Added Addresses relationship to the customer model
40+
### Fixed
41+
42+
- Fixes [Issue 24](https://github.com/getcandy/getcandy/issues/24) where URL relationship is `elements` when it should be `element`.
43+
- Fixed an issue where `now()->year` would return an int on single digit months, but we need to have a leading zero.
4444

4545
### Changed
46+
4647
- Customers `meta` column now uses Laravel's `casts` property and is cast to an object.
4748

48-
### Fixes
49-
- Fixes [Issue 24](https://github.com/getcandy/getcandy/issues/24) where URL relationship is `elements` when it should be `element`
50-
- Fixed an issue where `now()->year` would return an int on single digit months, but we need to have a leading zero.
49+
### Added
50+
51+
- Made customers searchable via Scout.
52+
- Added addresses relationship to the customer model.
5153

5254
[View Changes](https://github.com/getcandy/core/compare/2.0-beta3...2.0-beta4)
5355

5456
## 2.0-beta3 - 2021-12-24
5557

56-
### Fixes
58+
### Fixed
59+
5760
- Fixed and issue where the meilisearch set up wasn't creating the indexes it needed if they didn't exist.
5861

5962
[View Changes](https://github.com/getcandy/core/compare/2.0-beta2...2.0-beta3)
6063

6164
## 2.0-beta2 - 2021-12-23
6265

63-
### Added
64-
- Added a default collection group
65-
### Changes
66-
- Install command no longer publishes hub assets
67-
### Fixes
66+
### Fixed
6867
- Default currency has `enabled` set to true.
6968

69+
### Changed
70+
- Install command no longer publishes hub assets
71+
72+
### Added
73+
- Added a default `CollectionGroup`.
74+
7075
[View Changes](https://github.com/getcandy/core/compare/2.0-beta...2.0-beta2)
7176

7277
## 2.0-beta - 2021-12-22

database/migrations/2021_10_01_090000_create_orders_table.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,8 @@ class CreateOrdersTable extends Migration
99
public function up()
1010
{
1111
Schema::create($this->prefix.'orders', function (Blueprint $table) {
12-
$userModel = config('auth.providers.users.model');
1312
$table->bigIncrements('id');
14-
$table->foreignId('user_id')->nullable()->constrained(
15-
(new $userModel())->getTable()
16-
);
13+
$table->userForeignKey(nullable: true);
1714
$table->foreignId('channel_id')->constrained($this->prefix.'channels');
1815
$table->string('status')->index();
1916
$table->string('reference')->nullable()->unique();

database/migrations/2021_10_01_100000_create_carts_table.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@ class CreateCartsTable extends Migration
99
public function up()
1010
{
1111
Schema::create($this->prefix.'carts', function (Blueprint $table) {
12-
$userModel = config('auth.providers.users.model');
13-
1412
$table->bigIncrements('id');
15-
$table->foreignId('user_id')->nullable()->constrained(
16-
(new $userModel())->getTable()
17-
);
13+
$table->userForeignKey(nullable: true);
1814
$table->foreignId('merged_id')->nullable()->constrained($this->prefix.'carts');
1915
$table->foreignId('currency_id')->constrained($this->prefix.'currencies');
2016
$table->foreignId('channel_id')->constrained($this->prefix.'channels');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
use GetCandy\Base\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class AddCustomerIdToOrdersTable extends Migration
8+
{
9+
public function up()
10+
{
11+
Schema::table($this->prefix.'orders', function (Blueprint $table) {
12+
$table->foreignId('customer_id')->after('id')
13+
->nullable()
14+
->constrained($this->prefix.'customers');
15+
});
16+
}
17+
18+
public function down()
19+
{
20+
Schema::table($this->prefix.'orders', function ($table) {
21+
$table->dropForeign(['customer_id']);
22+
$table->dropColumn('customer_id');
23+
});
24+
}
25+
}

database/state/EnsureDefaultTaxClassExists.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ public function run()
1515

1616
// Get the first tax class and make it default
1717
$taxClass = TaxClass::first();
18-
$taxClass->update([
19-
'default' => true,
20-
]);
18+
19+
if ($taxClass) {
20+
$taxClass->update([
21+
'default' => true,
22+
]);
23+
}
2124
}
2225

2326
protected function canRun()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace GetCandy\Database\State;
4+
5+
use GetCandy\Models\Order;
6+
use Illuminate\Support\Facades\DB;
7+
use Illuminate\Support\Facades\Schema;
8+
9+
class EnsureUserOrdersHaveACustomer
10+
{
11+
public function run()
12+
{
13+
if (!$this->canRun()) {
14+
return;
15+
}
16+
17+
// Get any orders which have a user but not a customer id
18+
$orders = Order::with('user.customers')
19+
->whereNull('customer_id')
20+
->whereNotNull('user_id')
21+
->get();
22+
23+
DB::transaction(function () use ($orders) {
24+
foreach ($orders as $order) {
25+
$customer = $order->user->customers->first();
26+
$order->update([
27+
'customer_id' => $customer?->id,
28+
]);
29+
}
30+
});
31+
}
32+
33+
protected function canRun()
34+
{
35+
$prefix = config('getcandy.database.table_prefix');
36+
37+
return Schema::hasTable("{$prefix}orders");
38+
}
39+
}

src/GetCandyServiceProvider.php

+12-8
Original file line numberDiff line numberDiff line change
@@ -227,22 +227,26 @@ protected function registerBlueprintMacros(): void
227227
}
228228
});
229229

230-
Blueprint::macro('userForeignKey', function ($field_name = 'user_id') {
230+
Blueprint::macro('userForeignKey', function ($field_name = 'user_id', $nullable = false) {
231231
$userModel = config('auth.providers.users.model');
232232

233233
$type = config('getcandy.database.users_id_type', 'bigint');
234234

235235
if ($type == 'uuid') {
236-
$this->foreignUuId($field_name)->constrained(
237-
(new $userModel())->getTable()
238-
);
236+
$this->foreignUuId($field_name)
237+
->nullable($nullable)
238+
->constrained(
239+
(new $userModel())->getTable()
240+
);
239241
} elseif ($type == 'int') {
240-
$this->unsignedInteger($field_name);
242+
$this->unsignedInteger($field_name)->nullable($nullable);
241243
$this->foreign($field_name)->references('id')->on('users');
242244
} else {
243-
$this->foreignId($field_name)->constrained(
244-
(new $userModel())->getTable()
245-
);
245+
$this->foreignId($field_name)
246+
->nullable($nullable)
247+
->constrained(
248+
(new $userModel())->getTable()
249+
);
246250
}
247251
});
248252
}

src/Models/Order.php

+22
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,28 @@ public function refunds()
174174
return $this->transactions()->whereRefund(true);
175175
}
176176

177+
/**
178+
* Return the customer relationship.
179+
*
180+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
181+
*/
182+
public function customer()
183+
{
184+
return $this->belongsTo(Customer::class);
185+
}
186+
187+
/**
188+
* Return the user relationship.
189+
*
190+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
191+
*/
192+
public function user()
193+
{
194+
return $this->belongsTo(
195+
config('auth.providers.users.model')
196+
);
197+
}
198+
177199
public function toSearchableArray()
178200
{
179201
if (config('scout.driver') == 'mysql') {

tests/Unit/Models/OrderTest.php

+26
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace GetCandy\Tests\Unit\Models;
44

55
use DateTime;
6+
use GetCandy\Hub\Tests\Stubs\User;
67
use GetCandy\Models\Currency;
8+
use GetCandy\Models\Customer;
79
use GetCandy\Models\Order;
810
use GetCandy\Models\OrderLine;
911
use GetCandy\Models\ProductVariant;
@@ -162,4 +164,28 @@ public function can_retrieve_different_transaction_types_for_order()
162164
$this->assertEquals($charge->id, $order->charges->first()->id);
163165
$this->assertEquals($refund->id, $order->refunds->first()->id);
164166
}
167+
168+
/** @test */
169+
public function can_have_user_and_customer_associated()
170+
{
171+
$user = User::create([
172+
'name' => 'Test User',
173+
'email' => 'test@domain.com',
174+
'email_verified_at' => now(),
175+
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
176+
'remember_token' => \Illuminate\Support\Str::random(10),
177+
]);
178+
179+
$customer = $user->customers()->create(
180+
Customer::factory()->make()->toArray()
181+
);
182+
183+
$order = Order::factory()->create([
184+
'customer_id' => $customer->id,
185+
'user_id' => $user->id,
186+
]);
187+
188+
$this->assertEquals($customer->id, $order->customer->id);
189+
$this->assertEquals($user->id, $order->user->id);
190+
}
165191
}

0 commit comments

Comments
 (0)