Skip to content

Commit

Permalink
Merge pull request #1518 from bakaphp/hotfix-format-channel-pivot
Browse files Browse the repository at this point in the history
refact : variant channel relationship
  • Loading branch information
kaioken committed Jun 14, 2024
2 parents f59c2d5 + 8d0a8b1 commit 9b14a32
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 49 deletions.
13 changes: 9 additions & 4 deletions graphql/schemas/Inventory/variant.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ type Variant {
product: Product
warehouses: [WarehouseVariantRelationship!]!
@hasMany(relation: "variantWarehouses")
attributes: [VariantsAttributes!]!
attributes: [VariantsAttributes!]! @BelongsToMany(relation: "attributes")
companies: Company! @belongsTo(relation: "company")
channels: [VariantChannelRelationship!]!
@BelongsToMany(relation: "channels")
channel: VariantPricingInfo
@field(resolver: "App\\GraphQL\\Inventory\\Types\\ChannelInfoType@price")
@field(
resolver: "App\\GraphQL\\Inventory\\Types\\ChannelInfoType@price"
)
metadata: Mixed
@field(resolver: "App\\GraphQL\\Inventory\\Types\\MetadataType@linkedStores")
@field(
resolver: "App\\GraphQL\\Inventory\\Types\\MetadataType@linkedStores"
)
custom_fields: [CustomField!]!
@paginate(
defaultCount: 25
Expand All @@ -38,7 +43,7 @@ type Variant {
inventory_quantity: Int @method(name: "getTotalQuantity")
}

type VariantPricingInfo{
type VariantPricingInfo {
price: Float!
discounted_price: Float!
quantity: Int!
Expand Down
15 changes: 5 additions & 10 deletions graphql/schemas/Inventory/variantChannel.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,17 @@ type VariantChannelRelationship {
id: ID!
name: String
description: String
price: Float
discounted_price: Float
warehouses_id: ID!
slug: String
pivot: VariantChannelPivot
is_published: Boolean
prices_history: [ChannelsPricesHistoryRelationship!]!
@field(
resolver: "App\\GraphQL\\Inventory\\Builders\\Variants\\VariantChannelBuilder@getChannelHistory"
)
}

type VariantChannelPivot {
price: Float
channels_id: ID!
warehouses_id: ID!
discounted_price: Float
is_published: Boolean
}

input VariantChannelInput {
price: Float!
discounted_price: Float!
Expand Down Expand Up @@ -133,7 +127,8 @@ extend type Query {
id: ID!
attributes: Mixed
search: String @search
where: _ @whereConditions(columnsEnum: ChannelVariantsFilterByAttributesEnum)
where: _
@whereConditions(columnsEnum: ChannelVariantsFilterByAttributesEnum)
orderBy: _
@orderBy(
columns: [
Expand Down
58 changes: 29 additions & 29 deletions src/Domains/Inventory/Channels/Models/Channels.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@

namespace Kanvas\Inventory\Channels\Models;

use Baka\Traits\DatabaseSearchableTrait;
use Baka\Traits\SlugTrait;
use Baka\Traits\UuidTrait;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Kanvas\Apps\Models\Apps;
use Kanvas\Companies\Models\Companies;
use Kanvas\Inventory\Models\BaseModel;
use Baka\Traits\DatabaseSearchableTrait;
use Kanvas\Inventory\Traits\DefaultTrait;
use Kanvas\Inventory\Variants\Models\Variants;
use Kanvas\Inventory\Variants\Models\VariantsChannels;
use Kanvas\Users\Models\Users;

/**
* Class Channels.
Expand Down Expand Up @@ -44,30 +40,6 @@ class Channels extends BaseModel
protected $table = 'channels';
protected $guarded = [];

/**
* Get the companies that owns the Warehouses.
*/
public function companies(): BelongsTo
{
return $this->belongsTo(Companies::class, 'companies_id');
}

/**
*
*/
public function apps(): BelongsTo
{
return $this->belongsTo(Apps::class, 'apps_id');
}

/**
* users.
*/
public function users(): BelongsTo
{
return $this->belongsTo(Users::class, 'users_id');
}

/**
* Available products in this channel
*/
Expand Down Expand Up @@ -99,4 +71,32 @@ public function pricesHistory(): HasMany
'channels_id'
);
}

public function price(): Attribute
{
return Attribute::make(
get: fn () => $this->pivot->price,
);
}

public function discountedPrice(): Attribute
{
return Attribute::make(
get: fn () => $this->pivot->discounted_price,
);
}

public function isPublished(): Attribute
{
return Attribute::make(
get: fn () => $this->pivot->is_published,
);
}

public function warehousesId(): Attribute
{
return Attribute::make(
get: fn () => $this->pivot->warehouses_id,
);
}
}
7 changes: 2 additions & 5 deletions src/Domains/Inventory/Variants/Models/Variants.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public function addAttributes(UserInterface $user, array $attributes): void
'isVisible' => false,
'isSearchable' => false,
'isFiltrable' => false,
'slug' => Str::slug($attribute['name'])
'slug' => Str::slug($attribute['name']),
]);
$attributeModel = (new CreateAttribute($attributesDto, $user))->execute();
}
Expand Down Expand Up @@ -331,21 +331,18 @@ protected function makeAllSearchableUsing(Builder $query): Builder

/**
* Get the total amount of variants in all the warehouses.
*
* @return Int
*/
public function getTotalQuantity(): int
{
if (! $totalVariantQuantity = $this->get('total_variant_quantity')) {
return (int) $this->setTotalQuantity();
}

return (int) $totalVariantQuantity;
}

/**
* Set the total amount of variants in all the warehouses.
*
* @return Int
*/
public function setTotalQuantity(): int
{
Expand Down
2 changes: 1 addition & 1 deletion src/Kanvas/Enums/AppEnums.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function getValue(): mixed
self::KANVAS_APP_REGION_HEADER => 'X-Kanvas-Region',
self::KANVAS_APP_COMPANY_AUTH_HEADER => 'Company-Authorization', //@deprecated
self::DISPLAYNAME_LOGIN => 'displayname_login',
self::VERSION => '1.0-BETA-35',
self::VERSION => '1.0-BETA-36',
self::ANONYMOUS_USER_ID => -1,
self::DEFAULT_APP_JWT_TOKEN_NAME => 'kanvas-login',
};
Expand Down

0 comments on commit 9b14a32

Please sign in to comment.