Skip to content

Commit

Permalink
Merge pull request #863 from bakaphp/development
Browse files Browse the repository at this point in the history
feat: filter channelvariant by attributes
  • Loading branch information
kaioken authored Jan 29, 2024
2 parents 1909812 + 4d25069 commit c381a66
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 8 deletions.
38 changes: 31 additions & 7 deletions app/GraphQL/Inventory/Builders/Variants/VariantChannelBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\Facades\DB;
use Kanvas\Exceptions\ValidationException;
use Kanvas\Inventory\Channels\Models\Channels;
use Kanvas\Inventory\Variants\Models\Variants as ModelsVariants;
use Kanvas\Inventory\Variants\Models\VariantsChannels;
use Kanvas\Inventory\Variants\Repositories\VariantsChannelRepository;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
use stdClass;

Expand Down Expand Up @@ -48,6 +50,35 @@ public function allVariantsPublishedInChannel(
->where($variantsChannel->getTable() . '.is_published', 1);
}

public function allVariantsPublishedInChannelFilterByAttributes(
mixed $root,
array $args,
GraphQLContext $context,
ResolveInfo $resolveInfo
): Builder {
$channelUuid = $args['id'];
$attributes = $args['attributes'] ?? [];

if (isset($attributes['price']) && ! is_array($attributes['price'])) {
throw new ValidationException('Price must be an array');
}

if (isset($attributes['millage']) && ! is_array($attributes['millage'])) {
throw new ValidationException('millage must be an array');
}

$channel = Channels::getByUuid($channelUuid);

/**
* @var Builder
*/
return VariantsChannelRepository::filterByAttributes(
$channel->uuid,
$attributes,
$attributes['price'] ?? []
);
}

/**
* Format channel data from builder
*/
Expand Down Expand Up @@ -77,10 +108,6 @@ public function getChannel(mixed $root, array $req): array

/**
* Get filter variant by channel
*
* @param mixed $root
* @param array $req
* @return Collection
*/
public function getHasChannel(mixed $root, array $req): Collection
{
Expand All @@ -96,9 +123,6 @@ public function getHasChannel(mixed $root, array $req): Collection

/**
* Get channel price history
*
* @param mixed $root
* @return array
*/
public function getChannelHistory(mixed $root): array
{
Expand Down
2 changes: 1 addition & 1 deletion graphql/schemas/Inventory/variant.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ extend type Query @guard {
"is_published"
]
)
): [Variant]
): [Variant!]!
@paginate(
defaultCount: 25
scopes: ["fromApp", "fromCompany", "notDeleted"]
Expand Down
22 changes: 22 additions & 0 deletions graphql/schemas/Inventory/variantChannel.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,26 @@ extend type Query {
defaultCount: 25
builder: "App\\GraphQL\\Inventory\\Builders\\Variants\\VariantChannelBuilder@allVariantsPublishedInChannel"
)

channelVariantsFilterByAttributes(
id: ID!
attributes: Mixed
where: _ @whereConditions(columns: ["slug", "uuid", "name", "sku"])
orderBy: _
@orderBy(
columns: [
"id"
"created_at"
"updated_at"
"name"
"products_id"
"status_id"
"is_published"
]
)
): [VariantChannel!]!
@paginate(
defaultCount: 25
builder: "App\\GraphQL\\Inventory\\Builders\\Variants\\VariantChannelBuilder@allVariantsPublishedInChannelFilterByAttributes"
)
}
3 changes: 3 additions & 0 deletions src/Domains/Connectors/Zoho/Workflows/ZohoAgentActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Kanvas\Connectors\Zoho\Workflows;

use Baka\Contracts\AppInterface;
use Baka\Traits\KanvasJobsTrait;
use Baka\Users\Contracts\UserInterface;
use Exception;
use Illuminate\Database\Eloquent\Model;
Expand All @@ -20,10 +21,12 @@

