All files / api getGoalsData.js

100% Statements 36/36
100% Branches 28/28
100% Functions 7/7
100% Lines 33/33

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                1x                                                                   21x   12x                       3x                                             1x 3x 1x                       1x 3x 1x     3x 30x 6x   24x 12x   12x 5x         1x 4x 1x     3x 3x 3x 3x                       1x 6x 6x 1x   5x 5x     4x   1x 1x        
import {
	formatNumber,
	formatPercentage,
	getPercentage
} from '@/utils/formatting';
import { getData } from '@/utils/api';
import { __ } from '@wordpress/i18n';
 
export const goalsPlaceholderData = {
	today: {
		title: __( 'Today', 'burst-statistics' ),
		icon: 'goals'
	},
	total: {
		title: __( 'Total', 'burst-statistics' ),
		value: '-',
		icon: 'goals'
	},
	topPerformer: {
		title: '-',
		value: '-'
	},
	conversionMetric: {
		title: '-',
		value: '-',
		icon: 'visitors'
	},
	conversionPercentage: {
		title: '-',
		value: '-'
	},
	bestDevice: {
		title: '-',
		value: '-',
		icon: 'desktop'
	},
	dateCreated: 0,
	dateStart: 0,
	dateEnd: 0,
	status: 'inactive'
};
 
const hasValue = ( metric ) => Boolean( metric.value ) && '-' !== metric.value;
 
const metricWithDefaults = ( metric, defaults ) => ({
	...defaults,
	...( metric || {})
});
 
/**
 * Merge a response onto the placeholder data, guaranteeing that every
 * required nested metric object exists.
 *
 * @param {Object} response The raw goals response.
 * @return {Object} The response with defaults applied.
 */
const withGoalDefaults = ( response ) => ({
	...goalsPlaceholderData,
	...response,
	conversionPercentage: metricWithDefaults(
		response?.conversionPercentage,
		goalsPlaceholderData.conversionPercentage
	),
	conversionMetric: metricWithDefaults(
		response?.conversionMetric,
		goalsPlaceholderData.conversionMetric
	),
	total: metricWithDefaults( response?.total, goalsPlaceholderData.total ),
	bestDevice: metricWithDefaults(
		response?.bestDevice,
		goalsPlaceholderData.bestDevice
	)
});
 
/**
 * Calculate the conversion percentage from the total and conversion metric.
 *
 * @param {Object} goals The goals data with defaults applied.
 */
const applyConversionPercentage = ( goals ) => {
	if ( hasValue( goals.total ) && hasValue( goals.conversionMetric ) ) {
		goals.conversionPercentage.value = getPercentage(
			goals.total.value,
			goals.conversionMetric.value
		);
	}
};
 
/**
 * Format the display value of every metric in place.
 *
 * @param {Object} goals The goals data with defaults applied.
 */
const formatGoalValues = ( goals ) => {
	if ( hasValue( goals.bestDevice ) ) {
		goals.bestDevice.value = formatPercentage( goals.bestDevice.value );
	}
 
	for ( const [ key, metric ] of Object.entries( goals ) ) {
		if ( 'conversionPercentage' === key || 'bestDevice' === key ) {
			continue;
		}
		if ( 'object' !== typeof metric || null === metric ) {
			continue;
		}
		if ( hasValue( metric ) && ! isNaN( Number( metric.value ) ) ) {
			metric.value = formatNumber( metric.value );
		}
	}
};
 
const transformTotalGoalsData = ( response ) => {
	if ( ! response || 'object' !== typeof response ) {
		return goalsPlaceholderData;
	}
 
	const goals = withGoalDefaults( response );
	applyConversionPercentage( goals );
	formatGoalValues( goals );
	return goals;
};
 
/**
 * Get live goals
 * @param {Object} args
 * @param {string} args.startDate
 * @param {string} args.endDate
 * @param {string} args.range
 * @param {Object} args.filters
 * @return {Promise<*>}
 */
const getGoalsData = async( args ) => {
	const { startDate, endDate, range, goal_id } = args;
	if ( ! goal_id ) {
		return goalsPlaceholderData;
	}
	try {
		const { data } = await getData( 'goals', startDate, endDate, range, {
			goal_id
		});
		return transformTotalGoalsData( data );
	} catch ( error ) {
		console.error( 'Error fetching goals data:', error );
		return goalsPlaceholderData;
	}
};
export default getGoalsData;