diff --git a/src/Rules/MaxTokenCount.php b/src/Rules/MaxTokenCount.php index 272e976..fa3c6aa 100644 --- a/src/Rules/MaxTokenCount.php +++ b/src/Rules/MaxTokenCount.php @@ -11,6 +11,7 @@ use Enjin\Platform\Rules\Traits\HasDataAwareRule; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\ValidationRule; +use Illuminate\Support\Arr; class MaxTokenCount implements DataAwareRule, ValidationRule { @@ -41,6 +42,12 @@ public function validate(string $attribute, mixed $value, Closure $fail): void && ($collection = Collection::withCount('tokens')->firstWhere(['collection_chain_id' => $this->collectionId])) && ! is_null($this->limit = $collection->max_token_count) ) { + if ($this->limit == 0) { + $fail('enjin-platform-beam::validation.max_token_count')->translate(['limit' => $this->limit]); + + return; + } + $existingCount = BeamClaim::where('type', BeamType::MINT_ON_DEMAND->name) ->whereHas( 'beam', @@ -53,7 +60,26 @@ public function validate(string $attribute, mixed $value, Closure $fail): void ->groupBy('token_chain_id') ->count(); - [$integers, $ranges] = collect($this->data['tokens']) + $tokens = collect($this->data['tokens']) + ->filter(fn ($data) => !empty(Arr::get($data, 'tokenIds'))) + ->pluck('tokenIds') + ->flatten(); + + + collect($this->data['tokens']) + ->filter(fn ($data) => !empty(Arr::get($data, 'tokenIdDataUpload'))) + ->map(function ($data) use ($tokens) { + $handle = fopen($data['tokenIdDataUpload']->getPathname(), 'r'); + while (($line = fgets($handle)) !== false) { + if (! $this->tokenIdExists($tokens->all(), $tokenId = trim($line))) { + $tokens->push($tokenId); + } + } + fclose($handle); + }); + + [$integers, $ranges] = collect($tokens) + ->filter(fn ($val) => !empty(Arr::get($val, 'tokenIds'))) ->pluck('tokenIds') ->flatten() ->partition(fn ($val) => $this->integerRange($val) === false); diff --git a/tests/Feature/GraphQL/Mutations/CreateBeamTest.php b/tests/Feature/GraphQL/Mutations/CreateBeamTest.php index 25f2fe2..44ba5c3 100644 --- a/tests/Feature/GraphQL/Mutations/CreateBeamTest.php +++ b/tests/Feature/GraphQL/Mutations/CreateBeamTest.php @@ -343,18 +343,18 @@ public function test_it_will_fail_with_invalid_claim_quantity(): void ); $this->assertArraySubset(['tokens.0.claimQuantity' => ['The token count exceeded the maximum limit of 0 for this collection.']], $response['error']); - $this->collection->update(['max_token_count' => 2]); $response = $this->graphql( $this->method, $data = array_merge( $this->generateBeamData(BeamType::MINT_ON_DEMAND, 1), ['tokens' => [['tokenIds' => ['1'], 'type' => BeamType::MINT_ON_DEMAND->name]]] - ) + ), + true ); $this->assertNotEmpty($response); $response = $this->graphql($this->method, $data, true); - $this->assertArraySubset(['tokens.0.claimQuantity' => ['The token count exceeded the maximum limit of 2 for this collection.']], $response['error']); + $this->assertArraySubset(['tokens.0.claimQuantity' => ['The token count exceeded the maximum limit of 0 for this collection.']], $response['error']); } /**