Skip to content

Commit

Permalink
Merge pull request #1471 from bakaphp/sku-validation
Browse files Browse the repository at this point in the history
[Fix][Improvement] SKU validation
  • Loading branch information
kaioken authored Jun 8, 2024
2 parents 63390c7 + e49c619 commit 6a44c7b
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 5 deletions.
13 changes: 8 additions & 5 deletions app/GraphQL/Inventory/Mutations/Variants/Variants.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
use Kanvas\Inventory\Status\Repositories\StatusRepository;
use Kanvas\Inventory\Variants\Actions\AddAttributeAction;
use Kanvas\Inventory\Variants\Actions\AddToWarehouseAction as AddToWarehouse;
use Kanvas\Inventory\Variants\Actions\AddVariantToChannelAction;
use Kanvas\Inventory\Variants\Actions\CreateVariantsAction;
use Kanvas\Inventory\Variants\Actions\UpdateVariantsAction;
use Kanvas\Inventory\Variants\DataTransferObject\VariantChannel;
use Kanvas\Inventory\Variants\DataTransferObject\Variants as VariantDto;
use Kanvas\Inventory\Variants\DataTransferObject\VariantsWarehouses;
Expand Down Expand Up @@ -89,6 +89,7 @@ public function create(mixed $root, array $req): VariantModel
);
}
}

return $variantModel;
}

Expand All @@ -103,17 +104,19 @@ public function update(mixed $root, array $req): VariantModel
}

$variant = VariantsRepository::getById((int) $req['id'], $company);
$variant->update($req['input']);
$req['input']['products_id'] = $variant->product->getId();
$variantDto = VariantDto::viaRequest($req['input'], auth()->user());
$variantModel = (new UpdateVariantsAction($variant, $variantDto, auth()->user()))->execute();

if (isset($req['input']['attributes'])) {
$variant->addAttributes(auth()->user(), $req['input']['attributes']);
$variantModel->addAttributes(auth()->user(), $req['input']['attributes']);
}

if (isset($req['input']['warehouses'])) {
WarehouseService::updateWarehouseVariant($variant, auth()->user(), $req['input']['warehouses']);
WarehouseService::updateWarehouseVariant($variantModel, auth()->user(), $req['input']['warehouses']);
}

return $variant;
return $variantModel;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/Domains/Inventory/Variants/Actions/CreateVariantsAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

use Baka\Support\Str;
use Baka\Users\Contracts\UserInterface;
use Illuminate\Support\Facades\Validator;
use Kanvas\Companies\Repositories\CompaniesRepository;
use Kanvas\Exceptions\ValidationException;
use Kanvas\Inventory\Variants\DataTransferObject\Variants as VariantsDto;
use Kanvas\Inventory\Variants\Models\Variants;
use Kanvas\Inventory\Variants\Validations\UniqueSkuRule;

class CreateVariantsAction
{
Expand All @@ -31,6 +34,15 @@ public function execute(): Variants
$this->user
);

$validator = Validator::make(
['sku' => $this->variantDto->sku],
['sku' => new UniqueSkuRule($this->variantDto->product->app, $this->variantDto->product->company)]
);

if ($validator->fails()) {
throw new ValidationException($validator->messages()->__toString());
}

$search = [
'products_id' => $this->variantDto->product->getId(),
'slug' => $this->variantDto->slug ?? Str::slug($this->variantDto->name),
Expand Down
66 changes: 66 additions & 0 deletions src/Domains/Inventory/Variants/Actions/UpdateVariantsAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

declare(strict_types=1);

namespace Kanvas\Inventory\Variants\Actions;

use Baka\Support\Str;
use Baka\Users\Contracts\UserInterface;
use Illuminate\Support\Facades\Validator;
use Kanvas\Companies\Repositories\CompaniesRepository;
use Kanvas\Exceptions\ValidationException;
use Kanvas\Inventory\Variants\DataTransferObject\Variants as VariantsDto;
use Kanvas\Inventory\Variants\Models\Variants;
use Kanvas\Inventory\Variants\Validations\UniqueSkuRule;

class UpdateVariantsAction
{
/**
* __construct.
*/
public function __construct(
protected Variants $variant,
protected VariantsDto $variantDto,
protected UserInterface $user
) {
}

/**
* execute.
*/
public function execute(): Variants
{
CompaniesRepository::userAssociatedToCompany(
$this->variantDto->product->company()->get()->first(),
$this->user
);

$validator = Validator::make(
['sku' => $this->variantDto->sku],
['sku' => new UniqueSkuRule($this->variant->app, $this->variant->company, $this->variant)]
);

if ($validator->fails()) {
throw new ValidationException($validator->messages()->__toString());
}

$this->variant->update(
[
'name' => $this->variantDto->name,
'slug' => $this->variantDto->slug ?? Str::slug($this->variantDto->name),
'sku' => $this->variantDto->sku,
'users_id' => $this->user->getId(),
'description' => $this->variantDto->description,
'short_description' => $this->variantDto->short_description,
'html_description' => $this->variantDto->html_description,
'status_id' => $this->variantDto->status_id,
'ean' => $this->variantDto->ean,
'barcode' => $this->variantDto->barcode,
'serial_number' => $this->variantDto->serial_number,

]
);

return $this->variant;
}
}
36 changes: 36 additions & 0 deletions src/Domains/Inventory/Variants/Validations/UniqueSkuRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Kanvas\Inventory\Variants\Validations;

use Baka\Contracts\AppInterface;
use Baka\Contracts\CompanyInterface;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
use Kanvas\Inventory\Variants\Models\Variants;

class UniqueSkuRule implements ValidationRule
{
public function __construct(
protected AppInterface $app,
protected CompanyInterface $company,
protected ?Variants $variant = null
) {
}

public function validate(string $attribute, mixed $value, Closure $fail): void
{
$query = Variants::where('sku', $value)
->fromCompany($this->company)
->fromApp($this->app);

if ($this->variant) {
$query->where('id', '!=', $this->variant->getId());
}

if ($query->exists()) {
$fail("The $attribute has already been taken.");
}
}
}
2 changes: 2 additions & 0 deletions tests/GraphQL/Inventory/VariantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,15 @@ public function testUpdateVariant(): void
$data = [
'name' => fake()->name,
'description' => fake()->text,
'sku' => $data['sku']
];
$this->graphQL('
mutation($id: ID! $data: VariantsUpdateInput!) {
updateVariant(id: $id, input: $data)
{
id
name
sku
description
}
}', ['id' => $id, 'data' => $data])->assertJson([
Expand Down

0 comments on commit 6a44c7b

Please sign in to comment.