Skip to content

Commit

Permalink
backend: Add person search endpoint to enable Login As flow
Browse files Browse the repository at this point in the history
  • Loading branch information
AleksTeresh committed Mar 20, 2024
1 parent 47c14f0 commit 53c0edf
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
10 changes: 10 additions & 0 deletions backend/src/controllers/PersonController.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ const programmeService = require('../services/ProgrammeService')
// const emailInviteService = require('../services/EmailInviteService')
const emailService = require('../services/EmailService')

export async function findPersons(req, res) {
const { search } = req.query
if (search.length < 5) {
throw Error('Search string must be at least 5 characters long')
}

const persons = await personService.searchPersons(search)
res.status(200).json(persons)
}

/**
* Get persons that are of interest to the person doing query
*/
Expand Down
2 changes: 2 additions & 0 deletions backend/src/routes/persons.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ const jsonParser = bodyParser.json()
*/
router.get('/', auth.checkManagerOrAdmin, (req, res, next) => personController.getPersons(req, res).catch(next))

router.get('/search', auth.checkAdmin, (req, res, next) => personController.findPersons(req, res).catch(next))

/**
* @api {post} persons/invite Invite person to role
* @apiName InvitePerson
Expand Down
17 changes: 17 additions & 0 deletions backend/src/services/PersonService.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,23 @@ export const getPendingPersonsWithRole = async (roleId, programmeId) => (

export const getPersonByEmail = async email => Person.where({ email: email.toLowerCase() }).fetch()

export const searchPersons = async (userQuery) => {
let query

if (userQuery.split(' ').length === 2) {
const firstName = userQuery.split(' ')[0]
const lastName = userQuery.split(' ')[1]
query = qb => qb.where('firstname', 'iLIKE', `%${firstName}%`)
.orWhere('lastname', 'iLIKE', `%${lastName}%`)
} else {
query = qb => qb.where('email', 'iLIKE', `%${userQuery}%`)
.orWhere('firstname', 'iLIKE', `%${userQuery}%`)
.orWhere('lastname', 'iLIKE', `%${userQuery}%`)
}

return Person.query(query).fetchAll()
}

export const updateNonRegisteredPerson = async (person, studentNumber, shibbolethId) => (
person.set({ studentNumber, shibbolethId }).save()
)
Expand Down

0 comments on commit 53c0edf

Please sign in to comment.