Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

Commit

Permalink
feat: minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKanera committed Feb 20, 2021
1 parent 5352561 commit 766cc33
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 42 deletions.
2 changes: 1 addition & 1 deletion components/proposal-form/proposal-form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default defineComponent({
// @ts-ignore
const response = await ctx.root.$nuxt.$axios.get('/api/teachers/list', {
headers: {
authorization: mainStore.state.user.id,
authorization: `Bearer ${mainStore.state.user.idToken}`,
},
});
Expand Down
2 changes: 1 addition & 1 deletion components/ui/snackbar/snackbar.sass
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
.snackbar
@apply w-screen h-auto py-3 px-6 text-ps-primary font-semibold
@apply fixed bottom-0 bg-ps-linear-gradient rounded-t-lg shadow
@apply left-0
@apply left-0 z-999

@screen md
@apply w-8/12 left-1/2
Expand Down
2 changes: 1 addition & 1 deletion server/api/deadlines/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default async (req: Request, res: Response) => {
case 'Error: 403':
return res.status(403).send('Only admin can update deadlines');
default:
return res.status(500).send();
return res.status(500).send(e);
}
}
} catch (_) {
Expand Down
10 changes: 7 additions & 3 deletions server/api/project/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,15 @@ export default async (req: Request, res: Response) => {
if (!project?.exists) return res.status(404).send('Project does not exist');

if (!projectData?.public) {
const userAuth = !idToken ? { uid: 'public' } : await admin.auth().verifyIdToken(idToken);
const user = await admin.firestore().collection('users').doc(userAuth.uid).get();
try {
const userAuth = await admin.auth().verifyIdToken(idToken);
const user = await admin.firestore().collection('users').doc(userAuth.uid).get();

if (!user.data()?.admin && !(projectData?.studentId === userAuth.uid) && !(projectData?.teacherId === userAuth.uid) && !(projectData?.opponentId === userAuth.uid))
if (!user.data()?.admin && !(projectData?.studentId === userAuth.uid) && !(projectData?.teacherId === userAuth.uid) && !(projectData?.opponentId === userAuth.uid))
return res.status(403).send();
} catch (_) {
return res.status(403).send();
}
}

let deadlineDate = projectData?.deadlineDate;
Expand Down
23 changes: 19 additions & 4 deletions server/api/project/student-update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,19 @@ export default async (req: Request, res: Response) => {

const body = JSON.parse(req.body.projectData);

// TODO check mandatory files match type

if (!(typeof body.description === 'string' && Array.isArray(body.links) && req.params.id && checkLinks(body.links) && Array.isArray(body.keywords))) return res.status(400).send();
if (
!(
typeof body.description === 'string' &&
body.description.length <= 250 &&
Array.isArray(body.links) &&
req.params.id &&
checkLinks(body.links) &&
Array.isArray(body.keywords) &&
Array.isArray(body.mandatoryOrder) &&
Array.isArray(body.optionalOrder)
)
)
return res.status(400).send();

// @ts-ignore
const mandatoryFiles = req.files.mandatory;
Expand Down Expand Up @@ -116,7 +126,12 @@ export default async (req: Request, res: Response) => {

transaction.update(projectRef, {
description: body.description.trim(),
links: body.links,
links: body.links.map((link: any) => {
return {
placeholder: link.placeholder,
url: link.url,
};
}),
keywords: body.keywords,
});

Expand Down
9 changes: 7 additions & 2 deletions server/api/public-projects/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export default async (req: Request, res: Response) => {
return formatProjectsData(projects, transaction);
} else if (typeof lastProjectId === 'string') {
const lastDoc = await transaction.get(admin.firestore().collection('projects').doc(lastProjectId));
if (!lastDoc.exists) throw new Error('404');
const projects = await transaction.get(admin.firestore().collection('projects').where('public', '==', true).orderBy('currentYear', 'desc').startAfter(lastDoc).limit(limit));

if (!projects.docs.length) return [];
Expand All @@ -53,7 +54,11 @@ export default async (req: Request, res: Response) => {

return res.send(projects);
} catch (e) {
console.error(e);
return res.status(500).send();
switch (e.toString()) {
case 'Error: 404':
return res.status(404).send('Project with provided ID does not exist');
default:
return res.status(500).send(e);
}
}
};
7 changes: 5 additions & 2 deletions server/api/review/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default async (req: Request, res: Response) => {
const idToken = req.headers.authorization?.split(' ')[1] ?? '';
const projectId = req.params.id;

if (!projectId) return res.status(401).send('Missing params');
if (!projectId) return res.status(400).send('Missing params');

try {
const project = await admin.firestore().collection('projects').doc(projectId).get();
Expand All @@ -38,14 +38,17 @@ export default async (req: Request, res: Response) => {

const projectData = project.data();

if (!projectData?.public && idToken === 'undefined') return res.status(403).send();
if (projectData?.public && idToken === 'undefined') return res.status(200).send(await getReviewsUrls(projectData?.reviews));

try {
// Check auth
const userAuth = await admin.auth().verifyIdToken(idToken);
const userData = (await admin.firestore().collection('users').doc(userAuth.uid).get()).data();

return res.send(await getReviewsUrls(projectData?.reviews, userData?.admin || projectData?.teacherId === userAuth.uid || projectData?.opponentId === userAuth.uid));
} catch (_) {
return res.send(await getReviewsUrls(projectData?.reviews));
return res.status(401).send('Project is not public');
}
} catch (_) {
return res.status(500).send();
Expand Down
2 changes: 0 additions & 2 deletions server/api/student/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ export default async (req: Request, res: Response) => {
const user = await admin.auth().verifyIdToken(idToken);

if (!(await admin.firestore().collection('users').doc(user.uid).get()).data()?.admin) return res.status(403).send('Only admin can update student');

// if (!(await admin.firestore().collection('users').doc(studentId).get()).data()?.student) return res.status(404).send('No student with this id found');
} catch (_) {
return res.status(401).send('Unauthorized');
}
Expand Down
2 changes: 1 addition & 1 deletion server/api/teacher/extern-teacher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ export default async (req: Request, res: Response) => {

return res.status(200).send();
} catch (e) {
res.status(500).send(e);
return res.status(500).send(e);
}
};
49 changes: 24 additions & 25 deletions server/api/teacher/list.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,42 @@
import { Request, Response } from 'express';

import admin from 'firebase-admin';
import 'firebase/auth';

export default async (req: Request, res: Response) => {
const userId = req.headers.authorization ?? '';
const idToken = req.headers.authorization?.split(' ')[1] ?? '';

try {
await admin.auth().getUser(userId);
} catch (e) {
return res.status(401).send();
}
const userAuth = await admin.auth().verifyIdToken(idToken);

if ((await admin.firestore().collection('proposals').where('studentId', '==', userId).get()).docs[0]?.exists)
return res.status(202).send({ message: 'Proposal already submitted', status: 202 });
try {
if ((await admin.firestore().collection('proposals').where('studentId', '==', userAuth.uid).get()).docs[0]?.exists)
return res.status(202).send({ message: 'Proposal already submitted', status: 202 });

if ((await admin.firestore().collection('projects').where('studentId', '==', userId).get()).docs[0]?.exists)
return res.status(202).send({ message: 'You already have project', status: 202 });
if ((await admin.firestore().collection('projects').where('studentId', '==', userAuth.uid).get()).docs[0]?.exists)
return res.status(202).send({ message: 'You already have project', status: 202 });

const userData = (await admin.firestore().collection('users').doc(userId).get()).data();
const userData = (await admin.firestore().collection('users').doc(userAuth.uid).get()).data();

// User doesnt have current year set
if (!userData?.currentYear) return res.status(412).send();
// User doesnt have current year set
if (!userData?.currentYear) return res.status(412).send();

// Teacher cant submit proposal
if (userData?.teacher) return res.status(403).send('Teacher cannot submit proposal');
// Teacher cant submit proposal
if (userData?.teacher) return res.status(403).send('Teacher cannot submit proposal');

try {
const teachersData = (await admin.firestore().collection('users').where('teacher', '==', true).where('extern', '==', false).where('deleted', '==', false).get()).docs;
const teachersData = (await admin.firestore().collection('users').where('teacher', '==', true).where('extern', '==', false).where('deleted', '==', false).get()).docs;

const teachersList = teachersData.map((teacherDoc) => {
return {
placeholder: teacherDoc.data().displayName,
value: teacherDoc.id,
};
});
const teachersList = teachersData.map((teacherDoc) => {
return {
placeholder: teacherDoc.data().displayName,
value: teacherDoc.id,
};
});

return res.status(200).json(teachersList);
return res.status(200).json(teachersList);
} catch (e) {
return res.status(500).send(e);
}
} catch (e) {
return res.status(500).send(e);
return res.status(401).send();
}
};

0 comments on commit 766cc33

Please sign in to comment.