Skip to content

Commit

Permalink
Merge pull request #740 from bakaphp/refact-search-default-price
Browse files Browse the repository at this point in the history
Merge pull request #732 from bakaphp/hotfix-company-review-types
  • Loading branch information
kaioken authored Dec 21, 2023
2 parents 4d09c88 + 5059e3b commit 7c2a1c9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
15 changes: 15 additions & 0 deletions app/GraphQL/Inventory/Builders/Variants/VariantChannelBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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,
Expand Down
29 changes: 29 additions & 0 deletions src/Domains/Inventory/Variants/Models/Variants.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Domains/Social/Channels/Models/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 7c2a1c9

Please sign in to comment.