2018-12-04 02:58:07 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-07-16 08:56:24 +00:00
|
|
|
import { addQueryArgs } from '@wordpress/url';
|
2018-12-04 02:58:07 +00:00
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
2019-01-30 07:22:10 +00:00
|
|
|
import { getResourceIdentifier, getResourcePrefix } from 'wc-api/utils';
|
2019-02-07 22:39:56 +00:00
|
|
|
import { NAMESPACE } from 'wc-api/constants';
|
2018-12-04 02:58:07 +00:00
|
|
|
|
2019-01-21 15:46:21 +00:00
|
|
|
const statEndpoints = [
|
|
|
|
'coupons',
|
|
|
|
'downloads',
|
|
|
|
'orders',
|
|
|
|
'products',
|
|
|
|
'revenue',
|
2019-02-14 03:31:27 +00:00
|
|
|
'stock',
|
2019-01-21 15:46:21 +00:00
|
|
|
'taxes',
|
|
|
|
'customers',
|
|
|
|
];
|
2018-12-04 02:58:07 +00:00
|
|
|
|
2018-12-04 17:22:46 +00:00
|
|
|
const typeEndpointMap = {
|
|
|
|
'report-stats-query-orders': 'orders',
|
|
|
|
'report-stats-query-revenue': 'revenue',
|
|
|
|
'report-stats-query-products': 'products',
|
|
|
|
'report-stats-query-categories': 'categories',
|
2018-12-19 11:18:43 +00:00
|
|
|
'report-stats-query-downloads': 'downloads',
|
2018-12-04 17:22:46 +00:00
|
|
|
'report-stats-query-coupons': 'coupons',
|
2019-02-14 03:31:27 +00:00
|
|
|
'report-stats-query-stock': 'stock',
|
2018-12-04 17:22:46 +00:00
|
|
|
'report-stats-query-taxes': 'taxes',
|
2019-01-21 15:46:21 +00:00
|
|
|
'report-stats-query-customers': 'customers',
|
2018-12-04 17:22:46 +00:00
|
|
|
};
|
|
|
|
|
2018-12-04 02:58:07 +00:00
|
|
|
function read( resourceNames, fetch = apiFetch ) {
|
2018-12-04 17:22:46 +00:00
|
|
|
const filteredNames = resourceNames.filter( name => {
|
|
|
|
const prefix = getResourcePrefix( name );
|
|
|
|
return Boolean( typeEndpointMap[ prefix ] );
|
|
|
|
} );
|
2018-12-04 02:58:07 +00:00
|
|
|
|
|
|
|
return filteredNames.map( async resourceName => {
|
2018-12-04 17:22:46 +00:00
|
|
|
const prefix = getResourcePrefix( resourceName );
|
|
|
|
const endpoint = typeEndpointMap[ prefix ];
|
|
|
|
const query = getResourceIdentifier( resourceName );
|
2018-12-05 17:05:31 +00:00
|
|
|
|
2018-12-05 19:10:43 +00:00
|
|
|
const fetchArgs = {
|
|
|
|
parse: false,
|
|
|
|
};
|
2018-12-04 02:58:07 +00:00
|
|
|
|
2019-02-07 22:39:56 +00:00
|
|
|
if ( statEndpoints.indexOf( endpoint ) >= 0 ) {
|
2019-07-16 08:56:24 +00:00
|
|
|
fetchArgs.path = addQueryArgs( `${ NAMESPACE }/reports/${ endpoint }/stats`, query );
|
2018-12-05 19:10:43 +00:00
|
|
|
} else {
|
2019-07-16 08:56:24 +00:00
|
|
|
fetchArgs.path = addQueryArgs( endpoint, query );
|
2018-12-04 02:58:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2018-12-05 19:10:43 +00:00
|
|
|
const response = await fetch( fetchArgs );
|
2018-12-04 02:58:07 +00:00
|
|
|
const report = await response.json();
|
|
|
|
const totalResults = parseInt( response.headers.get( 'x-wp-total' ) );
|
|
|
|
const totalPages = parseInt( response.headers.get( 'x-wp-totalpages' ) );
|
|
|
|
|
|
|
|
return {
|
|
|
|
[ resourceName ]: {
|
|
|
|
data: report,
|
|
|
|
totalResults,
|
|
|
|
totalPages,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
} catch ( error ) {
|
|
|
|
return { [ resourceName ]: { error } };
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
read,
|
|
|
|
};
|