Skip to content

Commit

Permalink
working???
Browse files Browse the repository at this point in the history
  • Loading branch information
sid-anand committed Oct 18, 2022
1 parent 55a79d7 commit fd002db
Show file tree
Hide file tree
Showing 15 changed files with 492 additions and 391 deletions.
5 changes: 5 additions & 0 deletions server/.gcloudignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Exclude compiled .js files
*.js

# Exclude dependencies
node_modules/
11 changes: 9 additions & 2 deletions server/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
/node_modules
/.env
# Exclude compiled .js files
*.js

# Exclude dependencies
node_modules/

.env

dist/
1 change: 1 addition & 0 deletions server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# history-course-sorting-interface
11 changes: 11 additions & 0 deletions server/app.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
runtime: nodejs16

env_variables:
MONGODB_URI: "mongodb+srv://dummyUser:[email protected]/?retryWrites=true&w=majority"
PORT: 4000
CLIENT_URL: "https://sage-instrument-365221.uk.r.appspot.com/"
GOOGLE_CLIENT_ID: "108318053408-70egqlado3ahcqflcr87vqgl6g62qoda.apps.googleusercontent.com"
GOOGLE_CLIENT_SECRET: "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"
SESSION_SECRET: "secret"
GMAIL_USERNAME: "[email protected]"
GMAIL_PASSWORD: "hddfwgrunwajbnao"
713 changes: 383 additions & 330 deletions server/package-lock.json

Large diffs are not rendered by default.

7 changes: 4 additions & 3 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon --exec ts-node src/index.ts",
"other-start": "node src/index.ts"
"start": "node ./dist/index.js",
"gcp-build": "tsc -p .",
"deploy": "gcloud app deploy"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -43,4 +44,4 @@
"@types/nodemailer": "^6.4.5",
"typescript": "^4.7.4"
}
}
}
Binary file removed server/public/.DS_Store
Binary file not shown.
Binary file removed server/src/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions server/src/config/mongo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import mongoose from "mongoose";

