Skip to content

Commit

Permalink
Merge branch '0.7' into chore/merge_07_to_08
Browse files Browse the repository at this point in the history
  • Loading branch information
alecritson committed Dec 22, 2023
2 parents 55b4529 + 7f41cf6 commit f678cc9
Show file tree
Hide file tree
Showing 53 changed files with 473 additions and 555 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/bug-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name: "\U0001F41B Bug report"
about: Report something that's broken.
title: ''
labels: bug
labels: bug,unconfirmed
assignees: ''

---
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ This repository serves as a monorepo for the main packages that make up Lunar.
## Community

- [Join our discord server](https://discord.gg/v6qVWaf) and chat to the developers and people using Lunar.
- [We have a roadmap](https://github.com/orgs/lunarphp/projects/1) where we will be detailing which features are next.
- [We have a roadmap](https://github.com/orgs/lunarphp/projects/8) where we will be detailing which features are next.

## Packages in this monorepo

Expand Down
8 changes: 6 additions & 2 deletions packages/admin/src/AdminHubServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,12 @@ public function boot()
$this->registerPublishables();
$this->registerStateListeners();

Route::bind('product', function ($id) {
return Product::withTrashed()->findOrFail($id);
Route::bind('product', function (mixed $value, \Illuminate\Routing\Route $route) {
if (in_array(\Lunar\Hub\Http\Middleware\Authenticate::class, $route->middleware())) {
return Product::withTrashed()->findOrFail($value);
}

return $value;
});

// Commands
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ abstract class AbstractDiscount extends Component
* @var array
*/
public Collection $selectedProducts;

/**
* The product variants to restrict the coupon for.
*
Expand Down Expand Up @@ -122,32 +122,29 @@ public function mount()
$this->selectedProducts = $this->discount->purchasables()
->whereIn('type', ['limitation', 'exclusion'])
->wherePurchasableType(Product::class)
->whereHas('purchasable')
->get()
->map(function ($limitation) {
return array_merge($this->mapProductToArray($limitation->purchasable), ['type' => $limitation->type]);
});

$this->selectedProductVariants = $this->discount->purchasables()
->whereIn('type', ['limitation', 'exclusion'])
->wherePurchasableType(ProductVariant::class)
->whereHas('purchasable')
->get()
->map(function ($limitation) {
return array_merge($this->mapProductVariantToArray($limitation->purchasable), ['type' => $limitation->type]);
});

$this->selectedProductVariants = $this->discount->purchasableLimitations()
->wherePurchasableType(ProductVariant::class)
->get()
->map(function ($limitation) {
return $this->mapProductVariantToArray($limitation->purchasable);
});

$this->selectedConditions = $this->discount->purchasableConditions()
->wherePurchasableType(Product::class)
->whereHas('purchasable')
->pluck('purchasable_id')->values()->toArray();

$this->selectedRewards = $this->discount->purchasableRewards()
->wherePurchasableType(Product::class)
->whereHas('purchasable')
->pluck('purchasable_id')->values()->toArray();

$this->syncAvailability();
Expand Down Expand Up @@ -258,7 +255,7 @@ public function selectProducts(array $ids)
? $this->selectedProducts->merge($selectedProducts)
: $selectedProducts;
}

/**
* Select product variants given an array of IDs
*
Expand Down Expand Up @@ -345,7 +342,7 @@ public function removeProduct($index)
{
$this->selectedProducts->forget($index);
}

/**
* Remove the product variant by it's index.
*
Expand Down Expand Up @@ -414,7 +411,7 @@ public function save()
$this->discount->collections()->sync(
$this->selectedCollections->mapWithKeys(fn ($collection) => [$collection['id'] => ['type' => $collection['type']]])
);

$this->discount->purchasables()
->whereIn('type', ['exclusion', 'limitation'])
->where('purchasable_type', Product::class)
Expand All @@ -434,7 +431,7 @@ public function save()
])
->save();
}

$this->discount->purchasables()
->whereIn('type', ['exclusion', 'limitation'])
->where('purchasable_type', ProductVariant::class)
Expand All @@ -454,20 +451,6 @@ public function save()
])
->save();
}

$this->discount->purchasableLimitations()
->where('purchasable_type', ProductVariant::class)
->whereNotIn('purchasable_id', $this->selectedProductVariants->pluck('id'))
->delete();

foreach ($this->selectedProductVariants as $variant) {
$this->discount->purchasableLimitations()->firstOrCreate([
'discount_id' => $this->discount->id,
'type' => 'limitation',
'purchasable_type' => ProductVariant::class,
'purchasable_id' => $variant['id'],
]);
}
});

$this->emit('discount.saved', $this->discount->id);
Expand Down Expand Up @@ -579,7 +562,7 @@ private function mapProductToArray($product)
'thumbnail' => optional($product->thumbnail)->getUrl('small'),
];
}

/**
* Return the data we need from a product variant
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,40 @@ public function can_view_page_with_correct_permission()
->assertSeeLivewire('hub.components.products.show');
}

/** @test */
public function cant_view_soft_deleted_products()
{
$this->setupRolesPermissions();

$staff = Staff::factory()->create([
'admin' => false,
]);

$staff->givePermissionTo('catalogue:manage-products');

$this->actingAs($staff, 'staff');

$product = Product::factory()->create([
'deleted_at' => now(),
]);

$variant = ProductVariant::factory()->create([
'product_id' => $product->id,
]);

foreach (Currency::get() as $currency) {
Price::factory()->create([
'priceable_type' => ProductVariant::class,
'priceable_id' => $variant->id,
'currency_id' => $currency->id,
'tier' => 1,
]);
}

$this->get(route('hub.products.show', $product->id))
->assertSeeLivewire('hub.components.products.show');
}

/** @test */
public function product_with_one_variant_has_variant_components_visible()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Lunar\Hub\Tests\Unit\Http\Livewire\Components\Settings\Product;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Lunar\Hub\Http\Livewire\Components\Settings\Product\Options\OptionsIndex;
use Lunar\Hub\Models\Staff;
use Lunar\Hub\Tests\TestCase;
use Lunar\Models\Language;
use Lunar\Models\ProductOption;

class ProductOptionCreateTest extends TestCase
{
use RefreshDatabase;

public function setUp(): void
{
parent::setUp();

Language::factory()->create([
'default' => true,
'code' => 'en',
]);
}

/**
* @test
* */
public function can_populate_product_option_data()
{
$staff = Staff::factory()->create([
'admin' => true,
]);

LiveWire::actingAs($staff, 'staff')
->test(OptionsIndex::class)
->set('newProductOption.name.' . Language::getDefault()->code, 'Size')
->call('createOption');

$this->assertDatabaseHas((new ProductOption())->getTable(), [
'name' => json_encode([Language::getDefault()->code => 'Size']),
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Lunar\Hub\Tests\Unit\Http\Livewire\Components\Settings\Product;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Lunar\Hub\Http\Livewire\Components\Settings\Product\Options\OptionEdit;
use Lunar\Hub\Models\Staff;
use Lunar\Hub\Tests\TestCase;
use Lunar\Models\Language;
use Lunar\Models\ProductOption;
use Lunar\Models\ProductOptionValue;

class ProductOptionValueCreateTest extends TestCase
{
use RefreshDatabase;

public function setUp(): void
{
parent::setUp();

Language::factory()->create([
'default' => true,
'code' => 'en',
]);
}

/**
* @test
* */
public function can_populate_product_option_data()
{
ProductOption::factory(1)->create()->each(function ($option) {
$staff = Staff::factory()->create([
'admin' => true,
]);

LiveWire::actingAs($staff, 'staff')
->test(OptionEdit::class, ['productOption' => $option])
->set('newProductOptionValue.name.' . Language::getDefault()->code, 'Size')
->call('createOptionValue');

$this->assertDatabaseHas((new ProductOptionValue())->getTable(), [
'product_option_id' => $option->id,
'name' => json_encode([Language::getDefault()->code => 'Size'])
]);
});
}
}
88 changes: 46 additions & 42 deletions packages/core/database/state/ConvertTaxbreakdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,49 +14,20 @@ public function prepare()

public function run()
{
$prefix = config('lunar.database.table_prefix');
$updateTime = now();

if ($this->canRunOnOrders()) {
DB::table("{$prefix}orders")
->whereJsonContainsKey("${prefix}orders.tax_breakdown->[0]->total")
->orderBy('id')
->chunk(500, function ($rows) use ($prefix, $updateTime) {
foreach ($rows as $row) {
$originalBreakdown = json_decode($row->tax_breakdown, true);

DB::table("{$prefix}orders")->where('id', '=', $row->id)->update([
'tax_breakdown' => collect($originalBreakdown)->map(function ($breakdown) use ($row) {
return [
'description' => $breakdown['description'],
'identifier' => $breakdown['identifier'] ?? $breakdown['description'],
'percentage' => $breakdown['percentage'],
'value' => $breakdown['total'],
'currency_code' => $row->currency_code,
];
})->toJson(),
'updated_at' => $updateTime,
]);
}
});
}

if ($this->canRunOnOrderLines()) {
DB::table("{$prefix}order_lines")
->whereJsonContainsKey("${prefix}order_lines.tax_breakdown->[0]->total")
->orderBy("${prefix}order_lines.id")
->select(
"${prefix}order_lines.id",
"${prefix}order_lines.tax_breakdown",
"${prefix}orders.currency_code",
)
->join("${prefix}orders", "${prefix}order_lines.order_id", '=', "${prefix}orders.id")
->chunk(500, function ($rows) use ($prefix, $updateTime) {
DB::transaction(function () use ($prefix, $updateTime, $rows) {
DB::usingConnection(config('lunar.database.connection') ?: DB::getDefaultConnection(), function () {

$prefix = config('lunar.database.table_prefix');
$updateTime = now();

if ($this->canRunOnOrders()) {
DB::table("{$prefix}orders")
->whereJsonContainsKey("{$prefix}orders.tax_breakdown->[0]->total")
->orderBy('id')
->chunk(500, function ($rows) use ($prefix, $updateTime) {
foreach ($rows as $row) {
$originalBreakdown = json_decode($row->tax_breakdown, true);

DB::table("{$prefix}order_lines")->where('id', '=', $row->id)->update([
DB::table("{$prefix}orders")->where('id', '=', $row->id)->update([
'tax_breakdown' => collect($originalBreakdown)->map(function ($breakdown) use ($row) {
return [
'description' => $breakdown['description'],
Expand All @@ -70,8 +41,41 @@ public function run()
]);
}
});
});
}
}

if ($this->canRunOnOrderLines()) {
DB::table("{$prefix}order_lines")
->whereJsonContainsKey("{$prefix}order_lines.tax_breakdown->[0]->total")
->orderBy("{$prefix}order_lines.id")
->select(
"{$prefix}order_lines.id",
"{$prefix}order_lines.tax_breakdown",
"{$prefix}orders.currency_code",
)
->join("{$prefix}orders", "{$prefix}order_lines.order_id", '=', "{$prefix}orders.id")
->chunk(500, function ($rows) use ($prefix, $updateTime) {
DB::transaction(function () use ($prefix, $updateTime, $rows) {
foreach ($rows as $row) {
$originalBreakdown = json_decode($row->tax_breakdown, true);

DB::table("{$prefix}order_lines")->where('id', '=', $row->id)->update([
'tax_breakdown' => collect($originalBreakdown)->map(function ($breakdown) use ($row) {
return [
'description' => $breakdown['description'],
'identifier' => $breakdown['identifier'] ?? $breakdown['description'],
'percentage' => $breakdown['percentage'],
'value' => $breakdown['total'],
'currency_code' => $row->currency_code,
];
})->toJson(),
'updated_at' => $updateTime,
]);
}
});
});
}

});
}

protected function canRunOnOrders()
Expand Down
Loading

0 comments on commit f678cc9

Please sign in to comment.