Skip to content

Commit

Permalink
Merge pull request #427 from bakaphp/warehouse-channels-update
Browse files Browse the repository at this point in the history
  • Loading branch information
kaioken authored Jul 19, 2023
2 parents 5688ff5 + 6f8a82a commit e3894bb
Show file tree
Hide file tree
Showing 20 changed files with 620 additions and 208 deletions.
48 changes: 30 additions & 18 deletions app/GraphQL/Inventory/Mutations/Variants/Variants.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Kanvas\Inventory\Variants\DataTransferObject\Variants as VariantDto;
use Kanvas\Inventory\Variants\DataTransferObject\VariantsWarehouses;
use Kanvas\Inventory\Variants\Models\Variants as VariantModel;
use Kanvas\Inventory\Variants\Models\VariantsWarehouses as ModelsVariantsWarehouses;
use Kanvas\Inventory\Variants\Repositories\VariantsRepository;
use Kanvas\Inventory\Warehouses\Repositories\WarehouseRepository;

Expand All @@ -31,7 +32,12 @@ public function create(mixed $root, array $req): VariantModel
{
$variantDto = VariantDto::viaRequest($req['input']);
$action = new CreateVariantsAction($variantDto, auth()->user());
return $action->execute();
$variantModel = $action->execute();

WarehouseRepository::getById($variantDto->warehouse_id, $variantDto->product->company()->get()->first());
$variantModel->warehouses()->attach($variantDto->warehouse_id);

return $variantModel;
}

/**
Expand All @@ -44,7 +50,7 @@ public function create(mixed $root, array $req): VariantModel
*/
public function update(mixed $root, array $req): VariantModel
{
$variant = VariantsRepository::getById($req['id'], auth()->user()->getCurrentCompany());
$variant = VariantsRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany());
$variant->update($req['input']);
return $variant;
}
Expand All @@ -59,9 +65,9 @@ public function update(mixed $root, array $req): VariantModel
*/
public function delete(mixed $root, array $req): bool
{
$variant = VariantsRepository::getById($req['id'], auth()->user()->getCurrentCompany());
$variant = VariantsRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany());

return $variant->delete();
return $variant->softdelete();
}

/**
Expand All @@ -74,7 +80,7 @@ public function delete(mixed $root, array $req): bool
*/
public function addToWarehouse(mixed $root, array $req): VariantModel
{
$variant = VariantsRepository::getById($req['id'], auth()->user()->getCurrentCompany());
$variant = VariantsRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany());

$warehouse = WarehouseRepository::getById($req['warehouse_id']);
$variantWarehouses = VariantsWarehouses::from($req['input']);
Expand All @@ -91,7 +97,7 @@ public function addToWarehouse(mixed $root, array $req): VariantModel
*/
public function removeToWarehouse(mixed $root, array $req): VariantModel
{
$variant = VariantsRepository::getById($req['id'], auth()->user()->getCurrentCompany());
$variant = VariantsRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany());

$warehouse = WarehouseRepository::getById($req['warehouse_id']);
$variant->warehouses()->detach($warehouse);
Expand All @@ -108,9 +114,9 @@ public function removeToWarehouse(mixed $root, array $req): VariantModel
*/
public function addAttribute(mixed $root, array $req): VariantModel
{
$variant = VariantsRepository::getById($req['id'], auth()->user()->getCurrentCompany());
$variant = VariantsRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany());

$attribute = AttributesRepository::getById($req['attributes_id']);
$attribute = AttributesRepository::getById((int) $req['attributes_id']);
(new AddAttributeAction($variant, $attribute, $req['input']['value']))->execute();
return $variant;
}
Expand All @@ -125,9 +131,9 @@ public function addAttribute(mixed $root, array $req): VariantModel
*/
public function removeAttribute(mixed $root, array $req): VariantModel
{
$variant = VariantsRepository::getById($req['id'], auth()->user()->getCurrentCompany());
$variant = VariantsRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany());

$attribute = AttributesRepository::getById($req['attributes_id']);
$attribute = AttributesRepository::getById((int) $req['attributes_id']);
$variant->attributes()->detach($attribute);
return $variant;
}
Expand All @@ -142,12 +148,15 @@ public function removeAttribute(mixed $root, array $req): VariantModel
*/
public function addToChannel(mixed $root, array $req): VariantModel
{
$variant = VariantsRepository::getById($req['id'], auth()->user()->getCurrentCompany());
$variant = VariantsRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany());
$warehouse = WarehouseRepository::getById((int) $req['warehouses_id']);
$variantWarehouses = ModelsVariantsWarehouses::where('products_variants_id', $variant->getId())
->where('warehouses_id', $warehouse->getId())
->firstOrFail();

