Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
MooTamer committed Feb 11, 2024
2 parents c3de2f3 + 78212bc commit 0e9c112
Show file tree
Hide file tree
Showing 43 changed files with 1,591 additions and 1,315 deletions.
27 changes: 27 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: Build

on:
push:
branches:
- main


jobs:
build:
name: Build
runs-on: ubuntu-latest
permissions: read-all
steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
- uses: sonarsource/sonarqube-scan-action@master
env:
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
# If you wish to fail your job when the Quality Gate is red, uncomment the
# following lines. This would typically be used to fail a deployment.
# - uses: sonarsource/sonarqube-quality-gate-action@master
# timeout-minutes: 5
# env:
# SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
69 changes: 56 additions & 13 deletions TADirectory/controller/TADirectory.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,50 @@
const { TaModel } = require("../model/TADirectory");
const getCookie = require("../utils/cookies").getEntriesFromCookie;
const updateUserPoints = require("../utils/addPoints").updateUserPoints;
const { TaCourseModel } = require("../model/TADirectory");
const Redis = require("redis");
dotenv = require("dotenv");
dotenv.config();
// let client;
const DEFAULT_EXPIRATION = 600;
const client = Redis.createClient({
password: process.env.REDIS_PASSWORD,
socket: {
host: 'redis-16631.c299.asia-northeast1-1.gce.cloud.redislabs.com',
port: 16631
}
});

console.log(process.env.REDIS_HOSTNAME, process.env.REDIS_PORT, process.env.REDIS_PASSWORD);
client.connect();
client.on("connect", () => {
console.log("Connected to our redis instance!");
client.set("Greatest Basketball Player", "Lebron James");
});

