-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #438 from developmentseed/feature/search-by-username
Add OSM users by username
- Loading branch information
Showing
8 changed files
with
359 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { | ||
Box, | ||
Heading, | ||
Modal, | ||
ModalBody, | ||
ModalCloseButton, | ||
ModalContent, | ||
ModalFooter, | ||
ModalHeader, | ||
ModalOverlay, | ||
} from '@chakra-ui/react' | ||
import { AddMemberByIdForm, AddMemberByUsernameForm } from './add-member-form' | ||
|
||
export function AddMemberModal({ isOpen, onClose, onSubmit }) { | ||
return ( | ||
<Modal | ||
isCentered | ||
isOpen={isOpen} | ||
onClose={onClose} | ||
scrollBehavior={'inside'} | ||
> | ||
<ModalOverlay /> | ||
<ModalContent flexDirection={'column'} as='article' gap={2}> | ||
<ModalHeader gap={4} display='flex' flexDir={'column'}> | ||
<Heading size='sm' as='h3'> | ||
Add Member | ||
</Heading> | ||
<ModalCloseButton onClick={() => onClose()} /> | ||
</ModalHeader> | ||
<ModalBody display='flex' flexDirection={'column'} gap={2}> | ||
<Box> | ||
<Heading size='sm' as='h4'> | ||
OSM ID | ||
</Heading> | ||
<AddMemberByIdForm onSubmit={onSubmit} /> | ||
</Box> | ||
<Box> | ||
<Heading size='sm' as='h4'> | ||
OSM Username | ||
</Heading> | ||
<AddMemberByUsernameForm onSubmit={onSubmit} /> | ||
</Box> | ||
</ModalBody> | ||
<ModalFooter></ModalFooter> | ||
</ModalContent> | ||
</Modal> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
const db = require('../lib/db') | ||
|
||
/** | ||
* Get paginated list of teams | ||
* | ||
* @param options | ||
* @param {username} options.username - filter by OSM username | ||
* @return {Promise[Array]} | ||
**/ | ||
async function list(options = {}) { | ||
// Apply search | ||
let query = await db('osm_users') | ||
.select('id', 'name') | ||
.whereILike('name', `%${options.username}%`) | ||
|
||
return query | ||
} | ||
|
||
module.exports = { | ||
list, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
var fetch = require('node-fetch') | ||
const join = require('url-join') | ||
import * as Yup from 'yup' | ||
|
||
import Users from '../../../models/users' | ||
import { createBaseHandler } from '../../../middlewares/base-handler' | ||
import { validate } from '../../../middlewares/validation' | ||
import isAuthenticated from '../../../middlewares/can/authenticated' | ||
|
||
const handler = createBaseHandler() | ||
|
||
/** | ||
* @swagger | ||
* /users: | ||
* get: | ||
* summary: Get OSM users by username | ||
* tags: | ||
* - users | ||
* responses: | ||
* 200: | ||
* description: A list of OSM users | ||
* content: | ||
* application/json: | ||
* schema: | ||
* type: object | ||
* properties: | ||
* users: | ||
* $ref: '#/components/schemas/TeamMemberList' | ||
*/ | ||
handler.get( | ||
isAuthenticated, | ||
validate({ | ||
query: Yup.object({ | ||
search: Yup.string().required(), | ||
}).required(), | ||
}), | ||
async function getUsers(req, res) { | ||
const { search } = req.query | ||
let users = await Users.list({ username: search }) | ||
|
||
if (!users.length) { | ||
const resp = await fetch( | ||
join( | ||
process.env.OSM_API, | ||
`/api/0.6/changesets.json?display_name=${search}` | ||
) | ||
) | ||
if ([200, 304].includes(resp.status)) { | ||
const data = await resp.json() | ||
if (data.changesets) { | ||
const changeset = data.changesets[0] | ||
users = [ | ||
{ | ||
id: changeset.uid, | ||
name: changeset.user, | ||
}, | ||
] | ||
} | ||
} | ||
} | ||
|
||
let responseObject = Object.assign({}, { users }) | ||
|
||
return res.send(responseObject) | ||
} | ||
) | ||
|
||
export default handler |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.