$warehouse = WarehouseRepository::getById($req['warehouses_id']);
$channel = ChannelRepository::getById($req['channels_id']);
$channel = ChannelRepository::getById((int) $req['channels_id']);
$variantChannel = VariantChannel::from($req['input']);
(new AddVariantToChannel($variant, $channel, $warehouse, $variantChannel))->execute();
(new AddVariantToChannel($variantWarehouses, $channel, $variantChannel))->execute();
return $variant;
}

Expand All @@ -161,10 +170,13 @@ public function addToChannel(mixed $root, array $req): VariantModel
*/
public function removeChannel(mixed $root, array $req): VariantModel
{
$variant = VariantsRepository::getById($req['id'], auth()->user()->getCurrentCompany());

$channel = ChannelRepository::getById($req['channels_id']);
$variant->channels()->detach($channel->id);
$variant = VariantsRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany());
$warehouse = WarehouseRepository::getById((int) $req['warehouses_id']);
$variantWarehouses = ModelsVariantsWarehouses::where('products_variants_id', $variant->getId())
->where('warehouses_id', $warehouse->getId())
->firstOrFail();
$channel = ChannelRepository::getById((int) $req['channels_id']);
$variantWarehouses->channels()->where('id', $channel->getId())->detach($channel->id);
return $variant;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('products_variants_warehouses', function (Blueprint $table) {
Schema::disableForeignKeyConstraints();

$table->dropForeign('products_variants_warehouses_warehouses_id_foreign');
$table->dropForeign('products_variants_warehouses_products_variants_id_foreign');

$table->dropPrimary();
Schema::enableForeignKeyConstraints();
});

Schema::table('products_variants_warehouses', function (Blueprint $table) {
Schema::disableForeignKeyConstraints();
$table->id()->first();
$table->foreign('products_variants_id')->references('id')->on('products_variants');
$table->foreign('warehouses_id')->references('id')->on('warehouses');
$table->unique(['products_variants_id', 'warehouses_id'], 'product_variants_warehouses_unique');
Schema::enableForeignKeyConstraints();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('products_variants_channels', function (Blueprint $table) {
Schema::disableForeignKeyConstraints();
$table->dropForeign('products_variants_channels_products_variants_id_foreign');
$table->dropForeign('products_variants_channels_warehouses_id_foreign');
$table->dropForeign('products_variants_channels_channels_id_foreign');
$table->dropPrimary();
$table->dropColumn(['products_variants_id', 'warehouses_id']);

Schema::enableForeignKeyConstraints();
});
Schema::table('products_variants_channels', function (Blueprint $table) {
Schema::disableForeignKeyConstraints();

$table->bigInteger('product_variants_warehouse_id')->unsigned()->after('channels_id');
$table->foreign('product_variants_warehouse_id')->references('id')->on('products_variants_warehouses');
$table->foreign('channels_id')->references('id')->on('channels');

$table->unique(['product_variants_warehouse_id', 'channels_id'], 'variants_warehouse_channel');
Schema::enableForeignKeyConstraints();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('products_variants_channels', function (Blueprint $table) {
$table->double('price', 8, 2)->default(0.00)->change();
});

Schema::table('products_variants_warehouses', function (Blueprint $table) {
$table->double('price', 8, 2)->default(0.00)->change();
$table->integer('position')->default(0)->change();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('products_variants_channels', function (Blueprint $table) {
$table->double('price', 8, 2)->change();
});

Schema::table('products_variants_warehouses', function (Blueprint $table) {
$table->double('price', 8, 2)->change();
$table->integer('position')->change();
});
}
};
8 changes: 4 additions & 4 deletions graphql/schemas/Inventory/attributes.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ input AttributeUpdateInput {
}

type Attributes {
id: Int!
id: ID!
uuid: String
name: String
created_at: String
Expand All @@ -19,7 +19,7 @@ type Attributes {
}

type AttributesValue {
id: Int!
id: ID!
value: Mixed
}

Expand All @@ -28,11 +28,11 @@ extend type Mutation @guard {
@field(
resolver: "App\\GraphQL\\Inventory\\Mutations\\Attributes\\AttributeMutation@create"
)
updateAttribute(id: Int!, input: AttributeUpdateInput!): Attributes!
updateAttribute(id: ID!, input: AttributeUpdateInput!): Attributes!
@field(
resolver: "App\\GraphQL\\Inventory\\Mutations\\Attributes\\AttributeMutation@update"
)
deleteAttribute(id: Int!): Attributes!
deleteAttribute(id: ID!): Attributes!
@field(
resolver: "App\\GraphQL\\Inventory\\Mutations\\Attributes\\AttributeMutation@delete"
)
Expand Down
2 changes: 1 addition & 1 deletion graphql/schemas/Inventory/region.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Currency {
input RegionInput {
currency_id: Int!
name: String!
slug: String!
slug: String
short_slug: String!
settings: String
is_default: Int!
Expand Down
Loading

0 comments on commit e3894bb

Please sign in to comment.