Skip to content

Commit

Permalink
Merge pull request #10 from unovil/dashboard-addition
Browse files Browse the repository at this point in the history
Addition of a basic dashboard (admindashboard and dashboard)
  • Loading branch information
unovil authored Apr 12, 2024
2 parents e9e8faf + 8418677 commit f4620a0
Show file tree
Hide file tree
Showing 17 changed files with 2,252 additions and 827 deletions.
2,042 changes: 1,255 additions & 787 deletions package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
-- DropForeignKey
ALTER TABLE `admin` DROP FOREIGN KEY `Admin_userId_fkey`;

-- DropForeignKey
ALTER TABLE `equipment` DROP FOREIGN KEY `Equipment_schoolId_fkey`;

-- DropForeignKey
ALTER TABLE `facility` DROP FOREIGN KEY `Facility_schoolId_fkey`;

-- DropForeignKey
ALTER TABLE `section` DROP FOREIGN KEY `Section_schoolId_fkey`;

-- DropForeignKey
ALTER TABLE `student` DROP FOREIGN KEY `Student_sectionId_fkey`;

-- DropForeignKey
ALTER TABLE `student` DROP FOREIGN KEY `Student_userId_fkey`;

-- DropForeignKey
ALTER TABLE `user` DROP FOREIGN KEY `User_schoolId_fkey`;

-- AddForeignKey
ALTER TABLE `User` ADD CONSTRAINT `User_schoolId_fkey` FOREIGN KEY (`schoolId`) REFERENCES `School`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Section` ADD CONSTRAINT `Section_schoolId_fkey` FOREIGN KEY (`schoolId`) REFERENCES `School`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Admin` ADD CONSTRAINT `Admin_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Student` ADD CONSTRAINT `Student_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `User`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Student` ADD CONSTRAINT `Student_sectionId_fkey` FOREIGN KEY (`sectionId`) REFERENCES `Section`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Facility` ADD CONSTRAINT `Facility_schoolId_fkey` FOREIGN KEY (`schoolId`) REFERENCES `School`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Equipment` ADD CONSTRAINT `Equipment_schoolId_fkey` FOREIGN KEY (`schoolId`) REFERENCES `School`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
Warnings:
- Added the required column `description` to the `Equipment` table without a default value. This is not possible if the table is not empty.
- Added the required column `image` to the `Equipment` table without a default value. This is not possible if the table is not empty.
- Added the required column `description` to the `Facility` table without a default value. This is not possible if the table is not empty.
- Added the required column `image` to the `Facility` table without a default value. This is not possible if the table is not empty.
*/
-- AlterTable
ALTER TABLE `equipment` ADD COLUMN `description` TINYTEXT NOT NULL,
ADD COLUMN `image` BLOB NOT NULL;

