diff --git a/app/GraphQL/Inventory/Mutations/Products/Products.php b/app/GraphQL/Inventory/Mutations/Products/Products.php index a706d5413..8f3226c8b 100644 --- a/app/GraphQL/Inventory/Mutations/Products/Products.php +++ b/app/GraphQL/Inventory/Mutations/Products/Products.php @@ -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; } /** diff --git a/src/Domains/Inventory/Products/Actions/UpdateProductAction.php b/src/Domains/Inventory/Products/Actions/UpdateProductAction.php index ef8e7a569..6083abfe9 100644 --- a/src/Domains/Inventory/Products/Actions/UpdateProductAction.php +++ b/src/Domains/Inventory/Products/Actions/UpdateProductAction.php @@ -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(); diff --git a/src/Domains/Inventory/Products/Models/Products.php b/src/Domains/Inventory/Products/Models/Products.php index 4027cf421..707f2e437 100644 --- a/src/Domains/Inventory/Products/Models/Products.php +++ b/src/Domains/Inventory/Products/Models/Products.php @@ -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; @@ -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; @@ -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(); + } + } } diff --git a/src/Domains/Inventory/Products/Models/ProductsAttributes.php b/src/Domains/Inventory/Products/Models/ProductsAttributes.php index 5a310e168..f2c0af40d 100644 --- a/src/Domains/Inventory/Products/Models/ProductsAttributes.php +++ b/src/Domains/Inventory/Products/Models/ProductsAttributes.php @@ -25,6 +25,7 @@ class ProductsAttributes extends BaseModel use HasCompositePrimaryKeyTrait; protected $table = 'products_attributes'; + protected $forceDeleting = true; protected $guarded = [ 'products_id', 'attributes_id',