Skip to content

Commit

Permalink
feat: Add a new query param positions (#698)
Browse files Browse the repository at this point in the history
* feat: Add new param positions

* feat: Validate queryParam positions

* feat: Filter events by coordinates

* docs: Add a comment

* fix: Uses columns x and e instead of coordinates
  • Loading branch information
cyaiox authored Nov 4, 2024
1 parent c8c8f8a commit 8e11603
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/entities/Event/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,19 @@ export default class EventModel extends Model<DeprecatedEventAttributes> {
orderDirection = options.order === "asc" ? "ASC" : "DESC"
}

// Prioritizes "x" && "y" options params over positions
let positionsFilter = ""
if (
!Number.isFinite(options.x) &&
!Number.isFinite(options.y) &&
options.positions &&
options.positions.length > 0
) {
positionsFilter = options.positions
.map((position) => `(${position.join(",")})`)
.join(",")
}

const query = SQL`
SELECT
e.*
Expand Down Expand Up @@ -276,6 +289,10 @@ export default class EventModel extends Model<DeprecatedEventAttributes> {
Number.isFinite(options.x) && Number.isFinite(options.y),
SQL`AND e.x = ${options.x} AND e.y = ${options.y}`
)}
${conditional(
!!positionsFilter,
SQL`AND (e.x, e.y) = ANY(Array[${SQL.raw(positionsFilter)}])`
)}
${conditional(
!!options.estate_id,
SQL`AND e.estate_id = ${options.estate_id}`
Expand Down
21 changes: 21 additions & 0 deletions src/entities/Event/routes/getEventList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,27 @@ export async function getEventList(req: WithAuth) {
}
}

if (!!query.positions && query.positions.length > 0) {
options.positions = []

for (const position of query.positions) {
const [x, y] = position.split(",").slice(0, 2).map(Number) as [
number,
number
]

if (
!Number.isFinite(x) ||
!Number.isFinite(y) ||
!isInsideWorldLimits(x, y)
) {
return []
}

options.positions.push([x, y])
}
}

if (query.estate_id) {
const estateId = Number(query.estate_id)
if (estateId !== null && Number.isFinite(estateId)) {
Expand Down
6 changes: 6 additions & 0 deletions src/entities/Event/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ export const getEventListQuery: AjvObjectSchema = {
pattern: "^-?\\d{1,3},-?\\d{1,3}$",
description: "Filter events that will happend in a specific position",
},
positions: {
type: "array",
maxItems: 1000,
items: { type: "string", pattern: "^-?\\d{1,3},-?\\d{1,3}$" },
description: "Filter places in specific positions",
},
estate_id: {
type: "string",
format: "int",
Expand Down
2 changes: 2 additions & 0 deletions src/entities/Event/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ export type EventListParams = {
list?: EventListType
creator?: string
position?: string
positions?: string[]
estate_id?: string
only_attendee?: boolean
search?: string
Expand All @@ -191,6 +192,7 @@ export type EventListOptions = {
creator?: string
x?: number
y?: number
positions?: number[][]
estate_id?: string
only_attendee?: boolean
search?: string
Expand Down

0 comments on commit 8e11603

Please sign in to comment.