Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds /common/searchUser route for assigning managers #55

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/controllers/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { default as searchUsers } from './searchUsers'
49 changes: 49 additions & 0 deletions src/controllers/common/searchUsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import userModel from '@models/user/userModel'
import { Request, Response } from 'express'
import { IUser } from 'types'

interface searchUserRequest extends Request {
query: {
query: string
ids: string
}
}

const searchUsers = async (req: searchUserRequest, res: Response) => {
const { query, ids } = req.query
const objIds = ids?.split(',')

if (!query && !ids) {
return res.status(400).json({
message: 'Invalid query',
})
}

let findQuery = {}
if (query) {
findQuery = {
$or: [
{ 'personalDetails.name': { $regex: query, $options: 'i' } },
{ 'personalDetails.emailAdd': { $regex: query, $options: 'i' } },
],
}
} else if (ids) {
findQuery = { _id: { $in: objIds } }
}

const users: IUser[] = await userModel.find(findQuery)

if (users && users.length > 0) {
return res.status(200).json({
message: 'Users found',
users,
})
}

return res.status(404).json({
message: 'No users found',
})

}

export default searchUsers
2 changes: 1 addition & 1 deletion src/controllers/createQuiz/quiz/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ export { default as hostQuiz } from './createQuiz'
export { default as updateQuiz } from './updateQuiz'
export { default as publishQuiz } from './publishQuiz'
export { default as deleteQuiz } from './deleteQuiz'
export { default as quizGet } from './getQuiz'
export { default as quizGet } from './getQuiz'
10 changes: 10 additions & 0 deletions src/routers/common/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import express from 'express'
import * as commonController from '@controllers/common'
import isOnboard from '@utils/isOnboard'
import isAdmin from '@utils/isAdmin'

const router = express.Router()

router.get('/searchUsers', isOnboard, isAdmin, commonController.searchUsers)

export default router
2 changes: 2 additions & 0 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import authRouter from '@routers/auth'
import createQuizRouter from '@routers/createQuiz'
import giveQuizRouter from '@routers/giveQuiz'
import checkQuizRouter from '@routers/checkQuiz'
import commonRouter from '@routers/common'
import logRouter from '@routers/log'
import cookieParser from 'cookie-parser'
import morgan from 'morgan'
Expand Down Expand Up @@ -58,6 +59,7 @@ app.use('/auth', authRouter)
app.use('/checkQuiz', checkQuizRouter)
app.use('/createQuiz', createQuizRouter)
app.use('/giveQuiz', giveQuizRouter)
app.use('/common', commonRouter)
app.use('/log', logRouter)
app.get('/', (req: Request, res: Response) => {
res.send('Express + TypeScript Server')
Expand Down