-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
890 additions
and
45 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,40 +1,51 @@ | ||
import db, { eq, and } from "@pennlabs/pca-backend/db" | ||
import { $section, $statusUpdate, $course } from "@pennlabs/pca-backend/db/schema/course" | ||
import { Status, type WebhookPayload } from "@/types/alert" | ||
import db, { eq, and } from '@pennlabs/pca-backend/db' | ||
import { | ||
$section, | ||
$statusUpdate, | ||
$course, | ||
} from '@pennlabs/pca-backend/db/schema/course' | ||
import { Status, type WebhookPayload } from '@/types/alert' | ||
import { ENV } from '@/core/env' | ||
|
||
export const updateCourseStatus = async (payload: WebhookPayload, requestBody: string) => { | ||
const sectionQuery = await db | ||
.select({ | ||
id: $section.id, | ||
status: $section.status | ||
}) | ||
.from($section) | ||
.where(and(eq($section.fullCode, payload.section_id_normalized), eq($course.semester, ENV.CURRENT_SEMESTER))) | ||
.innerJoin($course, eq($section.courseId, $course.id)) | ||
const previousStatus = sectionQuery[0]?.status; | ||
const sectionId = sectionQuery[0]?.id; | ||
const typedPreviousStatus = Status[previousStatus as keyof typeof Status]; | ||
if (typedPreviousStatus !== payload.previous_status) { | ||
return false; | ||
} | ||
|
||
await db | ||
.update($section) | ||
.set({ | ||
status: payload.status | ||
}) | ||
.where(eq($section.id, sectionId)) | ||
await db | ||
.insert($statusUpdate) | ||
.values({ | ||
oldStatus: payload.previous_status, | ||
newStatus: payload.status, | ||
createdAt: new Date().toISOString(), | ||
alertSent: true, | ||
requestBody: requestBody, | ||
sectionId: Number(sectionId), | ||
inAddDropPeriod: false, | ||
}); | ||
return true; | ||
} | ||
export const updateCourseStatus = async ( | ||
payload: WebhookPayload, | ||
requestBody: string, | ||
) => { | ||
const sectionQuery = await db | ||
.select({ | ||
id: $section.id, | ||
status: $section.status, | ||
}) | ||
.from($section) | ||
.where( | ||
and( | ||
eq($section.fullCode, payload.section_id_normalized), | ||
eq($course.semester, ENV.CURRENT_SEMESTER), | ||
), | ||
) | ||
.innerJoin($course, eq($section.courseId, $course.id)) | ||
const previousStatus = sectionQuery[0]?.status | ||
const sectionId = sectionQuery[0]?.id | ||
const typedPreviousStatus = Status[previousStatus as keyof typeof Status] | ||
if (typedPreviousStatus !== payload.previous_status) { | ||
return false | ||
} | ||
await db.transaction(async tx => { | ||
await tx | ||
.update($section) | ||
.set({ | ||
status: payload.status, | ||
}) | ||
.where(eq($section.id, sectionId)) | ||
await tx.insert($statusUpdate).values({ | ||
oldStatus: payload.previous_status, | ||
newStatus: payload.status, | ||
createdAt: new Date().toISOString(), | ||
alertSent: true, | ||
requestBody: requestBody, | ||
sectionId: Number(sectionId), | ||
inAddDropPeriod: false, | ||
}) | ||
}) | ||
return true | ||
} |
Oops, something went wrong.