Add backend error checking for average spent filter

MAILPOET-4987
This commit is contained in:
John Oleksowicz
2023-05-01 11:27:56 -05:00
committed by Veljko V
parent d8e29e9edb
commit 9e5b5d5ebe
3 changed files with 9 additions and 0 deletions

View File

@ -149,6 +149,7 @@ class DynamicSegments extends APIEndpoint {
return __('Please select a type for the comparison, a number of orders and a number of days.', 'mailpoet');
case InvalidFilterException::MISSING_TOTAL_SPENT_FIELDS:
case InvalidFilterException::MISSING_SINGLE_ORDER_VALUE_FIELDS:
case InvalidFilterException::MISSING_AVERAGE_SPENT_FIELDS:
return __('Please select a type for the comparison, an amount and a number of days.', 'mailpoet');
case InvalidFilterException::MISSING_FILTER:
return __('Please add at least one condition for filtering.', 'mailpoet');

View File

@ -22,4 +22,5 @@ class InvalidFilterException extends InvalidStateException {
const MISSING_OPERATOR = 15;
const MISSING_PLAN_ID = 16;
const MISSING_SINGLE_ORDER_VALUE_FIELDS = 17;
const MISSING_AVERAGE_SPENT_FIELDS = 18;
};

View File

@ -285,6 +285,13 @@ class FilterDataMapper {
$filterData['operator'] = $data['operator'];
$filterData['value'] = $data['value'];
} elseif ($data['action'] === WooCommerceAverageSpent::ACTION) {
if (
!isset($data['average_spent_type'])
|| !isset($data['average_spent_amount']) || $data['average_spent_amount'] < 0
|| !isset($data['average_spent_days']) || $data['average_spent_days'] < 1
) {
throw new InvalidFilterException('Missing required fields', InvalidFilterException::MISSING_AVERAGE_SPENT_FIELDS);
}
$filterData['average_spent_days'] = $data['average_spent_days'];
$filterData['average_spent_amount'] = $data['average_spent_amount'];
$filterData['average_spent_type'] = $data['average_spent_type'];