chore: add grades edge function

This commit is contained in:
Rotimi Best
2024-10-14 01:51:57 +01:00
parent e1f32a21e4
commit 509d53ced3
7 changed files with 86 additions and 1 deletions
+16 -1
View File
@@ -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"
}
+4
View File
@@ -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'
+4
View File
@@ -0,0 +1,4 @@
export const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type'
};
+57
View File
@@ -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
});
}
});
+5
View File
@@ -0,0 +1,5 @@
{
"imports": {
"lodash": "https://cdn.skypack.dev/lodash"
}
}