Skip to content

Commit

Permalink
Merge pull request #121 from Generation-D/feat/answer
Browse files Browse the repository at this point in the history
Feat/answer
  • Loading branch information
Interperle authored Nov 27, 2023
2 parents 7ed1aa5 + 96f5d3b commit b49b566
Show file tree
Hide file tree
Showing 42 changed files with 1,938 additions and 440 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/eslint_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '20.6.1' # specify your Node.js version here
node-version: '20.6.1'

- name: Install Dependencies
run: |
Expand Down
2 changes: 1 addition & 1 deletion backend/apl_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,4 @@ questions:
order: 3
question: Please Upload this Video
mandatory: false
maxFileSizeInMB: 2.0 # in Mega Byte
maxFileSizeInMB: 10.0 # in Mega Byte
9 changes: 6 additions & 3 deletions backend/backend/sql/init_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ ALTER TABLE
CREATE TABLE
VIDEO_UPLOAD_ANSWER_TABLE (
answerid UUID PRIMARY KEY,
videourl VARCHAR(255) NOT NULL, -- change to Videoupload later on
videoname TEXT NOT NULL,
FOREIGN KEY (answerid) REFERENCES ANSWER_TABLE (answerid) ON DELETE CASCADE
);

Expand All @@ -223,7 +223,7 @@ ALTER TABLE
CREATE TABLE
IMAGE_UPLOAD_ANSWER_TABLE (
answerid UUID PRIMARY KEY,
imageurl VARCHAR(255) NOT NULL, -- change to Image Upload later on
imagename TEXT NOT NULL,
FOREIGN KEY (answerid) REFERENCES ANSWER_TABLE (answerid) ON DELETE CASCADE
);

Expand All @@ -233,7 +233,7 @@ ALTER TABLE
CREATE TABLE
PDF_UPLOAD_ANSWER_TABLE (
answerid UUID PRIMARY KEY,
pdfurl VARCHAR(255) NOT NULL, -- change to PDF Upload later on
pdfname TEXT NOT NULL,
FOREIGN KEY (answerid) REFERENCES ANSWER_TABLE (answerid) ON DELETE CASCADE
);

Expand Down Expand Up @@ -353,4 +353,7 @@ CREATE POLICY insert_profile ON user_profiles_table FOR INSERT
WITH CHECK (auth.uid() IS NOT NULL);

CREATE POLICY select_own_profile ON public.user_profiles_table FOR SELECT
USING (userid = auth.uid());

CREATE POLICY select_own_application ON application_table FOR SELECT
USING (userid = auth.uid());
13 changes: 12 additions & 1 deletion frontend/next.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
/** @type {import('next').NextConfig} */
const nextConfig = {};
const nextConfig = {
images: {
remotePatterns: [
{
protocol: 'http',
hostname: 'localhost',
port: '3000',
pathname: '/**',
},
],
},
};

module.exports = nextConfig;
20 changes: 20 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"autoprefixer": "^10.4.16",
"eslint-config-next": "14.0.3",
"fs": "^0.0.1-security",
"moment-timezone": "^0.5.43",
"next": "^14.0.3",
"nodemailer": "^6.9.7",
"pino": "^8.16.2",
Expand Down
169 changes: 169 additions & 0 deletions frontend/src/actions/answers/answers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
"use server";

import { createCurrentTimestamp } from "@/utils/helpers";
import { createServerClient } from "@supabase/ssr";
import { SupabaseClient, User, UserResponse } from "@supabase/supabase-js";
import { cookies } from "next/headers";
import { redirect } from "next/navigation";

export interface saveAnswerType {
supabase: SupabaseClient;
answerid: string;
reqtype: string;
}

export async function setupSupabaseClient(): Promise<SupabaseClient> {
const cookieStore = cookies();

const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
get(name: string) {
return cookieStore.get(name)?.value;
},
},
},
);
return supabase;
}

export async function getCurrentUser(supabase: SupabaseClient) {
const { data: userData, error: userError } = await supabase.auth.getUser();
if (userError) {
console.log(userError);
redirect("/");
}
return userData.user;
}

export async function getApplicationIdOfCurrentUser(
supabase: SupabaseClient,
user: User,
) {
const { data: applicationData, error: applicationError } = await supabase
.from("application_table")
.select("applicationid")
.eq("userid", user.id)
.single();
if (applicationError) {
console.log(applicationError);
}
return applicationData?.applicationid;
}

export async function fetchAnswerId(
supabase: SupabaseClient,
user: User,
applicationid: string,
questionid: string,
) {
const { data: answerData, error: answerError } = await supabase
.from("answer_table")
.select("answerid")
.eq("questionid", questionid)
.eq("applicationid", applicationid);

if (answerError) {
console.log(answerError);
}
if (answerData!.length == 0) {
return "";
}
return answerData![0].answerid;
}

