Skip to content

Commit

Permalink
Add untested version of fill debate bill IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
limdingwen committed Aug 1, 2024
1 parent 4df4bd7 commit ad05966
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
11 changes: 11 additions & 0 deletions scripts/deploy-and-run/fill-debate-bill-ids.sh
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" "{}"
5 changes: 5 additions & 0 deletions supabase/functions/fill-debate-bill-ids/index.ts
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 supabase/functions/lib/shared-functions/fill-debate-bill-ids.ts
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.`,
});
}

0 comments on commit ad05966

Please sign in to comment.