Respond with an error if there are no items to export.

This commit is contained in:
Jeff Stieler 2019-06-26 10:25:12 -06:00
parent 1650794f2b
commit 916ea55ae9
2 changed files with 30 additions and 18 deletions

View File

@ -163,25 +163,35 @@ class WC_Admin_REST_Reports_Export_Controller extends WC_Admin_REST_Reports_Cont
$user_id = get_current_user_id();
$export_id = str_replace( '.', '', microtime( true ) );
WC_Admin_Report_Exporter::queue_report_export( $user_id, $export_id, $report_type, $report_args );
WC_Admin_Report_Exporter::update_export_percentage_complete( $user_id, $report_type, $export_id, 0 );
$total_rows = WC_Admin_Report_Exporter::queue_report_export( $user_id, $export_id, $report_type, $report_args );
// @todo: handle error case?
$result = array(
'status' => 'success',
'message' => __( 'Your report file is being generated.', 'woocommerce-admin' ),
);
if ( 0 === $total_rows ) {
$response = rest_ensure_response(
array(
'status' => 'error',
'message' => __( 'There is no data to export for the given request.', 'woocommerce-admin' ),
)
);
// Wrap the data in a response object.
$response = rest_ensure_response( $result );
// Include a link to the export status endpoint.
$response->add_links(
array(
'status' => array(
'href' => rest_url( sprintf( '%s/reports/%s/export/%s/status', $this->namespace, $report_type, $export_id ) ),
),
)
);
} else {
$response = rest_ensure_response(
array(
'status' => 'success',
'message' => __( 'Your report file is being generated.', 'woocommerce-admin' ),
)
);
// Include a link to the export status endpoint.
$response->add_links(
array(
'status' => array(
'href' => rest_url( sprintf( '%s/reports/%s/export/%s/status', $this->namespace, $report_type, $export_id ) ),
),
)
);
WC_Admin_Report_Exporter::update_export_percentage_complete( $user_id, $report_type, $export_id, 0 );
}
$data = $this->prepare_response_for_collection( $response );

View File

@ -72,7 +72,7 @@ class WC_Admin_Report_Exporter {
* @param string $export_id Unique ID for report (timestamp expected).
* @param string $report_type Report type. E.g. 'customers'.
* @param array $report_args Report parameters, passed to data query.
* @return void
* @return int Number of items to export.
*/
public static function queue_report_export( $user_id, $export_id, $report_type, $report_args = array() ) {
$exporter = new WC_Admin_Report_CSV_Exporter( $report_type, $report_args );
@ -99,6 +99,8 @@ class WC_Admin_Report_Exporter {
self::QUEUE_GROUP
);
}
return $total_rows;
}
/**