Skip to content

Commit

Permalink
Merge pull request #1405 from bakaphp/product-category-multi-update
Browse files Browse the repository at this point in the history
[1.x] Product category multi update
  • Loading branch information
kaioken authored May 29, 2024
2 parents 3252299 + f1a1122 commit 35529fa
Show file tree
Hide file tree
Showing 9 changed files with 165 additions and 6 deletions.
1 change: 1 addition & 0 deletions graphql/schemas/Inventory/product.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ input ProductInputUpdate {
status: StatusReferenceInput
attributes: [ProductAttributesInput!]
files: [FilesystemInputUrl!]
categories: [ProductCategoriesReferenceInput!]
}

extend type Mutation @guard {
Expand Down
2 changes: 1 addition & 1 deletion graphql/schemas/Inventory/variant.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ input VariantsUpdateInput {
attributes: [VariantsAttributesInput!]
serial_number: String
is_published: Boolean
warehouses: [VariantsWarehousesInput!]
warehouses: [WarehouseReferenceInput!]
}

extend type Mutation @guard {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ class ProductsCategoriesObserver
{
public function saved(ProductsCategories $productsCategories): void
{
$productsCategories->set(
$productsCategories->category->set(
'total_products',
$productsCategories->categories->getTotalProducts()
$productsCategories->category->getTotalProducts()
);
}
}
28 changes: 28 additions & 0 deletions src/Domains/Inventory/Products/Actions/AddCategoryAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Kanvas\Inventory\Products\Actions;

use Kanvas\Inventory\Categories\Models\Categories;
use Kanvas\Inventory\Products\Models\Products;
use Kanvas\Inventory\Products\Models\ProductsCategories;

class AddCategoryAction
{
public function __construct(
protected Products $product,
protected Categories $category
) {
}

public function execute(): void
{
ProductsCategories::firstOrCreate(
[
'categories_id' => $this->category->getId(),
'products_id' => $this->product->getId(),
]
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ public function execute(): Products
if ($this->productDto->categories) {
foreach ($this->productDto->categories as $category) {
$category = CategoriesRepository::getById((int) $category['id'], $this->productDto->company);
$products->categories()->attach($category);
(new AddCategoryAction(
$products,
$category
))->execute();
}
}

Expand Down
12 changes: 12 additions & 0 deletions src/Domains/Inventory/Products/Actions/UpdateProductAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Kanvas\Companies\Repositories\CompaniesRepository;
use Kanvas\Inventory\Categories\Repositories\CategoriesRepository;
use Kanvas\Inventory\Products\DataTransferObject\Product as ProductDto;
use Kanvas\Inventory\Products\Models\Products;
use Throwable;
Expand Down Expand Up @@ -61,6 +62,17 @@ public function execute(): Products
$this->product->addMultipleFilesFromUrl($this->productDto->files);
}

if (! empty($this->productDto->categories)) {
$this->product->productsCategories()->forceDelete();

foreach ($this->productDto->categories as $category) {
(new AddCategoryAction(
$this->product,
CategoriesRepository::getById((int) $category['id'], $this->product->company)
))->execute();
}
}

DB::connection('inventory')->commit();
} catch (Throwable $e) {
DB::connection('inventory')->rollback();
Expand Down
2 changes: 1 addition & 1 deletion src/Domains/Inventory/Products/Models/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function categories(): BelongsToMany
{
return $this->belongsToMany(
Categories::class,
'products_categories',
ProductsCategories::class,
'products_id',
'categories_id'
);
Expand Down
6 changes: 5 additions & 1 deletion src/Domains/Inventory/Products/Models/ProductsCategories.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

use Awobaz\Compoships\Compoships;
use Baka\Traits\HasCompositePrimaryKeyTrait;
use Baka\Traits\NoAppRelationshipTrait;
use Baka\Traits\NoCompanyRelationshipTrait;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Kanvas\Inventory\Categories\Models\Categories;
use Kanvas\Inventory\Models\BaseModel;
Expand All @@ -22,10 +24,12 @@
class ProductsCategories extends BaseModel
{
use HasCompositePrimaryKeyTrait;
use NoAppRelationshipTrait;
use NoCompanyRelationshipTrait;
use Compoships;

protected $table = 'products_categories';
protected $guarded = [
protected $fillable = [
'products_id',
'categories_id'
];
Expand Down
111 changes: 111 additions & 0 deletions tests/GraphQL/Inventory/ProductCategoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

declare(strict_types=1);

namespace Tests\GraphQL\Inventory;

use Tests\TestCase;

class ProductCategoryTest extends TestCase
{
public function testAddProductCategoryTest(): void
{
$dataRegion = [
'name' => 'Test Region',
'slug' => 'test-region',
'short_slug' => 'test-region',
'is_default' => 1,
'currency_id' => 1,
];
$response = $this->graphQL('
mutation($data: RegionInput!) {
createRegion(input: $data)
{
id
name
slug
short_slug
currency_id
is_default
}
}', ['data' => $dataRegion])
->assertJson([
'data' => ['createRegion' => $dataRegion]
]);
$idRegion = $response->json()['data']['createRegion']['id'];
$data = [
'regions_id' => $idRegion,
'name' => 'Test Warehouse',
'location' => 'Test Location',
'is_default' => true,
'is_published' => true,
];

$response = $this->graphQL('
mutation($data: WarehouseInput!) {
createWarehouse(input: $data)
{
id
regions_id
name
location
is_default
is_published
}
}', ['data' => $data])->assertJson([
'data' => ['createWarehouse' => $data]
]);

$categoryData = [
'name' => fake()->name,
'code' => fake()->name,
'position' => 1,
'is_published' => true,
'weight' => 0
];
$categoryResponse = $this->graphQL('
mutation($data: CategoryInput!) {
createCategory(input: $data)
{
id
name,
code,
position,
is_published
weight
}
}', ['data' => $categoryData])->assertJson([
'data' => ['createCategory' => $categoryData]
]);
$idCategory = $categoryResponse->json()['data']['createCategory']['id'];

$data = [
'name' => fake()->name,
'sku' => fake()->time,
'description' => fake()->text,
'categories' => [
[
'id' => $idCategory
]
]
];

$response = $this->graphQL('
mutation($data: ProductInput!) {
createProduct(input: $data)
{
id
name
description
categories{
id
}
}
}', ['data' => $data]);

unset($data['sku']);
$response->assertJson([
'data' => ['createProduct' => $data],
]);
}
}

0 comments on commit 35529fa

Please sign in to comment.