class ZohoAgentActivity extends Activity implements WorkflowActivityInterface
{
use KanvasJobsTrait;
public $tries = 10;

public function execute(Model $user, AppInterface $app, array $params): array
{
$this->overwriteAppService($app);
if (! isset($params['company'])) {
throw new Exception('Company is required');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Kanvas\Inventory\Variants\Repositories;

use Illuminate\Contracts\Database\Eloquent\Builder;
use Kanvas\Inventory\Variants\Models\Variants;

class VariantsChannelRepository
{
/**
* this is a temp solution to filter variants / product by attributes
* we should aim for shopify query implementation in the future.
*/
public static function filterByAttributes(string $channelId, array $attributes, array $priceRange = []): Builder
{
$query = Variants::query();

unset($attributes['millage']);
unset($attributes['price']);
$index = 1;
foreach ($attributes as $name => $value) {
$alias = 'pva' . $index; // Start with pva2 since pva1 is already used
$query->join("products_variants_attributes as $alias", function ($join) use ($alias, $value, $index, $name) {
$join->on('products_variants.id', '=', "$alias.products_variants_id")
->join('attributes as a' . $index, function ($join) use ($alias, $value, $name, $index) {
$join->on("$alias.attributes_id", '=', 'a' . $index . '.id')
->where('a' . $index . '.name', '=', $name)
->where("$alias.value", '=', $value);
});
});

$index++;
}

$query->join('products_variants_channels as pvc', 'products_variants.id', '=', 'pvc.products_variants_id')
->join('channels as c', 'pvc.channels_id', '=', 'c.id')
->where('c.uuid', '=', $channelId)
->where('products_variants.is_deleted', '=', 0)
->where('pvc.is_deleted', '=', 0)
->where('c.is_deleted', '=', 0);

if ($priceRange) {
$query->whereBetween('pvc.price', $priceRange);
}

return $query->select('products_variants.*')->distinct();
}
}
3 changes: 3 additions & 0 deletions src/Kanvas/Notifications/Jobs/PushNotificationsHandlerJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Kanvas\Notifications\Jobs;

use Baka\Contracts\AppInterface;
use Baka\Traits\KanvasJobsTrait;
use Berkayk\OneSignal\OneSignalClient;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
Expand All @@ -20,6 +21,7 @@ class PushNotificationsHandlerJob implements ShouldQueue
use InteractsWithQueue;
use Queueable;
use SerializesModels;
use KanvasJobsTrait;

/**
* Create a new job instance.
Expand All @@ -39,6 +41,7 @@ public function __construct(
*/
public function handle()
{
$this->overwriteAppService($this->app);
$userOneSignalId = UsersLinkedSourcesRepository::getByUsersId($this->usersFollowId)->source_users_id;

if (getenv('APP_ENV') !== 'testing') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Kanvas\Notifications\Jobs;

use Baka\Contracts\AppInterface;
use Baka\Traits\KanvasJobsTrait;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
Expand All @@ -24,6 +25,7 @@ class SendMessageNotificationsToAllFollowersJob implements ShouldQueue
use InteractsWithQueue;
use Queueable;
use SerializesModels;
use KanvasJobsTrait;

public function __construct(
protected Users $fromUser,
Expand All @@ -38,6 +40,7 @@ public function __construct(
*/
public function handle(): void
{
$this->overwriteAppService($this->app);
$dynamicNotification = new DynamicKanvasNotification(
$this->notificationType,
$this->messagePayload->message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Kanvas\Notifications\Jobs;

use Baka\Contracts\AppInterface;
use Baka\Traits\KanvasJobsTrait;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
Expand All @@ -22,6 +23,7 @@ class SendMessageNotificationsToUsersJob implements ShouldQueue
use InteractsWithQueue;
use Queueable;
use SerializesModels;
use KanvasJobsTrait;

public function __construct(
protected Users $fromUser,
Expand All @@ -36,6 +38,7 @@ public function __construct(
*/
public function handle(): void
{
$this->overwriteAppService($this->app);
$dynamicNotification = new DynamicKanvasNotification(
$this->notificationType,
$this->messagePayload->message
Expand Down
1 change: 1 addition & 0 deletions src/Kanvas/Users/Jobs/OnBoardingJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class OnBoardingJob implements ShouldQueue
use Queueable;
use SerializesModels;
use KanvasJobsTrait;

public $failOnTimeout = false;
public $timeout = 120000;

Expand Down

0 comments on commit c381a66

Please sign in to comment.