Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UXE-5966] fix: incorrect argument when saving criteria rules engine #2038

Merged
merged 5 commits into from
Dec 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AxiosHttpClientAdapter } from '@/services/axios/AxiosHttpClientAdapter'
import { makeEdgeApplicationV4BaseUrl } from '@/services/edge-application-services/v4/make-edge-application-v4-base-url'
import * as Errors from '@/services/axios/errors'
import { adaptBehavior } from './helper-behavior'
import { adaptCriteria } from './helper-criteria'
import { extractApiError } from '@/helpers/extract-api-error'

export const editRulesEngineService = async ({ id, payload, reorder = false }) => {
Expand All @@ -28,7 +29,7 @@ const adapt = (payload, reorder) => {
name,
phase: phase.content || phase,
behaviors: adaptBehavior(behaviors),
criteria,
criteria: adaptCriteria(criteria),
active: isActive,
description
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const isExistenceOperator = (operator) => ['exists', 'does_not_exist'].includes(operator)

const processCriteria = (criteria) => {
if (!isExistenceOperator(criteria.operator)) {
return criteria
}

// eslint-disable-next-line no-unused-vars
const { argument, ...processedCriteria } = criteria
return processedCriteria
}

export const adaptCriteria = (criterias) => {
return criterias.map(criteriaArray => {
return criteriaArray.map(processCriteria)
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { AxiosHttpClientAdapter } from '@/services/axios/AxiosHttpClientAdapter'
import * as Errors from '@/services/axios/errors'
import { createRulesEngineService } from '@/services/edge-application-rules-engine-services/v4'
import { describe, expect, it, vi } from 'vitest'
import { adaptCriteria } from '@/services/edge-application-rules-engine-services/v4/helper-criteria'

const fixtures = {
rulesEngineMock: {
Expand Down Expand Up @@ -104,4 +105,39 @@ describe('EdgeApplicationRulesEngineService', () => {

expect(apiErrorResponse).rejects.toBe('api error message')
})

it('should remove argument from criteria when operator is "exists" or "does_not_exist"', () => {
const criterias = [
[
{
variable: 'remote_addr',
operator: 'exists',
argument: '192.168.1.1'
},
{
variable: 'remote_addr',
operator: 'is_equal',
argument: '192.168.1.1'
}
]
]

const expectedCriterias = [
[
{
variable: 'remote_addr',
operator: 'exists'
},
{
variable: 'remote_addr',
operator: 'is_equal',
argument: '192.168.1.1'
}
]
]

const result = adaptCriteria(criterias)

expect(result).toEqual(expectedCriterias)
})
})
Loading