Skip to content

Commit

Permalink
pagebody: update ITS-controlled attrs
Browse files Browse the repository at this point in the history
  • Loading branch information
makinbacon21 committed Jul 26, 2024
1 parent deb30ac commit 7bc8e82
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
39 changes: 33 additions & 6 deletions components/pagebody.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,26 @@ async function getPhoto(uid: string) {

async function filterData(searchParams: { query?: string; filters?: string }) {
const searchQuery = (searchParams?.query || "").split(" ").filter((value: string) => {
// account for just spaces
if (value.match(/^\s*$/g))
return false;
return true;
});

// account for no terms
if (!searchQuery.length) {
return [];
}

// replace whitespace lumps with space for parsing0
searchQuery[0] = searchQuery[0].replaceAll(/\W/g, ' ');


// store prisma queries to execute
let prismaQuery: any[] = [];

const filters = (searchParams?.filters || "").split(",");

// query based on search terms
let filterString: string = searchQuery[0]
? searchQuery
.map((filter: string) => {
Expand Down Expand Up @@ -120,6 +124,7 @@ async function filterData(searchParams: { query?: string; filters?: string }) {
.join(") AND (")
: "";

// query based on filters
if (filters[0]) {
filterString += searchQuery[0] ? ") AND (" : "";
filterString += filters
Expand Down Expand Up @@ -162,15 +167,15 @@ async function filterData(searchParams: { query?: string; filters?: string }) {
.join(") AND (");
}

// build full sql query
const query = `SELECT FIRST_NAME, LAST_NAME, GRAD_YEAR, DORM, DORM_ROOM, \
USER_ID FROM student_data WHERE (${filterString})`;


// @ts-ignore
const raw: any[] = await queryDb(query);
const data: StudentInfo[] = [];

// reuse query instead of reprocessing
// execute query
const overlay: StudentOverlay[] = await prisma.studentOverlay.findMany({
where: {
AND: prismaQuery
Expand Down Expand Up @@ -198,12 +203,33 @@ async function filterData(searchParams: { query?: string; filters?: string }) {
}),
raw.map(async (student) => {

if (overlay.find((record) => {
// check if in overlay with find => function
const overlayRecord = overlay.find((record) => {
return record.uid === student["USER_ID"]
})) {
return;
})

if(overlayRecord) {
// check if properties ITS should control have changed
if(overlayRecord.dorm !== student["DORM"] ||
overlayRecord.dormRoom !== student["DORM_ROOM"] ||
overlayRecord.gradYear !== student["GRAD_YEAR"]
) {
overlayRecord.dorm = student["DORM"]
overlayRecord.dormRoom = student["DORM_ROOM"]
overlayRecord.gradYear = student["GRAD_YEAR"]

prisma.studentOverlay.update({
where: {
uid: overlayRecord.uid
},
data: overlayRecord
})

return // use overlay listing
}
}

// construct StudentInfo dict from ITS db props
let newStudent: StudentInfo = {
first: student["FIRST_NAME"],
last: student["LAST_NAME"],
Expand All @@ -224,6 +250,7 @@ async function filterData(searchParams: { query?: string; filters?: string }) {

await records;

// sort merged records
return data.sort((a: StudentInfo, b: StudentInfo) => {
if (a.first == b.first)
if (a.last < b.last)
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "cygnet",
"version": "2.1.1",
"version": "2.1.2",
"private": true,
"scripts": {
"dev": "npx next dev",
Expand Down

0 comments on commit 7bc8e82

Please sign in to comment.