diff --git a/.env.template b/.env.template index bc7ec1c..ab94b56 100644 --- a/.env.template +++ b/.env.template @@ -1,2 +1,3 @@ NEXT_PUBLIC_SUPABASE_URL=YOUR_SUPABASE_URL NEXT_PUBLIC_SUPABASE_ANON_KEY=YOUR_SUPABASE_ANON_KEY +NEXT_PRIVATE_SUPABASE_SERVICE_KEY=YOUR_SUPABASE_SERVICE_KEY diff --git a/src/pages/api/submit_question.js b/src/pages/api/submit_question.js new file mode 100644 index 0000000..4067fa6 --- /dev/null +++ b/src/pages/api/submit_question.js @@ -0,0 +1,41 @@ +// Next.js API route support: https://nextjs.org/docs/api-routes/introduction +import { createClient, SupabaseClient } from '@supabase/supabase-js'; + +// TODO: move the following somewhere else. +const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL || ''; +const supabaseAnonKey = process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY || ''; +const supabaseServiceKey = process.env.NEXT_PRIVATE_SUPABASE_SERVICE_KEY || ''; +const supabase = createClient(supabaseUrl, supabaseAnonKey); +const supabaseSecret = createClient(supabaseUrl, supabaseServiceKey); + +export default async function handler(req, res) { + // data format: + // { + // company: "a", + // location: "a", + // position: "b", + // question: "a", + // question_details: "", + // recency-weeks: "2", + // stay_anonymous: false + // } + const input_data = JSON.parse(req.body); + const jwt = req.headers.authentication; + const asked_date = new Date(new Date().setDate(new Date().getDate() - Number(input_data['recency-weeks']) * 7)); + + input_data['asked_date'] = asked_date; + delete input_data['recency-weeks']; + + const { data: user, userError } = await supabase.auth.api.getUser(jwt); + const id = user.identities[0]['id']; + + input_data['created_by'] = id; + + const { data, error } = await supabaseSecret.from('questions').insert([input_data]); + + if (error) { + res.status(500).json({ error }); + } else { + res.status(200).json({ message: 'success!' }); + } +} diff --git a/src/pages/api/submit_question.ts b/src/pages/api/submit_question.ts deleted file mode 100644 index 9eed00f..0000000 --- a/src/pages/api/submit_question.ts +++ /dev/null @@ -1,25 +0,0 @@ -// Next.js API route support: https://nextjs.org/docs/api-routes/introduction -import type { NextApiRequest, NextApiResponse } from 'next'; - -type Data = { - temp_message: string; -}; - -export default function handler(req: NextApiRequest, res: NextApiResponse) { - // data format: - // { - // company: "a", - // location: "a", - // position: "b", - // question: "a", - // question-details: "", - // recency: "Within the past week", - // stay-anonymous: false - // } - const data = JSON.parse(req.body); - const jwt = req.headers.authentication; - - debugger; // TODO: remove this and insert data in Supabase. - - res.status(200).json({ temp_message: 'hello' }); -} diff --git a/src/pages/submit.tsx b/src/pages/submit.tsx index 362fdef..a2e0105 100644 --- a/src/pages/submit.tsx +++ b/src/pages/submit.tsx @@ -97,21 +97,22 @@ const Submit: NextPage = ({ session }) => {
-
@@ -131,13 +132,13 @@ const Submit: NextPage = ({ session }) => {
-