mirror of
https://github.com/hexastack/hexabot
synced 2025-06-26 18:27:28 +00:00
fix: resolve search filterpipe null checks issues
This commit is contained in:
parent
2910de0058
commit
94decddf25
@ -29,7 +29,10 @@ export class SearchFilterPipe<T>
|
|||||||
{
|
{
|
||||||
constructor(
|
constructor(
|
||||||
private readonly props: {
|
private readonly props: {
|
||||||
allowedFields: TFilterNestedKeysOfType<T, string | string[]>[];
|
allowedFields: TFilterNestedKeysOfType<
|
||||||
|
T,
|
||||||
|
undefined | string | string[]
|
||||||
|
>[];
|
||||||
},
|
},
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@ -45,7 +48,7 @@ export class SearchFilterPipe<T>
|
|||||||
private isAllowedField(field: string) {
|
private isAllowedField(field: string) {
|
||||||
if (
|
if (
|
||||||
this.props.allowedFields.includes(
|
this.props.allowedFields.includes(
|
||||||
field as TFilterNestedKeysOfType<T, string | string[]>,
|
field as TFilterNestedKeysOfType<T, undefined | string | string[]>,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
return true;
|
return true;
|
||||||
@ -58,29 +61,39 @@ export class SearchFilterPipe<T>
|
|||||||
if (Types.ObjectId.isValid(String(val))) {
|
if (Types.ObjectId.isValid(String(val))) {
|
||||||
return {
|
return {
|
||||||
_operator: 'eq',
|
_operator: 'eq',
|
||||||
[field === 'id' ? '_id' : field]: this.getNullableValue(String(val)),
|
data: {
|
||||||
|
[field === 'id' ? '_id' : field]: this.getNullableValue(
|
||||||
|
String(val),
|
||||||
|
),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
return {};
|
return {};
|
||||||
} else if (val['contains'] || val[field]?.['contains']) {
|
} else if (val?.['contains'] || val?.[field]?.['contains']) {
|
||||||
return {
|
return {
|
||||||
_operator: 'iLike',
|
_operator: 'iLike',
|
||||||
[field]: this.getRegexValue(
|
data: {
|
||||||
String(val['contains'] || val[field]['contains']),
|
[field]: this.getRegexValue(
|
||||||
),
|
String(val['contains'] || val[field]['contains']),
|
||||||
|
),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
} else if (val['!=']) {
|
} else if (val?.['!=']) {
|
||||||
return {
|
return {
|
||||||
_operator: 'neq',
|
_operator: 'neq',
|
||||||
[field]: this.getNullableValue(val['!=']),
|
data: {
|
||||||
|
[field]: this.getNullableValue(val['!=']),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
_operator: 'eq',
|
_operator: 'eq',
|
||||||
[field]: Array.isArray(val)
|
data: {
|
||||||
? val.map((v) => this.getNullableValue(v)).filter((v) => v)
|
[field]: Array.isArray(val)
|
||||||
: this.getNullableValue(String(val)),
|
? val.map((v) => this.getNullableValue(v)).filter((v) => v)
|
||||||
|
: this.getNullableValue(String(val)),
|
||||||
|
},
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -90,10 +103,11 @@ export class SearchFilterPipe<T>
|
|||||||
|
|
||||||
if (whereParams?.['or']) {
|
if (whereParams?.['or']) {
|
||||||
Object.values(whereParams['or'])
|
Object.values(whereParams['or'])
|
||||||
.filter((val) => this.isAllowedField(Object.keys(val)[0]))
|
.filter((val) => val && this.isAllowedField(Object.keys(val)[0]))
|
||||||
.map((val) => {
|
.map((val) => {
|
||||||
|
if (!val) return false;
|
||||||
const [field] = Object.keys(val);
|
const [field] = Object.keys(val);
|
||||||
const filter = this.transformField(field, val[field]);
|
const filter = this.transformField(field, val?.[field]);
|
||||||
if (filter._operator)
|
if (filter._operator)
|
||||||
filters.push({
|
filters.push({
|
||||||
...filter,
|
...filter,
|
||||||
@ -119,24 +133,24 @@ export class SearchFilterPipe<T>
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return filters.reduce((acc, { _context, _operator, ...filter }) => {
|
return filters.reduce((acc, { _context, _operator, data, ...filter }) => {
|
||||||
switch (_operator) {
|
switch (_operator) {
|
||||||
case 'neq':
|
case 'neq':
|
||||||
return {
|
return {
|
||||||
...acc,
|
...acc,
|
||||||
$nor: [...(acc?.$nor || []), filter],
|
$nor: [...(acc?.$nor || []), { ...filter, ...data }],
|
||||||
};
|
};
|
||||||
default:
|
default:
|
||||||
switch (_context) {
|
switch (_context) {
|
||||||
case 'or':
|
case 'or':
|
||||||
return {
|
return {
|
||||||
...acc,
|
...acc,
|
||||||
$or: [...(acc?.$or || []), filter],
|
$or: [...(acc?.$or || []), { ...filter, ...data }],
|
||||||
};
|
};
|
||||||
case 'and':
|
case 'and':
|
||||||
return {
|
return {
|
||||||
...acc,
|
...acc,
|
||||||
$and: [...(acc?.$and || []), filter],
|
$and: [...(acc?.$and || []), { ...filter, ...data }],
|
||||||
};
|
};
|
||||||
default:
|
default:
|
||||||
return acc; // Handle any other cases if necessary
|
return acc; // Handle any other cases if necessary
|
||||||
|
|||||||
@ -105,10 +105,12 @@ type TOperator = 'eq' | 'iLike' | 'neq';
|
|||||||
type TContext = 'and' | 'or';
|
type TContext = 'and' | 'or';
|
||||||
|
|
||||||
export type TTransformFieldProps = {
|
export type TTransformFieldProps = {
|
||||||
[x: string]: string | RegExp | string[];
|
|
||||||
_id?: string;
|
_id?: string;
|
||||||
_context?: TContext;
|
_context?: TContext;
|
||||||
_operator?: TOperator;
|
_operator?: TOperator;
|
||||||
|
data?: {
|
||||||
|
[x: string]: undefined | string | RegExp | (string | undefined)[];
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
/* mongoose */
|
/* mongoose */
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user