Skip to content

Commit

Permalink
Merge pull request #12 from unovil/student-updates
Browse files Browse the repository at this point in the history
Student updates
  • Loading branch information
unovil authored Apr 20, 2024
2 parents 66b6e71 + 9191701 commit 18ed100
Show file tree
Hide file tree
Showing 19 changed files with 922 additions and 531 deletions.
25 changes: 13 additions & 12 deletions src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
<script lang="ts">
import "../app.css";
import type { LayoutData } from "./$types";
import { page } from '$app/stores';
import { page } from "$app/stores";
export let data: LayoutData;
</script>

{#if ($page.url.pathname === "/") || (data.isLoggedIn === false || data.needsRegisterFollowup === true)}
<div></div>
{:else if data.isStudent === true}
<a href="/dashboard">Dashboard</a>
<a href="/search">Search</a>
{:else if data.isAdmin === true}
<a href="/admindashboard">Dashboard</a>
<a href="/approvals">Approvals</a>
{#if !($page.url.pathname === "/" || data.isLoggedIn === false || data.needsRegisterFollowup === true)}
{#if data.isStudent === true}
<a href="/dashboard">Dashboard</a>
<a href="/approvals">Approvals</a>
<a href="/search">Search</a>
<br />
{:else if data.isAdmin === true}
<a href="/admindashboard">Dashboard</a>
<a href="/approvals">Approvals</a>
<br />
{/if}
{/if}

<br>

<slot />
<slot />
4 changes: 2 additions & 2 deletions src/routes/admindashboard/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
{/if}
<tr class="bg-green-800">
<td></td>
<td>Add more facilities under your purview.</td>
<td><a href="/facility/add">Add more facilities under your purview.</a></td>
<td></td>
</tr>
</table>
Expand Down Expand Up @@ -61,7 +61,7 @@
{/if}
<tr class="bg-green-800">
<td></td>
<td>Add more equipment under your purview.</td>
<td><a href="/equipment/add">Add more equipment under your purview.</a></td>
<td></td>
</tr>
</table>
Expand Down
17 changes: 9 additions & 8 deletions src/routes/approvals/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,21 @@ export const load: PageServerLoad = async (event) => {
throw error(401, "Unauthorized")
}

if (user.role == "STUDENT" && user.student) {
throw error(401, "Unauthorized")
}

if (!user.admin && !user.student) {
redirect(302, "/register/next");
}

if (!(user.role == "ADMIN" || user.admin)) {
const isValidStudent = (user.role == "STUDENT" && user.student) ? true : false
const isValidAdmin = (user.role == "ADMIN" && user.admin) ? true : false

if (!isValidAdmin && !isValidStudent) {
throw error(401, "Unauthorized")
}

const requests = await db.request.findMany({
where: { admins: { some: { userId: event.locals.user.id } } },
where: (isValidStudent) ?
{ students: { some: { userId: event.locals.user.id } } } :
{ admins: { some: { userId: event.locals.user.id } } },
select: {
id: true,
requestStatus: true,
Expand All @@ -47,7 +48,7 @@ export const load: PageServerLoad = async (event) => {
}
})

return { requests, user }
return { requests, user, isValidAdmin, isValidStudent }
}

export const actions = {
Expand Down Expand Up @@ -102,7 +103,7 @@ export const actions = {
requestStatus: placeRequest?.requestStatus
}
})

if (!(placeRequest?.requestStatus.map(status => status.requestStatus).every(status => status === "APPROVED"))) return

if (type === "facility") {
Expand Down
96 changes: 64 additions & 32 deletions src/routes/approvals/+page.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<script lang="ts">
import { enhance } from "$app/forms";
import type { ActionData, PageData } from "./$types";
import type { PageData } from "./$types";
export let data: PageData;
const items = data.requests
Expand Down Expand Up @@ -35,7 +34,11 @@
});
</script>

For your approvals:
{#if data.isValidAdmin}
For your approvals:
{:else if data.isValidStudent}
See your approval requests here:
{/if}

<table>
<thead></thead>
Expand All @@ -49,9 +52,7 @@ For your approvals:
<ul>
{#each item.requestDates as requestDate}
<li>
{new Date(requestDate.start).toLocaleDateString()} - {new Date(
requestDate.end
).toLocaleDateString()}
{new Date(requestDate.start).toLocaleString()} - {new Date(requestDate.end).toLocaleString()}
</li>
{:else}
<i>No dates given.</i>
Expand Down Expand Up @@ -79,32 +80,63 @@ For your approvals:
{/if}
</td>
<td>
{#if item.adminsStatus.find((admin) => admin.id === data.user.admin?.id)?.status === "REJECTED"}
<p>DENIED</p>
{:else if item.adminsStatus.find((admin) => admin.id === data.user.admin?.id)?.status === "APPROVED"}
<p>APPROVED</p>
{:else}
<form method="post">
<input
type="text"
hidden
value={item.requestId}
name="requestId"
/>
<input type="text" hidden value={item.placeType} name="type" />
<input
type="text"
hidden
value={data.user.admin?.id}
name="adminId"
/>
<div>
<button type="submit" formaction="?/approve">Approve</button>
</div>
<div>
<button type="submit" formaction="?/deny">Deny</button>
</div>
</form>
{#if data.isValidAdmin}
{#if item.adminsStatus.find((admin) => admin.id === data.user.admin?.id)?.status === "REJECTED"}
<p>You said:</p>
<p>DENIED</p>
<br>
<p>Overall status: </p>
{#if item.adminsStatus.every(status => status.status === "REJECTED")}
<p>DENIED</p>
{:else if item.adminsStatus.every(status => status.status === "APPROVED")}
<p>APPROVED</p>
{:else}
<p>WAITING</p>
{/if}
{:else if item.adminsStatus.find((admin) => admin.id === data.user.admin?.id)?.status === "APPROVED"}
<p>You said:</p>
<p>APPROVED</p>
<br>
<p>Overall status: </p>
{#if item.adminsStatus.every(status => status.status === "REJECTED")}
<p>DENIED</p>
{:else if item.adminsStatus.every(status => status.status === "APPROVED")}
<p>APPROVED</p>
{:else}
<p>WAITING</p>
{/if}
{:else}
<form method="post">
<input
type="text"
hidden
value={item.requestId}
name="requestId"
/>
<input type="text" hidden value={item.placeType} name="type" />
<input
type="text"
hidden
value={data.user.admin?.id}
name="adminId"
/>
<div>
<button type="submit" formaction="?/approve">Approve</button>
</div>
<div>
<button type="submit" formaction="?/deny">Deny</button>
</div>
</form>
{/if}
{:else if data.isValidStudent}
<p>Overall status: </p>
{#if item.adminsStatus.every(status => status.status === "REJECTED")}
<p>DENIED</p>
{:else if item.adminsStatus.every(status => status.status === "APPROVED")}
<p>APPROVED</p>
{:else}
<p>WAITING</p>
{/if}
{/if}
</td>
</tr>
Expand Down
53 changes: 24 additions & 29 deletions src/routes/dashboard/+page.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { lucia } from "$lib/server/auth";
import { fail, redirect } from "@sveltejs/kit";
import { error, fail, redirect } from "@sveltejs/kit";
import type { Actions, PageServerLoad } from "./$types";
import db from "$lib/prisma";
import type { Section, Student, User } from "@prisma/client";
Expand All @@ -9,14 +9,6 @@ export const load: PageServerLoad = async (event) => {
redirect(302, "/login");
}

let returnInformation: {
userInfo: Omit<User, 'hashedPassword'>,
sectionInfo: { section: string, students: ({ user: User } & Student)[] } | null
} = {
userInfo: { ...event.locals.user },
sectionInfo: null
}

const user = await db.user.findUnique({
select: { role: true, student: true, admin: true },
where: { id: event.locals.user.id }
Expand All @@ -30,34 +22,37 @@ export const load: PageServerLoad = async (event) => {
redirect(302, "/register/next");
}

if (user?.role == "STUDENT" && user?.student) {
const section = await db.section.findUnique({
where: { id: user.student.sectionId },
include: {
students: {
include: { user: true }
}
if (!(user?.role == "STUDENT" && user?.student)) {
throw error(401, "Unauthorized")
}

const section = await db.section.findUnique({
where: { id: user.student.sectionId },
include: {
students: {
include: { user: true }
}
})
}
})

if (section?.students) {
section.students.sort((a, b) => {
// Sort by last name
const lastNameComparison = a.user.lastName.localeCompare(b.user.lastName);
if (lastNameComparison !== 0) return lastNameComparison;
if (section?.students) {
section.students.sort((a, b) => {
// Sort by last name
const lastNameComparison = a.user.lastName.localeCompare(b.user.lastName);
if (lastNameComparison !== 0) return lastNameComparison;

// If last names are equal, sort by first name
return a.user.firstName.localeCompare(b.user.firstName);
});
}
// If last names are equal, sort by first name
return a.user.firstName.localeCompare(b.user.firstName);
});
}

returnInformation.sectionInfo = {
return {
userInfo: {...event.locals.user},
sectionInfo: {
section: section?.grade + " " + section?.name,
students: section?.students ?? []
}
}

return returnInformation;
};

export const actions = {
Expand Down
2 changes: 1 addition & 1 deletion src/routes/equipment/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { redirect } from "@sveltejs/kit";
import type { PageServerLoad } from "./$types";

export const load: PageServerLoad = async () => {
redirect(302, "/search")
redirect(302, "/search?cat=equipment")
};
12 changes: 10 additions & 2 deletions src/routes/equipment/[equipmentId]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ import { redirect, error } from "@sveltejs/kit";
import type { PageServerLoad } from "./$types";
import db from "$lib/prisma";

export const load: PageServerLoad = async ({ params, locals }) => {
export const load: PageServerLoad = async ({ params, locals, cookies }) => {

const isBookSuccess = typeof cookies.get("Booking-Success") !== "undefined"
if (isBookSuccess) cookies.set("Booking-Success", "", {
httpOnly: true,
path: "/",
maxAge: 0
})

if (!locals.user) {
redirect(302, "/login");
}
Expand Down Expand Up @@ -38,5 +46,5 @@ export const load: PageServerLoad = async ({ params, locals }) => {
throw error(401, "Unauthorized")
}

return { equipment }
return { equipment, isBookSuccess }
};
Loading

0 comments on commit 18ed100

Please sign in to comment.