Refactor filtering and condition rule evaluation across Self-Serve components
- Standardized `filters` usage in `get.all` methods for `MyWashStart.vue`, `SelfServeTryModal.vue`, and `ObjectsGlobal.vue`. - Updated rule evaluation logic to return `false` for empty condition rules and simplified sub-rule handling. - Improved filter transformation to handle arrays in the `ObjectsGlobal.vue` API for more flexible requests. - Consolidated duplicate logic for conditions and tasks across modules.
This commit is contained in:
@@ -38,19 +38,25 @@ const fetchData = async () => {
|
||||
|
||||
const [q, c, t] = await Promise.all([
|
||||
SessionUser.objects.self_serve_questions.get.all({
|
||||
department: deptId,
|
||||
product: vTypeId,
|
||||
...(lId !== null ? { lane: lId } : {})
|
||||
filters: {
|
||||
department: deptId,
|
||||
product: vTypeId,
|
||||
...(lId !== null ? { lane: lId } : {})
|
||||
}
|
||||
}),
|
||||
SessionUser.objects.self_serve_conditions.get.all({
|
||||
department: deptId,
|
||||
product: vTypeId,
|
||||
...(lId !== null ? { lane: lId } : {})
|
||||
filters: {
|
||||
department: deptId,
|
||||
product: vTypeId,
|
||||
...(lId !== null ? { lane: lId } : {})
|
||||
}
|
||||
}),
|
||||
SessionUser.objects.self_serve_tasks.get.all({
|
||||
department: deptId,
|
||||
product: vTypeId,
|
||||
...(lId !== null ? { lane: lId } : {})
|
||||
filters: {
|
||||
department: deptId,
|
||||
product: vTypeId,
|
||||
...(lId !== null ? { lane: lId } : {})
|
||||
}
|
||||
}),
|
||||
]);
|
||||
|
||||
@@ -70,7 +76,9 @@ const fetchData = async () => {
|
||||
const conditionIds = conditions.value.map(item => item.id);
|
||||
if (conditionIds.length > 0) {
|
||||
rules.value = await SessionUser.objects.self_serve_condition_rules.get.all({
|
||||
condition_id: conditionIds
|
||||
filters: {
|
||||
condition_id: conditionIds
|
||||
}
|
||||
});
|
||||
} else {
|
||||
rules.value = [];
|
||||
@@ -136,7 +144,7 @@ const evaluateRule = (rule, visited) => {
|
||||
case 'IS_TRUE_OR_ANY_TRUE':
|
||||
if (rule.object_type === 'condition') {
|
||||
const subRules = rules.value.filter(r => parseInt(r.condition_id) === parseInt(rule.object_id));
|
||||
if (subRules.length === 0) return true;
|
||||
if (subRules.length === 0) return false;
|
||||
return evaluateCondition(rule.object_id, visited) || subRules.some(r => evaluateRule(r, new Set([...visited, parseInt(rule.object_id)])));
|
||||
}
|
||||
return objectValue === true;
|
||||
@@ -162,14 +170,7 @@ const evaluateCondition = (conditionId, visited = new Set()) => {
|
||||
newVisited.add(parseInt(conditionId));
|
||||
|
||||
const conditionRules = rules.value.filter(r => parseInt(r.condition_id) === parseInt(conditionId));
|
||||
if (conditionRules.length === 0) return true;
|
||||
|
||||
if (conditionRules.some(r => r.type === 'IS_TRUE_OR_ANY_TRUE')) {
|
||||
return conditionRules.some(rule => evaluateRule(rule, newVisited));
|
||||
}
|
||||
if (conditionRules.some(r => r.type === 'IS_FALSE_OR_ANY_FALSE')) {
|
||||
return conditionRules.some(rule => rule.type === 'IS_FALSE_OR_ANY_FALSE' ? evaluateRule(rule, newVisited) : !evaluateRule(rule, newVisited));
|
||||
}
|
||||
if (conditionRules.length === 0) return false;
|
||||
|
||||
return conditionRules.every(rule => evaluateRule(rule, newVisited));
|
||||
};
|
||||
|
||||
@@ -397,7 +397,12 @@ export const ObjectsGlobal = {
|
||||
"GET",
|
||||
{
|
||||
// Map the filters as: filter1:value1,filter2:value2,filter3:value3
|
||||
filters: Object.entries(optionsObject.filters).map(([key, value]) => `${key}:${value}`).join(','),
|
||||
filters: Object.entries(optionsObject.filters).flatMap(([key, value]) => {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(v => `${key}:${v}`);
|
||||
}
|
||||
return `${key}:${value}`;
|
||||
}).join(','),
|
||||
page: optionsObject.pagination.page,
|
||||
limit: optionsObject.pagination.limit,
|
||||
search: optionsObject.search || '',
|
||||
@@ -416,6 +421,14 @@ export const ObjectsGlobal = {
|
||||
* @returns {Promise} The promise
|
||||
*/
|
||||
objects: async (endpoint, data = {}) => {
|
||||
if (data.filters && typeof data.filters === 'object') {
|
||||
data.filters = Object.entries(data.filters).flatMap(([key, value]) => {
|
||||
if (Array.isArray(value)) {
|
||||
return value.map(v => `${key}:${v}`);
|
||||
}
|
||||
return `${key}:${value}`;
|
||||
}).join(',');
|
||||
}
|
||||
return await authenticatedRequest(
|
||||
endpoint,
|
||||
"GET",
|
||||
|
||||
@@ -598,10 +598,9 @@ const fetchSelfServeData = async () => {
|
||||
|
||||
const conditionIds = conditions.value.map(item => item.id);
|
||||
if (conditionIds.length > 0) {
|
||||
// condition_id:['1','2','3']
|
||||
rules.value = await SessionUser.objects.self_serve_condition_rules.get.all({
|
||||
filters: {
|
||||
condition_id: conditionIds.map(id => id.toString()),
|
||||
condition_id: conditionIds
|
||||
}
|
||||
});
|
||||
} else {
|
||||
@@ -658,8 +657,10 @@ const fetchPreviousAnswers = async () => {
|
||||
|
||||
try {
|
||||
const previousAnswers = await SessionUser.objects.self_serve_vehicle_conditions.get.all({
|
||||
reg: reg,
|
||||
customer_id: customerId
|
||||
filters: {
|
||||
reg: reg,
|
||||
customer_id: customerId
|
||||
}
|
||||
});
|
||||
|
||||
if (previousAnswers && previousAnswers.length > 0) {
|
||||
@@ -711,7 +712,7 @@ const evaluateRule = (rule, visited) => {
|
||||
case 'IS_TRUE_OR_ANY_TRUE':
|
||||
if (rule.object_type === 'condition') {
|
||||
const subRules = rules.value.filter(r => parseInt(r.condition_id) === parseInt(rule.object_id));
|
||||
if (subRules.length === 0) return true;
|
||||
if (subRules.length === 0) return false;
|
||||
return evaluateCondition(rule.object_id, visited) || subRules.some(r => evaluateRule(r, new Set([...visited, parseInt(rule.object_id)])));
|
||||
}
|
||||
return objectValue === true;
|
||||
@@ -737,14 +738,7 @@ const evaluateCondition = (conditionId, visited = new Set()) => {
|
||||
newVisited.add(parseInt(conditionId));
|
||||
|
||||
const conditionRules = rules.value.filter(r => parseInt(r.condition_id) === parseInt(conditionId));
|
||||
if (conditionRules.length === 0) return true;
|
||||
|
||||
if (conditionRules.some(r => r.type === 'IS_TRUE_OR_ANY_TRUE')) {
|
||||
return conditionRules.some(rule => evaluateRule(rule, newVisited));
|
||||
}
|
||||
if (conditionRules.some(r => r.type === 'IS_FALSE_OR_ANY_FALSE')) {
|
||||
return conditionRules.some(rule => rule.type === 'IS_FALSE_OR_ANY_FALSE' ? evaluateRule(rule, newVisited) : !evaluateRule(rule, newVisited));
|
||||
}
|
||||
if (conditionRules.length === 0) return false;
|
||||
|
||||
return conditionRules.every(rule => evaluateRule(rule, newVisited));
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user