Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional Scout Indexers #1252

Merged
merged 7 commits into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/core/config/search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
],
/*
Expand All @@ -39,6 +39,8 @@
'indexers' => [
\Lunar\Models\Brand::class => \Lunar\Search\BrandIndexer::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,
],
];
95 changes: 95 additions & 0 deletions packages/core/src/Search/OrderIndexer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php

namespace Lunar\Search;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class OrderIndexer extends ScoutIndexer
{
public function getSortableFields(): array
{
return [
'customer_id',
'user_id',
'channel_id',
'created_at',
'updated_at',
];
}

public function getFilterableFields(): array
{
return [
'customer_id',
'user_id',
'channel_id',
'__soft_deleted',
];
}

public function makeAllSearchableUsing(Builder $query): Builder
{
return $query->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;
}
}
54 changes: 54 additions & 0 deletions packages/core/src/Search/ProductOptionIndexer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Lunar\Search;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

class ProductOptionIndexer extends ScoutIndexer
{
public function getSortableFields(): array
{
return [
'created_at',
'updated_at',
];
}

public function getFilterableFields(): array
{
return [
'__soft_deleted',
];
}

public function makeAllSearchableUsing(Builder $query): Builder
{
return $query;
}

public function toSearchableArray(Model $model): array
{
$data['id'] = $model->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;
}
}
1 change: 1 addition & 0 deletions packages/core/tests/Unit/Search/BrandIndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

/**
* @group lunar.search
* @group lunar.search.brand
*/
class BrandIndexerTest extends TestCase
{
Expand Down
1 change: 1 addition & 0 deletions packages/core/tests/Unit/Search/CustomerIndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

/**
* @group lunar.search
* @group lunar.search.customer
*/
class CustomerIndexerTest extends TestCase
{
Expand Down
44 changes: 44 additions & 0 deletions packages/core/tests/Unit/Search/OrderIndexerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Lunar\Tests\Unit\Search;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Lunar\Models\Currency;
use Lunar\Models\Order;
use Lunar\Search\OrderIndexer;
use Lunar\Tests\TestCase;

/**
* @group lunar.search
* @group lunar.search.order
*/
class OrderIndexerTest extends TestCase
{
use RefreshDatabase;

/** @test */
public function can_return_correct_searchable_data()
{
Currency::factory()->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']);
}
}
1 change: 1 addition & 0 deletions packages/core/tests/Unit/Search/ProductIndexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

/**
* @group lunar.search
* @group lunar.search.product
*/
class ProductIndexerTest extends TestCase
{
Expand Down
28 changes: 28 additions & 0 deletions packages/core/tests/Unit/Search/ProductOptionIndexerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Lunar\Tests\Unit\Search;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Lunar\Models\ProductOption;
use Lunar\Search\ProductOptionIndexer;
use Lunar\Tests\TestCase;

/**
* @group lunar.search
* @group lunar.search.product_option
*/
class ProductOptionIndexerTest extends TestCase
{
use RefreshDatabase;

/** @test */
public function can_return_correct_searchable_data()
{
$productOption = ProductOption::factory()->create();

$data = app(ProductOptionIndexer::class)->toSearchableArray($productOption);

$this->assertEquals($productOption->name->en, $data['name_en']);
$this->assertEquals($productOption->label->en, $data['label_en']);
}
}
Loading