From 12c1fbdb045991c01c8ad0bee55c735bfc5686b8 Mon Sep 17 00:00:00 2001 From: Herpaderp Aldent Date: Thu, 16 Mar 2023 20:32:02 +0100 Subject: [PATCH] Fix/job container (#1350) * Fix old job container and esi requests * Fix styling * remove non-necessary method --------- Co-authored-by: herpaderpaldent --- .../Components/SlideOver/DispatchUpdate.vue | 22 --- src/Contracts/WebJobsRepository.php | 175 ++++++++++++++++++ .../Queue/DispatchJobController.php | 16 +- .../Controllers/Shared/HelperController.php | 10 +- .../DispatchCorporationOrAllianceInfoJob.php | 18 +- src/Services/GetCharacterAffiliations.php | 12 +- src/Services/GetCorporationInfo.php | 12 +- src/Services/GetIdsFromNamesService.php | 12 +- src/Services/GetNamesFromIdsService.php | 12 +- src/Services/SearchService.php | 20 +- src/WebServiceProvider.php | 2 + tests/Integration/AccessControlTest.php | 2 + tests/Integration/HelperControllerTest.php | 12 +- tests/Pest.php | 2 +- 14 files changed, 238 insertions(+), 89 deletions(-) create mode 100644 src/Contracts/WebJobsRepository.php diff --git a/resources/js/Shared/Components/SlideOver/DispatchUpdate.vue b/resources/js/Shared/Components/SlideOver/DispatchUpdate.vue index 0fdfcbd3..8af9b1e2 100644 --- a/resources/js/Shared/Components/SlideOver/DispatchUpdate.vue +++ b/resources/js/Shared/Components/SlideOver/DispatchUpdate.vue @@ -23,14 +23,6 @@ import InfiniteLoadingHelper from "@/Shared/InfiniteLoadingHelper.vue"; export default { name: "DispatchUpdate", components: {InfiniteLoadingHelper, DispatchableEntry}, - data: function () { - return { - //job_name: this.$page.props.dispatch_transfer_object.manual_job, - //dispatch_transfer_object: this.$page.props.dispatch_transfer_object, - entities: [], - infiniteId: new Date() - } - }, computed: { dispatch_transfer_object() { return this.$page.props.dispatch_transfer_object != null ? this.$page.props.dispatch_transfer_object : this.$page.props.dispatchTransferObject @@ -39,13 +31,6 @@ export default { return _.get(this.dispatch_transfer_object, 'manual_job') } }, - created() { - - /*if(!this.dispatch_transfer_object) - this.dispatch_transfer_object = this.$page.props.dispatchTransferObject*/ - - this.getEntities(); - }, methods: { dispatchJob(entity) { @@ -58,14 +43,7 @@ export default { }), { dispatch_transfer_object: this.dispatch_transfer_object }) - - setTimeout(() => this.getEntities(), 100) - } } } - - diff --git a/src/Contracts/WebJobsRepository.php b/src/Contracts/WebJobsRepository.php new file mode 100644 index 00000000..433eb083 --- /dev/null +++ b/src/Contracts/WebJobsRepository.php @@ -0,0 +1,175 @@ +jobs = [ + // Character + 'contacts' => fn (RefreshToken $refresh_token) => $this->getContactJobs($refresh_token), + 'assets' => fn (RefreshToken $refresh_token) => $this->getAssetJobs($refresh_token), + 'wallet' => fn (RefreshToken $refresh_token) => $this->getWalletJobs($refresh_token), + 'contract' => fn (RefreshToken $refresh_token) => $this->getContractJobs($refresh_token), + 'skills' => fn (RefreshToken $refresh_token) => $this->getSkillsJobs($refresh_token), + 'mails' => fn (RefreshToken $refresh_token) => $this->getMailsJobs($refresh_token), + // Corporation + 'corporation.wallet' => fn (RefreshToken $refresh_token) => $this->getCorporationWalletJobs($refresh_token), + 'membertracking' => fn (RefreshToken $refresh_token) => $this->getCorporationWalletJobs($refresh_token), + ]; + } + + public function addJob(string $key, \Closure $build_function): void + { + $this->jobs[$key] = $build_function; + } + + public function addJobs(array $jobs): void + { + $this->jobs = array_merge($this->jobs, $jobs); + } + + public function getJobKeys(): array + { + return array_keys($this->jobs); + } + + public function getConstructedJobs(string $key, RefreshToken $refresh_token): array + { + return $this->jobs[$key]($refresh_token); + } + + private function getContactJobs(RefreshToken $refresh_token): array + { + $jobs = []; + + // if refresh token has scope for reading contacts add the job to the jobs array + if ($refresh_token->hasScope('esi-characters.read_contacts.v1')) { + $jobs[] = [ + new CharacterContactJob($refresh_token->character_id), + new CharacterContactLabelJob($refresh_token->character_id), + ]; + } + + // if refresh token has scope for reading corporation contacts add the job to the jobs array + if ($refresh_token->hasScope('esi-corporations.read_contacts.v1')) { + $jobs[] = [ + new CorporationContactJob($refresh_token->character_id), + new CorporationContactLabelJob($refresh_token->character_id), + ]; + } + + // if refresh token has scope for reading alliance contacts add the job to the jobs array + if ($refresh_token->hasScope('esi-alliances.read_contacts.v1')) { + $jobs[] = [ + new AllianceContactJob($refresh_token->character_id), + new AllianceContactLabelJob($refresh_token->character_id), + ]; + } + + return $jobs; + } + + private function getAssetJobs(RefreshToken $refresh_token): array + { + if (! $refresh_token->hasScope('esi-assets.read_assets.v1')) { + return []; + } + + return [ + new CharacterAssetJob($refresh_token->character_id), + new CharacterAssetsNameJob($refresh_token->character_id), + ]; + } + + private function getWalletJobs(RefreshToken $refresh_token): array + { + if (! $refresh_token->hasScope('esi-wallet.read_character_wallet.v1')) { + return []; + } + + return [ + new CharacterWalletJournalJob($refresh_token->character_id), + new CharacterWalletTransactionJob($refresh_token->character_id), + new CharacterBalanceJob($refresh_token->character_id), + ]; + } + + private function getContractJobs(RefreshToken $refresh_token): array + { + if (! $refresh_token->hasScope('esi-contracts.read_character_contracts.v1')) { + return []; + } + + return [ + new CharacterContractsJob($refresh_token->character_id), + ]; + } + + private function getSkillsJobs(RefreshToken $refresh_token): array + { + if (! $refresh_token->hasScope('esi-skills.read_skills.v1')) { + return []; + } + + return [ + new SkillsJob($refresh_token->character_id), + ]; + } + + private function getMailsJobs(RefreshToken $refresh_token): array + { + if (! $refresh_token->hasScope('esi-mail.read_mail.v1')) { + return []; + } + + return [ + new MailHeaderJob($refresh_token->character_id), + ]; + } + + private function getCorporationWalletJobs(RefreshToken $refresh_token): array + { + if (! $refresh_token->hasScope('esi-wallet.read_corporation_wallets.v1')) { + return []; + } + + return [ + new CorporationWalletJournalJob($refresh_token->corporation_id), + new CorporationBalanceJob($refresh_token->corporation_id), + ]; + } + + public function getCorporationMemberTrackingJobs(RefreshToken $refresh_token): array + { + if (! $refresh_token->hasScope('esi-corporations.track_members.v1')) { + return []; + } + + return [ + new CorporationMemberTrackingJob($refresh_token->corporation_id), + ]; + } +} diff --git a/src/Http/Controllers/Queue/DispatchJobController.php b/src/Http/Controllers/Queue/DispatchJobController.php index 86580cf4..9fdbf5ad 100644 --- a/src/Http/Controllers/Queue/DispatchJobController.php +++ b/src/Http/Controllers/Queue/DispatchJobController.php @@ -31,9 +31,9 @@ use Illuminate\Support\Arr; use Illuminate\Support\Facades\Bus; use Seatplus\Auth\Services\Dtos\AffiliationsDto; -use Seatplus\Eveapi\Containers\JobContainer; use Seatplus\Eveapi\Models\RefreshToken; use Seatplus\Eveapi\Services\FindCorporationRefreshToken; +use Seatplus\Web\Contracts\WebJobsRepository; use Seatplus\Web\Http\Controllers\Controller; use Seatplus\Web\Http\Controllers\Request\DispatchIndividualJob; use Seatplus\Web\Jobs\ManualDispatchedJob; @@ -42,26 +42,28 @@ class DispatchJobController extends Controller { protected array $dispatch_transfer_object; + public function __construct( + private WebJobsRepository $web_jobs + ) { + } + public function dispatch(DispatchIndividualJob $job) { $this->dispatch_transfer_object = $job->get('dispatch_transfer_object'); $id = $job->get('character_id') ?? $job->get('corporation_id'); + $manual_job = Arr::get($this->dispatch_transfer_object, 'manual_job'); - $cache_key = $this->getCacheKey(Arr::get($this->dispatch_transfer_object, 'manual_job'), $id); + $cache_key = "${manual_job}:${id}"; if (cache($cache_key)) { return redirect()->back()->with('error', 'job was already queued'); } - $hydrate_job_string = config('web.jobs.' . Arr::get($this->dispatch_transfer_object, 'manual_job')); - $job_container = new JobContainer(['refresh_token' => $this->getRefreshToken($job)]); - - $hydrate_job = new $hydrate_job_string($job_container); $batch_name = sprintf('Manual batch update of %s', $cache_key); $batch_id = (new ManualDispatchedJob) - ->setJobs([$hydrate_job]) + ->setJobs($this->web_jobs->getConstructedJobs($manual_job, $this->getRefreshToken($job))) ->setName($batch_name) ->handle(); diff --git a/src/Http/Controllers/Shared/HelperController.php b/src/Http/Controllers/Shared/HelperController.php index e2006764..8dea83b6 100644 --- a/src/Http/Controllers/Shared/HelperController.php +++ b/src/Http/Controllers/Shared/HelperController.php @@ -163,11 +163,11 @@ public function getMarketsPrices() return $prices->toJson(); } - $container = new EsiRequestContainer([ - 'method' => 'get', - 'version' => 'v1', - 'endpoint' => '/markets/prices/', - ]); + $container = new EsiRequestContainer( + method: 'get', + version: 'v1', + endpoint: '/markets/prices/', + ); $esi_results = RetrieveEsiData::execute($container); diff --git a/src/Services/DispatchCorporationOrAllianceInfoJob.php b/src/Services/DispatchCorporationOrAllianceInfoJob.php index 23232afb..8ff3c46a 100644 --- a/src/Services/DispatchCorporationOrAllianceInfoJob.php +++ b/src/Services/DispatchCorporationOrAllianceInfoJob.php @@ -26,7 +26,6 @@ namespace Seatplus\Web\Services; -use Seatplus\Eveapi\Containers\JobContainer; use Seatplus\Eveapi\Jobs\Alliances\AllianceInfoJob; use Seatplus\Eveapi\Jobs\Corporation\CorporationInfoJob; use Seatplus\Eveapi\Models\Alliance\AllianceInfo; @@ -35,18 +34,9 @@ class DispatchCorporationOrAllianceInfoJob { public function handle(string $type, int $id) { - $type === AllianceInfo::class ? $this->handleAllianceInfo($id) : $this->handleCorporationInfo($id); - } - - private function handleAllianceInfo(int $entity_id) - { - $job_container = new JobContainer(['alliance_id' => $entity_id]); - AllianceInfoJob::dispatchSync($job_container); - } - - private function handleCorporationInfo(int $entity_id) - { - $job_container = new JobContainer(['corporation_id' => $entity_id]); - CorporationInfoJob::dispatchSync($job_container); + match ($type) { + AllianceInfo::class => AllianceInfoJob::dispatchSync($id), + default => CorporationInfoJob::dispatchSync($id), + }; } } diff --git a/src/Services/GetCharacterAffiliations.php b/src/Services/GetCharacterAffiliations.php index d78aefce..266e96db 100644 --- a/src/Services/GetCharacterAffiliations.php +++ b/src/Services/GetCharacterAffiliations.php @@ -34,12 +34,12 @@ class GetCharacterAffiliations { public function execute(array $character_ids): Collection { - $character_affiliation_container = new EsiRequestContainer([ - 'method' => 'post', - 'version' => 'v1', - 'endpoint' => '/characters/affiliation/', - 'request_body' => $character_ids, - ]); + $character_affiliation_container = new EsiRequestContainer( + method: 'post', + version: 'v1', + endpoint: '/characters/affiliation/', + request_body: $character_ids, + ); $character_affiliations = RetrieveEsiData::execute($character_affiliation_container); diff --git a/src/Services/GetCorporationInfo.php b/src/Services/GetCorporationInfo.php index 69fbcc9d..a7ec7d60 100644 --- a/src/Services/GetCorporationInfo.php +++ b/src/Services/GetCorporationInfo.php @@ -33,12 +33,12 @@ class GetCorporationInfo { public function execute($corporation_id) { - $corporation_info_container = new EsiRequestContainer([ - 'method' => 'get', - 'version' => 'v4', - 'endpoint' => '/corporations/{corporation_id}/', - 'path_values' => ['corporation_id' => $corporation_id], - ]); + $corporation_info_container = new EsiRequestContainer( + method: 'get', + version: 'v4', + endpoint: '/corporations/{corporation_id}/', + path_values: ['corporation_id' => $corporation_id], + ); return RetrieveEsiData::execute($corporation_info_container); } diff --git a/src/Services/GetIdsFromNamesService.php b/src/Services/GetIdsFromNamesService.php index 2e125263..20c5b6c6 100644 --- a/src/Services/GetIdsFromNamesService.php +++ b/src/Services/GetIdsFromNamesService.php @@ -60,12 +60,12 @@ public function execute(array $names): Collection return $this->result; } - $container = new EsiRequestContainer([ - 'method' => 'post', - 'version' => 'v1', - 'endpoint' => '/universe/ids/', - 'request_body' => [...$names_to_resolve->toArray()], - ]); + $container = new EsiRequestContainer( + method: 'post', + version: 'v1', + endpoint: '/universe/ids/', + request_body: [...$names_to_resolve->toArray()], + ); $esi_results = RetrieveEsiData::execute($container); diff --git a/src/Services/GetNamesFromIdsService.php b/src/Services/GetNamesFromIdsService.php index 2eb08e4d..b1dad579 100644 --- a/src/Services/GetNamesFromIdsService.php +++ b/src/Services/GetNamesFromIdsService.php @@ -55,12 +55,12 @@ public function execute(array $ids): Collection return $this->result; } - $container = new EsiRequestContainer([ - 'method' => 'post', - 'version' => 'v3', - 'endpoint' => '/universe/names/', - 'request_body' => [...$ids_to_resolve->toArray()], - ]); + $container = new EsiRequestContainer( + method: 'post', + version: 'v3', + endpoint: '/universe/names/', + request_body: [...$ids_to_resolve->toArray()], + ); $esi_results = RetrieveEsiData::execute($container); diff --git a/src/Services/SearchService.php b/src/Services/SearchService.php index 446930f8..76c6d881 100644 --- a/src/Services/SearchService.php +++ b/src/Services/SearchService.php @@ -36,23 +36,21 @@ class SearchService { public function execute(RefreshToken $token, array $categories, string $term) { - $container = new EsiRequestContainer([ - 'method' => 'get', - 'version' => 'v3', - 'endpoint' => '/characters/{character_id}/search/', - 'path_values' => [ + $container = new EsiRequestContainer( + method: 'get', + version: 'v3', + endpoint: '/characters/{character_id}/search/', + refresh_token: $token, + path_values: [ 'character_id' => $token->character_id, ], - 'refresh_token' => $token, - 'query_parameters' => [ + query_parameters: [ 'categories' => implode(',', $categories), 'search' => $term, - ], - ]); + ] + ); return RetrieveEsiData::execute($container); - - return count($categories) === 1 ? ($result->$categories[0] ?? []) : collect($result)->toArray(); } public static function getTokenFromCurrentUser(): ?RefreshToken diff --git a/src/WebServiceProvider.php b/src/WebServiceProvider.php index 98609049..87bb89e7 100644 --- a/src/WebServiceProvider.php +++ b/src/WebServiceProvider.php @@ -33,6 +33,7 @@ use Seatplus\Auth\Services\Affiliations\GetOwnedAffiliatedIdsService; use Seatplus\Auth\Services\Dtos\AffiliationsDto; use Seatplus\Web\Console\Commands\AssignSuperuser; +use Seatplus\Web\Contracts\WebJobsRepository; use Seatplus\Web\Exception\Handler; use Seatplus\Web\Http\Middleware\Authenticate; use Seatplus\Web\Http\Middleware\CheckACLPermission; @@ -82,6 +83,7 @@ public function register() $this->mergeConfigurations(); $this->app->singleton(ExceptionHandler::class, Handler::class); + $this->app->singleton(WebJobsRepository::class); } private function addPublications() diff --git a/tests/Integration/AccessControlTest.php b/tests/Integration/AccessControlTest.php index 08325d2d..9fb4df03 100644 --- a/tests/Integration/AccessControlTest.php +++ b/tests/Integration/AccessControlTest.php @@ -7,6 +7,8 @@ use Seatplus\Auth\Models\User; use Seatplus\Web\Services\Sidebar\SidebarEntries; +uses(\Seatplus\Web\Tests\Traits\MockRetrieveEsiDataAction::class); + it('has control groups', function () { assignPermissionToTestUser(['view access control']); diff --git a/tests/Integration/HelperControllerTest.php b/tests/Integration/HelperControllerTest.php index 233de4d6..3d45278c 100644 --- a/tests/Integration/HelperControllerTest.php +++ b/tests/Integration/HelperControllerTest.php @@ -11,6 +11,8 @@ use Seatplus\Eveapi\Models\Universe\Type; use Seatplus\Eveapi\Services\Facade\RetrieveEsiData; +uses(\Seatplus\Web\Tests\Traits\MockRetrieveEsiDataAction::class); + it('stores resolved id to cache', function () { $id = test()->test_character->character_id; @@ -187,11 +189,11 @@ }); test('one can get market prices', function () { - $container = new EsiRequestContainer([ - 'method' => 'get', - 'version' => 'v1', - 'endpoint' => '/markets/prices/', - ]); + $container = new EsiRequestContainer( + method: 'get', + version: 'v1', + endpoint: '/markets/prices/', + ); test()->mockRetrieveEsiDataAction([ (object) [ diff --git a/tests/Pest.php b/tests/Pest.php index cd88769a..50eb417d 100644 --- a/tests/Pest.php +++ b/tests/Pest.php @@ -19,7 +19,7 @@ /** @link https://pestphp.com/docs/underlying-test-case */ uses(TestCase::class)->in('Integration', 'Unit'); -//uses(TestCase::class)->in('Unit'); +//uses(TestCase::class); /* |--------------------------------------------------------------------------