exports.getAllTas = async (req, res) => {
try {
let tas = await client.get("tas");
// get when will it expire
// const ttl = await client.ttl("tas");
// console.log("ttl", ttl);
if (!tas || tas.length === 0) {
console.log("tas not found in cache");
tas = await TaModel.find();
client.setEx("tas", DEFAULT_EXPIRATION, JSON.stringify(tas));
} else tas = JSON.parse(tas);

return res.status(200).json(tas);
} catch (err) {
console.error(err);
res.status(500).json({ message: "Internal server error" });
}
};
// TODO make url deployed
exports.addTa = async (req, res) => {
const { name, email, officeLocation } = req.body;
const uniEmail = email + "@giu-uni.de";
// reset the cache
client.del("tas");
console.log(uniEmail);
const found = await TaModel.find({ email: uniEmail });
if (found !== null && found.length > 0) {
Expand All @@ -24,19 +64,16 @@ exports.addTa = async (req, res) => {
}
res.status(200).json({ message: "TA created" });
};
23;

exports.getAllTas = async (req, res) => {
try {
const tas = await TaCourseModel.find();
res.status(200).json(tas);
} catch (err) {
console.error(err);
res.status(500).json({ message: "Internal server error" });
}
};
exports.getTaCourses = async (req, res) => {
try {
const tas = await TaCourseModel.find();
let tas = await client.get("taCourses");
if (!tas || tas.length === 0) {
console.log("tas not found in cache");
tas = await TaCourseModel.find();
client.setEx("taCourses", DEFAULT_EXPIRATION, JSON.stringify(tas));
} else tas = JSON.parse(tas);
res.status(200).json(tas);
} catch (err) {
console.error(err);
Expand All @@ -47,7 +84,9 @@ exports.getTaCourses = async (req, res) => {
exports.assignTa = async (req, res) => {
// TODO get all tuts already exist for this course and compare with the new ones
const { email, tutorials, courseName, officeHours } = req.body;
console.log(email, tutorials, courseName, officeHours);
//reset cache
client.del("taCourses");

// const userEmail = getCookie(req).email;
let found = await TaModel.find({ email: email });
if (found === null || found.length === 0) {
Expand All @@ -73,7 +112,8 @@ exports.assignTa = async (req, res) => {
};
exports.deleteTa = async (req, res) => {
const { email } = req.body;
console.log(email);
// reset cache
client.del("tas");
try {
await TaModel.deleteOne({ email });
res.status(200).json({ message: "TA deleted" });
Expand All @@ -87,6 +127,9 @@ exports.deleteTa = async (req, res) => {
};
exports.deleteTaCourse = async (req, res) => {
const { _id } = req.body;
// reset cache
client.del("taCourses");

try {
await TaCourseModel.deleteOne({ _id });
res.status(200).json({ message: "TA course deleted" });
Expand Down
6 changes: 0 additions & 6 deletions TADirectory/middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,27 @@ const getCookie = require("../utils/cookies").getEntriesFromCookie;
const config = process.env;
module.exports.verifyToken = (req, res, next) => {
const authcookie = getCookie(req).email;
console.log("token verfied");
console.log("the cookieee "+ getCookie(req));
if (!authcookie) {
return res.status(403).send("A token is required for authentication");
}
return next();
};
module.exports.verifyRole = async (req, res, next) => {
const authcookie = getCookie(req);
console.log(authcookie);
if (!authcookie) {
return res.status(403).send("A token is required for authentication");
}
if (!authcookie.isAdmin) {
return res.status(401).send("Invalid Token you are not admin");
}
console.log("role verfied");
next();
};
module.exports.testVerifyToken = (req, res, next) => {
console.log(" you made it to the protected route");
res.send("You made it to the route.");
next();
};

module.exports.testVerifyRole = (req, res, next) => {
console.log(" you made it to the admin route");
res.send("You made it to the route.");
next();
};
3 changes: 2 additions & 1 deletion TADirectory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"express-session": "^1.17.3",
"jsonwebtoken": "^9.0.2",
"mongoose": "^7.5.3",
"nodemon": "^3.0.1"
"nodemon": "^3.0.1",
"redis": "^4.6.13"
}
}
4 changes: 0 additions & 4 deletions TADirectory/utils/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const refreshSecret = process.env.REFRESH_TOKEN_SECRET;
function getEntriesFromCookie(req) {
let authCookie = "";
let refreshToken = "";
console.log("headderss \n\n\n" + req.headers.cookie);
if (req.headers.cookie.includes("authcookie")) {
authCookie = req.headers.cookie.split("authcookie=")[1].split(";")[0];
}
Expand All @@ -19,23 +18,20 @@ function getEntriesFromCookie(req) {
const decodedAccessToken = jwt.verify(authCookie, secret);

// If the access token is valid, return its payload
console.log("Access token payload:", decodedAccessToken);
return decodedAccessToken;
} catch (accessError) {
// Access token has expired or is invalid, let's try to use the refresh token
try {
// Verify the refresh token
const decodedRefreshToken = jwt.verify(refreshToken, refreshSecret);
const { email, isAdmin } = decodedRefreshToken;
console.log("the tokens = " + " " + email + " " + isAdmin + "\n\n\n");
const newAccessToken = jwt.sign({ email, isAdmin }, secret, {
expiresIn: "2h",
});
// Generate a new access token with the same payload data as the one we just decoded
//decode it
const newDecodedToken = jwt.verify(newAccessToken, secret);

console.log("New access token:", newDecodedToken);
return newDecodedToken;
} catch (refreshError) {
// Both access and refresh tokens are invalid, handle the error
Expand Down
6 changes: 4 additions & 2 deletions client/css/TA-directory.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
display: grid;
grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
gap: 40px;
margin: 50px 0;
margin: 50px 0 180px;
}
/* start search bar */

Expand Down Expand Up @@ -137,10 +137,11 @@
-o-border-radius: 0.25rem;
border-radius: 0.25rem;
color: var(--third-color);
/* text-transform: capitalize; */
text-transform: capitalize;
font-size: 1.2rem;
margin: 0;
overflow: hidden;
margin-top: 30px;
}

.taDirectory .container .card .wrap-data .wrap {
Expand Down Expand Up @@ -183,6 +184,7 @@
justify-content: end;
gap: 20px;
}

.taDirectory .container .addTaBtn {
border-radius: 0.65rem;
padding: 10px 12px;
Expand Down
132 changes: 132 additions & 0 deletions client/css/assignTA.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
.taCreation .container form {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 40px;
background-color: white;
-webkit-border-radius: 1.25rem;
-moz-border-radius: 1.25rem;
-ms-border-radius: 1.25rem;
-o-border-radius: 1.25rem;
border-radius: 1.25rem;
border: 1px solid;
box-shadow: 7px 7px 0px 3px var(--secondary-color);
cursor: pointer;
text-align: center;
text-transform: capitalize;
margin-top: 60px;
}

.taCreation .container form .img {
display: grid;
place-content: center;
background-color: var(--third-color);
border-radius: 1.25rem 0 0 1.25rem;
-webkit-border-radius: 1.25rem 0 0 1.25rem;
-moz-border-radius: 1.25rem 0 0 1.25rem;
-ms-border-radius: 1.25rem 0 0 1.25rem;
-o-border-radius: 1.25rem 0 0 1.25rem;
background: linear-gradient(
-45deg,
var(--main-color),
var(--secondary-color),
var(--third-color),
var(--fourth-color)
);
background-size: 400% 400%;
display: grid;
place-content: center;
}

.taCreation .container form .img img {
width: 100%;
}

.taCreation .container form .box {
display: flex;
flex-direction: column;
justify-content: space-around;
padding: 40px;
}

@media (max-width: 767px) {
.taCreation .container form {
display: flex;
flex-direction: column;
}
.taCreation .container form .box {
gap: 20px;
}

.taCreation .container form .img {
display: none;
}
}

.taCreation .container form .box h3 {
color: var(--third-color);
}

.taCreation .container form .box input {
border: 0;
padding: 15px;
box-shadow:
rgba(0, 0, 0, 0.05) 0px 6px 24px 0px,
rgba(0, 0, 0, 0.08) 0px 0px 0px 1px;
border-radius: 6px;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
-ms-border-radius: 6px;
-o-border-radius: 6px;
}

.taCreation .container form .box input::placeholder {
text-transform: capitalize;
}

.taCreation .container form .box input:focus {
outline: none;
}

.taCreation .container form .box .tutorial-wrap {
display: flex;
justify-content: space-between;
align-items: center;
}

.taCreation .container form .box .listOfTutorials {
display: grid;
gap: 5px;
}

.taCreation .container form .box .listOfTutorials .tut {
display: flex;
justify-content: space-between;
transition: 0.3s;
-webkit-transition: 0.3s;
-moz-transition: 0.3s;
-ms-transition: 0.3s;
-o-transition: 0.3s;
}

.taCreation .container form .box .listOfTutorials .tut .del {
font-weight: 500;
}

.taCreation .container form .box .listOfTutorials .tut .del:hover {
color: #e06c75;
}

.taCreation .container form .box button {
background-color: var(--fourth-color);
color: white;
border: 0;
border-radius: 6px;
-webkit-border-radius: 6px;
-moz-border-radius: 6px;
-ms-border-radius: 6px;
-o-border-radius: 6px;
padding: 10px;
cursor: pointer;
font-size: 20px;
text-transform: capitalize;
}
Loading

0 comments on commit 0e9c112

Please sign in to comment.