瀏覽代碼

get all applicants, get profile, update profile API modules added

master
kj1352 4 年之前
父節點
當前提交
0483c41d5a
共有 6 個文件被更改,包括 86 次插入7 次删除
  1. +3
    -3
      models/quiz.ts
  2. +3
    -2
      models/user.ts
  3. +2
    -1
      src/authentication/auth.ts
  4. +1
    -1
      src/authentication/routes.ts
  5. +2
    -0
      src/index.ts
  6. +75
    -0
      src/user-profile/routes.ts

+ 3
- 3
models/quiz.ts 查看文件

@@ -1,4 +1,4 @@
export default interface Question {
export type Question = {
type: 'INPUT' | 'MCQ',
text: string,
choices?: Array<string>,
@@ -7,13 +7,13 @@ export default interface Question {
marks: number,
}

export default interface Quiz {
export type Quiz = {
questions: Array<Question>,
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,


+ 3
- 2
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,


+ 2
- 1
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) {


+ 1
- 1
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';


+ 2
- 0
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'));


+ 75
- 0
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;
});

Loading…
取消
儲存