Skip to content

Commit

Permalink
Check highest bid (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
enjinabner authored Jul 28, 2023
1 parent 7485bc4 commit c85facd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/Rules/MinimumPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,13 @@ public function setData($data)
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]);
if (!$listing = MarketplaceListing::where('listing_id', $listingId)->with('highestBid')->first()) {
return;
}

$price = $listing?->highestBid?->price ?? $listing?->price;
if ($value < $price) {
$fail('enjin-platform-marketplace::validation.minimum_price')->translate(['price' => $price]);
}
}
}
Expand Down
20 changes: 17 additions & 3 deletions tests/Feature/GraphQL/Mutations/PlaceBidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,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' => $price = fake()->numberBetween(1, 1000)];
$data = ['listingId' => $listing->listing_id, 'price' => fake()->numberBetween(1, 1000)];
$response = $this->graphql(
$this->method,
array_merge($data, ['price' => null]),
Expand Down Expand Up @@ -107,13 +107,27 @@ public function test_it_will_fail_with_invalid_parameter_price(): void
$response['error']
);

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

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

Expand Down

0 comments on commit c85facd

Please sign in to comment.