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

[1.x] Product attribute update #1771

Merged
merged 6 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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' => app(Apps::class),
arfenis marked this conversation as resolved.
Show resolved Hide resolved
'user' => $user,
'company' => $this->product->company,
'name' => $attribute['name'],
'value' => $attribute['value'],
'isVisible' => false,
'isSearchable' => false,
'isFiltrable' => false,
'slug' => Str::slug($attribute['name']),
arfenis marked this conversation as resolved.
Show resolved Hide resolved
]);
$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
Loading