-
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.
Merge pull request #689 from pennlabs/alert-2.0-status-update
Alert 2.0 Proper Status Update Handling
- Loading branch information
Showing
8 changed files
with
939 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,9 +23,9 @@ dist-ssr | |
*.sln | ||
*.sw? | ||
|
||
|
||
*.env | ||
|
||
.terraform | ||
*.tfstate | ||
*.tfstate.backup | ||
*.tfstate.backup | ||
>>>>>>> alert-2.0 |
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,3 +1,4 @@ | ||
export * from './auth.user.model' | ||
export * from './course.model' | ||
export * from './section.model' | ||
export * from "./statusupdate.model" |
33 changes: 33 additions & 0 deletions
33
services/alert/backend/src/core/db/schema/course/statusupdate.model.ts
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,33 @@ | ||
import { | ||
bigint, | ||
bigserial, | ||
boolean, | ||
doublePrecision, | ||
pgTable, | ||
text, | ||
timestamp, | ||
varchar, | ||
} from 'drizzle-orm/pg-core' | ||
|
||
import { $section } from '@/core/db/schema/course/section.model' | ||
|
||
/** | ||
* This model was autogenerated / edited using drizzle-kit. | ||
* The source of truth is the model defined in the Django backend, | ||
* so be sure to sync changes with the backend. | ||
* DO NOT EXPORT/SYNC THIS MODEL. | ||
*/ | ||
export const $statusUpdate = pgTable("courses_statusupdate", { | ||
id: bigserial({ mode: "bigint" }).primaryKey().notNull(), | ||
oldStatus: varchar("old_status", { length: 16 }).notNull(), | ||
newStatus: varchar("new_status", { length: 16 }).notNull(), | ||
createdAt: timestamp("created_at", { withTimezone: true, mode: 'string' }).notNull(), | ||
alertSent: boolean("alert_sent").notNull(), | ||
requestBody: text("request_body").notNull(), | ||
// You can use { mode: "bigint" } if numbers are exceeding js number limitations | ||
sectionId: bigint("section_id", { mode: "number" }) | ||
.notNull() | ||
.references(() => $section.id), | ||
inAddDropPeriod: boolean("in_add_drop_period").notNull(), | ||
percentThroughAddDropPeriod: doublePrecision("percent_through_add_drop_period"), | ||
}) |
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,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 { 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.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 | ||
} |
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
Oops, something went wrong.