// connect to mongodb
export function mongoConnection() {
console.log("MONGO URI!!!", process.env.MONGODB_URI);
mongoose.connect(
process.env.MONGODB_URI,
() => console.log("connected to mongodb")
Expand Down
11 changes: 9 additions & 2 deletions server/src/config/passport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ import User, { IUser } from "../models/User";
import { ROLES } from "../models/User"

export function passportInit() {
console.log('start of passport')
// serialize the user.id to save in the cookie session
// so the browser will remember the user when login
passport.serializeUser((user: any, done) => {
console.log('passport serializeUser')
done(null, user.id);
});

// deserialize the cookieUserId to user in the database
passport.deserializeUser((id, done) => {
console.log('passport deserializeUser')
User.findById(id)
.then((user) => {
done(null, user);
Expand Down Expand Up @@ -59,6 +62,8 @@ export function passportInit() {
displayPicURL = profile.photos[0].value;
}

console.log('about to find user')

try {
// searches for user in mongoDB collection by googleId
let user = await User.findOne({ googleId: profile.id });
Expand All @@ -68,10 +73,11 @@ export function passportInit() {
} else {
// if no google id, try email
// this will be how all prefilled professors log in for the first time
user = await User.findOne({email: profile._json.email});
user = await User.findOne({ email: profile._json.email });
if (user) {
// update user to have googleId so they can log in normally next time
await User.updateOne({_id: user._id}, {googleId: profile.id})
await User.updateOne({ _id: user._id }, { googleId: profile.id })
console.log('about to be done')
done(null, user)
} else {
// creates a new user object
Expand All @@ -84,6 +90,7 @@ export function passportInit() {
};
// creates new user if not in mongoDB collection
user = await User.create(newUser);
console.log('about to be done')
done(null, user);
}
}
Expand Down
81 changes: 43 additions & 38 deletions server/src/index.ts
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,55 @@ import { passportInit } from "./config/passport";
import { mongoConnection } from "./config/mongo";

export function main() {
passportInit();
const app = express();
passportInit();
const app = express();

mongoConnection();
mongoConnection();

// express session
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: { maxAge: 86400000 * 30 }, // 30 days cookie expiry
store: MongoStore.create({
mongoUrl: process.env.MONGODB_URI,
}),
})
);
// express session
app.use(
session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: false,
cookie: { maxAge: 86400000 * 30 }, // 30 days cookie expiry
store: MongoStore.create({
mongoUrl: process.env.MONGODB_URI,
}),
})
);

// parse incoming cookies of html requests
app.use(cookieParser());
// parse body of http request
app.use(express.json());
// parse incoming cookies of html requests
app.use(cookieParser());
// parse body of http request
app.use(express.json());

// initialize and set up passport to use sessions
app.use(passport.initialize());
app.use(passport.session());
// initialize and set up passport to use sessions
app.use(passport.initialize());
app.use(passport.session());

// set up cors to allow us to accept requests from front end client
app.use(
cors({
origin: process.env.CLIENT_URL,
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
credentials: true,
})
);
// set up cors to allow us to accept requests from front end client
app.use(
cors({
origin: process.env.CLIENT_URL,
methods: "GET,HEAD,PUT,PATCH,POST,DELETE",
credentials: true,
})
);

// set up routes
app.use("/auth", authRouter);
app.use("/courses", courseRouter);
app.use("/users", userRouter);
// set up routes
app.use("/auth", authRouter);
app.use("/courses", courseRouter);
app.use("/users", userRouter);

// server starts listening
app.listen(process.env.PORT, () => {
console.log(`Server running on port ${process.env.PORT}`);
});
app.get("/", (req, res, next) => res.send("helloooo"));

console.log('about to listen')

// server starts listening
app.listen(8080, () => {
console.log(`Server running on port 8080`);
console.log('after server running')
});
}
main();
22 changes: 11 additions & 11 deletions server/src/routes/courses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ function strToBool(str: string): boolean {
// search courses
courseRouter.get("/search/:finalized", authCheck, async (req: IGetUserAuthInfoRequest, res: Response) => {

const search_term = req.query;
const search_term = (req as any).query;
let finalized_term = {};

if (!search_term.proposal_status && typeof req.params.finalized !== 'undefined') { // if finalized exists (has been set)
if (strToBool(req.params.finalized)) { // if finalized is true
if (!search_term.proposal_status && typeof (req as any).params.finalized !== 'undefined') { // if finalized exists (has been set)
if (strToBool((req as any).params.finalized)) { // if finalized is true
finalized_term = { proposal_status: PROPOSAL_STATUS.CCC_ACCEPTED };
} else { // want proposed courses
finalized_term = { proposal_status: { $ne: PROPOSAL_STATUS.CCC_ACCEPTED } };
Expand All @@ -42,11 +42,11 @@ courseRouter.get("/search/:finalized", authCheck, async (req: IGetUserAuthInfoRe

// NOT TO BE USED BY FRONTEND
courseRouter.get("/search-dev-only/:finalized", async (req: IGetUserAuthInfoRequest, res: Response) => {
const search_term = req.query;
const search_term = (req as any).query;
let finalized_term = {};

if (!search_term.proposal_status && typeof req.params.finalized !== 'undefined') { // if finalized exists (has been set)
if (strToBool(req.params.finalized)) { // if finalized is true
if (!search_term.proposal_status && typeof (req as any).params.finalized !== 'undefined') { // if finalized exists (has been set)
if (strToBool((req as any).params.finalized)) { // if finalized is true
finalized_term = { proposal_status: PROPOSAL_STATUS.CCC_ACCEPTED };
} else { // want proposed courses
finalized_term = { proposal_status: { $ne: PROPOSAL_STATUS.CCC_ACCEPTED } };
Expand Down Expand Up @@ -93,7 +93,7 @@ interface ICourseProposalRequest {

// submit a course
courseRouter.post("/submit", authCheck, async (req: IGetUserAuthInfoRequest, res: Response) => {
const proposalRequest = req.body as ICourseProposalRequest;
const proposalRequest = (req as any).body as ICourseProposalRequest;
const status = getCourseStatus(proposalRequest.proposed, proposalRequest.original);

if ((await Course.find(proposalRequest.proposed)).length > 0) { // duplicate course
Expand All @@ -119,7 +119,7 @@ courseRouter.post("/submit", authCheck, async (req: IGetUserAuthInfoRequest, res

// edit a course
courseRouter.post("/edit", authCheck, async (req: IGetUserAuthInfoRequest, res: Response) => {
var course = req.body as ICourse;
var course = (req as any).body as ICourse;

if (req.user.role === ROLES.DEFAULT) {
res.status(403).json({
Expand Down Expand Up @@ -219,16 +219,16 @@ courseRouter.post("/edit", authCheck, async (req: IGetUserAuthInfoRequest, res:

// NOT TO BE USED BY FRONTEND
courseRouter.post("/submit-dev-only", async (req: IGetUserAuthInfoRequest, res: Response) => {
const proposalRequest = req.body as ICourse;
const proposalRequest = (req as any).body as ICourse;
const newCourse = await Course.create({
...proposalRequest
});
res.status(200).json({ newCourse });
});

courseRouter.post("/accept-reject/:is_accept", authCheck, async (req: IGetUserAuthInfoRequest, res: Response) => {
const isAccept = typeof req.params.is_accept !== 'undefined' && strToBool(req.params.is_accept);
const { course, reason } = req.body as { course: ICourse, reason: string };
const isAccept = typeof (req as any).params.is_accept !== 'undefined' && strToBool((req as any).params.is_accept);
const { course, reason } = (req as any).body as { course: ICourse, reason: string };
var new_status = '';

if (req.user.role === ROLES.MANAGER || req.user.role === ROLES.GRAD_DIRECTOR || req.user.role === ROLES.UG_DIRECTOR) {
Expand Down
6 changes: 3 additions & 3 deletions server/src/routes/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ interface RoleBody {
// change user's role; this should only be used by managers
// id is the string form of the _id of the user whose role is being changed, role is their new role as a string in req.body
userRouter.post("/change-role/:id", authCheck, async (req: IGetUserAuthInfoRequest, res: Response) => {
const role_body = req.body as RoleBody;
const role_body = (req as any).body as RoleBody;

if (typeof req.params.id === 'undefined') {
if (typeof (req as any).params.id === 'undefined') {
res.status(400).json({
message: "specify the user whose role is to be changed",
});
Expand Down Expand Up @@ -69,7 +69,7 @@ userRouter.post("/change-role/:id", authCheck, async (req: IGetUserAuthInfoReque
return;
}

const user_id = req.params.id;
const user_id = (req as any).params.id;

try {
await User.updateOne({ _id: user_id }, {
Expand Down
8 changes: 6 additions & 2 deletions server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
"module": "commonjs",
"outDir": "dist",
"sourceMap": true,
"types": ["node"],
"types": [
"node"
],
"moduleResolution": "node",
"esModuleInterop": true
},
"include": [
"src/**/*.ts"],
"src/**/*.ts",
"src/index.js"
],
"exclude": [
"node_modules"
]
Expand Down
6 changes: 6 additions & 0 deletions server/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "tslint:recommended",
"rules": {
"no-console": false
}
}

0 comments on commit fd002db

Please sign in to comment.