-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from unovil/dashboard-addition
Addition of a basic dashboard (admindashboard and dashboard)
- Loading branch information
Showing
17 changed files
with
2,252 additions
and
827 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
41 changes: 41 additions & 0 deletions
41
prisma/migrations/20240318072654_added_referential_on_delete/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
16 changes: 16 additions & 0 deletions
16
prisma/migrations/20240319070312_added_description_image_facility_equipment/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
14 changes: 14 additions & 0 deletions
14
prisma/migrations/20240319071638_added_admin_ownership_equipment/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
prisma/migrations/20240411131336_changed_image/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Oops, something went wrong.