diff --git a/.vscode/settings.json b/.vscode/settings.json index d34c0017..c5859bca 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,20 @@ { "deno.enable": true, "deno.lint": false, - "deno.unstable": false + "deno.unstable": [ + "bare-node-builtins", + "byonm", + "sloppy-imports", + "unsafe-proto", + "webgpu", + "broadcast-channel", + "worker-options", + "cron", + "kv", + "ffi", + "fs", + "http", + "net" + ], + "deno.importMap": "./supabase/functions/import_map.json" } diff --git a/supabase/config.toml b/supabase/config.toml index 6cdfd97d..918bb4a1 100644 --- a/supabase/config.toml +++ b/supabase/config.toml @@ -132,3 +132,7 @@ port = 54327 vector_port = 54328 # Configure one of the supported backends: `postgres`, `bigquery`. backend = "postgres" + +[functions.grades-tmp] +verify_jwt = false +import_map = './functions/import_map.json' \ No newline at end of file diff --git a/supabase/functions/_shared/cors.ts b/supabase/functions/_shared/cors.ts new file mode 100644 index 00000000..9cee30ae --- /dev/null +++ b/supabase/functions/_shared/cors.ts @@ -0,0 +1,4 @@ +export const corsHeaders = { + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type' +}; diff --git a/supabase/functions/_shared/supabaseAdmin.ts b/supabase/functions/_shared/supabaseAdmin.ts new file mode 100644 index 00000000..e69de29b diff --git a/supabase/functions/_shared/supabaseClient.ts b/supabase/functions/_shared/supabaseClient.ts new file mode 100644 index 00000000..e69de29b diff --git a/supabase/functions/grades-tmp/index.ts b/supabase/functions/grades-tmp/index.ts new file mode 100644 index 00000000..3b609ace --- /dev/null +++ b/supabase/functions/grades-tmp/index.ts @@ -0,0 +1,57 @@ +// import lodash from 'lodash' +import { corsHeaders } from '../_shared/cors.ts'; + +import { createClient } from 'jsr:@supabase/supabase-js@2'; + +Deno.serve(async (req) => { + const { url, method } = req; + + if (method === 'OPTIONS') { + return new Response('ok', { headers: corsHeaders }); + } + + const key = new URL(url).searchParams.get('key'); + + // Use a secret key inplace of Authorization header + if (key !== Deno.env.get('SECRET_KEY')) { + return new Response('Bad request', { + headers: { ...corsHeaders, 'Content-Type': 'application/json' }, + status: 400 + }); + } + + try { + const supabase = createClient( + Deno.env.get('SUPABASE_URL') ?? '', + Deno.env.get('SUPABASE_SERVICE_ROLE_KEY') ?? '' + ); + + const taskPattern = new URLPattern({ pathname: '/grades-tmp/:id' }); + const matchingPath = taskPattern.exec(url); + const courseId = matchingPath ? matchingPath.pathname.groups.id : null; + + const { data: exercises, error: exercisesError } = await supabase + .rpc('get_exercises') + .eq('course_id', courseId); + + if (exercisesError) { + throw exercisesError; + } + + const { data: marks, error } = await supabase.rpc('get_marks').eq('course_id', courseId); + + if (error) { + throw error; + } + + return new Response(JSON.stringify({ exercises, marks }), { + headers: { ...corsHeaders, 'Content-Type': 'application/json' }, + status: 200 + }); + } catch (err) { + return new Response(JSON.stringify({ error: err.message }), { + headers: { ...corsHeaders, 'Content-Type': 'application/json' }, + status: 400 + }); + } +}); diff --git a/supabase/functions/import_map.json b/supabase/functions/import_map.json new file mode 100644 index 00000000..2eeaeb0a --- /dev/null +++ b/supabase/functions/import_map.json @@ -0,0 +1,5 @@ +{ + "imports": { + "lodash": "https://cdn.skypack.dev/lodash" + } +}