Skip to content

Commit

Permalink
Merge pull request #15 from fac29/Added_Quiz
Browse files Browse the repository at this point in the history
Added quiz
  • Loading branch information
diddydeeable authored Aug 4, 2024
2 parents 0008207 + ca87c7f commit 3983f23
Show file tree
Hide file tree
Showing 12 changed files with 358 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/deployVercel.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Deploy to Vercel

on:
push:
branches: ['deployed']
branches: ['main']

jobs:
deploy:
Expand Down
3 changes: 3 additions & 0 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"recommendations": ["denoland.vscode-deno"]
}
10 changes: 10 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"deno.enablePaths": [
"supabase/functions"
],
"deno.lint": true,
"deno.unstable": true,
"[typescript]": {
"editor.defaultFormatter": "denoland.vscode-deno"
}
}
16 changes: 16 additions & 0 deletions seeds/answers.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
id,answer_text,question_id,goal_id,created_at
1,Emergency Savings,1,2,2024-07-31 11:02:32.428066+00
2,Managing Expenses,1,4,2024-07-31 11:03:14.550314+00
3,Investing for the Future,1,16,2024-07-31 11:04:54.834584+00
4,Giving Back,1,22,2024-07-31 11:05:36.106429+00
5,Student or graduate,2,1,2024-07-31 11:06:25.780938+00
6,Moved home,2,4,2024-07-31 11:07:00.694809+00
7,Changed jobs or role,2,14,2024-07-31 11:07:34.794972+00
8,Been on sabbatical,2,1,2024-07-31 11:07:56.631059+00
9,Taken maternity leave,2,16,2024-07-31 11:08:33.577078+00
10,None applicable,2,1,2024-07-31 11:08:54.462665+00
11,Taxes,3,5,2024-07-31 11:09:23.526468+00
12,Household bills,3,3,2024-07-31 11:09:57.679285+00
13,Pensions and employee benefits,3,14,2024-07-31 11:10:21.463339+00
14,Halal investing,3,16,2024-07-31 11:10:46.983309+00
15,Student loans,3,11,2024-07-31 11:11:24.339196+00
5 changes: 5 additions & 0 deletions seeds/questions.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
id,question,created_at
1,Which areas of your personal finances were you planning to focus on?,2024-07-31 10:57:12.586849+00
2,In the last two years which of these life events have you experienced?,2024-07-31 10:57:34.65335+00
3,Which topics below you would like more information on?,2024-07-31 10:57:53.108628+00
4,Is there any question you have and would like to ask?,2024-07-31 10:58:06.319872+00
2 changes: 2 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import cors from 'cors'
import userRoutes from './routes/users'
import goalRoutes from './routes/goals'
import quoteRoutes from './routes/quotes'
import quizRoutes from './routes/quiz'
import contact_nisaRoutes from './routes/contactnisa'
import config from './config/config'

Expand All @@ -22,6 +23,7 @@ app.use(express.json())
app.use('/users', userRoutes)
app.use('/goals', goalRoutes)
app.use('/quotes', quoteRoutes)
app.use('/quiz', quizRoutes)
app.use('/contactnisa', contact_nisaRoutes)

