| @@ -1,75 +1,13 @@ | |||||
| import express from 'express'; | import express from 'express'; | ||||
| import passport, { use } from 'passport'; | |||||
| import { getDatabaseClient, DB_NAME } from '../db-utils'; | |||||
| import passport from 'passport'; | |||||
| import { IUser } from '../../models/user'; | import { IUser } from '../../models/user'; | ||||
| export const userProfileRoutes = express.Router(); | export const userProfileRoutes = express.Router(); | ||||
| export const jwtAuthentication = passport.authenticate('jwt', { session: false }); | export const jwtAuthentication = passport.authenticate('jwt', { session: false }); | ||||
| userProfileRoutes.get('/all-applicants/', jwtAuthentication, async (request, response) => { | |||||
| const userCollection = getDatabaseClient().db(DB_NAME).collection('users'); | |||||
| const user: IUser = (request.user as any); | |||||
| if (user.userType === 'ADMIN') { | |||||
| const allUsers = await userCollection.find({ | |||||
| userType: 'APPLICANT' | |||||
| }).toArray(); | |||||
| for (let i = 0; i < allUsers.length; i += 1) { | |||||
| delete allUsers[i].password; | |||||
| delete allUsers[i].otp; | |||||
| } | |||||
| response.json(allUsers); | |||||
| } else { | |||||
| response.status(401); | |||||
| response.send('Lol, you new to the platform?'); | |||||
| } | |||||
| return; | |||||
| }); | |||||
| userProfileRoutes.get('/profile/', jwtAuthentication, async (request, response) => { | userProfileRoutes.get('/profile/', jwtAuthentication, async (request, response) => { | ||||
| const user: IUser = (request.user as any); | const user: IUser = (request.user as any); | ||||
| response.json({ | |||||
| id: user._id, | |||||
| name: user.name, | |||||
| email: user.email, | |||||
| isVerified: user.isVerified, | |||||
| userType: user.userType, | |||||
| skillSet: user.skillSet, | |||||
| progress: user.progress, | |||||
| userDocuments: user.userDocuments | |||||
| }); | |||||
| return; | |||||
| }); | |||||
| userProfileRoutes.post('/profile/', jwtAuthentication, async (request, response) => { | |||||
| const userCollection = getDatabaseClient().db(DB_NAME).collection('users'); | |||||
| const user: IUser = (request.user as any); | |||||
| try { | |||||
| await userCollection.updateOne({ | |||||
| email: user.email | |||||
| }, { | |||||
| $set: { | |||||
| skillSet: request.body.skillSet || user.skillSet, | |||||
| progress: request.body.progress || user.progress, | |||||
| userDocuments: request.body.userDocuments || user.userDocuments, | |||||
| } | |||||
| }); | |||||
| response.send("Updated"); | |||||
| } catch(e) { | |||||
| console.log(e); | |||||
| response.status(500); | |||||
| response.send("Weird, could not find the user even though your were authenticated..."); | |||||
| } | |||||
| response.json(user); | |||||
| return; | return; | ||||
| }); | }); | ||||