Skip to content
This repository has been archived by the owner on Aug 19, 2022. It is now read-only.

Commit

Permalink
Support in udaru core for searching users
Browse files Browse the repository at this point in the history
  • Loading branch information
dberesford committed Feb 18, 2018
1 parent 0a500ec commit d676325
Show file tree
Hide file tree
Showing 5 changed files with 972 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/core/index.js
Expand Up @@ -84,7 +84,8 @@ function buildUdaruCore (dbPool, config) {
deletePolicy: userOps.deleteUserPolicy,
listUserTeams: userOps.listUserTeams,
replaceTeams: userOps.replaceUserTeams,
deleteTeams: userOps.deleteUserFromTeams
deleteTeams: userOps.deleteUserFromTeams,
search: userOps.search
}
}
}
Expand Down
19 changes: 19 additions & 0 deletions lib/core/lib/ops/userOps.js
Expand Up @@ -647,6 +647,25 @@ function buildUserOps (db, config) {

userOps.readUser({ id, organizationId }, cb)
})
},

// See https://www.postgresql.org/docs/current/static/textsearch.html
search: function search (params, cb) {
const { organizationId, query } = params
Joi.validate({ organizationId, query }, validationRules.searchUser, function (err) {
if (err) return cb(Boom.badRequest(err))

const sqlQuery = SQL`
SELECT *
FROM users
WHERE org_id=${organizationId} AND
to_tsvector(name) @@ to_tsquery(${query})
`
db.query(sqlQuery, (err, result) => {
if (err) return cb(Boom.badImplementation(err))
return cb(null, result.rows.map(mapping.user), result.rows.length)
})
})
}
}

Expand Down
4 changes: 4 additions & 0 deletions lib/core/lib/ops/validation.js
Expand Up @@ -109,6 +109,10 @@ const users = {
deleteUserFromTeams: {
id: validationRules.userId,
organizationId: validationRules.organizationId
},
searchUser: {
organizationId: validationRules.organizationId,
query: Joi.string()
}
}

Expand Down

0 comments on commit d676325

Please sign in to comment.