All files / api getTopPerformersData.js

81.81% Statements 36/44
57.14% Branches 24/42
87.5% Functions 7/8
81.81% Lines 36/44

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165                                        1x                                         1x 6x                   1x 9x   7x   1x   1x                               1x 9x 1x       8x   8x 2x     6x   5x       1x                             1x 9x 8x 8x 8x             1x 1x                     1x 9x   9x 9x 9x                   9x         6x     9x 9x 9x     9x   9x      
import { getData } from '../utils/api';
import {
	formatCurrency,
	getCountryName,
	formatCurrencyCompact
} from '../utils/formatting';
import { resolveChangeIndicator } from '../utils/changeIndicator';
import { __ } from '@wordpress/i18n';
 
/**
 * Get top performers
 *
 * @param { Object } args           - The arguments object.
 * @param { string } args.startDate - The start date for the data range.
 * @param { string } args.endDate   - The end date for the data range.
 * @param { string } args.range     - The range of data to retrieve.
 * @param { Object } args.filters   - Additional filters to apply to the data.
 *
 * @return {Promise<*>} The formatted number of live visitors.
 */
const getTopPerformers = async( args ) => {
	const { startDate, endDate, range, filters } = args;
	const { data } = await getData(
		'ecommerce/top-performers',
		startDate,
		endDate,
		range,
		{
			filters
		}
	);
	return data;
};
 
/**
 * Get the value for a top performer metric from a period's data.
 *
 * @param {string} selectedOption The selected option ('revenue' or 'count').
 * @param {Object} data           The current or previous data object.
 * @return {number} The metric value.
 */
const getMetricValue = ( selectedOption, data ) =>
	'revenue' === selectedOption ?
		data.total_revenue ?? 0 :
		data.total_quantity_sold ?? 0;
 
/**
 * Get the default subtitle for a top performer metric.
 *
 * @param {string} name The metric name.
 * @return {string} The default subtitle.
 */
const getDefaultSubtitle = ( name ) => {
	switch ( name ) {
		case 'top-product':
			return __( 'No product data available', 'burst-statistics' );
		case 'top-device':
			return __( 'No device data available', 'burst-statistics' );
		case 'top-country':
			return __( 'No country data available', 'burst-statistics' );
		case 'top-campaign':
			return __( 'No campaign data available', 'burst-statistics' );
		default:
			return __( 'No data available', 'burst-statistics' );
	}
};
 
/**
 * Get the subtitle for a top performer metric.
 *
 * @param {string} name            The metric name.
 * @param {Object} current         The current data object.
 * @param {string} defaultSubtitle The fallback subtitle.
 * @return {string} The subtitle.
 */
const getPerformerSubtitle = ( name, current, defaultSubtitle ) => {
	if ( ! current ) {
		return defaultSubtitle;
	}
 
	const hasSales =
		0 < ( current.total_revenue ?? 0 ) ||
		0 < ( current.total_quantity_sold ?? 0 );
	if ( ! hasSales ) {
		return defaultSubtitle;
	}
 
	switch ( name ) {
		case 'top-product':
			return current.product_name || defaultSubtitle;
		case 'top-device':
			return current.device_name || defaultSubtitle;
		case 'top-country':
			return getCountryName( current.country_code );
		case 'top-campaign':
			return current.campaign_name || defaultSubtitle;
		default:
			return defaultSubtitle;
	}
};
 
/**
 * Build the value fields for a top performer metric.
 *
 * @param {string} selectedOption The selected option ('revenue' or 'count').
 * @param {Object} current        The current data object.
 * @return {Object} The value, exactValue and (for revenue) tooltipText.
 */
const buildPerformerValue = ( selectedOption, current ) => {
	if ( 'revenue' === selectedOption ) {
		const revenue = current?.total_revenue ?? 0;
		const currency = current?.currency ?? 'USD';
		return {
			value: formatCurrencyCompact( currency, revenue ),
			exactValue: revenue,
			tooltipText: formatCurrency( currency, revenue )
		};
	}
 
	const quantity = current?.total_quantity_sold ?? 0;
	return { value: quantity, exactValue: quantity };
};
 
/**
 * Transform top performers data.
 *
 * @param { Object } data           - The raw data array.
 * @param { string } selectedOption - The selected option for data retrieval.
 *
 * @return { Object } The transformed data array.
 */
export const transformTopPerformersData = ( data, selectedOption ) => {
	const transformedData = {};
 
	Object.entries( data ).forEach( ([ name, value ]) => {
		const { current, previous, revenue_change } = value;
		const entry = {
			title: value.label,
			subtitle: getPerformerSubtitle(
				name,
				current,
				getDefaultSubtitle( name )
			),
			...buildPerformerValue( selectedOption, current )
		};
 
		const indicator = resolveChangeIndicator({
			rateChange: revenue_change,
			current,
			previous,
			getValue: ( periodData ) =>
				getMetricValue( selectedOption, periodData ),
			noData: { change: null, changeStatus: '-' }
		});
		Eif ( indicator ) {
			entry.change = indicator.change;
			entry.changeStatus = indicator.changeStatus;
		}
 
		transformedData[name] = entry;
	});
	return transformedData;
};
export default getTopPerformers;