Skip to content

Commit

Permalink
Merge pull request #1771 from bakaphp/product-attribute-update
Browse files Browse the repository at this point in the history
[1.x] Product attribute update
  • Loading branch information
arfenis committed Aug 2, 2024
2 parents c62a00c + 5bed7c9 commit 2c19eb0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/GraphQL/Inventory/Mutations/Products/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public function update(mixed $root, array $req): ProductsModel

$product = ProductsRepository::getById((int) $req['id'], $company);
$productDto = ProductDto::viaRequest($req['input'], $product->company);
$action = new UpdateProductAction($product, $productDto, auth()->user());
$productModel = (new UpdateProductAction($product, $productDto, auth()->user()))->execute();

return $action->execute();
return $productModel;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public function execute(): Products
}
}

if ($this->productDto->attributes || empty($this->productDto->attributes)) {
$this->product->attributeValues()->forceDelete();
$this->product->addAttributes($this->user, $this->productDto->attributes);
}

DB::connection('inventory')->commit();
} catch (Throwable $e) {
DB::connection('inventory')->rollback();
Expand Down
38 changes: 38 additions & 0 deletions src/Domains/Inventory/Products/Models/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Awobaz\Compoships\Compoships;
use Baka\Traits\HasLightHouseCache;
use Baka\Support\Str;
use Baka\Traits\SlugTrait;
use Baka\Traits\UuidTrait;
use Baka\Users\Contracts\UserInterface;
Expand All @@ -16,6 +17,9 @@
use Kanvas\Apps\Models\Apps;
use Kanvas\Connectors\Shopify\Traits\HasShopifyCustomField;
use Kanvas\Inventory\Attributes\Models\Attributes;
use Kanvas\Inventory\Attributes\DataTransferObject\Attributes as AttributesDto;
use Kanvas\Inventory\Attributes\Actions\CreateAttribute;
use Kanvas\Inventory\Products\Actions\AddAttributeAction;
use Kanvas\Inventory\Categories\Models\Categories;
use Kanvas\Inventory\Models\BaseModel;
use Kanvas\Inventory\Products\Factories\ProductFactory;
Expand Down Expand Up @@ -250,4 +254,38 @@ public static function newFactory()
{
return new ProductFactory();
}

/**
* Add/create new attributes from a product.
* @psalm-suppress MixedAssignment
* @psalm-suppress MixedArrayAccess
* @psalm-suppress MixedPropertyFetch
*/
public function addAttributes(UserInterface $user, array $attributes): void
{
foreach ($attributes as $attribute) {
if (empty($attribute['value'])) {
continue;
}

if (isset($attribute['id'])) {
$attributeModel = Attributes::getById((int) $attribute['id'], $this->app);
} else {
$attributesDto = AttributesDto::from([
'app' => $this->app,
'user' => $user,
'company' => $this->product->company,
'name' => $attribute['name'],
'value' => $attribute['value'],
'isVisible' => false,
'isSearchable' => false,
'isFiltrable' => false,
'slug' => Str::slug($attribute['name']),
]);
$attributeModel = (new CreateAttribute($attributesDto, $user))->execute();
}

(new AddAttributeAction($this, $attributeModel, $attribute['value']))->execute();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class ProductsAttributes extends BaseModel
use HasCompositePrimaryKeyTrait;

protected $table = 'products_attributes';
protected $forceDeleting = true;
protected $guarded = [
'products_id',
'attributes_id',
Expand Down

0 comments on commit 2c19eb0

Please sign in to comment.