From 6c0d29e6c72dc6f128abed5e32168799b49f14c4 Mon Sep 17 00:00:00 2001 From: Lim Ding Wen Date: Tue, 6 Aug 2024 19:22:26 +0800 Subject: [PATCH] Fix new bills not being added --- .../scrape-bills-introduced.ts | 23 ++++++++++++++----- 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/supabase/functions/lib/shared-functions/scrape-bills-introduced.ts b/supabase/functions/lib/shared-functions/scrape-bills-introduced.ts index ff17198..3f4ca4c 100644 --- a/supabase/functions/lib/shared-functions/scrape-bills-introduced.ts +++ b/supabase/functions/lib/shared-functions/scrape-bills-introduced.ts @@ -5,6 +5,7 @@ import { Element, } from "https://deno.land/x/deno_dom@v0.1.46/deno-dom-wasm.ts"; import buildResponseProxy from "../utils/build-response-proxy.ts"; +import { SupabaseClient } from "https://esm.sh/v135/@supabase/supabase-js@2.24.0/dist/module/index.d.ts"; function toIsoDate(dateString: string): string { const [day, month, year] = dateString.split("."); @@ -16,6 +17,21 @@ function sanitiseUrl(url: string) { } // Scrapes recent bills metadata +async function checkIfBillExists( + supabase: SupabaseClient, + scrapedData: { + bill_no: string; + }, +) { + const { data, error } = await supabase + .from("bill") + .select("bill_no") + .eq("bill_no", scrapedData.bill_no) + .maybeSingle(); + if (error) throw error; + return data != null; +} + // Does not scrape the actual bill text and does not actually summarise them export default async function scrapeBillsIntroduced(req: Request) { const supabase = createSupabase(); @@ -87,12 +103,7 @@ export default async function scrapeBillsIntroduced(req: Request) { summary: null, }; - const billExists = - (await supabase - .from("bill") - .select("bill_no") - .eq("bill_no", scrapedData.bill_no) - .maybeSingle()) != null; + const billExists = await checkIfBillExists(supabase, scrapedData); if (billExists) { // Make sure that we don't overwrite the original_text and summary values