export async function saveAnswer(questionid: string): Promise<saveAnswerType> {
const supabase = await setupSupabaseClient();
const user = await getCurrentUser(supabase);
const applicationid = await getApplicationIdOfCurrentUser(supabase, user);
let answerid = await fetchAnswerId(supabase, user, applicationid, questionid);
const now = createCurrentTimestamp();

let reqtype = "";
if (answerid == "") {
const insertAnswerResponse = await supabase.from("answer_table").insert({
questionid: questionid,
applicationid: applicationid,
created: now,
lastupdated: now,
});

if (insertAnswerResponse) {
console.log(insertAnswerResponse);
}

const selectAnswerResponse = await supabase
.from("answer_table")
.select("answerid")
.eq("applicationid", applicationid)
.eq("questionid", questionid)
.single();

if (selectAnswerResponse) {
console.log(selectAnswerResponse);
}
answerid = selectAnswerResponse!.data!.answerid;
reqtype = "created";
} else {
reqtype = "updated";
}

return { supabase: supabase, answerid: answerid, reqtype: reqtype };
}

export async function deleteAnswer(questionid: string, answertype: string) {
const supabase = await setupSupabaseClient();
const user = await getCurrentUser(supabase);
const applicationid = await getApplicationIdOfCurrentUser(supabase, user);
let answerid = await fetchAnswerId(supabase, user, applicationid, questionid);
console.log(answerid);
if (answerid != "") {
const deleteAnswerResponse = await supabase
.from("answer_table")
.delete()
.eq("questionid", questionid)
.eq("applicationid", applicationid);
console.log(deleteAnswerResponse);
const deleteAnswerTypeResponse = await supabase
.from(answertype)
.delete()
.eq("answerid", answerid);
console.log(deleteAnswerTypeResponse);
}
}

export async function saveDatePickerAnswer(
pickeddate: Date,
questionid: string,
) {
const { supabase, answerid } = await saveAnswer(questionid);

const insertDatePickerAnswerResponse = await supabase
.from("date_picker_answer_table")
.insert({
answerid: answerid,
pickeddate: pickeddate,
});
if (insertDatePickerAnswerResponse) {
console.log(insertDatePickerAnswerResponse);
}
}

export async function saveDateTimePickerAnswer(
pickeddatetime: string,
questionid: string,
) {
const { supabase, answerid } = await saveAnswer(questionid);

const insertDatePickerAnswerResponse = await supabase
.from("datetime_picker_answer_table")
.insert({
answerid: answerid,
pickeddatetime: pickeddatetime,
});
if (insertDatePickerAnswerResponse) {
console.log(insertDatePickerAnswerResponse);
}
}
59 changes: 59 additions & 0 deletions frontend/src/actions/answers/datePicker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
"use server";
import {
deleteAnswer,
fetchAnswerId,
getApplicationIdOfCurrentUser,
getCurrentUser,
saveAnswer,
setupSupabaseClient,
} from "./answers";

export async function saveDatePickerAnswer(
pickeddate: string,
questionid: string,
) {
console.log("%" + pickeddate + "%");
if (pickeddate == "") {
await deleteAnswer(questionid, "date_picker_answer_table");
return;
} else {
const { supabase, answerid, reqtype } = await saveAnswer(questionid);
if (reqtype == "created") {
const insertDatePickerAnswerResponse = await supabase
.from("date_picker_answer_table")
.insert({
answerid: answerid,
pickeddate: new Date(pickeddate),
});
if (insertDatePickerAnswerResponse) {
console.log(insertDatePickerAnswerResponse);
}
} else if (reqtype == "updated") {
const updateDatePickerAnswerResponse = await supabase
.from("date_picker_answer_table")
.update({
pickeddate: new Date(pickeddate),
})
.eq("answerid", answerid);
if (updateDatePickerAnswerResponse) {
console.log(updateDatePickerAnswerResponse);
}
}
}
}

export async function fetchDatePickerAnswer(questionid: string) {
const supabase = await setupSupabaseClient();
const user = await getCurrentUser(supabase);
const applicationid = await getApplicationIdOfCurrentUser(supabase, user);
let answerid = await fetchAnswerId(supabase, user, applicationid, questionid);
if (answerid) {
const { data: datePickerData, error: datePickerError } = await supabase
.from("date_picker_answer_table")
.select("pickeddate")
.eq("answerid", answerid)
.single();
return datePickerData!.pickeddate;
}
return "";
}
Loading

0 comments on commit b49b566

Please sign in to comment.