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

handle shorthand filter with object or encrypted #506

Merged
merged 4 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion libs/velo-external-db-core/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "@wix-velo/velo-external-db-core",
"version": "2.0.0",
"version": "2.0.1",
"type": "commonjs"
}
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,28 @@ describe('Filter Transformer', () => {
value: ctx.fieldValue
})
})

test('correctly transform short syntax filter with object value', () => {
const filter = {
[ctx.fieldName]: ctx.objectValue
}
expect(env.FilterTransformer.transform(filter)).toEqual({
fieldName: ctx.fieldName,
operator: env.FilterTransformer.wixOperatorToAdapterOperator('$eq'),
value: ctx.objectValue
})
})

test('correctly transform short syntax filter with encrypted value', () => {
const filter = {
[ctx.fieldName]: { $encrypted: ctx.fieldValue }
}
expect(env.FilterTransformer.transform(filter)).toEqual({
fieldName: ctx.fieldName,
operator: env.FilterTransformer.wixOperatorToAdapterOperator('$eq'),
value: { $encrypted: ctx.fieldValue }
})
})
})

interface Enviorment {
Expand All @@ -211,7 +233,8 @@ describe('Filter Transformer', () => {
fieldValue: Uninitialized,
operator: Uninitialized,
fieldListValue: Uninitialized,
veloDate: Uninitialized
veloDate: Uninitialized,
objectValue: Uninitialized,
}

beforeEach(() => {
Expand All @@ -223,6 +246,7 @@ describe('Filter Transformer', () => {
ctx.operator = gen.randomOperator() as WixDataMultiFieldOperators | WixDataSingleFieldOperators
ctx.fieldListValue = [chance.word(), chance.word(), chance.word(), chance.word(), chance.word()]
ctx.veloDate = genCommon.veloDate()
ctx.objectValue = genCommon.randomObject()
})
})

20 changes: 14 additions & 6 deletions libs/velo-external-db-core/src/converters/filter_transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,16 +118,24 @@ export default class FilterTransformer implements IFilterTransformer {
}

valueAndOperatorFromFilter(filter: any, fieldName: string) {
// there are two options:
// { fieldName: { operator: value } }
// or
// { fieldName: value }
const filterValue = filter[fieldName]
const operator = isObject(filterValue) ? Object.keys(filterValue)[0] : '$eq'
const value = isObject(filterValue) ? filterValue[operator] : filterValue
if (isObject(filterValue) && this.isOperator(Object.keys(filterValue)[0])) {
const operator = Object.keys(filterValue)[0]
const value = filterValue[operator]
return {
operator,
value
}
}
const operator = '$eq'
const value = filterValue
return {
operator,
value
}
}

private isOperator(operator: string) {
return operator.startsWith('$') && operator !== '$encrypted'
}
}
Loading