Skip to content

Commit

Permalink
fix: Get most active places (#569)
Browse files Browse the repository at this point in the history
* fix: Get most active places

* fix: default value

* fix: Refactor logic
  • Loading branch information
cyaiox authored Dec 12, 2024
1 parent 706b29e commit 40069c9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
23 changes: 23 additions & 0 deletions src/entities/Place/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,20 @@ export default class PlaceModel extends Model<PlaceAttributes> {
`p.${orderBy} ${orderDirection.toUpperCase()} NULLS LAST, p."deployed_at" DESC`
)

const filterMostActivePlaces =
options.order_by === PlaceListOrderBy.MOST_ACTIVE &&
!!options.hotScenesPositions &&
options.hotScenesPositions.length > 0

const sql = SQL`
${conditional(
filterMostActivePlaces,
SQL`WITH most_active_places AS (
SELECT DISTINCT base_position
FROM "place_positions"
WHERE position IN ${values(options.hotScenesPositions || [])}
)`
)}
SELECT p.*
${conditional(
!!options.user,
Expand All @@ -177,6 +190,10 @@ export default class PlaceModel extends Model<PlaceAttributes> {
SQL`, not coalesce(ul."like",true) as "user_dislike"`
)}
${conditional(!options.user, SQL`, false as "user_dislike"`)}
${conditional(
filterMostActivePlaces,
SQL`, (map.base_position IS NOT NULL)::int AS is_most_active_place`
)}
FROM ${table(this)} p
${conditional(
Expand Down Expand Up @@ -206,6 +223,11 @@ export default class PlaceModel extends Model<PlaceAttributes> {
)}`
)}
${conditional(
filterMostActivePlaces,
SQL`LEFT JOIN most_active_places "map" ON p.base_position = map.base_position`
)}
${conditional(
!!options.search,
SQL`, ts_rank_cd(p.textsearch, to_tsquery(${tsquery(
Expand All @@ -227,6 +249,7 @@ export default class PlaceModel extends Model<PlaceAttributes> {
)`
)}
ORDER BY
${conditional(filterMostActivePlaces, SQL`is_most_active_place DESC, `)}
${conditional(!!options.search, SQL`rank DESC, `)}
${order}
${limit(options.limit, { max: 100 })}
Expand Down
7 changes: 2 additions & 5 deletions src/entities/Place/routes/getPlaceMostActiveList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,14 @@ export const getPlaceMostActiveList = Router.memo(
return new ApiResponse([], { total: 0 })
}

const positions = new Set(query.positions)

const options: FindWithAggregatesOptions = {
user: userAuth?.address,
offset: numeric(query.offset, { min: 0 }) ?? 0,
limit: numeric(query.limit, { min: 0, max: 100 }) ?? 100,
only_favorites: !!bool(query.only_favorites),
only_highlighted: !!bool(query.only_highlighted),
positions: query.positions.length
? hotScenesPositions.filter((position) => positions.has(position))
: hotScenesPositions,
positions: query.positions,
hotScenesPositions: hotScenesPositions,
order_by: PlaceListOrderBy.MOST_ACTIVE,
order: query.order,
search: query.search,
Expand Down
1 change: 1 addition & 0 deletions src/entities/Place/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export type PlaceListOptions = {

export type FindWithAggregatesOptions = PlaceListOptions & {
user?: string
hotScenesPositions?: string[]
}

export const unwantedThumbnailHash = [
Expand Down

0 comments on commit 40069c9

Please sign in to comment.