Skip to content

Commit 51a10a5

Browse files
committed
Allow api to accept bbox for paginatedList
1 parent 7beae3a commit 51a10a5

File tree

3 files changed

+38
-12
lines changed

3 files changed

+38
-12
lines changed

src/components/tables/teams.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,24 @@ import qs from 'qs'
1010

1111
const APP_URL = process.env.APP_URL
1212

13-
function TeamsTable({ type, orgId }) {
13+
function TeamsTable({ type, orgId, bbox }) {
1414
const [page, setPage] = useState(1)
1515
const [search, setSearch] = useState(null)
1616
const [sort, setSort] = useState({
1717
key: 'name',
1818
direction: 'asc',
1919
})
2020

21-
const querystring = qs.stringify({
22-
search,
23-
page,
24-
sort: sort.key,
25-
order: sort.direction,
26-
})
21+
const querystring = qs.stringify(
22+
{
23+
search,
24+
page,
25+
sort: sort.key,
26+
order: sort.direction,
27+
bbox: bbox,
28+
},
29+
{ arrayFormat: 'comma' }
30+
)
2731

2832
let apiBasePath
2933
let emptyMessage

src/models/team.js

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,24 +349,35 @@ async function paginatedList(options = {}) {
349349
*
350350
* @param options
351351
* @param {Array[float]} options.bbox - filter for teams whose location is in bbox (xmin, ymin, xmax, ymax)
352+
* @param {int} options.organizationId - filter by whether team belongs to organization
352353
* @return {[Array]} Array of teams
353354
**/
354-
async function list({ bbox }) {
355+
async function list({ bbox, organizationId, includePrivate }) {
355356
// TODO: this method should be merged to the paginatedList() method when possible
356357
// for consistency, as they both return a list of teams.
357358

358359
const st = knexPostgis(db)
359360

360-
let query = db('team')
361-
.select(...teamAttributes, st.asGeoJSON('location'))
362-
.where('privacy', 'public')
361+
let query = db('team').select(...teamAttributes, st.asGeoJSON('location'))
363362

364363
if (bbox) {
365364
query = query.where(
366365
st.boundingBoxContained('location', st.makeEnvelope(...bbox))
367366
)
368367
}
369368

369+
if (!includePrivate) {
370+
query.where('privacy', 'public')
371+
}
372+
373+
if (organizationId) {
374+
query = query.whereIn('id', function () {
375+
this.select('team_id')
376+
.from('organization_team')
377+
.where('organization_id', organizationId)
378+
})
379+
}
380+
370381
return query
371382
}
372383

src/pages/api/organizations/[orgId]/teams.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createBaseHandler } from '../../../../middlewares/base-handler'
22
import { validate } from '../../../../middlewares/validation'
33
import Organization from '../../../../models/organization'
44
import Team from '../../../../models/team'
5+
import Boom from '@hapi/boom'
56
import * as Yup from 'yup'
67
import canCreateOrgTeam from '../../../../middlewares/can/create-org-team'
78
import canViewOrgTeams from '../../../../middlewares/can/view-org-teams'
@@ -103,15 +104,25 @@ handler.get(
103104
}).required(),
104105
}),
105106
async function (req, res) {
106-
const { orgId, page, perPage, search, sort, order } = req.query
107+
const { orgId, page, perPage, search, sort, order, bbox } = req.query
107108
const {
108109
org: { isMember, isOwner, isManager },
109110
} = req
111+
112+
let bounds = bbox || null
113+
if (bbox) {
114+
bounds = bbox.split(',').map((num) => parseFloat(num))
115+
if (bounds.length !== 4) {
116+
throw Boom.badRequest('error in bbox param')
117+
}
118+
}
119+
110120
return res.send(
111121
await Team.paginatedList({
112122
organizationId: orgId,
113123
page,
114124
perPage,
125+
bbox: bounds,
115126
search,
116127
sort,
117128
order,

0 commit comments

Comments
 (0)