Skip to content

Commit

Permalink
Fix validation
Browse files Browse the repository at this point in the history
  • Loading branch information
enjinabner committed Sep 12, 2023
1 parent ace365d commit e99946e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/Rules/TokenUploadNotExistInBeam.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@

namespace Enjin\Platform\Beam\Rules;

use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule;
use Enjin\Platform\Beam\Rules\Traits\IntegerRange;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\LazyCollection;

class TokenUploadNotExistInBeam implements Rule
class TokenUploadNotExistInBeam implements DataAwareRule, Rule
{
use IntegerRange;
use HasDataAwareRule;

public function __construct(protected ?Model $beam = null)
{
Expand Down Expand Up @@ -37,7 +41,7 @@ public function passes($attribute, $value)
fclose($handle);
});

$prepare = TokensDoNotExistInBeam::prepareStatement($this->beam);
$prepare = TokensDoNotExistInBeam::prepareStatement($this->beam, Arr::get($this->data, 'collectionId'));
foreach ($tokens->chunk(1000) as $tokenIds) {
$integers = collect($tokenIds)->filter(fn ($val) => false === $this->integerRange($val))->all();
if ($integers) {
Expand Down
16 changes: 11 additions & 5 deletions src/Rules/TokensDoNotExistInBeam.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@
namespace Enjin\Platform\Beam\Rules;

use Enjin\Platform\Beam\Models\BeamClaim;
use Enjin\Platform\Beam\Rules\Traits\HasDataAwareRule;
use Enjin\Platform\Beam\Rules\Traits\IntegerRange;
use Enjin\Platform\Enums\Substrate\TokenMintCapType;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\Rule;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;

class TokensDoNotExistInBeam implements Rule
class TokensDoNotExistInBeam implements DataAwareRule, Rule
{
use IntegerRange;
use HasDataAwareRule;

public function __construct(protected ?Model $beam = null)
{
Expand All @@ -27,7 +31,7 @@ public function __construct(protected ?Model $beam = null)
*/
public function passes($attribute, $value)
{
$prepare = static::prepareStatement($this->beam);
$prepare = static::prepareStatement($this->beam, Arr::get($this->data, 'collectionId'));
$integers = collect($value)->filter(fn ($val) => false === $this->integerRange($val))->all();
if ($integers) {
if ($prepare->whereIn('beam_claims.token_chain_id', $integers)->exists()) {
Expand All @@ -48,15 +52,17 @@ public function passes($attribute, $value)
/**
* Prepare the statement to check if the token ids exist in the beam.
*/
public static function prepareStatement(?Model $beam): Builder
public static function prepareStatement(?Model $beam, ?string $collectionId = null): Builder
{
$collectionId = Arr::get($beam, 'collection_chain_id', $collectionId);

return BeamClaim::whereHas('beam', fn ($query) => $query->where('end', '>', now()))
->join(
'collections',
fn ($join) => $join->on('collections.id', '=', 'beam_claims.collection_id')
->when(
$beam,
fn ($query) => $query->where('collections.collection_chain_id', $beam->collection_chain_id)
$collectionId,
fn ($query) => $query->where('collections.collection_chain_id', $collectionId)
)
)->leftJoin(
'tokens',
Expand Down

0 comments on commit e99946e

Please sign in to comment.