Skip to content

Commit

Permalink
Add price validation (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
enjinabner authored Jul 26, 2023
1 parent 259a7c4 commit 7485bc4
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 7 deletions.
1 change: 1 addition & 0 deletions lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
'future_block'=> 'The :attribute must be at least :block.',
'enough_token_supply' => 'The token supply is not enough.',
'listing_not_cancelled' => 'The listing is already cancelled.',
'minimum_price' => 'The minimum bidding price is :price.',
];
4 changes: 2 additions & 2 deletions src/GraphQL/Mutations/FillListingMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
use Closure;
use Enjin\Platform\GraphQL\Types\Input\Substrate\Traits\HasIdempotencyField;
use Enjin\Platform\Interfaces\PlatformBlockchainTransaction;
use Enjin\Platform\Marketplace\Rules\ListingNotCancelled;
use Enjin\Platform\Marketplace\Services\TransactionService;
use Enjin\Platform\Models\Transaction;
use Enjin\Platform\Rules\MaxBigInt;
use Enjin\Platform\Rules\MinBigInt;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use Rebing\GraphQL\Support\Facades\GraphQL;

class FillListingMutation extends Mutation implements PlatformBlockchainTransaction
Expand Down Expand Up @@ -83,7 +83,7 @@ protected function rules(array $args = []): array
'bail',
'filled',
'max:255',
Rule::exists('marketplace_listings', 'listing_id'),
new ListingNotCancelled(),
],
'amount' => [
'bail',
Expand Down
4 changes: 2 additions & 2 deletions src/GraphQL/Mutations/FinalizeAuctionMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
use Closure;
use Enjin\Platform\GraphQL\Types\Input\Substrate\Traits\HasIdempotencyField;
use Enjin\Platform\Interfaces\PlatformBlockchainTransaction;
use Enjin\Platform\Marketplace\Rules\ListingNotCancelled;
use Enjin\Platform\Marketplace\Services\TransactionService;
use Enjin\Platform\Models\Transaction;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use Rebing\GraphQL\Support\Facades\GraphQL;

class FinalizeAuctionMutation extends Mutation implements PlatformBlockchainTransaction
Expand Down Expand Up @@ -77,7 +77,7 @@ protected function rules(array $args = []): array
'bail',
'filled',
'max:255',
Rule::exists('marketplace_listings', 'listing_id'),
new ListingNotCancelled(),
],
];
}
Expand Down
6 changes: 4 additions & 2 deletions src/GraphQL/Mutations/PlaceBidMutation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
use Closure;
use Enjin\Platform\GraphQL\Types\Input\Substrate\Traits\HasIdempotencyField;
use Enjin\Platform\Interfaces\PlatformBlockchainTransaction;
use Enjin\Platform\Marketplace\Rules\ListingNotCancelled;
use Enjin\Platform\Marketplace\Rules\MinimumPrice;
use Enjin\Platform\Marketplace\Services\TransactionService;
use Enjin\Platform\Models\Transaction;
use Enjin\Platform\Rules\MaxBigInt;
use Enjin\Platform\Rules\MinBigInt;
use GraphQL\Type\Definition\ResolveInfo;
use GraphQL\Type\Definition\Type;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use Rebing\GraphQL\Support\Facades\GraphQL;

class PlaceBidMutation extends Mutation implements PlatformBlockchainTransaction
Expand Down Expand Up @@ -83,12 +84,13 @@ protected function rules(array $args = []): array
'bail',
'filled',
'max:255',
Rule::exists('marketplace_listings', 'listing_id'),
new ListingNotCancelled(),
],
'price' => [
'bail',
new MinBigInt(1),
new MaxBigInt(),
new MinimumPrice(),
],
];
}
Expand Down
48 changes: 48 additions & 0 deletions src/Rules/MinimumPrice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Enjin\Platform\Marketplace\Rules;

use Closure;
use Enjin\Platform\Marketplace\Models\Laravel\MarketplaceListing;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Support\Arr;

