fix: properly handle nullable minDistance and maxDistance in near query (#10622)
Fixes https://github.com/payloadcms/payload/issues/10521
This commit is contained in:
@@ -349,10 +349,11 @@ export const sanitizeQueryValue = ({
|
||||
$geometry: { type: 'Point', coordinates: [parseFloat(lng), parseFloat(lat)] },
|
||||
}
|
||||
|
||||
if (maxDistance) {
|
||||
if (maxDistance && !Number.isNaN(Number(maxDistance))) {
|
||||
formattedValue.$maxDistance = parseFloat(maxDistance)
|
||||
}
|
||||
if (minDistance) {
|
||||
|
||||
if (minDistance && !Number.isNaN(Number(minDistance))) {
|
||||
formattedValue.$minDistance = parseFloat(minDistance)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -319,12 +319,22 @@ export function parseParams({
|
||||
|
||||
case 'near': {
|
||||
const [lng, lat, maxDistance, minDistance] = queryValue as number[]
|
||||
const geoConstraints: SQL[] = []
|
||||
|
||||
let constraint = sql`ST_DWithin(ST_Transform(${table[columnName]}, 3857), ST_Transform(ST_SetSRID(ST_MakePoint(${lng}, ${lat}), 4326), 3857), ${maxDistance})`
|
||||
if (typeof minDistance === 'number' && !Number.isNaN(minDistance)) {
|
||||
constraint = sql`${constraint} AND ST_Distance(ST_Transform(${table[columnName]}, 3857), ST_Transform(ST_SetSRID(ST_MakePoint(${lng}, ${lat}), 4326), 3857)) >= ${minDistance}`
|
||||
if (typeof maxDistance === 'number' && !Number.isNaN(maxDistance)) {
|
||||
geoConstraints.push(
|
||||
sql`ST_DWithin(ST_Transform(${table[columnName]}, 3857), ST_Transform(ST_SetSRID(ST_MakePoint(${lng}, ${lat}), 4326), 3857), ${maxDistance})`,
|
||||
)
|
||||
}
|
||||
|
||||
if (typeof minDistance === 'number' && !Number.isNaN(minDistance)) {
|
||||
geoConstraints.push(
|
||||
sql`ST_Distance(ST_Transform(${table[columnName]}, 3857), ST_Transform(ST_SetSRID(ST_MakePoint(${lng}, ${lat}), 4326), 3857)) >= ${minDistance}`,
|
||||
)
|
||||
}
|
||||
if (geoConstraints.length) {
|
||||
constraints.push(and(...geoConstraints))
|
||||
}
|
||||
constraints.push(constraint)
|
||||
break
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user