Skip to content

Commit

Permalink
Add validation for cancelled listing (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
enjinabner authored Jul 24, 2023
1 parent 5a49047 commit 259a7c4
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
1 change: 1 addition & 0 deletions lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
return [
'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.',
];
4 changes: 2 additions & 2 deletions src/GraphQL/Mutations/CancelListingMutation.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 CancelListingMutation 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
29 changes: 29 additions & 0 deletions src/Rules/ListingNotCancelled.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Enjin\Platform\Marketplace\Rules;

use Closure;
use Enjin\Platform\Marketplace\Enums\ListingState;
use Enjin\Platform\Marketplace\Models\Laravel\MarketplaceListing;
use Illuminate\Contracts\Validation\ValidationRule;

class ListingNotCancelled implements ValidationRule
{
/**
* Run the validation rule.
*
* @param \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (!$listing = MarketplaceListing::where('listing_id', $value)->with('state')->first()) {
$fail('validation.exists')->translate();

return;
}

if ($listing->state?->state === ListingState::CANCELLED->name) {
$fail('enjin-platform-marketplace::validation.listing_not_cancelled')->translate();
}
}
}
18 changes: 18 additions & 0 deletions tests/Feature/GraphQL/Mutations/CancelListingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

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

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

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

$listing = MarketplaceListing::factory()->create(['seller_wallet_id' => $this->wallet->id]);
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']
);
}
}

0 comments on commit 259a7c4

Please sign in to comment.