diff --git a/app/actions.tsx b/app/actions.tsx index aa2913f..7c4b472 100644 --- a/app/actions.tsx +++ b/app/actions.tsx @@ -127,6 +127,10 @@ export async function submitData(prevState: { showProfile: formData.get("showProfile"), }; + //@ts-ignore + const itsRecord: any[] = await queryDb(`SELECT GRAD_YEAR, DORM, DORM_ROOM \ + FROM student_data WHERE USER_ID='${id}' `); + await prisma.studentOverlay.upsert({ where: { uid: id, @@ -146,6 +150,9 @@ export async function submitData(prevState: { lastName: rawFormData.lastName, pronouns: rawFormData.pronouns, photoPath: photo_path, + dorm: itsRecord[0]["DORM"], + dormRoom: itsRecord[0]["DORM_ROOM"], + gradYear: itsRecord[0]["GRAD_YEAR"], showProfile: rawFormData.showProfile === "on" ? true : false, showDorm: rawFormData.showDorm === "on" ? true : false, showPhoto: rawFormData.showPicture === "on" ? true : false, diff --git a/app/settings/page.tsx b/app/settings/page.tsx index 029ef2c..434629e 100644 --- a/app/settings/page.tsx +++ b/app/settings/page.tsx @@ -31,12 +31,12 @@ export default async function Settings() { pronouns: user_data_prisma.pronouns, id: user_data_prisma.uid, photo_path: user_data_prisma.photoPath, + year: user_data_prisma.gradYear, + dorm: user_data_prisma.dorm, + room: user_data_prisma.dormRoom, showDorm: user_data_prisma.showDorm, showPicture: user_data_prisma.showPhoto, showProfile: user_data_prisma.showProfile, - year: cygnet_user_data.year, - dorm: cygnet_user_data.dorm, - room: cygnet_user_data.room, }; } else { user_data = { diff --git a/components/pagebody.tsx b/components/pagebody.tsx index 84f8de8..428f4be 100644 --- a/components/pagebody.tsx +++ b/components/pagebody.tsx @@ -4,170 +4,262 @@ import fs from "fs"; import { Suspense } from "react"; import CardBody from "./cardbody"; import prisma from "@/lib/prisma"; +import { StudentOverlay } from "@prisma/client"; //import TextModerate from 'text-moderate'; export interface DbInfo { - FIRST_NAME: string; - LAST_NAME: string; - GRAD_YEAR: string; - DORM: string; - DORM_ROOM: string; - USER_ID: string; + FIRST_NAME: string; + LAST_NAME: string; + GRAD_YEAR: string; + DORM: string; + DORM_ROOM: string; + USER_ID: string; } export interface StudentInfo { - first: string; - last: string; - year: string; - dorm: string; - room: string; - id: string; - photo_path: string; - pronouns: string; - showDorm: boolean; - showProfile: boolean; - showPicture: boolean; + first: string; + last: string; + year: string; + dorm: string; + room: string; + id: string; + photo_path: string; + pronouns: string; + showDorm: boolean; + showProfile: boolean; + showPicture: boolean; +} + +async function getPhoto(uid: string) { + let path = "/placeholder.jpg"; + + const modPath = `/mod/${uid}_m.jpg`; + const genModPath = `${__dirname}/../../../photos${modPath}`; + + const staticPath = `/${uid}.jpg`; + const genPath = `${__dirname}/../../../photos${staticPath}`; + + if (fs.existsSync(genModPath)) { + path = modPath; + } else if (fs.existsSync(genPath)) { + //path = fullPath + path = staticPath; + } else { + const imgBuffer = await queryDb( + `SELECT PHOTO FROM student_data WHERE USER_ID='${uid}' ` + ); + + // @ts-ignore + fs.writeFileSync(genPath, imgBuffer[0]["PHOTO"]); + // path = fullPath + path = staticPath; + } + + return path; } async function filterData(searchParams: { query?: string; filters?: string }) { - const searchQuery = (searchParams?.query || "").split(" ").filter((value: string) => { - if(value.match(/^\s*$/g)) - return false; - return true; - }); - - if(!searchQuery.length) { - return []; - } - - const filters = (searchParams?.filters || "").split(","); - - let filterString: string = searchQuery[0] - ? searchQuery - .map((filter: string) => { - return `FIRST_NAME LIKE '%${filter}%' OR LAST_NAME LIKE \ + const searchQuery = (searchParams?.query || "").split(" ").filter((value: string) => { + if (value.match(/^\s*$/g)) + return false; + return true; + }); + + if (!searchQuery.length) { + return []; + } + + let prismaQuery: any[] = []; + + const filters = (searchParams?.filters || "").split(","); + + let filterString: string = searchQuery[0] + ? searchQuery + .map((filter: string) => { + prismaQuery.push({ + OR: [{ + firstName: { + contains: filter, + } + }, + { + lastName: { + contains: filter, + } + }, + { + gradYear: { + contains: filter, + } + }, + { + dorm: { + contains: filter, + } + }, + { + dormRoom: { + contains: filter, + } + }, + { + uid: { + contains: filter, + } + }] + }); + return `FIRST_NAME LIKE '%${filter}%' OR LAST_NAME LIKE \ '%${filter}%' OR GRAD_YEAR LIKE '%${filter}%' OR DORM LIKE \ '%${filter}%' OR DORM_ROOM LIKE '%${filter}%' OR USER_ID LIKE \ '%${filter}%'`; - }) - .join(") AND (") - : ""; - - if (filters[0]) { - filterString += searchQuery[0] ? ") AND (" : ""; - filterString += filters - .map((filter: string) => { - return `GRAD_YEAR LIKE '%${filter}%' OR DORM LIKE \ + }) + .join(") AND (") + : ""; + + if (filters[0]) { + filterString += searchQuery[0] ? ") AND (" : ""; + filterString += filters + .map((filter: string) => { + prismaQuery.push({ + OR: [{ + firstName: { + contains: filter, + } + }, + { + lastName: { + contains: filter, + } + }, + { + gradYear: { + contains: filter, + } + }, + { + dorm: { + contains: filter, + } + }, + { + dormRoom: { + contains: filter, + } + }, + { + uid: { + contains: filter, + } + }] + }); + return `GRAD_YEAR LIKE '%${filter}%' OR DORM LIKE \ '%${filter}%' OR DORM_ROOM LIKE '%${filter}%'`; - }) - .join(") AND ("); - } + }) + .join(") AND ("); + } - let query = `SELECT FIRST_NAME, LAST_NAME, GRAD_YEAR, DORM, DORM_ROOM, \ + const query = `SELECT FIRST_NAME, LAST_NAME, GRAD_YEAR, DORM, DORM_ROOM, \ USER_ID FROM student_data WHERE (${filterString}) `; - // @ts-ignore - let raw: any[] = await queryDb(query); - let data: StudentInfo[] = []; + // @ts-ignore + const raw: any[] = await queryDb(query); + const data: StudentInfo[] = []; - await Promise.all( - raw.map(async (student) => { - const user = await prisma.studentOverlay.findUnique({ + // reuse query instead of reprocessing + const overlay: StudentOverlay[] = await prisma.studentOverlay.findMany({ where: { - uid: student["USER_ID"], - } - }) - - if (user && !user.showProfile) { - return; - } - - let path = "/placeholder.jpg"; - - if (!user || user.showPhoto) { - const modPath = `/mod/${student["USER_ID"]}_m.jpg`; - const genModPath = `${__dirname}/../../../photos${modPath}`; - - const staticPath = `/${student["USER_ID"]}.jpg`; - const genPath = `${__dirname}/../../../photos${staticPath}`; - - if (fs.existsSync(genModPath)) { - path = modPath; - } else if (fs.existsSync(genPath)) { - //path = fullPath - path = staticPath; - } else { - const imgBuffer = await queryDb( - `SELECT PHOTO FROM student_data WHERE USER_ID='${student["USER_ID"]}' ` - ); - - // @ts-ignore - fs.writeFileSync(genPath, imgBuffer[0]["PHOTO"]); - // path = fullPath - path = staticPath; + AND: prismaQuery } - } - - let newStudent: StudentInfo = { - first: user?.firstName ? user.firstName : student["FIRST_NAME"], - last: user?.lastName ? user.lastName : student["LAST_NAME"], - year: student["GRAD_YEAR"], - dorm: student["DORM"], - room: student["DORM_ROOM"], - id: student["USER_ID"], - photo_path: path, - pronouns: user?.pronouns ? user.pronouns : "", - showDorm: true, - showPicture: true, - showProfile: true, - }; - - if (user && !user.showDorm) { - newStudent.showDorm = false; - } - - data.push(newStudent); }) - ); - return data; + const records = Promise.allSettled([ + overlay.map(async (student) => { + let newStudent: StudentInfo = { + first: student.firstName, + last: student.lastName, + year: student.gradYear, + dorm: student.dorm, + room: student.dormRoom, + id: student.uid, + photo_path: student.photoPath, + pronouns: student.pronouns, + showDorm: student.showDorm, + showPicture: student.showPhoto, + showProfile: student.showProfile, + }; + + data.push(newStudent); + }), + raw.map(async (student) => { + + if (overlay.find((record) => { + return record.uid === student["USER_ID"] + })) { + return; + } + + let newStudent: StudentInfo = { + first: student["FIRST_NAME"], + last: student["LAST_NAME"], + year: student["GRAD_YEAR"], + dorm: student["DORM"], + room: student["DORM_ROOM"], + id: student["USER_ID"], + photo_path: await getPhoto(student["USER_ID"]), + pronouns: "", + showDorm: true, + showPicture: true, + showProfile: true, + }; + + data.push(newStudent); + })] + ); + + await records; + + return data.sort((a: StudentInfo, b: StudentInfo) => { + if (a.first < b.first) + return -1; + return 1; + }); } -export default function PageBody({ - searchParams, +export default async function PageBody({ + searchParams, }: { - searchParams?: { - query?: string; - filters?: string; - }; + searchParams?: { + query?: string; + filters?: string; + }; }) { - if (searchParams?.query || searchParams?.filters) { - const filteredData = filterData(searchParams); + if (searchParams?.query || searchParams?.filters) { + const filteredData = filterData(searchParams); - return ( - }> - - - ); - } else { - return ( -
-
-

- Welcome to
- the CYGNET by SCCS -

-
-
-

- Enter a query or add a filter to begin -

+ return ( + }> + + + ); + } else { + return ( +
+
+

+ Welcome to
+ the CYGNET by SCCS +

+
+
+

+ Enter a query or add a filter to begin +

+
-
- ) - } + ) + } } diff --git a/prisma/migrations/20240429004435_init/migration.sql b/prisma/migrations/20240429004435_init/migration.sql new file mode 100644 index 0000000..8f52c32 --- /dev/null +++ b/prisma/migrations/20240429004435_init/migration.sql @@ -0,0 +1,12 @@ +/* + Warnings: + + - Added the required column `dorm` to the `StudentOverlay` table without a default value. This is not possible if the table is not empty. + - Added the required column `dormRoom` to the `StudentOverlay` table without a default value. This is not possible if the table is not empty. + - Added the required column `gradYear` to the `StudentOverlay` table without a default value. This is not possible if the table is not empty. + +*/ +-- AlterTable +ALTER TABLE `StudentOverlay` ADD COLUMN `dorm` VARCHAR(191) NOT NULL, + ADD COLUMN `dormRoom` VARCHAR(191) NOT NULL, + ADD COLUMN `gradYear` VARCHAR(191) NOT NULL; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index f630135..badba87 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -13,6 +13,9 @@ model StudentOverlay { firstName String lastName String pronouns String + dorm String + dormRoom String + gradYear String photoPath String showProfile Boolean @default(true) showDorm Boolean @default(true) diff --git a/scripts/migrate_from_old.js b/scripts/migrate_from_old.js index d8ea535..a77c2e7 100644 --- a/scripts/migrate_from_old.js +++ b/scripts/migrate_from_old.js @@ -89,6 +89,9 @@ async function copyRecord(uid) { lastName: record['LAST_NAME'], pronouns: "", photoPath: path, + dorm: record["DORM"], + dormRoom: record["DORM_ROOM"], + gradYear: record["GRAD_YEAR"], showProfile: user_settings[0]["EXCLUDED_USERS"].includes(uid) ? false : true, showDorm: user_settings[0]["ROOM_HIDDEN"].includes(uid) ? false : true, showPhoto: user_settings[0]["PHOTO_HIDDEN"].includes(uid) ? false : true,