Skip to content

Commit

Permalink
[PLA-1778] Add singleUseCodes filter to GetClaims query. (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
v16Studios authored May 30, 2024
1 parent b03d2e0 commit 91347ae
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 9 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"scripts": {
"build-sr25519": "cd vendor/gmajor/sr25519-bindings/go && go build -buildmode=c-shared -o sr25519.so . && mv sr25519.so ../src/Crypto/sr25519.so",
"analyse": "vendor/bin/phpstan analyse",
"fix": "vendor/bin/php-cs-fixer fix",
"test": "vendor/bin/phpunit",
"test-coverage": "vendor/bin/phpunit --coverage-html ../../temp/coverage",
"post-autoload-dump": [
Expand Down
1 change: 1 addition & 0 deletions lang/en/mutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
return [
'claim_beam.args.account' => 'The wallet account.',
'claim_beam.args.code' => 'The beam code.',
'claim_beam.args.single_use_code' => 'The beam single use code.',
'claim_beam.args.cryptoSignatureType' => 'The signature crypto type. This field is optional and it will use sr25519 by default.',
'claim_beam.args.signature' => 'The signed message.',
'claim_beam.description' => 'Mutation for claiming a beam.',
Expand Down
11 changes: 9 additions & 2 deletions src/GraphQL/Queries/GetClaimsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ public function args(): array
'codes' => [
'type' => GraphQL::type('[String]'),
'description' => __('enjin-platform-beam::mutation.claim_beam.args.code'),
'rules' => ['prohibits:ids'],
'rules' => ['prohibits:ids', 'array', 'max:100'],
],
'singleUseCodes' => [
'type' => GraphQL::type('[String]'),
'description' => __('enjin-platform-beam::mutation.claim_beam.args.single_use_code'),
'rules' => ['prohibits:ids', 'array', 'max:100'],
],
'accounts' => [
'type' => GraphQL::type('[String]'),
Expand All @@ -80,6 +85,7 @@ public function resolve(
return BeamClaim::loadSelectFields($resolveInfo, $this->name)
->when(Arr::get($args, 'ids'), fn ($query) => $query->whereIn('id', $args['ids']))
->when(Arr::get($args, 'codes'), fn ($query) => $query->hasCode($args['codes']))
->when(Arr::get($args, 'singleUseCodes'), fn ($query) => $query->hasSingleUseCode($args['singleUseCodes']))
->when(Arr::get($args, 'accounts'), fn ($query) => $query->whereIn(
'wallet_public_key',
collect($args['accounts'])->map(fn ($account) => SS58Address::getPublicKey($account))
Expand All @@ -95,7 +101,8 @@ protected function rules(array $args = []): array
{
return [
'ids.*' => [new MinBigInt(1), new MaxBigInt()],
'codes.*' => ['max:1024'],
'codes.*' => ['max:32'],
'singleUseCodes.*' => ['max:512'],
'accounts.*' => [new ValidSubstrateAccount()],
];
}
Expand Down
11 changes: 6 additions & 5 deletions src/Models/Laravel/BeamClaim.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Enjin\Platform\Beam\Database\Factories\BeamClaimFactory;
use Enjin\Platform\Beam\Enums\BeamFlag;
use Enjin\Platform\Beam\Enums\BeamRoute;
use Enjin\Platform\Beam\Models\Laravel\Traits\HasSingleUseCodeScope;
use Enjin\Platform\Beam\Services\BeamService;
use Enjin\Platform\Models\BaseModel;
use Enjin\Platform\Models\Laravel\Collection;
Expand All @@ -25,15 +26,15 @@

class BeamClaim extends BaseModel
{
use HasEagerLimit;
use HasFactory;
use HasSingleUseCodeScope;
use MassPrunable;
use SoftDeletes;
use Traits\EagerLoadSelectFields;
use Traits\HasBeamQr;
use Traits\HasCodeScope;
use MassPrunable;
use Traits\HasClaimable;
use Traits\EagerLoadSelectFields;
use HasEagerLimit;

use Traits\HasCodeScope;

/**
* The attributes that aren't mass assignable.
Expand Down
29 changes: 29 additions & 0 deletions src/Models/Laravel/Traits/HasSingleUseCodeScope.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Enjin\Platform\Beam\Models\Laravel\Traits;

use Illuminate\Contracts\Database\Eloquent\Builder;
use Illuminate\Support\Arr;

trait HasSingleUseCodeScope
{
/**
* Local scope for beam code.
*/
public function scopeHasSingleUseCode(Builder $query, string|array|null $code): Builder
{
try {
if (is_array($code)) {
$singleUseCode = array_map(function ($item) {
return explode(':', decrypt($item))[0];
}, $code);
} else {
$singleUseCode = explode(':', decrypt($code))[0];
}

return $query->whereIn('code', Arr::wrap($singleUseCode));
} catch (\Throwable $exception) {
return $query;
}
}
}
45 changes: 43 additions & 2 deletions tests/Feature/GraphQL/Queries/GetClaimsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,24 @@ public function test_it_can_get_claims_with_codes(): void
$this->assertNotEmpty($response['totalCount']);
}

/**
* Test get beam with codes.
*/
public function test_it_can_get_claims_with_single_use_codes(): void
{
$response = $this->graphql($this->method, ['singleUseCodes' => [$this->beam->claims[0]->singleUseCode]]);
$this->assertNotEmpty($response['totalCount']);
}

/**
* Test get beam with codes.
*/
public function test_it_can_get_claims_with_multiple_single_use_codes(): void
{
$response = $this->graphql($this->method, ['singleUseCodes' => [$this->beam->claims[0]->singleUseCode, $this->beam->claims[1]->singleUseCode]]);
$this->assertNotEmpty($response['totalCount']);
}

/**
* Test get beam with accounts.
*/
Expand All @@ -74,15 +92,38 @@ public function test_it_can_get_claims_with_statuses(): void
*/
public function test_will_fail_with_invalid_parameters(): void
{
$codes = array_fill(0, 10, fake()->text(32));
$codes[0] = fake()->text(2000);
$response = $this->graphql($this->method, [
'ids' => [1],
'codes' => [fake()->text(2000)],
'codes' => $codes,
'singleUseCodes' => [fake()->text(2000)],
], true);

$this->assertArraySubset([
'ids' => ['The ids field prohibits codes from being present.'],
'codes' => ['The codes field prohibits ids from being present.'],
'codes.0' => ['The codes.0 field must not be greater than 1024 characters.'],
'codes.0' => ['The codes.0 field must not be greater than 32 characters.'],
'singleUseCodes' => ['The single use codes field prohibits ids from being present.'],
'singleUseCodes.0' => ['The singleUseCodes.0 field must not be greater than 512 characters.'],
], $response['error']);
}

/**
* Test get claims too many codes.
*/
public function test_will_fail_with_too_many_codes(): void
{
$codes = array_fill(0, 101, fake()->text(32));
$singleUseCodes = array_fill(0, 101, fake()->text(384));
$response = $this->graphql($this->method, [
'codes' => $codes,
'singleUseCodes' => $singleUseCodes,
], true);

$this->assertArraySubset([
'codes' => ['The codes field must not have more than 100 items.'],
'singleUseCodes' => ['The single use codes field must not have more than 100 items.'],
], $response['error']);
}
}
2 changes: 2 additions & 0 deletions tests/Feature/GraphQL/Resources/GetClaims.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
query GetClaims(
$ids: [BigInt]
$codes: [String]
$singleUseCodes: [String]
$accounts: [String]
$states: [ClaimStatus]
$after: String
Expand All @@ -9,6 +10,7 @@ query GetClaims(
GetClaims(
ids: $ids
codes: $codes
singleUseCodes: $singleUseCodes
accounts: $accounts
states: $states
after: $after
Expand Down

0 comments on commit 91347ae

Please sign in to comment.