-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathparseParams.ts
More file actions
126 lines (118 loc) · 4.12 KB
/
parseParams.ts
File metadata and controls
126 lines (118 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import type { FilterQuery } from 'mongoose'
import type { FlattenedField, Operator, Payload, Where } from 'payload'
import { deepMergeWithCombinedArrays } from 'payload'
import { validOperatorSet } from 'payload/shared'
import { buildAndOrConditions } from './buildAndOrConditions.js'
import { buildSearchParam } from './buildSearchParams.js'
export async function parseParams({
collectionSlug,
fields,
globalSlug,
locale,
parentIsLocalized,
payload,
where,
}: {
collectionSlug?: string
fields: FlattenedField[]
globalSlug?: string
locale?: string
parentIsLocalized: boolean
payload: Payload
where: Where
}): Promise<Record<string, unknown>> {
let result = {} as FilterQuery<any>
if (typeof where === 'object') {
// We need to determine if the whereKey is an AND, OR, or a schema path
for (const relationOrPath of Object.keys(where)) {
const condition = where[relationOrPath]
let conditionOperator: '$and' | '$not' | '$or' | null = null
if (relationOrPath.toLowerCase() === 'and') {
conditionOperator = '$and'
} else if (relationOrPath.toLowerCase() === 'or') {
conditionOperator = '$or'
} else if (relationOrPath.toLowerCase() === 'not') {
conditionOperator = '$not'
}
if (Array.isArray(condition)) {
const builtConditions = await buildAndOrConditions({
collectionSlug,
fields,
globalSlug,
locale,
parentIsLocalized,
payload,
where: condition,
})
if (builtConditions.length > 0 && conditionOperator !== null) {
result[conditionOperator] = builtConditions
}
} else if (conditionOperator === '$not' && typeof condition === 'object') {
const builtCondition = await parseParams({
collectionSlug,
fields,
globalSlug,
locale,
parentIsLocalized,
payload,
where: condition as Where,
})
if (Object.keys(builtCondition).length > 0) {
result.$nor = [builtCondition]
}
} else {
// It's a path - and there can be multiple comparisons on a single path.
// For example - title like 'test' and title not equal to 'tester'
// So we need to loop on keys again here to handle each operator independently
const pathOperators = where[relationOrPath]
if (typeof pathOperators === 'object') {
const validOperators: Operator[] = Object.keys(pathOperators).filter((operator) =>
validOperatorSet.has(operator as Operator),
) as Operator[]
for (const operator of validOperators) {
const searchParam = await buildSearchParam({
collectionSlug,
fields,
globalSlug,
incomingPath: relationOrPath,
locale,
operator,
parentIsLocalized,
payload,
val: (pathOperators as Record<string, Where>)[operator],
})
if (searchParam?.value && searchParam?.path) {
if (validOperators.length > 1) {
if (!result.$and) {
result.$and = []
}
result.$and.push({
[searchParam.path]: searchParam.value,
})
} else {
if (result[searchParam.path]) {
if (!result.$and) {
result.$and = []
}
result.$and.push({ [searchParam.path]: result[searchParam.path] })
result.$and.push({
[searchParam.path]: searchParam.value,
})
delete result[searchParam.path]
} else {
result[searchParam.path] = searchParam.value
}
}
} else if (typeof searchParam?.value === 'object') {
result = deepMergeWithCombinedArrays(result, searchParam.value ?? {}, {
// dont clone Types.ObjectIDs
clone: false,
})
}
}
}
}
}
}
return result
}