diff --git a/models/quiz.ts b/models/quiz.ts index d06eb84..2885188 100644 --- a/models/quiz.ts +++ b/models/quiz.ts @@ -1,4 +1,4 @@ -export default interface Question { +export type Question = { type: 'INPUT' | 'MCQ', text: string, choices?: Array, @@ -7,13 +7,13 @@ export default interface Question { marks: number, } -export default interface Quiz { +export type Quiz = { questions: Array, minScoreToPass: number, totalDuration?: number, // Military time 060 is 6 AM, 1300 is 1 PM } -export default interface Assignment { +export type Assignment = { heading: string, description: string, //Rich text deadlineDate: string, diff --git a/models/user.ts b/models/user.ts index b292be2..8d0e7f5 100644 --- a/models/user.ts +++ b/models/user.ts @@ -1,9 +1,10 @@ -export default interface skillInformation { +export type skillInformation = { name: string, category: 'FRONT_END' | 'BACKEND' | 'DATABASE' | 'BENIFICIAL' }; -export default interface IUser { +export type IUser = { + _id: string, name: string, email: string, password: string, diff --git a/src/authentication/auth.ts b/src/authentication/auth.ts index 14c4ade..82c1c41 100644 --- a/src/authentication/auth.ts +++ b/src/authentication/auth.ts @@ -2,6 +2,7 @@ import passport from 'passport'; import passportJWT, { StrategyOptions } from 'passport-jwt'; import jwt from 'jsonwebtoken'; import { getDatabaseClient, DB_NAME } from '../db-utils'; +import { ObjectId } from 'bson'; const DEFAULT_SECRET = process.env.SECRET_KEY || '02faf720-e46c-4af8-b4f8-8cdc8ba1aaf5'; export const SALT_ROUNDS = 12; @@ -20,7 +21,7 @@ passport.use(new JwtStrategy(strategyOptions, async (jwtPayload, done) => { const usersCollection = getDatabaseClient().db(DB_NAME).collection('users'); try { const user = await usersCollection.findOne({ - _id: jwtPayload.sub + _id: new ObjectId(jwtPayload.sub) }); if (user && !user.archived) { diff --git a/src/authentication/routes.ts b/src/authentication/routes.ts index 21a2203..4ca6d54 100644 --- a/src/authentication/routes.ts +++ b/src/authentication/routes.ts @@ -1,4 +1,4 @@ -import express, { response } from 'express'; +import express from 'express'; import { DB_NAME, getDatabaseClient } from '../db-utils'; import bcrypt from 'bcrypt'; import { generateJWT, SALT_ROUNDS } from './auth'; diff --git a/src/index.ts b/src/index.ts index 0d06b92..d1a8ada 100644 --- a/src/index.ts +++ b/src/index.ts @@ -4,6 +4,7 @@ import passport from 'passport'; import authRoutes from './authentication/routes'; import { connectToDatabaseServer } from './db-utils'; import sendGridMail from '@sendgrid/mail'; +import { userProfileRoutes } from './user-profile/routes'; const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY || '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'; @@ -15,6 +16,7 @@ app.use(express.json()); app.use(passport.initialize()); app.set('port', process.env.PORT || 8001); app.use('/', authRoutes); +app.use('/', userProfileRoutes); app.get('/', (request, response) => { response.send('Server running' + app.get('port')); diff --git a/src/user-profile/routes.ts b/src/user-profile/routes.ts new file mode 100644 index 0000000..9269b51 --- /dev/null +++ b/src/user-profile/routes.ts @@ -0,0 +1,75 @@ +import express from 'express'; +import passport, { use } from 'passport'; +import { getDatabaseClient, DB_NAME } from '../db-utils'; +import { IUser } from '../../models/user'; + +export const userProfileRoutes = express.Router(); + +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) => { + 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..."); + } + + return; +}); \ No newline at end of file