From 60681cf87a7ba6f9dc8051ee8f11138b98c7493e Mon Sep 17 00:00:00 2001 From: Logan Ford <110533855+Logannford@users.noreply.github.com> Date: Wed, 11 Dec 2024 22:37:24 +0000 Subject: [PATCH 1/5] create base slack edge function --- .../slack_send-daily-question/index.ts | 29 +++++++++++++++++++ .../slack_send-daily-question/readme.md | 4 +++ 2 files changed, 33 insertions(+) create mode 100644 supabase/functions/slack_send-daily-question/index.ts create mode 100644 supabase/functions/slack_send-daily-question/readme.md diff --git a/supabase/functions/slack_send-daily-question/index.ts b/supabase/functions/slack_send-daily-question/index.ts new file mode 100644 index 000000000..d2aaccde1 --- /dev/null +++ b/supabase/functions/slack_send-daily-question/index.ts @@ -0,0 +1,29 @@ +// Follow this setup guide to integrate the Deno language server with your editor: +// https://deno.land/manual/getting_started/setup_your_environment +// This enables autocomplete, go to definition, etc. + +console.log("Hello from Functions!") + +Deno.serve(async (req) => { + const { name } = await req.json() + const data = { + message: `Hello ${name}!`, + } + + return new Response( + JSON.stringify(data), + { headers: { "Content-Type": "application/json" } }, + ) +}) + +/* To invoke locally: + + 1. Run `supabase start` (see: https://supabase.com/docs/reference/cli/supabase-start) + 2. Make an HTTP request: + + curl -i --location --request POST 'http://127.0.0.1:54321/functions/v1/slack_send-daily-question' \ + --header 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZS1kZW1vIiwicm9sZSI6ImFub24iLCJleHAiOjE5ODM4MTI5OTZ9.CRXP1A7WOeoJeXxjNni43kdQwgnWNReilDMblYTn_I0' \ + --header 'Content-Type: application/json' \ + --data '{"name":"Functions"}' + +*/ diff --git a/supabase/functions/slack_send-daily-question/readme.md b/supabase/functions/slack_send-daily-question/readme.md new file mode 100644 index 000000000..bf2b958a6 --- /dev/null +++ b/supabase/functions/slack_send-daily-question/readme.md @@ -0,0 +1,4 @@ +To update this edge function: + +- ensure docker is running on your desktop +- `supabase functions deploy slack_send-daily-question From 7b69069c9561c89d84c6e66f8be6638b93f456d9 Mon Sep 17 00:00:00 2001 From: Logan Ford <110533855+Logannford@users.noreply.github.com> Date: Wed, 11 Dec 2024 22:59:49 +0000 Subject: [PATCH 2/5] playing around with edge function --- src/app/(marketing)/page.tsx | 11 +++ .../slack_send-daily-question/index.ts | 71 ++++++++++++++++--- 2 files changed, 73 insertions(+), 9 deletions(-) diff --git a/src/app/(marketing)/page.tsx b/src/app/(marketing)/page.tsx index 8b6c3b0cd..fd7dc1182 100644 --- a/src/app/(marketing)/page.tsx +++ b/src/app/(marketing)/page.tsx @@ -6,11 +6,22 @@ import HomepageHero from '@/components/marketing/homepage/hero/hero'; import HomepageHeroImages from '@/components/marketing/homepage/hero/hero-images'; import HomepageLargeText from '@/components/marketing/large-text'; +import { supabase } from '@/lib/supabase'; + import posthog from 'posthog-js'; export default async function AuthedPage() { posthog.capture('page_view', { page_name: 'Landing Page' }); + const { data, error } = await supabase.functions.invoke( + 'slack_send-daily-question' + ); + + console.log({ + data, + error + }); + return (
diff --git a/supabase/functions/slack_send-daily-question/index.ts b/supabase/functions/slack_send-daily-question/index.ts index d2aaccde1..f0ff0beae 100644 --- a/supabase/functions/slack_send-daily-question/index.ts +++ b/supabase/functions/slack_send-daily-question/index.ts @@ -1,20 +1,73 @@ +import { createClient, type SupabaseClient } from '@supabase/supabase-js'; +import { corsHeaders } from '../_shared/cors.ts'; + // Follow this setup guide to integrate the Deno language server with your editor: // https://deno.land/manual/getting_started/setup_your_environment // This enables autocomplete, go to definition, etc. -console.log("Hello from Functions!") +console.log('Hello from Functions!'); Deno.serve(async (req) => { - const { name } = await req.json() - const data = { - message: `Hello ${name}!`, + if (req.method === 'OPTIONS') { + return new Response('ok', { headers: corsHeaders }); + } + + // get today's date + const today = new Date(); + + // get the day of the week + const day = today.getDay(); + + // if today is Sunday or Saturday, return + if (day === 0 || day === 6) { + return new Response("It's the weekend! No daily question today."); + } + + // get the current time + const time = today.getHours(); + + // if it's before 9am, return + + try { + const supabase = createClient( + Deno.env.get('SUPABASE_URL') ?? '', + Deno.env.get('SUPABASE_ANON_KEY') ?? '', + { + global: { + headers: { Authorization: req.headers.get('Authorization') ?? '' } + } + } + ); + + const { data, error } = await supabase + .from('Questions') + .select('uid, correctAnswer') + .eq(questionDate, today.toISOString().split('T')[0]) + .single(); + + if (error) { + throw error; + } + + if (!data) { + throw new Error('No question found for today'); + } + + return new Response(data); + } catch (error) { + console.error(error); } - return new Response( - JSON.stringify(data), - { headers: { "Content-Type": "application/json" } }, - ) -}) + // const { name } = await req.json() + // const data = { + // message: `Hello ${name}!`, + // } + + // return new Response( + // JSON.stringify(data), + // { headers: { "Content-Type": "application/json" } }, + // ) +}); /* To invoke locally: From 7073bb5bf05dd759591b56c152b6b7bcfa1b7a8c Mon Sep 17 00:00:00 2001 From: Logan Ford <110533855+Logannford@users.noreply.github.com> Date: Wed, 11 Dec 2024 23:16:27 +0000 Subject: [PATCH 3/5] build err & getting today's q in function --- src/components/global/logo.tsx | 4 ++-- supabase/functions/slack_send-daily-question/index.ts | 5 ++++- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/components/global/logo.tsx b/src/components/global/logo.tsx index 9820652b9..a08ae8b82 100644 --- a/src/components/global/logo.tsx +++ b/src/components/global/logo.tsx @@ -20,9 +20,9 @@ export default function Logo() { diff --git a/supabase/functions/slack_send-daily-question/index.ts b/supabase/functions/slack_send-daily-question/index.ts index f0ff0beae..65d98274f 100644 --- a/supabase/functions/slack_send-daily-question/index.ts +++ b/supabase/functions/slack_send-daily-question/index.ts @@ -53,7 +53,10 @@ Deno.serve(async (req) => { throw new Error('No question found for today'); } - return new Response(data); + return new Response(JSON.stringify(data), { + headers: { ...corsHeaders, 'Content-Type': 'application/json' }, + status: 200 + }); } catch (error) { console.error(error); } From 331e89eb8be8a46a8d9bef9a53f425aa1e1ea335 Mon Sep 17 00:00:00 2001 From: Logan Ford <110533855+Logannford@users.noreply.github.com> Date: Wed, 11 Dec 2024 23:25:40 +0000 Subject: [PATCH 4/5] first runthrough of models for workspaces --- prisma/schema.prisma | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 54b1fb97e..ee639e0f3 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -42,7 +42,7 @@ model Waitlist { } model Users { - uid String @id() + uid String @id @default(cuid()) email String @unique username String? firstName String? @@ -68,6 +68,45 @@ model Users { streak Streaks? roadmaps UserRoadmaps[] + + workspaceOwner Workspaces[] @relation(name: "WorkspaceOwner") + + workspaceUsers Workspaces[] @relation(name: "WorkspaceUsers") + + WorkspaceUsers WorkspaceUsers[] +} + +model Workspaces { + uid String @id @default(cuid()) + createdAt DateTime @default(now()) + updatedAt DateTime @updatedAt + + name String + description String? + ownerUid String + owner Users @relation(fields: [ownerUid], references: [uid], onDelete: Cascade, onUpdate: Cascade, name: "WorkspaceOwner") + + users WorkspaceUsers[] + + @@unique([name, ownerUid]) + Users Users[] @relation(name: "WorkspaceUsers") +} + +model WorkspaceUsers { + uid String @id @default(cuid()) + workspaceUid String + workspace Workspaces @relation(fields: [workspaceUid], references: [uid], onDelete: Cascade) + userUid String + user Users @relation(fields: [userUid], references: [uid], onDelete: Cascade) + role UserRole @default(MEMBER) + + @@unique([workspaceUid, userUid]) +} + +enum UserRole { + ADMIN + MEMBER + VIEWER } model Subscriptions { From 988fedcc8eddbb179e658083d0139a6dfbfb1a78 Mon Sep 17 00:00:00 2001 From: Logan Ford <110533855+Logannford@users.noreply.github.com> Date: Wed, 11 Dec 2024 23:31:09 +0000 Subject: [PATCH 5/5] build --- src/app/(marketing)/page.tsx | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/app/(marketing)/page.tsx b/src/app/(marketing)/page.tsx index fd7dc1182..338bbe2bd 100644 --- a/src/app/(marketing)/page.tsx +++ b/src/app/(marketing)/page.tsx @@ -13,14 +13,14 @@ import posthog from 'posthog-js'; export default async function AuthedPage() { posthog.capture('page_view', { page_name: 'Landing Page' }); - const { data, error } = await supabase.functions.invoke( - 'slack_send-daily-question' - ); + // const { data, error } = await supabase.functions.invoke( + // 'slack_send-daily-question' + // ); - console.log({ - data, - error - }); + // console.log({ + // data, + // error + // }); return (