-- AlterTable
ALTER TABLE `facility` ADD COLUMN `description` TEXT NOT NULL,
ADD COLUMN `image` BLOB NOT NULL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
-- CreateTable
CREATE TABLE `_EquipmentToAdmin` (
`A` INTEGER NOT NULL,
`B` INTEGER NOT NULL,

UNIQUE INDEX `_EquipmentToAdmin_AB_unique`(`A`, `B`),
INDEX `_EquipmentToAdmin_B_index`(`B`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `_EquipmentToAdmin` ADD CONSTRAINT `_EquipmentToAdmin_A_fkey` FOREIGN KEY (`A`) REFERENCES `Admin`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `_EquipmentToAdmin` ADD CONSTRAINT `_EquipmentToAdmin_B_fkey` FOREIGN KEY (`B`) REFERENCES `Equipment`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
12 changes: 12 additions & 0 deletions prisma/migrations/20240411131336_changed_image/migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
Warnings:
- You are about to alter the column `image` on the `equipment` table. The data in that column could be lost. The data in that column will be cast from `Blob` to `VarChar(191)`.
- You are about to alter the column `image` on the `facility` table. The data in that column could be lost. The data in that column will be cast from `Blob` to `VarChar(191)`.
*/
-- AlterTable
ALTER TABLE `equipment` MODIFY `image` VARCHAR(191) NOT NULL;

-- AlterTable
ALTER TABLE `facility` MODIFY `image` VARCHAR(191) NOT NULL;
50 changes: 28 additions & 22 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ model User {
email String @unique
firstName String
lastName String
school School? @relation(fields: [schoolId], references: [id])
school School? @relation(fields: [schoolId], references: [id], onDelete: Cascade)
role UserRole @default(STUDENT)
admin Admin? @relation(name: "AdminToUser")
student Student? @relation(name: "StudentToUser")
Expand Down Expand Up @@ -50,16 +50,17 @@ model Section {
grade Int
name String @unique
students Student[] @relation(name: "SectionToStudent")
school School @relation(name: "SchoolToSection", fields: [schoolId], references: [id])
school School @relation(name: "SchoolToSection", fields: [schoolId], references: [id], onDelete: Cascade)
schoolId Int
}

model Admin {
id Int @id @default(autoincrement())
user User @relation(name: "AdminToUser", fields: [userId], references: [id])
userId String @unique
facilities Facility[] @relation(name: "FacilityToAdmin")
requests Request[] @relation(name: "AdminToRequest")
id Int @id @default(autoincrement())
user User @relation(name: "AdminToUser", fields: [userId], references: [id], onDelete: Cascade)
userId String @unique
facilities Facility[] @relation(name: "FacilityToAdmin")
equipments Equipment[] @relation(name: "EquipmentToAdmin")
requests Request[] @relation(name: "AdminToRequest")
/// [DepartmentsType]
departments Json
Expand All @@ -68,33 +69,38 @@ model Admin {
model Student {
id Int @id @default(autoincrement())
lrn String @unique @db.Char(12)
user User @relation(name: "StudentToUser", fields: [userId], references: [id])
section Section @relation(name: "SectionToStudent", fields: [sectionId], references: [id])
user User @relation(name: "StudentToUser", fields: [userId], references: [id], onDelete: Cascade)
section Section @relation(name: "SectionToStudent", fields: [sectionId], references: [id], onDelete: Cascade)
sectionId Int
userId String @unique
requests Request[] @relation(name: "StudentToRequest")
}

model Facility {
id Int @id @default(autoincrement())
schoolId Int
school School @relation(name: "FacilityToSchool", fields: [schoolId], references: [id])
admins Admin[] @relation(name: "FacilityToAdmin")
name String
department Department @default(MISC)
requests Request[] @relation(name: "FacilityToRequest")
id Int @id @default(autoincrement())
schoolId Int
school School @relation(name: "FacilityToSchool", fields: [schoolId], references: [id], onDelete: Cascade)
admins Admin[] @relation(name: "FacilityToAdmin")
name String
description String @db.Text
image String
department Department @default(MISC)
requests Request[] @relation(name: "FacilityToRequest")
/// [DatesType]
blockedDates Json
}

model Equipment {
id Int @id @default(autoincrement())
schoolId Int
school School @relation(name: "EquipmentToSchool", fields: [schoolId], references: [id])
name String
department Department @default(MISC)
requests Request[] @relation(name: "EquipmentToRequest")
id Int @id @default(autoincrement())
schoolId Int
school School @relation(name: "EquipmentToSchool", fields: [schoolId], references: [id], onDelete: Cascade)
name String
admins Admin[] @relation(name: "EquipmentToAdmin")
description String @db.TinyText
image String
department Department @default(MISC)
requests Request[] @relation(name: "EquipmentToRequest")
/// [DatesType]
blockedDates Json
Expand Down
49 changes: 49 additions & 0 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { PrismaClient, UserRole } from "@prisma/client";
import { faker } from "@faker-js/faker";
import { Argon2id } from "oslo/password";
import type { Departments } from "../src/app"

const db = new PrismaClient()
const repeats = 5;
const role: "ADMIN" | "STUDENT" | null = UserRole.ADMIN

function getWeightedUserRole() {
// Generate a random number between 0 and 1
const randomNumber = Math.random();

// Return 'ADMIN' 10% of the time and 'STUDENT' 90% of the time
return randomNumber < 0.1 ? UserRole.ADMIN : UserRole.STUDENT;
}

for (let i = 0; i < repeats; i++) {
const userResponse = await db.user.create({
data: {
firstName: faker.person.firstName(),
lastName: faker.person.lastName(),
role: role ? role : getWeightedUserRole(),
email: faker.internet.email(),
hashedPassword: await new Argon2id().hash("baba black sheep"),
schoolId: 1
},
include: {
school: true
}
})

if (userResponse.role == "STUDENT") {
const studentResponse = await db.student.create({
data: {
lrn: faker.string.numeric(12),
userId: userResponse.id,
sectionId: faker.number.int({ min: 1, max: 8 })
}
})
} else if (userResponse.role == "ADMIN") {
const adminResponse = await db.admin.create({
data: {
userId: userResponse.id,
departments: {} as Departments
}
})
}
}
3 changes: 1 addition & 2 deletions src/lib/prisma.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Prisma, PrismaClient, UserRole } from '@prisma/client'
import { Department, type Admin, type Facility } from '@prisma/client'
import { PrismaClient } from '@prisma/client'

// expose a singleton
const db = new PrismaClient()
Expand Down
76 changes: 76 additions & 0 deletions src/routes/admindashboard/+page.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import { redirect } from "@sveltejs/kit";
import type { Equipment, Facility, Student, User } from "@prisma/client";
import db from "$lib/prisma";
import type { PageServerLoad } from "./$types";

export const load: PageServerLoad = async (event) => {
if (!event.locals.user) {
redirect(302, "/login");
}

let returnInformation: {
userInfo: Omit<User, 'hashedPassword'>,
facilities: ({ admins: { user: Pick<User, 'id' | 'firstName' | 'lastName'> }[]; } & Omit<Facility, 'image'> & { image: string })[] | null,
equipments: ({ admins: { user: Pick<User, 'id' | 'firstName' | 'lastName'> }[]; } & Omit<Equipment, 'image'> & { image: string })[] | null
} = {
userInfo: { ...event.locals.user },
facilities: null,
equipments: null
}

const user = await db.user.findUnique({
select: { role: true, student: true, admin: true },
where: { id: event.locals.user.id }
})

if (user?.role == "STUDENT" && user?.student) {
redirect(302, "/dashboard");
}

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

if (user?.role == "ADMIN" && user?.admin) {
const response = await db.admin.findUnique({
select: {
facilities: {
include: {
admins: {
select: {
user: { select: { firstName: true, lastName: true, id: true } }
}
}
}
},
equipments: {
include: {
admins: {
select: {
user: { select: { firstName: true, lastName: true, id: true } }
}
}
}
}
},
where: { userId: event.locals.user.id }
})

returnInformation.facilities = (response?.facilities.map(facility => {
const { image, ...otherProps } = facility;
return {
...otherProps,
image,
};
})) ?? null
returnInformation.equipments = (response?.equipments.map(equipment => {
const { image, ...otherProps } = equipment;
return {
...otherProps,
image,
};
})) ?? null
}

return returnInformation;
};
85 changes: 85 additions & 0 deletions src/routes/admindashboard/+page.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<script lang="ts">
import type { PageData } from "./$types";
export let data: PageData;
</script>

<main>
<p>Welcome, <strong>{data.userInfo.firstName}</strong>!</p>
<br />
<p>List of facilities you have:</p>
<table>
<tr>
<th>Facility</th>
<th>Owned by</th>
<th>Department</th>
<th>Image</th>
</tr>
{#if data?.facilities && data?.facilities.length > 0}
{#each data?.facilities as facility (facility.id)}
<tr>
<td>{facility.name}</td>
<td
>{facility.admins
.map((admin) => `${admin.user.firstName} ${admin.user.lastName}`)
.join(", ")}</td
>
<td>{facility.department}</td>
<td
><img
src={facility.image}
class="h-32"
alt={facility.name}
/></td
>
</tr>
{/each}
{/if}
<tr class="bg-green-800">
<td></td>
<td>Add more facilities under your purview.</td>
<td></td>
</tr>
</table>
<br />
<p>List of equipment you have:</p>
<table>
<tr>
<th>Equipment</th>
<th>Owned by</th>
<th>Department</th>
<th>Image</th>
</tr>
{#if data?.equipments && data?.equipments.length > 0}
{#each data?.equipments as equipment (equipment.id)}
<tr>
<td>{equipment.name}</td>
<td
>{equipment.admins
.map((admin) => `${admin.user.firstName} ${admin.user.lastName}`)
.join(", ")}</td
>
<td>{equipment.department}</td>
<td
><img
src={equipment.image}
class="h-32"
alt={equipment.name}
/></td
>
</tr>
{/each}
{/if}
<tr class="bg-green-800">
<td></td>
<td>Add more equipment under your purview.</td>
<td></td>
</tr>
</table>
</main>
<br />
<aside>
<form method="post">
<button type="submit" formaction="/dashboard?/logout">LOGOUT</button>
</form>
</aside>
Loading

0 comments on commit f4620a0

Please sign in to comment.