-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add untested version of fill debate bill IDs
- Loading branch information
1 parent
4df4bd7
commit ad05966
Showing
3 changed files
with
94 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
set -e | ||
|
||
source ../.env | ||
source ../lib/functions/deploy.sh | ||
source ../lib/functions/run.sh | ||
|
||
FUNCTION_NAME="fill-debate-bill-ids" | ||
|
||
deploy "$SUPABASE_PROJECT_ID" "$FUNCTION_NAME" | ||
run "$SUPABASE_PROJECT_ID" "$SUPABASE_SERVICE_ROLE_KEY" "$FUNCTION_NAME" "{}" |
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,5 @@ | ||
import "https://esm.sh/@supabase/functions-js/src/edge-runtime.d.ts"; | ||
import proxyToResponseWrapper from "../lib/utils/proxy-to-response-wrapper.ts"; | ||
import fillDebateBillIds from "../lib/shared-functions/fill-debate-bill-ids.ts"; | ||
|
||
Deno.serve(proxyToResponseWrapper(fillDebateBillIds)); |
78 changes: 78 additions & 0 deletions
78
supabase/functions/lib/shared-functions/fill-debate-bill-ids.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,78 @@ | ||
import buildResponseProxy from "../utils/build-response-proxy.ts"; | ||
import { createSupabase } from "../utils/create-supabase.ts"; | ||
import { isAdmin } from "../utils/check-admin.ts"; | ||
import { SupabaseClient } from "https://esm.sh/v135/@supabase/[email protected]/dist/module/index.d.ts"; | ||
|
||
async function getDebatesWithNoBillLinked(supabase: SupabaseClient) { | ||
const { data, error } = await supabase | ||
.from("debate") | ||
.select("id, title, bill_id") | ||
.is("bill_id", null); | ||
if (error) throw error; | ||
return data; | ||
} | ||
|
||
async function fetchBillIdFromBillName( | ||
supabase: SupabaseClient, | ||
billName: string, | ||
) { | ||
const { data, error } = await supabase | ||
.from("bill") | ||
.select("id") | ||
.eq("name", billName) | ||
.limit(1) | ||
.maybeSingle(); | ||
if (error) throw error; | ||
|
||
const result = data ? data.id : null; | ||
console.log(`Found bill ID for ${billName}: ${result}`); | ||
|
||
return result; | ||
} | ||
|
||
async function updateDebateWithBillId( | ||
supabase: SupabaseClient, | ||
rowId: number, | ||
billId: number, | ||
) { | ||
const { error } = await supabase | ||
.from("debate") | ||
.update({ bill_id: billId }) | ||
.eq("id", rowId); | ||
if (error) throw error; | ||
} | ||
|
||
// Goes through all debate speeches and fills in the bill_id field via the title field | ||
// This is to enable dynamic aliasing without needing to re-scrape the entire database | ||
export default async function fillDebateBillIds(req: Request) { | ||
const supabase = createSupabase(); | ||
|
||
if (!isAdmin(req)) { | ||
return buildResponseProxy({ message: "Unauthorised." }, 401); | ||
} | ||
|
||
let rowCount = 0; | ||
let potentialRowCount = 0; | ||
|
||
const debatesWithNoBillLinked = await getDebatesWithNoBillLinked(supabase); | ||
|
||
for (const debateWithNoBillLinked of debatesWithNoBillLinked) { | ||
const billId = await fetchBillIdFromBillName( | ||
supabase, | ||
debateWithNoBillLinked.title, | ||
); | ||
|
||
// We assume that we are only working with billId = null rows, so if billId is still null, | ||
// don't bother updating it. | ||
if (billId) { | ||
await updateDebateWithBillId(supabase, debateWithNoBillLinked.id, billId); | ||
rowCount++; | ||
} | ||
|
||
potentialRowCount++; | ||
} | ||
|
||
return buildResponseProxy({ | ||
message: `Filled bill IDs for ${rowCount} out of ${potentialRowCount} debates.`, | ||
}); | ||
} |