Skip to content

Commit

Permalink
Merge pull request #1711 from bakaphp/development
Browse files Browse the repository at this point in the history
v1.0-RC5
  • Loading branch information
kaioken authored Jul 28, 2024
2 parents 87707a7 + a6ca555 commit f1b678d
Show file tree
Hide file tree
Showing 93 changed files with 2,445 additions and 831 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"hashtable",
"indice",
"Meili",
"Metafield",
"Nuwave"
]
}
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ Todo:

## Prerequisites

- PHP ^8.2
- Laravel ^10.0
- PHP ^8.3
- Laravel ^11.0

## Initial Setup

Expand Down Expand Up @@ -124,4 +124,4 @@ Note:

``` sh
docker compose -f docker-compose.franken.yml up -d --build
```
```
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Kanvas\Apps\Models\Apps;
use Kanvas\Companies\Models\CompaniesBranches;
use Kanvas\Connectors\Shopify\Actions\DownloadAllShopifyProductsAction;
use Kanvas\Inventory\Channels\Models\Channels;
use Kanvas\Inventory\Warehouses\Models\Warehouses;

class ShopifyInventoryDownloadCommand extends Command
Expand All @@ -17,7 +18,7 @@ class ShopifyInventoryDownloadCommand extends Command
*
* @var string
*/
protected $signature = 'kanvas:inventory-shopify-sync {app_id} {branch_id} {warehouse_id}';
protected $signature = 'kanvas:inventory-shopify-sync {app_id} {branch_id} {warehouse_id} {channel_id?}';

/**
* The console command description.
Expand All @@ -35,13 +36,15 @@ public function handle()
{
$app = Apps::getById((int) $this->argument('app_id'));
$branch = CompaniesBranches::getById((int) $this->argument('branch_id'));
$warehouse = Warehouses::fromApp($app)->where('id', $this->argument('warehouse_id'))->firstOrFail();
$warehouse = Warehouses::fromApp($app)->fromCompany($branch->company)->where('id', $this->argument('warehouse_id'))->firstOrFail();
$channel = $this->argument('channel_id') ? Channels::fromApp($app)->fromCompany($branch->company)->where('id', $this->argument('channel_id'))->firstOrFail() : null;

$downloadProduct = new DownloadAllShopifyProductsAction(
$warehouse->app,
$warehouse,
$branch,
$branch->company->user
$branch->company->user,
$channel
);

$total = $downloadProduct->execute();
Expand Down
52 changes: 52 additions & 0 deletions app/Console/Commands/Connectors/Zoho/ZohoLeadsDownloadCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands\Connectors\Zoho;

use Baka\Traits\KanvasJobsTrait;
use Illuminate\Console\Command;
use Kanvas\Apps\Models\Apps;
use Kanvas\Companies\Models\Companies;
use Kanvas\Connectors\Zoho\Actions\DownloadAllZohoLeadAction;
use Kanvas\Guild\Leads\Models\LeadReceiver;

class ZohoLeadsDownloadCommand extends Command
{
use KanvasJobsTrait;
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'kanvas:guild-zoho-lead-sync {app_id} {company_id} {receiver_id} {page=50} {leadsPerPage=200}';

/**
* The console command description.
*
* @var string|null
*/
protected $description = 'Download all leads from Zoho to this branch';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$app = Apps::getById((int) $this->argument('app_id'));
$this->overwriteAppService($app);
$company = Companies::getById((int) $this->argument('company_id'));
$leadReceiver = LeadReceiver::getByIdFromCompanyApp((int) $this->argument('receiver_id'), $company, $app);
$page = (int) $this->argument('page');
$leadsPerPage = (int) $this->argument('leadsPerPage');

$downloadAllLeads = new DownloadAllZohoLeadAction($app, $company, $leadReceiver);
$downloadAllLeads->execute($page, $leadsPerPage);

$this->info($downloadAllLeads->getTotalLeadsProcessed() . ' leads downloaded from Zoho to ' . $leadReceiver->name);

return;
}
}
8 changes: 4 additions & 4 deletions app/Console/Commands/KanvasImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Kanvas\Apps\Models\Apps;
use Kanvas\Apps\Repositories\AppsRepository;
use Laravel\Scout\Console\ImportCommand;
use Kanvas\Apps\Support\MountedAppProvider;

class KanvasImportCommand extends ImportCommand
{
Expand All @@ -19,11 +20,10 @@ class KanvasImportCommand extends ImportCommand
public function handle(Dispatcher $events)
{
$appUuid = $this->option('app');
if (! $appUuid) {
}
$app = AppsRepository::findFirstByKey($appUuid);
app()->scoped(Apps::class, function () use ($app) {
return $app;
});

(new MountedAppProvider($app))->register();
parent::handle($events);
}
}
15 changes: 11 additions & 4 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

namespace App\Console;

use App\Console\Commands\Connectors\Notifications\MailCaddieLabCommand;
use App\Console\Commands\DeleteUsersRequestedCommand;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
use Spatie\Health\Commands\DispatchQueueCheckJobsCommand;
use Spatie\Health\Commands\RunHealthChecksCommand;
use Spatie\Health\Commands\ScheduleCheckHeartbeatCommand;
use App\Console\Commands\DeleteUsersRequestedCommand;

class Kernel extends ConsoleKernel
{
Expand All @@ -22,9 +23,15 @@ protected function schedule(Schedule $schedule)
$schedule->command(DispatchQueueCheckJobsCommand::class)->everyMinute();
$schedule->command(ScheduleCheckHeartbeatCommand::class)->everyMinute();
$schedule->command(DeleteUsersRequestedCommand::class)->dailyAt('00:00');
/* $schedule->command(MailCaddieLabCommand::class, [getenv('CADDIE_APP_KEY')])
->dailyAt('13:00')
->timezone('America/Santo_Domingo') ; */

/**
* @todo move this to a cron subSystem
*/
if (getenv('CADDIE_APP_KEY')) {
$schedule->command(MailCaddieLabCommand::class, [getenv('CADDIE_APP_KEY')])
->dailyAt('13:00')
->timezone('America/Santo_Domingo');
}
}

/**
Expand Down
42 changes: 42 additions & 0 deletions app/GraphQL/ActionEngine/Subscriptions/TaskItemSubscription.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace App\GraphQL\ActionEngine\Subscriptions;

use Exception;
use Illuminate\Http\Request;
use Kanvas\ActionEngine\Tasks\Models\TaskListItem;
use Kanvas\Users\Repositories\UsersRepository;
use Nuwave\Lighthouse\Execution\ResolveInfo;
use Nuwave\Lighthouse\Schema\Types\GraphQLSubscription;
use Nuwave\Lighthouse\Subscriptions\Subscriber;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;

class TaskItemSubscription extends GraphQLSubscription
{
public function authorize(Subscriber $subscriber, Request $request): bool
{
return true;
}

public function filter(Subscriber $subscriber, mixed $root): bool
{
try {
UsersRepository::belongsToCompany($subscriber->context->user, $root->companyAction->company);
} catch (Exception $e) {
return false;
}

return true;
}

public function resolve(
mixed $root,
array $args,
GraphQLContext $context,
ResolveInfo $resolveInfo
): TaskListItem {
return $root;
}
}
111 changes: 111 additions & 0 deletions app/GraphQL/Directives/CacheRedisDirective.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php

declare(strict_types=1);

namespace App\GraphQL\Directives;

use GraphQL\Deferred;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Redis;
use Nuwave\Lighthouse\Cache\CacheDirective as CacheCacheDirective;
use Nuwave\Lighthouse\Execution\Resolved;
use Nuwave\Lighthouse\Execution\ResolveInfo;
use Nuwave\Lighthouse\Schema\Values\FieldValue;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;

class CacheRedisDirective extends CacheCacheDirective
{
public static function definition(): string
{
return /** @lang GraphQL */ <<<'GRAPHQL'
"""
Cache the result of a resolver.
Place this after other field middleware to ensure it caches the correct transformed value.
"""
directive @cacheRedis(
"""
Set the duration it takes for the cache to expire in seconds.
If not given, the result will be stored forever.
"""
maxAge: Int
"""
Limit access to cached data to the currently authenticated user.
When the field is accessible by guest users, this will not have
any effect, they will access a shared cache.
"""
private: Boolean = false
) on FIELD_DEFINITION
GRAPHQL;
}

public function handleField(FieldValue $fieldValue): void
{
$rootCacheKey = $fieldValue->getParent()->cacheKey();
$shouldUseTags = $this->shouldUseTags();
$maxAge = $this->directiveArgValue('maxAge');
$isPrivate = $this->directiveArgValue('private', false);

$fieldValue->wrapResolver(fn (callable $resolver): \Closure => function (mixed $root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($rootCacheKey, $shouldUseTags, $resolver, $maxAge, $isPrivate) {
$parentName = $resolveInfo->parentType->name;
$rootID = $root !== null && $rootCacheKey !== null
? data_get($root, $rootCacheKey)
: null;
$fieldName = $resolveInfo->fieldName;
$path = $resolveInfo->path;

$cache = Redis::connection('graph-cache');

$cacheKey = $this->cacheKeyAndTags->key(
$context->user(),
$isPrivate,
$parentName,
$rootID,
$fieldName,
$args,
$path,
);

// We found a matching value in the cache, so we can just return early without actually running the query.
$value = $cache->get($cacheKey);
if ($value !== null) {
// Deferring the result will allow nested deferred resolves to be bundled together, see https://github.com/nuwave/lighthouse/pull/2270#discussion_r1072414584.
return new Deferred(static fn () => $value);
}

// In Laravel cache, null is considered a non-existent value, see https://laravel.com/docs/9.x/cache#checking-for-item-existence:
// > The `has` method [...] will also return false if the item exists but its value is null.
//
// If caching `null` value becomes something worthwhile, one possible way to achieve it is to
// encapsulate the `$result` at writing time :
//
// $storeInCache = static function ($result) use ($cacheKey, $maxAge, $cache): void {
// $value = ['rawValue' => $result];
// $maxAge
// ? $cache->put($cacheKey, $value, Carbon::now()->addSeconds($maxAge))
// : $cache->forever($cacheKey, $value);
// };
//
// and restoring original value back at reading :
//
// if (is_array($value) && array_key_exists('rawValue', $value)) { // don't use isset !
// return $value['rawValue'];
// }
//
// Such a change would introduce some potential BC, if for instance cached value was already containing
// an object with a `rawValue` key prior the implementation change. A possible workaround is to choose a
// less collision-probable key instead of `rawValue` (e.g. "lighthouse:rawValue").

$resolved = $resolver($root, $args, $context, $resolveInfo);

$storeInCache = $maxAge
? static fn ($result): bool => $cache->set($cacheKey, $result, $maxAge)
: static fn ($result): bool => $cache->set($cacheKey, $result);

Resolved::handle($resolved, $storeInCache);

return $resolved;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function getAllByGraphType(
*/
public function get(mixed $rootValue, array $request): mixed
{
$customFieldInput = new CustomFieldInput(
$customFieldInput = new CustomFieldInput(
$request['name'],
$request['system_module_uuid'],
$request['entity_id']
Expand All @@ -65,7 +65,7 @@ public function get(mixed $rootValue, array $request): mixed
*/
public function getAll(mixed $rootValue, array $request): array
{
$customFieldInput = new CustomFieldInput(
$customFieldInput = new CustomFieldInput(
$request['name'],
$request['system_module_uuid'],
$request['entity_id']
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class AttributeMutation
*/
public function create(mixed $root, array $req): AttributeModel
{
$dto = AttributeDto::viaRequest($req['input']);
$dto = AttributeDto::viaRequest($req['input'], auth()->user());
$action = new CreateAttribute($dto, auth()->user());
$attributeModel = $action->execute();

Expand All @@ -45,7 +45,7 @@ public function create(mixed $root, array $req): AttributeModel
public function update(mixed $root, array $req): AttributeModel
{
$attribute = AttributesRepository::getById((int) $req['id'], auth()->user()->getCurrentCompany());
$dto = AttributeDto::viaRequest($req['input']);
$dto = AttributeDto::viaRequest($req['input'], auth()->user());
(new UpdateAttribute($attribute, $dto, auth()->user()))->execute();

if (isset($req['input']['values'])) {
Expand Down
Loading

0 comments on commit f1b678d

Please sign in to comment.