Skip to content

Commit

Permalink
[#82] Start Auction by sending to Auctioneer (#116)
Browse files Browse the repository at this point in the history
* [#81] Set and Get the GUID for Auctions

* [#81] Added Auction End Date/Time field

* [#81] API access method + documentation

* Fix a PHP bug.

* VS Code settings and extensions recommendations

* Update Auction Meta Box, Determine Auction Status

* Just some PR feedback that got left off.

* [#26] Data validation, restructure, & Status Column

* [#26] Added Time Until info

* Styling tweaks

* [#26] Update Auction meta when auction has started.

* [#26] Cron hook documentation

* [#26] Last minute tweaks.

* [#26] Better method name.

* [#26] Removed a space

* [#26] Swap API for $this

* [#26] More last second fixes.

* [N/A] Modified Auction Metrics block.

* [N/A] Last change to Pattern

* [#82] Add conditional trigger wrapper to Auction Start cron job.

* [#82] Bad merge

* [#82] Auctioneer class

* [#82] WIP: Trigger Start of Auction to Auctioneer

* [#82] Catch DateTime Exception

* [#82] Some Cleanup and Documentation

* [#82] Endpoint adjustment, stripped down payload, removed msg check.

* [#82] Change to Auction Start endpoint

* [#82] Added 1 last check for the guid

* [#82] Move constants to config.json

---------

Co-authored-by: Nathan Schmidt <[email protected]>
  • Loading branch information
bd-viget and nathan-schmidt-viget authored Jan 5, 2024
1 parent b97d92f commit 259cece
Show file tree
Hide file tree
Showing 6 changed files with 409 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ vendor
# PhpStorm
.idea/

# Credentials
# Sensitive data
auth.json
client-mu-plugins/vip-env-vars.local.php
31 changes: 29 additions & 2 deletions client-mu-plugins/goodbids/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,36 @@ Returns the current Auction ID (if valid).
`goodbids()->auctions->get_setting( string $setting, int $auction_id )`
Returns a setting value for an auction. If `$auction_id` is not provided, the current post ID will be used.

`goodbids()->auctions->get_bid_product_id( int $auction_id )`
Returns the Auction's Bid Product ID. If `$auction_id` is not provided, the current post ID will be used.

`goodbids()->auctions->get_bid_product( int $auction_id )`
Returns the Auction's Bid Product object. If `$auction_id` is not provided, the current post ID will be used.

`goodbids()->auctions->get_reward_product_id( int $auction_id )`
Returns the Auction's Reward Product ID. If `$auction_id` is not provided, the current post ID will be used.

`goodbids()->auctions->get_reward_product( int $auction_id )`
Returns the Auction's Reward Product object. If `$auction_id` is not provided, the current post ID will be used.

`goodbids()->auctions->get_product_type( int $product_id )`
Returns the type of the given product ID. Available types are: "bids" and "rewards".

`goodbids()->auctions->get_estimated_value( int $auction_id )`
Returns the Auction Reward's Estimated Value. If `$auction_id` is not provided, the current post ID will be used.

`goodbids()->auctions->get_start_date_time( int $auction_id )`
`goodbids()->auctions->get_start_date_time( int $auction_id, string $format )`
Returns the Auction's Start Date/Time in MySQL format. If `$auction_id` is not provided, the current post ID will be used.

`goodbids()->auctions->get_end_date_time( int $auction_id )`
`goodbids()->auctions->get_end_date_time( int $auction_id, string $format )`
Returns the Auction's End Date/Time in MySQL format. If `$auction_id` is not provided, the current post ID will be used.

`goodbids()->auctions->has_started( int $auction_id )`
Checks if the Auction has started. If `$auction_id` is not provided, the current post ID will be used.

`goodbids()->auctions->has_ended( int $auction_id )`
Checks if the Auction has ended. If `$auction_id` is not provided, the current post ID will be used.

`goodbids()->auctions->get_bid_increment( int $auction_id )`
Returns the Auction's Bid Increment value. If `$auction_id` is not provided, the current post ID will be used.

Expand All @@ -76,6 +91,18 @@ Returns the Auction's Expected High Bid value. If `$auction_id` is not provided,
`goodbids()->auctions->get_guid( int $auction_id )`
Returns the Auction's Unique GUID. If `$auction_id` is not provided, the current post ID will be used.

`goodbids()->auctions->get_bid_orders( int $auction_id, int $limit )`
Returns an array of Bid Order IDs for the given Auction ID. `$limit` can be specified to return a set number of orders.

`goodbids()->auctions->get_bid_order( int $auction_id, int $limit )`
Returns an array of Bid Order objects for the given Auction ID. `$limit` can be specified to return a set number of orders.

`goodbids()->auctions->get_last_bid( int $auction_id )`
Returns the Bid Order object of the last bid for the given Auction ID.

`goodbids()->auctions->get_status( int $auction_id )`
Returns the status of the Auction. Possible values are: "Upcoming", "Live", and "Closed". If the Auction has not yet been published, it will return "Draft".

### Bids Functions

`goodbids()->auctions->bids->get_auction_id( int $bid_product_id )`
Expand Down
9 changes: 8 additions & 1 deletion client-mu-plugins/goodbids/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,12 @@
"accessibility-checker",
"svg-support",
"user-switching"
]
],
"vip-constants": {
"auctioneer": {
"local": "GOODBIDS_AUCTIONEER_URL_LOCAL",
"develop": "GOODBIDS_AUCTIONEER_URL_DEVELOP",
"production": "GOODBIDS_AUCTIONEER_URL_PRODUCTION"
}
}
}
241 changes: 241 additions & 0 deletions client-mu-plugins/goodbids/src/classes/Auctioneer/Auctioneer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
<?php
/**
* The Auctioneer functionality
*
* @since 1.0.0
* @package GoodBids
*/

namespace GoodBids\Auctioneer;

/**
* Class for Auctioneer, our Node.js server
*
* @since 1.0.0
*/
class Auctioneer {

/**
* Defines the environment to use.
*
* @since 1.0.0
* @var string
*/
private string $environment = 'develop';

/**
* @since 1.0.0
* @var ?string
*/
private ?string $url = null;

/**
* @since 1.0.0
* @var bool
*/
private bool $initialized = false;

/**
* Initialize Auctioneer
*
* @since 1.0.0
*/
public function __construct() {
$environments = goodbids()->get_config( 'vip-constants.auctioneer' );

if ( ! $environments || empty( $environments[ $this->environment ] ) ) {
add_action(
'admin_notices',
function() {
printf(
'<div class="notice notice-error is-dismissible">
<p>%s</p>
</div>',
esc_html__( 'Missing Auctioneer constants config.', 'goodbids' )
);
}
);

return;
}

$this->url = vip_get_env_var( $environments[ $this->environment ], null );

// Abort if missing environment variable.
if ( ! $this->url ) {
add_action(
'admin_notices',
function() {
printf(
'<div class="notice notice-error is-dismissible">
<p>%s</p>
</div>',
esc_html__( 'Missing Auctioneer environment variables.', 'goodbids' )
);
}
);

return;
}

$this->init();
}

/**
* Run initialization tasks
*
* @since 1.0.0
*
* @return void
*/
private function init(): void {
$this->initialized = true;
}

/**
* Make a request to an API endpoint
*
* @since 1.0.0
*
* @param string $endpoint
* @param array $params
* @param string $method
*
* @return ?array
*/
private function request( string $endpoint, array $params = [], string $method = 'GET' ): ?array {
$url = trailingslashit( $this->url ) . $endpoint;
$response = wp_remote_request(
$url,
[
'method' => $method,
'headers' => [
'Content-Type' => 'application/json',
],
'body' => wp_json_encode( $params ),
]
);

if ( ! is_array( $response ) ) {
return null;
}

return $response;
}

/**
* Trigger an Auction Start event for an Auction
*
* @since 1.0.0
*
* @param int $auction_id
*
* @return array|bool
*/
public function auction_start( int $auction_id ): array|bool {
$guid = goodbids()->auctions->get_guid( $auction_id );

if ( ! $guid ) {
// TODO: Log error.
return false;
}

$endpoint = sprintf( 'auctions/%s/start', $guid );
$bid_product = goodbids()->auctions->get_bid_product( $auction_id );

// TODO: Refactor using individual Response classes.
$payload = [
'id' => $guid,
'startTime' => goodbids()->auctions->get_start_date_time( $auction_id, 'c' ),
'endTime' => goodbids()->auctions->get_end_date_time( $auction_id, 'c' ),
'currentBid' => floatval( $bid_product?->get_price( 'edit' ) ),
'requestTime' => current_datetime()->format( 'c' ),
];

$response = $this->request( $endpoint, $payload, 'POST' );

if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
// TODO: Log Response
error_log( '[GB] ' . wp_strip_all_tags( $this->get_response_message( $response ) ) );
return $response;
}

return true;
}

/**
* Get the message from the response array.
*
* @since 1.0.0
*
* @param array $response
*
* @return string
*/
public function get_response_message( array $response ): string {
$data = $this->get_response_json( $response );
$msg_raw = $this->get_response_message_raw( $response );

if ( ! $msg_raw ) {
return '';
}

$message = sprintf(
'<p><strong>%s:</strong></p>',
esc_html( $msg_raw )
);

if ( ! empty( $data['details'] ) ) {
$message .= sprintf(
'<ul><li>%s</li></ul>',
is_array( $data['details'] ) ? wp_kses_post( implode( '</li><li>', $data['details'] ) ) : esc_html( $data['details'] )
);
}

return $message;
}

/**
* Get the raw value for the message from the response array.
*
* @since 1.0.0
*
* @param array $response
*
* @return string
*/
public function get_response_message_raw( array $response ): string {
$data = $this->get_response_json( $response );

if ( empty( $data['message'] ) ) {
return '';
}

return $data['message'];
}

/**
* Get the Response JSON as an array
*
* @since 1.0.0
*
* @param array $response
*
* @return ?array
*/
public function get_response_json( array $response ): ?array {
$body = wp_remote_retrieve_body( $response );

if ( ! $body ) {
return null;
}

$data = json_decode( $body, true );

if ( ! $data ) {
return null;
}

return $data;
}
}
Loading

0 comments on commit 259cece

Please sign in to comment.