Skip to content

Commit

Permalink
Merge pull request #2590 from woocommerce/update/shipping-time-contro…
Browse files Browse the repository at this point in the history
…llers-to-work-with-max-time

Update the shipping time controllers to handle the maximum shipping time
  • Loading branch information
jorgemd24 authored Sep 15, 2024
2 parents 351bec5 + af40ca3 commit 8ffaa67
Show file tree
Hide file tree
Showing 5 changed files with 401 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected function get_batch_create_callback(): callable {
return function ( Request $request ) {
$country_codes = $request->get_param( 'country_codes' );
$time = $request->get_param( 'time' );
$max_time = $request->get_param( 'max_time' );

$responses = [];
$errors = [];
Expand All @@ -62,6 +63,7 @@ protected function get_batch_create_callback(): callable {
[
'country_code' => $country_code,
'time' => $time,
'max_time' => $max_time,
]
);

Expand Down
70 changes: 65 additions & 5 deletions src/API/Site/Controllers/MerchantCenter/ShippingTimeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Automattic\WooCommerce\GoogleListingsAndAds\Vendor\Psr\Container\ContainerInterface;
use WP_REST_Request as Request;
use WP_REST_Response as Response;
use WP_Error;

defined( 'ABSPATH' ) || exit;

Expand Down Expand Up @@ -61,7 +62,7 @@ public function register_routes(): void {
'methods' => TransportMethods::CREATABLE,
'callback' => $this->get_create_time_callback(),
'permission_callback' => $this->get_permission_callback(),
'args' => $this->get_schema_properties(),
'args' => $this->get_args_schema(),
],
'schema' => $this->get_api_response_schema_callback(),
]
Expand Down Expand Up @@ -100,6 +101,7 @@ protected function get_read_times_callback(): callable {
[
'country_code' => $time['country'],
'time' => $time['time'],
'max_time' => $time['max_time'],
],
$request
);
Expand Down Expand Up @@ -134,6 +136,7 @@ protected function get_read_time_callback(): callable {
[
'country_code' => $time[0]['country'],
'time' => $time[0]['time'],
'max_time' => $time[0]['max_time'],
],
$request
);
Expand All @@ -153,8 +156,9 @@ protected function get_create_time_callback(): callable {

try {
$data = [
'country' => $country_code,
'time' => $request->get_param( 'time' ),
'country' => $country_code,
'time' => $request->get_param( 'time' ),
'max_time' => $request->get_param( 'max_time' ),
];
if ( $existing ) {
$query->update(
Expand Down Expand Up @@ -265,13 +269,69 @@ protected function get_schema_properties(): array {
],
'time' => [
'type' => 'integer',
'description' => __( 'The shipping time in days.', 'google-listings-and-ads' ),
'description' => __( 'The minimum shipping time in days.', 'google-listings-and-ads' ),
'context' => [ 'view', 'edit' ],
'validate_callback' => [ $this, 'validate_shipping_times' ],
],
'max_time' => [
'type' => 'integer',
'description' => __( 'The maximum shipping time in days.', 'google-listings-and-ads' ),
'context' => [ 'view', 'edit' ],
'validate_callback' => 'rest_validate_request_arg',
'validate_callback' => [ $this, 'validate_shipping_times' ],
],
];
}

/**
* Get the args schema for the controller.
*
* @return array
*/
protected function get_args_schema(): array {
$schema = $this->get_schema_properties();
$schema['time']['required'] = true;
$schema['max_time']['required'] = true;
return $schema;
}

/**
* Validate the shipping times.
*
* @param mixed $value
* @param Request $request
* @param string $param
*
* @return WP_Error|true
*/
public function validate_shipping_times( $value, $request, $param ) {
$time = $request->get_param( 'time' );
$max_time = $request->get_param( 'max_time' );

if ( rest_is_integer( $value ) === false ) {
return new WP_Error(
'rest_invalid_type',
/* translators: 1: Parameter, 2: Type name. */
sprintf( __( '%1$s is not of type %2$s.', 'google-listings-and-ads' ), $param, 'integer' ),
[ 'param' => $param ]
);
}

if ( ! $value ) {
return new WP_Error( 'invalid_shipping_times', __( 'Shipping times are required.', 'google-listings-and-ads' ), [ 'param' => $param ] );
}

if ( $value < 0 ) {
return new WP_Error( 'invalid_shipping_times', __( 'Shipping times cannot be negative.', 'google-listings-and-ads' ), [ 'param' => $param ] );
}

if ( $time > $max_time ) {
return new WP_Error( 'invalid_shipping_times', __( 'The minimum shipping time cannot be greater than the maximum shipping time.', 'google-listings-and-ads' ), [ 'param' => $param ] );
}

return true;
}


/**
* Get the item schema name for the controller.
*
Expand Down
7 changes: 4 additions & 3 deletions src/DB/Table/ShippingTimeTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@ public static function get_raw_name(): string {
*/
public function get_columns(): array {
return [
'id' => true,
'country' => true,
'time' => true,
'id' => true,
'country' => true,
'time' => true,
'max_time' => true,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public function test_create_shipping_time_batch() {
$payload = [
'country_codes' => [ 'US', 'GB' ],
'time' => 5,
'max_time' => 10,
];

$response = $this->do_request( self::ROUTE_SHIPPING_TIME_BATCH, 'POST', $payload );
Expand Down
Loading

0 comments on commit 8ffaa67

Please sign in to comment.