Skip to content

Commit

Permalink
Add remove beam pack tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
enjinabner committed Aug 5, 2024
1 parent befef64 commit 0fd5c15
Show file tree
Hide file tree
Showing 11 changed files with 432 additions and 157 deletions.
30 changes: 30 additions & 0 deletions database/factories/BeamPackFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Enjin\Platform\Beam\Database\Factories;

use Enjin\Platform\Beam\Models\BeamPack;
use Illuminate\Database\Eloquent\Factories\Factory;

class BeamPackFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var BeamPack
*/
protected $model = BeamPack::class;

/**
* Define the model's default state.
*
* @return array<string, mixed>
*/
public function definition()
{
return [
'is_claimed' => fake()->boolean(),
'code' => fake()->text(),
'nonce' => 1,
];
}
}
2 changes: 2 additions & 0 deletions lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@
'tokens_doesnt_exist_in_beam' => 'The :attribute already exist in beam.',
'tokens_exist_in_beam' => 'The :attribute doesn\'t exist in beam.',
'not_owner' => 'The :attribute should not be the owner of the collection.',
'beam_pack_exist_in_beam' => 'The :attribute doesn\'t exist in beam.',
'tokens_exist_in_beam_pack' => 'The :attribute doesn\'t exist in beam pack.',
];
66 changes: 64 additions & 2 deletions src/GraphQL/Mutations/RemoveTokensMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@
namespace Enjin\Platform\Beam\GraphQL\Mutations;

use Closure;
use Enjin\Platform\Beam\Models\Beam;
use Enjin\Platform\Beam\Rules\BeamExists;
use Enjin\Platform\Beam\Rules\BeamPackExistInBeam;
use Enjin\Platform\Beam\Rules\TokensExistInBeam;
use Enjin\Platform\Beam\Rules\TokensExistInBeamPack;
use Enjin\Platform\Beam\Services\BeamService;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Rebing\GraphQL\Support\Facades\GraphQL;

Expand Down Expand Up @@ -43,9 +47,13 @@ public function args(): array
'description' => __('enjin-platform-beam::mutation.claim_beam.args.code'),
],
'tokenIds' => [
'type' => GraphQL::type('[IntegerRangeString!]!'),
'type' => GraphQL::type('[IntegerRangeString!]'),
'description' => __('enjin-platform-beam::mutation.remove_tokens.args.tokenIds'),
],
'packs' => [
'type' => GraphQL::type('[RemoveBeamPack!]'),
'description' => __('enjin-platform-beam::input_type.remove_beam_pack.description'),
],
];
}

Expand All @@ -60,21 +68,43 @@ public function resolve(
Closure $getSelectFields,
BeamService $beam
) {
return DB::transaction(fn () => $beam->removeTokens($args['code'], $args['tokenIds']));
return DB::transaction(
fn () => $beam->removeTokens(
$args['code'],
Arr::get($args, 'tokenIds', []),
Arr::get($args, 'packs', []),
)
);
}

/**
* Get the mutation's request validation rules.
*/
protected function rules(array $args = []): array
{
$beam = Beam::whereCode($args['code'])->first();

return [
'code' => [
'filled',
'max:1024',
new BeamExists(),
],
...match (true) {
!$beam => [],
!$beam?->is_pack => $this->tokenIdRules(),
default => $this->packTokenIdRules(),
},
];
}

protected function tokenIdRules(): array
{
return [
'tokenIds' => [
'bail',
'required',
'prohibits:packs',
'array',
'min:1',
'max:1000',
Expand All @@ -87,4 +117,36 @@ protected function rules(array $args = []): array
],
];
}

protected function packTokenIdRules(): array
{
return [
'packs' => [
'bail',
'required',
'prohibits:tokens',
'array',
'min:1',
'max:1000',
],
'packs.*.id' => [
'filled',
'integer',
'distinct',
new BeamPackExistInBeam(),
],
'packs.*.tokenIds' => [
'array',
'min:1',
'max:1000',
'distinct',
],
'packs.*.tokenIds.*' => [
'bail',
'filled',
'distinct',
new TokensExistInBeamPack(),
],
];
}
}
36 changes: 36 additions & 0 deletions src/GraphQL/Types/Input/RemoveBeamPackType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Enjin\Platform\Beam\GraphQL\Types\Input;

use Rebing\GraphQL\Support\Facades\GraphQL;

class RemoveBeamPackType extends InputType
{
/**
* Get the input type's attributes.
*/
public function attributes(): array
{
return [
'name' => 'RemoveBeamPack',
'description' => __('enjin-platform-beam::input_type.remove_beam_pack.description'),
];
}

/**
* Get the input type's fields.
*/
public function fields(): array
{
return [
'id' => [
'type' => GraphQL::type('Int!'),
'description' => __('enjin-platform-beam::input_type.beam_pack.field.id'),
],
'tokenIds' => [
'type' => GraphQL::type('[IntegerRangeString!]'),
'description' => __('enjin-platform-beam::mutation.remove_tokens.args.tokenIds'),
],
];
}
}
9 changes: 9 additions & 0 deletions src/Models/Laravel/BeamClaim.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class BeamClaim extends BaseModel
'beam_id',
'nonce',
'code',
'beam_pack_id',
];

/**
Expand Down Expand Up @@ -129,6 +130,14 @@ public function collection(): BelongsTo
return $this->belongsTo(Collection::class);
}

/**
* The Beam Pack's relationship.
*/
public function beamPack(): BelongsTo
{
return $this->belongsTo(BeamPack::class, 'beam_pack_id');
}

/**
* Local scope for single use.
*/
Expand Down
Loading

0 comments on commit 0fd5c15

Please sign in to comment.