@@ -1,4 +1,4 @@ | |||||
export default interface Question { | |||||
export type Question = { | |||||
type: 'INPUT' | 'MCQ', | type: 'INPUT' | 'MCQ', | ||||
text: string, | text: string, | ||||
choices?: Array<string>, | choices?: Array<string>, | ||||
@@ -7,13 +7,13 @@ export default interface Question { | |||||
marks: number, | marks: number, | ||||
} | } | ||||
export default interface Quiz { | |||||
export type Quiz = { | |||||
questions: Array<Question>, | questions: Array<Question>, | ||||
minScoreToPass: number, | minScoreToPass: number, | ||||
totalDuration?: number, // Military time 060 is 6 AM, 1300 is 1 PM | totalDuration?: number, // Military time 060 is 6 AM, 1300 is 1 PM | ||||
} | } | ||||
export default interface Assignment { | |||||
export type Assignment = { | |||||
heading: string, | heading: string, | ||||
description: string, //Rich text | description: string, //Rich text | ||||
deadlineDate: string, | deadlineDate: string, | ||||
@@ -1,9 +1,10 @@ | |||||
export default interface skillInformation { | |||||
export type skillInformation = { | |||||
name: string, | name: string, | ||||
category: 'FRONT_END' | 'BACKEND' | 'DATABASE' | 'BENIFICIAL' | category: 'FRONT_END' | 'BACKEND' | 'DATABASE' | 'BENIFICIAL' | ||||
}; | }; | ||||
export default interface IUser { | |||||
export type IUser = { | |||||
_id: string, | |||||
name: string, | name: string, | ||||
email: string, | email: string, | ||||
password: string, | password: string, | ||||
@@ -2,6 +2,7 @@ import passport from 'passport'; | |||||
import passportJWT, { StrategyOptions } from 'passport-jwt'; | import passportJWT, { StrategyOptions } from 'passport-jwt'; | ||||
import jwt from 'jsonwebtoken'; | import jwt from 'jsonwebtoken'; | ||||
import { getDatabaseClient, DB_NAME } from '../db-utils'; | import { getDatabaseClient, DB_NAME } from '../db-utils'; | ||||
import { ObjectId } from 'bson'; | |||||
const DEFAULT_SECRET = process.env.SECRET_KEY || '02faf720-e46c-4af8-b4f8-8cdc8ba1aaf5'; | const DEFAULT_SECRET = process.env.SECRET_KEY || '02faf720-e46c-4af8-b4f8-8cdc8ba1aaf5'; | ||||
export const SALT_ROUNDS = 12; | 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'); | const usersCollection = getDatabaseClient().db(DB_NAME).collection('users'); | ||||
try { | try { | ||||
const user = await usersCollection.findOne({ | const user = await usersCollection.findOne({ | ||||
_id: jwtPayload.sub | |||||
_id: new ObjectId(jwtPayload.sub) | |||||
}); | }); | ||||
if (user && !user.archived) { | if (user && !user.archived) { | ||||
@@ -1,4 +1,4 @@ | |||||
import express, { response } from 'express'; | |||||
import express from 'express'; | |||||
import { DB_NAME, getDatabaseClient } from '../db-utils'; | import { DB_NAME, getDatabaseClient } from '../db-utils'; | ||||
import bcrypt from 'bcrypt'; | import bcrypt from 'bcrypt'; | ||||
import { generateJWT, SALT_ROUNDS } from './auth'; | import { generateJWT, SALT_ROUNDS } from './auth'; | ||||
@@ -4,6 +4,7 @@ import passport from 'passport'; | |||||
import authRoutes from './authentication/routes'; | import authRoutes from './authentication/routes'; | ||||
import { connectToDatabaseServer } from './db-utils'; | import { connectToDatabaseServer } from './db-utils'; | ||||
import sendGridMail from '@sendgrid/mail'; | import sendGridMail from '@sendgrid/mail'; | ||||
import { userProfileRoutes } from './user-profile/routes'; | |||||
const SENDGRID_API_KEY = process.env.SENDGRID_API_KEY || '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'; | 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.use(passport.initialize()); | ||||
app.set('port', process.env.PORT || 8001); | app.set('port', process.env.PORT || 8001); | ||||
app.use('/', authRoutes); | app.use('/', authRoutes); | ||||
app.use('/', userProfileRoutes); | |||||
app.get('/', (request, response) => { | app.get('/', (request, response) => { | ||||
response.send('Server running' + app.get('port')); | response.send('Server running' + app.get('port')); | ||||
@@ -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; | |||||
}); |