From d9e3681b16ea16a46a8784716665885bf6e93e74 Mon Sep 17 00:00:00 2001 From: arfenis Date: Tue, 9 Jan 2024 23:39:57 -0400 Subject: [PATCH 1/4] Allow to search by channel on where --- .../Variants/VariantChannelBuilder.php | 20 +++++++++++++++++++ graphql/schemas/Inventory/variant.graphql | 7 +++++++ .../Inventory/Variants/Models/Variants.php | 2 +- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/app/GraphQL/Inventory/Builders/Variants/VariantChannelBuilder.php b/app/GraphQL/Inventory/Builders/Variants/VariantChannelBuilder.php index f49e00b23c..3a590c3459 100644 --- a/app/GraphQL/Inventory/Builders/Variants/VariantChannelBuilder.php +++ b/app/GraphQL/Inventory/Builders/Variants/VariantChannelBuilder.php @@ -6,6 +6,7 @@ use GraphQL\Type\Definition\ResolveInfo; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\Collection; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Support\Facades\DB; use Kanvas\Inventory\Channels\Models\Channels; @@ -74,4 +75,23 @@ public function getChannel(mixed $root, array $req): array 'is_published' => $root->is_published, ]; } + + /** + * Get filter variant by channel + * @todo Improve this to only get the UUID's one channel + * @param mixed $root + * @param array $req + * @return Collection + */ + public function getHasChannel(mixed $root, array $req): Collection + { + $channelUuid = $req['HAS']['condition']['value']; + $collection = $root->get(); + + foreach($collection as $data) + { + $data->channels = $data->channels->where('uuid', $channelUuid); + } + return $collection; + } } diff --git a/graphql/schemas/Inventory/variant.graphql b/graphql/schemas/Inventory/variant.graphql index 6087e7b4c6..343255097d 100644 --- a/graphql/schemas/Inventory/variant.graphql +++ b/graphql/schemas/Inventory/variant.graphql @@ -24,6 +24,7 @@ type Variant { @hasMany(relation: "variantWarehouses") attributes: [VariantsAttributes!]! companies: Company + channels: [VariantChannelRelationship] } input VariantsAttributesInput { @@ -119,6 +120,12 @@ extend type Query @guard { relation: "attributeValues" columns: ["products_variants_id", "value", "attributes_id"] ) + hasChannel: _ + @whereHasConditions( + relation: "channels" + columns: ["uuid"] + handler: "App\\GraphQL\\Inventory\\Builders\\Variants\\VariantChannelBuilder@getHasChannel" + ) orderBy: _ @orderBy( columns: [ diff --git a/src/Domains/Inventory/Variants/Models/Variants.php b/src/Domains/Inventory/Variants/Models/Variants.php index 9e6f7e4073..af54d686de 100644 --- a/src/Domains/Inventory/Variants/Models/Variants.php +++ b/src/Domains/Inventory/Variants/Models/Variants.php @@ -171,7 +171,7 @@ public function channels(): BelongsToMany 'products_variants_id', 'channels_id' ) - ->withPivot('price', 'discounted_price', 'is_published'); + ->withPivot('price', 'discounted_price', 'is_published', 'warehouses_id'); } /** From 60f340644c8e20e992f727dada6924d72f686996 Mon Sep 17 00:00:00 2001 From: arfenis Date: Wed, 10 Jan 2024 15:11:02 -0400 Subject: [PATCH 2/4] use eager loader --- .../Builders/Variants/VariantChannelBuilder.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/app/GraphQL/Inventory/Builders/Variants/VariantChannelBuilder.php b/app/GraphQL/Inventory/Builders/Variants/VariantChannelBuilder.php index 3a590c3459..9576258d02 100644 --- a/app/GraphQL/Inventory/Builders/Variants/VariantChannelBuilder.php +++ b/app/GraphQL/Inventory/Builders/Variants/VariantChannelBuilder.php @@ -78,20 +78,20 @@ public function getChannel(mixed $root, array $req): array /** * Get filter variant by channel - * @todo Improve this to only get the UUID's one channel + * * @param mixed $root * @param array $req * @return Collection */ public function getHasChannel(mixed $root, array $req): Collection { + if (empty($req['HAS']['condition']['value'])) { + return collect(); + } $channelUuid = $req['HAS']['condition']['value']; - $collection = $root->get(); - foreach($collection as $data) - { - $data->channels = $data->channels->where('uuid', $channelUuid); - } - return $collection; + return $root->with(['channels' => function ($query) use ($channelUuid) { + $query->where('uuid', $channelUuid); + }])->get(); } } From c536d1e92d549578d5f36417a1dd46c362e9f363 Mon Sep 17 00:00:00 2001 From: arfenis Date: Wed, 10 Jan 2024 15:12:11 -0400 Subject: [PATCH 3/4] stylo --- .../Inventory/Builders/Variants/VariantChannelBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/GraphQL/Inventory/Builders/Variants/VariantChannelBuilder.php b/app/GraphQL/Inventory/Builders/Variants/VariantChannelBuilder.php index 9576258d02..0b9d5c0e74 100644 --- a/app/GraphQL/Inventory/Builders/Variants/VariantChannelBuilder.php +++ b/app/GraphQL/Inventory/Builders/Variants/VariantChannelBuilder.php @@ -78,7 +78,7 @@ public function getChannel(mixed $root, array $req): array /** * Get filter variant by channel - * + * * @param mixed $root * @param array $req * @return Collection From 26a49981e1522c5b6c8ffbd38098f82e788e1816 Mon Sep 17 00:00:00 2001 From: arfenis Date: Wed, 10 Jan 2024 15:33:59 -0400 Subject: [PATCH 4/4] not null return --- graphql/schemas/Inventory/variant.graphql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphql/schemas/Inventory/variant.graphql b/graphql/schemas/Inventory/variant.graphql index 343255097d..7b71af57f7 100644 --- a/graphql/schemas/Inventory/variant.graphql +++ b/graphql/schemas/Inventory/variant.graphql @@ -24,7 +24,7 @@ type Variant { @hasMany(relation: "variantWarehouses") attributes: [VariantsAttributes!]! companies: Company - channels: [VariantChannelRelationship] + channels: [VariantChannelRelationship!]! } input VariantsAttributesInput {