class MinimumPrice implements DataAwareRule, ValidationRule
{
/**
* All of the data under validation.
*
* @var array
*/
protected $data = [];

/**
* Set the data under validation.
*
* @param array $data
*
* @return $this
*/
public function setData($data)
{
$this->data = $data;

return $this;
}

/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if ($listingId = Arr::get($this->data, 'listingId')) {
$listing = MarketplaceListing::where('listing_id', $listingId)->first();
if ($listing && $value < $listing->price) {
$fail('enjin-platform-marketplace::validation.minimum_price')->translate(['price' => $listing->price]);
}
}
}
}
16 changes: 16 additions & 0 deletions tests/Feature/GraphQL/Mutations/FillListingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Enjin\Platform\Marketplace\Tests\Feature\GraphQL\Mutations;

use Enjin\Platform\Marketplace\Enums\ListingState;
use Enjin\Platform\Marketplace\Models\MarketplaceState;
use Enjin\Platform\Marketplace\Tests\Feature\GraphQL\TestCaseGraphQL;
use Enjin\Platform\Support\Hex;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -69,6 +71,20 @@ public function test_it_will_fail_with_invalid_parameter_listing_id(): void
['listingId' => ['The selected listing id is invalid.']],
$response['error']
);

MarketplaceState::create([
'state' => ListingState::CANCELLED->name,
'marketplace_listing_id' => $listing->id,
]);
$response = $this->graphql(
$this->method,
$data,
true
);
$this->assertArraySubset(
['listingId' => ['The listing is already cancelled.']],
$response['error']
);
}

public function test_it_will_fail_with_invalid_parameter_amount(): void
Expand Down
17 changes: 17 additions & 0 deletions tests/Feature/GraphQL/Mutations/FinalizeAuctionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Enjin\Platform\Marketplace\Tests\Feature\GraphQL\Mutations;

use Enjin\Platform\Marketplace\Enums\ListingState;
use Enjin\Platform\Marketplace\Models\MarketplaceState;
use Enjin\Platform\Marketplace\Tests\Feature\GraphQL\TestCaseGraphQL;
use Illuminate\Support\Str;

Expand Down Expand Up @@ -66,5 +68,20 @@ public function test_it_will_fail_with_invalid_parameter_listing_id(): void
['listingId' => ['The selected listing id is invalid.']],
$response['error']
);

$listing = $this->createListing();
MarketplaceState::create([
'state' => ListingState::CANCELLED->name,
'marketplace_listing_id' => $listing->id,
]);
$response = $this->graphql(
$this->method,
['listingId' => $listing->listing_id],
true
);
$this->assertArraySubset(
['listingId' => ['The listing is already cancelled.']],
$response['error']
);
}
}
28 changes: 27 additions & 1 deletion tests/Feature/GraphQL/Mutations/PlaceBidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Enjin\Platform\Marketplace\Tests\Feature\GraphQL\Mutations;

use Enjin\Platform\Marketplace\Enums\ListingState;
use Enjin\Platform\Marketplace\Models\MarketplaceState;
use Enjin\Platform\Marketplace\Tests\Feature\GraphQL\TestCaseGraphQL;
use Enjin\Platform\Support\Hex;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -74,7 +76,7 @@ public function test_it_will_fail_with_invalid_parameter_listing_id(): void
public function test_it_will_fail_with_invalid_parameter_price(): void
{
$listing = $this->createListing();
$data = ['listingId' => $listing->listing_id, 'price' => fake()->numberBetween(1, 1000)];
$data = ['listingId' => $listing->listing_id, 'price' => $price = fake()->numberBetween(1, 1000)];
$response = $this->graphql(
$this->method,
array_merge($data, ['price' => null]),
Expand Down Expand Up @@ -104,5 +106,29 @@ public function test_it_will_fail_with_invalid_parameter_price(): void
'Cannot represent following value as uint256: 1.1579208923732E+77',
$response['error']
);

$response = $this->graphql(
$this->method,
array_merge($data, ['price' => $listing->price - 1]),
true
);
$this->assertArraySubset(
['price' => ["The minimum bidding price is {$listing->price}."]],
$response['error']
);

MarketplaceState::create([
'state' => ListingState::CANCELLED->name,
'marketplace_listing_id' => $listing->id,
]);
$response = $this->graphql(
$this->method,
$data,
true
);
$this->assertArraySubset(
['listingId' => ['The listing is already cancelled.']],
$response['error']
);
}
}

0 comments on commit 7485bc4

Please sign in to comment.