Skip to content

Commit

Permalink
fix(webhook): use transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
esinx committed Nov 19, 2024
1 parent c63295c commit 204b3cc
Show file tree
Hide file tree
Showing 3 changed files with 890 additions and 45 deletions.
5 changes: 4 additions & 1 deletion services/alert/webhook/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@
"main": "index.js",
"license": "MIT",
"type": "module",
"prettier": "@esinx/prettier-config",
"scripts": {
"build:lambda": "tsx ./scripts/build.lambda"
},
"devDependencies": {
"@esinx/eslint-config": "^2.0.1",
"@esinx/prettier-config": "^1.0.0-3",
"@types/aws-lambda": "^8.10.145",
"@types/node": "^22.5.1",
"@types/pg": "^8.11.10",
"aws-lambda": "^1.0.7",
"esbuild": "^0.24.0",
"tsconfig-paths": "^4.2.0",
"tsx": "^4.19.1",
"typescript": "^5.5.4",
"typescript": "^5.6.3",
"vite-tsconfig-paths": "^5.1.0",
"vitest": "^2.1.4"
},
Expand Down
87 changes: 49 additions & 38 deletions services/alert/webhook/src/core/save.ts
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
}
Loading

0 comments on commit 204b3cc

Please sign in to comment.