diff --git a/packages/core/config/search.php b/packages/core/config/search.php index a46359123a..5678ac64be 100644 --- a/packages/core/config/search.php +++ b/packages/core/config/search.php @@ -13,12 +13,12 @@ */ 'models' => [ // These models are required by the system, do not change them. + \Lunar\Models\Brand::class, \Lunar\Models\Collection::class, + \Lunar\Models\Customer::class, + \Lunar\Models\Order::class, \Lunar\Models\Product::class, \Lunar\Models\ProductOption::class, - \Lunar\Models\Order::class, - \Lunar\Models\Customer::class, - \Lunar\Models\Brand::class, // Below you can add your own models for indexing ], /* @@ -38,7 +38,10 @@ 'indexers' => [ \Lunar\Models\Brand::class => \Lunar\Search\BrandIndexer::class, + \Lunar\Models\Collection::class => \Lunar\Search\CollectionIndexer::class, \Lunar\Models\Customer::class => \Lunar\Search\CustomerIndexer::class, + \Lunar\Models\Order::class => \Lunar\Search\OrderIndexer::class, \Lunar\Models\Product::class => \Lunar\Search\ProductIndexer::class, + \Lunar\Models\ProductOption::class => \Lunar\Search\ProductOptionIndexer::class, ], ]; diff --git a/packages/core/src/Models/Collection.php b/packages/core/src/Models/Collection.php index 7900f2bf61..d80e2505af 100644 --- a/packages/core/src/Models/Collection.php +++ b/packages/core/src/Models/Collection.php @@ -45,22 +45,6 @@ class Collection extends BaseModel implements SpatieHasMedia NodeTrait::usesSoftDelete insteadof Searchable; } - /** - * Define our base filterable attributes. - * - * @var array - */ - protected $filterable = []; - - /** - * Define our base sortable attributes. - * - * @var array - */ - protected $sortable = [ - 'name', - ]; - /** * Define which attributes should be cast. * diff --git a/packages/core/src/Models/Order.php b/packages/core/src/Models/Order.php index b17de2d93b..b33be914bc 100644 --- a/packages/core/src/Models/Order.php +++ b/packages/core/src/Models/Order.php @@ -48,30 +48,6 @@ class Order extends BaseModel LogsActivity, Searchable; - /** - * Define our base filterable attributes. - * - * @var array - */ - protected $filterable = [ - '__soft_deleted', - 'status', - 'created_at', - 'placed_at', - 'tags', - ]; - - /** - * Define our base sortable attributes. - * - * @var array - */ - protected $sortable = [ - 'created_at', - 'placed_at', - 'total', - ]; - /** * {@inheritDoc} */ @@ -102,16 +78,6 @@ protected static function newFactory(): OrderFactory return OrderFactory::new(); } - /** - * Get the name of the index associated with the model. - * - * @return string - */ - public function searchableAs() - { - return config('scout.prefix').'orders'; - } - /** * Getter for status label. * @@ -296,63 +262,6 @@ public function user() ); } - /** - * {@inheritDoc} - */ - protected function getSearchableAttributes() - { - $data = [ - 'id' => $this->id, - 'channel' => $this->channel->name, - 'reference' => $this->reference, - 'customer_reference' => $this->customer_reference, - 'status' => $this->status, - 'placed_at' => optional($this->placed_at)->timestamp, - 'created_at' => $this->created_at->timestamp, - 'sub_total' => $this->sub_total->value, - 'total' => $this->total->value, - 'currency_code' => $this->currency_code, - 'charges' => $this->transactions->map(function ($transaction) { - return [ - 'reference' => $transaction->reference, - ]; - }), - 'currency' => $this->currency_code, - 'lines' => $this->productLines->map(function ($line) { - return [ - 'description' => $line->description, - 'identifier' => $line->identifier, - ]; - })->toArray(), - ]; - - foreach ($this->addresses as $address) { - $fields = [ - 'first_name', - 'last_name', - 'company_name', - 'line_one', - 'line_two', - 'line_three', - 'city', - 'state', - 'postcode', - 'contact_email', - 'contact_phone', - ]; - - foreach ($fields as $field) { - $data["{$address->type}_{$field}"] = $address->getAttribute($field); - } - - $data["{$address->type}_country"] = optional($address->country)->name; - } - - $data['tags'] = $this->tags->pluck('value')->toArray(); - - return $data; - } - /** * Determines if this is a draft order. */ diff --git a/packages/core/src/Models/ProductOption.php b/packages/core/src/Models/ProductOption.php index d9462eba92..bb90f21709 100644 --- a/packages/core/src/Models/ProductOption.php +++ b/packages/core/src/Models/ProductOption.php @@ -30,23 +30,6 @@ class ProductOption extends BaseModel implements SpatieHasMedia use HasTranslations; use Searchable; - /** - * Define our base filterable attributes. - * - * @var array - */ - protected $filterable = []; - - /** - * Define our base sortable attributes. - * - * @var array - */ - protected $sortable = [ - 'name', - 'label', - ]; - /** * Define which attributes should be cast. * @@ -57,16 +40,6 @@ class ProductOption extends BaseModel implements SpatieHasMedia 'label' => AsCollection::class, ]; - /** - * Get the name of the index associated with the model. - * - * @return string - */ - public function searchableAs() - { - return config('scout.prefix').'product_options'; - } - /** * Return a new factory instance for the model. */ @@ -110,32 +83,4 @@ public function values() { return $this->hasMany(ProductOptionValue::class)->orderBy('position'); } - - /** - * {@inheritDoc} - */ - public function getSearchableAttributes() - { - $data['id'] = $this->id; - - // Loop for add option name - foreach ($this->name as $locale => $name) { - $data['name_'.$locale] = $name; - } - - // Loop for add option label - foreach ($this->name as $locale => $name) { - $data['label_'.$locale] = $name; - } - - // Loop for add options - foreach ($this->values as $option) { - foreach ($option->name as $locale => $name) { - $key = 'option_'.$option->id.'_'.$locale; - $data[$key] = $name; - } - } - - return $data; - } } diff --git a/packages/core/src/Search/CollectionIndexer.php b/packages/core/src/Search/CollectionIndexer.php new file mode 100644 index 0000000000..665250ddef --- /dev/null +++ b/packages/core/src/Search/CollectionIndexer.php @@ -0,0 +1,30 @@ +with([ + 'channel', + 'transactions', + 'productLines', + 'addresses', + 'tags', + ]); + } + + public function toSearchableArray(Model $model): array + { + $data = [ + 'id' => $model->id, + 'channel' => $model->channel->name, + 'reference' => $model->reference, + 'customer_reference' => $model->customer_reference, + 'status' => $model->status, + 'placed_at' => optional($model->placed_at)->timestamp, + 'created_at' => $model->created_at->timestamp, + 'sub_total' => $model->sub_total->value, + 'total' => $model->total->value, + 'currency_code' => $model->currency_code, + 'charges' => $model->transactions->map(function ($transaction) { + return [ + 'reference' => $transaction->reference, + ]; + }), + 'currency' => $model->currency_code, + 'lines' => $model->productLines->map(function ($line) { + return [ + 'description' => $line->description, + 'identifier' => $line->identifier, + ]; + })->toArray(), + ]; + + foreach ($model->addresses as $address) { + $fields = [ + 'first_name', + 'last_name', + 'company_name', + 'line_one', + 'line_two', + 'line_three', + 'city', + 'state', + 'postcode', + 'contact_email', + 'contact_phone', + ]; + + foreach ($fields as $field) { + $data["{$address->type}_{$field}"] = $address->getAttribute($field); + } + + $data["{$address->type}_country"] = optional($address->country)->name; + } + + $data['tags'] = $model->tags->pluck('value')->toArray(); + + return $data; + } +} diff --git a/packages/core/src/Search/ProductOptionIndexer.php b/packages/core/src/Search/ProductOptionIndexer.php new file mode 100644 index 0000000000..86c6d6586c --- /dev/null +++ b/packages/core/src/Search/ProductOptionIndexer.php @@ -0,0 +1,54 @@ +id; + + // Loop for add option name + foreach ($model->name as $locale => $name) { + $data['name_'.$locale] = $name; + } + + // Loop for add option label + foreach ($model->name as $locale => $name) { + $data['label_'.$locale] = $name; + } + + // Loop for add options + foreach ($model->values as $option) { + foreach ($option->name as $locale => $name) { + $key = 'option_'.$option->id.'_'.$locale; + $data[$key] = $name; + } + } + + return $data; + } +} diff --git a/packages/core/tests/Unit/Search/BrandIndexerTest.php b/packages/core/tests/Unit/Search/BrandIndexerTest.php index c240124c39..3cb2ca5318 100644 --- a/packages/core/tests/Unit/Search/BrandIndexerTest.php +++ b/packages/core/tests/Unit/Search/BrandIndexerTest.php @@ -13,6 +13,7 @@ /** * @group lunar.search + * @group lunar.search.brand */ class BrandIndexerTest extends TestCase { diff --git a/packages/core/tests/Unit/Search/CustomerIndexerTest.php b/packages/core/tests/Unit/Search/CustomerIndexerTest.php index 19758b6a3a..936af0dced 100644 --- a/packages/core/tests/Unit/Search/CustomerIndexerTest.php +++ b/packages/core/tests/Unit/Search/CustomerIndexerTest.php @@ -14,6 +14,7 @@ /** * @group lunar.search + * @group lunar.search.customer */ class CustomerIndexerTest extends TestCase { diff --git a/packages/core/tests/Unit/Search/OrderIndexerTest.php b/packages/core/tests/Unit/Search/OrderIndexerTest.php new file mode 100644 index 0000000000..ca12c879bd --- /dev/null +++ b/packages/core/tests/Unit/Search/OrderIndexerTest.php @@ -0,0 +1,44 @@ +create([ + 'code' => 'GBP', + 'default' => true, + ]); + + $order = Order::factory()->create([ + 'user_id' => null, + 'placed_at' => now(), + 'meta' => [ + 'foo' => 'bar', + ], + 'tax_breakdown' => [ + ['name' => 'VAT', 'percentage' => 20, 'total' => 200], + ], + ]); + + $data = app(OrderIndexer::class)->toSearchableArray($order); + + $this->assertEquals('GBP', $data['currency_code']); + $this->assertEquals($order->channel->name, $data['channel']); + $this->assertEquals($order->total->value, $data['total']); + } +} diff --git a/packages/core/tests/Unit/Search/ProductIndexerTest.php b/packages/core/tests/Unit/Search/ProductIndexerTest.php index 8abe392c54..c0c18557cc 100644 --- a/packages/core/tests/Unit/Search/ProductIndexerTest.php +++ b/packages/core/tests/Unit/Search/ProductIndexerTest.php @@ -14,6 +14,7 @@ /** * @group lunar.search + * @group lunar.search.product */ class ProductIndexerTest extends TestCase { diff --git a/packages/core/tests/Unit/Search/ProductOptionIndexerTest.php b/packages/core/tests/Unit/Search/ProductOptionIndexerTest.php new file mode 100644 index 0000000000..f583303988 --- /dev/null +++ b/packages/core/tests/Unit/Search/ProductOptionIndexerTest.php @@ -0,0 +1,28 @@ +create(); + + $data = app(ProductOptionIndexer::class)->toSearchableArray($productOption); + + $this->assertEquals($productOption->name->en, $data['name_en']); + $this->assertEquals($productOption->label->en, $data['label_en']); + } +}