if (config.nodeEnv !== 'production') {
Expand Down
268 changes: 268 additions & 0 deletions src/routes/quiz.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
import express from 'express'
import supabase from '../supabaseClient'

const router = express.Router()

// Questions Endpoints
router.get('/questions', async (req, res) => {
try {
const { data, error } = await supabase.from('questions').select('*')
if (error) throw new Error(error.message)
res.json(data)
} catch (error: unknown) {
res.status(500).json({
error:
error instanceof Error ? error.message : 'An unknown error occurred',
})
}
})

// Get question by ID
router.get('/questions/:questionId', async (req, res) => {
try {
const { data, error } = await supabase
.from('questions')
.select('*')
.eq('id', req.params.questionId)
.single()
if (error) throw new Error(error.message)
if (!data) return res.status(404).json({ error: 'Question not found' })
res.json(data)
} catch (error: unknown) {
res.status(500).json({
error:
error instanceof Error ? error.message : 'An unknown error occurred',
})
}
})

// Create questionac
router.post('/questions', async (req, res) => {
const { id,...questionData } = req.body

try {
// Start a transaction
const { data: newQuestion, error: questionError } = await supabase
.from('questions')
.insert([questionData])
.select()
.single()

if (questionError) throw new Error(questionError.message)

res.status(201).json(newQuestion)
} catch (error: unknown) {
res
.status(500)
.json({
error:
error instanceof Error ? error.message : 'An unknown error occurred',
})
}
})

// Edit question
router.put('/questions/:questionId', async (req, res) => {
try {
const { data, error } = await supabase
.from('questions')
.update(req.body)
.eq('id', req.params.questionId)
.select()
if (error) throw new Error(error.message)
if (data.length === 0)
return res.status(404).json({ error: 'Question not found' })
res.json(data[0])
} catch (error: unknown) {
res.status(500).json({
error:
error instanceof Error ? error.message : 'An unknown error occurred',
})
}
})

// Delete question
router.delete('/questions/:questionId', async (req, res) => {
try {
const { error } = await supabase
.from('questions')
.delete()
.eq('id', req.params.questionId)
if (error) throw new Error(error.message)
res.status(204).send()
} catch (error: unknown) {
res.status(500).json({
error:
error instanceof Error ? error.message : 'An unknown error occurred',
})
}
})

// Answers Endpoints
// Get all answers
router.get('/answers', async (req, res) => {
try {
const { data, error } = await supabase.from('answers').select('*')
if (error) throw new Error(error.message)
res.json(data)
} catch (error: unknown) {
res.status(500).json({
error:
error instanceof Error ? error.message : 'An unknown error occurred',
})
}
})

// Get answer by ID
router.get('/answers/:answerId', async (req, res) => {
try {
const { data, error } = await supabase
.from('answers')
.select('*')
.eq('id', req.params.answerId)
.single()
if (error) throw new Error(error.message)
if (!data) return res.status(404).json({ error: 'Answer not found' })
res.json(data)
} catch (error: unknown) {
res.status(500).json({
error:
error instanceof Error ? error.message : 'An unknown error occurred',
})
}
})

// Create Answer
router.post('/answers', async (req, res) => {
const { id, ...answerData } = req.body

try {
// Start a transaction
const { data: newAnswer, error: answerError } = await supabase
.from('answers')
.insert([answerData])
.select()
.single()

if (answerError) throw new Error(answerError.message)

res.status(201).json(newAnswer)
} catch (error: unknown) {
res.status(500).json({
error:
error instanceof Error ? error.message : 'An unknown error occurred',
})
}
})

// Edit answer
router.put('/answers/:answerId', async (req, res) => {
try {
const { data, error } = await supabase
.from('answers')
.update(req.body)
.eq('id', req.params.answerId)
.select()
if (error) throw new Error(error.message)
if (data.length === 0)
return res.status(404).json({ error: 'Answer not found' })
res.json(data[0])
} catch (error: unknown) {
res.status(500).json({
error:
error instanceof Error ? error.message : 'An unknown error occurred',
})
}
})

//Delete Answer
router.delete('/answers/:answerId', async (req, res) => {
try {
const { error } = await supabase
.from('answers')
.delete()
.eq('id', req.params.answerId)
if (error) throw new Error(error.message)
res.status(204).send()
} catch (error: unknown) {
res.status(500).json({
error:
error instanceof Error ? error.message : 'An unknown error occurred',
})
}
})

// Get all questions with their answers
router.get('/questions-with-answers', async (req, res) => {
try {
const { data: questions, error: questionsError } = await supabase
.from('questions')
.select('*')

if (questionsError) throw new Error(questionsError.message)

if (!questions || questions.length === 0) {
return res.status(404).json({ error: 'No questions found' })
}

const questionsWithAnswers = await Promise.all(questions.map(async (question) => {
const { data: answers, error: answersError } = await supabase
.from('answers')
.select('*')
.eq('question_id', question.id)

if (answersError) throw new Error(answersError.message)

return {
...question,
answers: answers || []
}
}))

res.json(questionsWithAnswers)
} catch (error: unknown) {
res.status(500).json({
error:
error instanceof Error ? error.message : 'An unknown error occurred',
})
}
})

// Get goals associated with a list of answers
router.post('/goals-for-answers', async (req, res) => {
const { answerIds } = req.body;

if (!answerIds || !Array.isArray(answerIds) || answerIds.length === 0) {
return res.status(400).json({ error: 'Invalid or missing answerIds in request body' });
}

try {
// First, get all answers with the given IDs
const { data: answers, error: answersError } = await supabase
.from('answers')
.select('id, goal_id')
.in('id', answerIds);

if (answersError) throw new Error(answersError.message);

// Extract unique goal IDs from the answers
const goalIds = [...new Set(answers.map(answer => answer.goal_id))];

// Then, get all goals associated with these goal IDs
const { data: goals, error: goalsError } = await supabase
.from('goals')
.select('*')
.in('id', goalIds);

if (goalsError) throw new Error(goalsError.message);

res.json(goals);
} catch (error: unknown) {
res.status(500).json({
error:
error instanceof Error ? error.message : 'An unknown error occurred',
});
}
});

export default router
Original file line number Diff line number Diff line change
@@ -1,5 +1,2 @@
create type "public"."recurrence_type" as enum ('day', 'week', 'month', 'year');

create type "public"."status" as enum ('not_done', 'in_progress', 'completed');


create type "public"."status" as enum ('not_done', 'in_progress', 'completed');
1 change: 1 addition & 0 deletions supabase/hooks/post-migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
\i ../seed.sql
Loading

0 comments on commit 3983f23

Please sign in to comment.