diff --git a/src/controllers/common/index.ts b/src/controllers/common/index.ts new file mode 100644 index 0000000..9989102 --- /dev/null +++ b/src/controllers/common/index.ts @@ -0,0 +1 @@ +export { default as searchUsers } from './searchUsers' diff --git a/src/controllers/common/searchUsers.ts b/src/controllers/common/searchUsers.ts new file mode 100644 index 0000000..f53c264 --- /dev/null +++ b/src/controllers/common/searchUsers.ts @@ -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 \ No newline at end of file diff --git a/src/controllers/createQuiz/quiz/index.ts b/src/controllers/createQuiz/quiz/index.ts index ca74eb1..00f82a1 100644 --- a/src/controllers/createQuiz/quiz/index.ts +++ b/src/controllers/createQuiz/quiz/index.ts @@ -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' \ No newline at end of file diff --git a/src/routers/common/index.ts b/src/routers/common/index.ts new file mode 100644 index 0000000..6c31fd4 --- /dev/null +++ b/src/routers/common/index.ts @@ -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 \ No newline at end of file diff --git a/src/server.ts b/src/server.ts index c7d8653..e3d5cc3 100644 --- a/src/server.ts +++ b/src/server.ts @@ -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' @@ -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')