From 5059e3b2d569b529617be3bcba9686b356ffa638 Mon Sep 17 00:00:00 2001 From: kaioken Date: Thu, 21 Dec 2023 07:23:08 -0400 Subject: [PATCH] refact: update VariantChannelBuilder and Variants models, add channel functionality --- .../Variants/VariantChannelBuilder.php | 15 ++++++++++ .../Inventory/Variants/Models/Variants.php | 29 +++++++++++++++++++ .../Social/Channels/Models/Channel.php | 2 +- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/app/GraphQL/Inventory/Builders/Variants/VariantChannelBuilder.php b/app/GraphQL/Inventory/Builders/Variants/VariantChannelBuilder.php index ee60431447..f49e00b23c 100644 --- a/app/GraphQL/Inventory/Builders/Variants/VariantChannelBuilder.php +++ b/app/GraphQL/Inventory/Builders/Variants/VariantChannelBuilder.php @@ -6,11 +6,13 @@ use GraphQL\Type\Definition\ResolveInfo; use Illuminate\Database\Eloquent\Builder; +use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Support\Facades\DB; use Kanvas\Inventory\Channels\Models\Channels; use Kanvas\Inventory\Variants\Models\Variants as ModelsVariants; use Kanvas\Inventory\Variants\Models\VariantsChannels; use Nuwave\Lighthouse\Support\Contracts\GraphQLContext; +use stdClass; class VariantChannelBuilder { @@ -50,6 +52,19 @@ public function allVariantsPublishedInChannel( */ public function getChannel(mixed $root, array $req): array { + //@todo send the channel via header + if (! isset($root->channel_name)) { + try { + $defaultChannelVariant = $root->getPriceInfoFromDefaultChannel(); + $root = new stdClass(); + $root->channel_name = $defaultChannelVariant->name; + $root->price = $defaultChannelVariant->pivot->price; + $root->discounted_price = $defaultChannelVariant->pivot->discounted_price; + $root->is_published = $defaultChannelVariant->pivot->is_published; + } catch(ModelNotFoundException $e) { + } + } + //@todo doesnt work with search return [ 'name' => $root->channel_name, diff --git a/src/Domains/Inventory/Variants/Models/Variants.php b/src/Domains/Inventory/Variants/Models/Variants.php index cbd31c201b..b9bb9a87ed 100644 --- a/src/Domains/Inventory/Variants/Models/Variants.php +++ b/src/Domains/Inventory/Variants/Models/Variants.php @@ -4,6 +4,7 @@ namespace Kanvas\Inventory\Variants\Models; +use Baka\Enums\StateEnums; use Baka\Traits\SlugTrait; use Baka\Traits\UuidTrait; use Baka\Users\Contracts\UserInterface; @@ -15,6 +16,7 @@ use Kanvas\Inventory\Attributes\Actions\CreateAttribute; use Kanvas\Inventory\Attributes\DataTransferObject\Attributes as AttributesDto; use Kanvas\Inventory\Attributes\Models\Attributes; +use Kanvas\Inventory\Channels\Models\Channels; use Kanvas\Inventory\Enums\AppEnums; use Kanvas\Inventory\Models\BaseModel; use Kanvas\Inventory\Products\Models\Products; @@ -138,6 +140,33 @@ public function attributes(): BelongsToMany ->withPivot('value'); } + public function channels(): BelongsToMany + { + return $this->belongsToMany( + Channels::class, + VariantsChannels::class, + 'products_variants_id', + 'channels_id' + ) + ->withPivot('price', 'discounted_price', 'is_published'); + } + + /** + * @psalm-suppress MixedInferredReturnType + */ + public function getPriceInfoFromDefaultChannel(): Channels + { + //@todo add is_default to channels + $channel = Channels::where('slug', 'default') + ->where('apps_id', $this->apps_id) + ->notDeleted() + ->where('is_published', StateEnums::ON->getValue()) + ->where('companies_id', $this->companies_id) + ->firstOrFail(); + + return $this->channels()->where('channels_id', $channel->getId())->firstOrFail(); + } + /** * Add/create new attributes from a variant. * @psalm-suppress MixedAssignment diff --git a/src/Domains/Social/Channels/Models/Channel.php b/src/Domains/Social/Channels/Models/Channel.php index e29230b5f9..f3cb42fb0d 100644 --- a/src/Domains/Social/Channels/Models/Channel.php +++ b/src/Domains/Social/Channels/Models/Channel.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Kanvas\Social\Channels\Models; +namespace Kanvas\Social\Channels\Models; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsToMany;