From ea261de70338542d923a5e20639ed598a13d2b84 Mon Sep 17 00:00:00 2001 From: Chris John Date: Wed, 23 Aug 2023 00:35:39 -0400 Subject: [PATCH] converting types to prisma generated type, converting some supabase stored procedures to prisma --- .../AdminEditDashboard/AdminEditItem.tsx | 4 +- .../admin/Evaluation/EvaluationStore.tsx | 10 +- .../Evaluation/OutcomeModal/OutcomeModal.tsx | 6 +- src/components/dashboard/DashboardStore.tsx | 7 +- src/components/roundDetails/RoundDetails.tsx | 2 +- .../roundDetails/RoundDetailsStore.tsx | 11 +-- src/components/shared/Submission.tsx | 10 +- src/components/submission/SubmissionPage.tsx | 8 +- src/components/submission/SubmissionStore.tsx | 10 +- src/components/voting/VotingStore.tsx | 7 +- src/components/voting/VotingTableBody.tsx | 30 +++--- src/lib/UserProfileStore.tsx | 5 +- src/lib/types.ts | 65 ++++++++----- src/lib/utils.ts | 4 +- src/server/routes/admin/createEvaluation.ts | 8 +- src/server/routes/admin/getStatusStore.ts | 36 +++++-- src/server/routes/admin/getSubmissions.ts | 6 +- .../routes/admin/setEvaluationEndTime.ts | 2 +- .../routes/admin/setEvaluationStartTime.ts | 2 +- src/server/routes/user/createSubmission.ts | 97 +++++++++++-------- src/server/routes/user/getDashboardStore.ts | 38 ++++++++ .../routes/user/getRoundDetailsStore.ts | 54 +++++++++-- src/server/routes/user/getUserProfile.ts | 6 +- src/server/routes/user/getVotingStore.ts | 82 +++++++++++++++- src/server/routes/user/setResetVotes.ts | 36 +++++-- src/server/routes/user/setVote.ts | 54 +++++++++-- 26 files changed, 433 insertions(+), 167 deletions(-) diff --git a/src/components/admin/AdminEditDashboard/AdminEditItem.tsx b/src/components/admin/AdminEditDashboard/AdminEditItem.tsx index a022529..6ef88a7 100644 --- a/src/components/admin/AdminEditDashboard/AdminEditItem.tsx +++ b/src/components/admin/AdminEditDashboard/AdminEditItem.tsx @@ -1,4 +1,4 @@ -import { DashboardEvaluation } from "src/lib"; +import { evaluation } from "@prisma/client"; import Link from "next/link"; import { returnTime, returnDate } from "src/lib/utils"; @@ -19,7 +19,7 @@ enum RoundTiming { type EvaluationItemProps = { first: boolean; last: boolean; - evaluation: DashboardEvaluation; + evaluation: evaluation; }; export const AdminEditItem = ({ evaluation, first, last }: EvaluationItemProps) => { return ( diff --git a/src/components/admin/Evaluation/EvaluationStore.tsx b/src/components/admin/Evaluation/EvaluationStore.tsx index aaa2148..1622366 100644 --- a/src/components/admin/Evaluation/EvaluationStore.tsx +++ b/src/components/admin/Evaluation/EvaluationStore.tsx @@ -1,6 +1,7 @@ import { create } from "zustand"; import { v4 as uuid } from "uuid"; import { rpc, Submission } from "src/lib"; +import { submission } from "@prisma/client"; import { trpc } from "src/lib/trpc"; export interface EvaluationStore { @@ -20,7 +21,7 @@ export interface EvaluationStore { setFormFieldCharCount: (count: number, id: string) => void; deleteFormField: (id: string) => void; deleteEvaluation: () => void; - createSubmission: () => Promise; + createSubmission: () => Promise; createInvitation: (invitation?: any) => void; setInvitationCode: (code: string, id: string) => void; setInvitationCredits: (credits: number, id: string) => void; @@ -398,7 +399,7 @@ export const useEvaluationStore = create()((set, get) => ({ }); }, - createSubmission: async (): Promise => { + createSubmission: async (): Promise => { const evaluation = get().evaluation; if (get().fetching || !evaluation) { @@ -406,13 +407,8 @@ export const useEvaluationStore = create()((set, get) => ({ } const newSubmission = Submission.init({ - description: "", evaluation_id: evaluation.id, - name: "", user_id: null, - github_link: "", - github_handle: "", - links: [], }); set({ diff --git a/src/components/admin/Evaluation/OutcomeModal/OutcomeModal.tsx b/src/components/admin/Evaluation/OutcomeModal/OutcomeModal.tsx index 28393b2..3170e33 100644 --- a/src/components/admin/Evaluation/OutcomeModal/OutcomeModal.tsx +++ b/src/components/admin/Evaluation/OutcomeModal/OutcomeModal.tsx @@ -35,7 +35,7 @@ const OutcomeModal = ({ handleClose, open, submission, store }: OutcomeModalProp specs: storeSubmissionExists?.description.specs, github_link: storeSubmissionExists?.github_link, links: storeSubmissionExists?.links || [], - githubHandle: storeSubmissionExists?.github_handle || "", + github_handle: storeSubmissionExists?.github_handle || "", user_id: storeSubmissionExists?.user_id, }); @@ -56,7 +56,7 @@ const OutcomeModal = ({ handleClose, open, submission, store }: OutcomeModalProp specs: storeSubmissionExists?.description.specs, github_link: storeSubmissionExists?.github_link, links: storeSubmissionExists?.links || [], - githubHandle: storeSubmissionExists?.github_handle || "", + github_handle: storeSubmissionExists?.github_handle || "", user_id: storeSubmissionExists?.user_id, }); }, [storeSubmissionExists]); @@ -64,7 +64,7 @@ const OutcomeModal = ({ handleClose, open, submission, store }: OutcomeModalProp const isDisabled = !formInputs.name || !formInputs.github_link || - !formInputs.githubHandle || + !formInputs.github_handle || !formInputs.user_id || !submissionFormCustomFieldsCheck(formInputs, submission?.id); return ( diff --git a/src/components/dashboard/DashboardStore.tsx b/src/components/dashboard/DashboardStore.tsx index 49f17b6..026eb3d 100644 --- a/src/components/dashboard/DashboardStore.tsx +++ b/src/components/dashboard/DashboardStore.tsx @@ -1,5 +1,6 @@ import { create } from "zustand"; import { Evaluation, rpc, DashboardEvaluation } from "src/lib"; +import { evaluation } from "@prisma/client"; import { trpc } from "src/lib/trpc"; export interface DashboardStore { @@ -7,7 +8,7 @@ export interface DashboardStore { error?: any; evaluations: DashboardEvaluation[]; load: () => void; - createEvaluation: () => Promise; + createEvaluation: () => Promise; } export const useDashboardStore = create()((set, get) => ({ @@ -30,8 +31,8 @@ export const useDashboardStore = create()((set, get) => ({ }); }, - createEvaluation: async (): Promise => { - const newEvaluation: Evaluation = { + createEvaluation: async (): Promise => { + const newEvaluation: evaluation = { ...Evaluation.init(), }; diff --git a/src/components/roundDetails/RoundDetails.tsx b/src/components/roundDetails/RoundDetails.tsx index c99c852..615a25b 100644 --- a/src/components/roundDetails/RoundDetails.tsx +++ b/src/components/roundDetails/RoundDetails.tsx @@ -8,7 +8,7 @@ import EvaluationLinkButton from "../dashboard/EvaluationLinkButton"; import SmallTitle from "../shared/SmallTitle"; import Title from "../shared/Title"; import VotingTableBody from "../voting/VotingTableBody"; -import { Submission, VotingTableBodySubmission } from "src/lib"; +import { VotingTableBodySubmission } from "src/lib"; import Add from "public/images/svg/Add"; import QuadraticVotingModal from "../voting/QuadraticVotingModal"; import parse from "html-react-parser"; diff --git a/src/components/roundDetails/RoundDetailsStore.tsx b/src/components/roundDetails/RoundDetailsStore.tsx index 3d73d98..74d5296 100644 --- a/src/components/roundDetails/RoundDetailsStore.tsx +++ b/src/components/roundDetails/RoundDetailsStore.tsx @@ -1,19 +1,19 @@ import { create } from "zustand"; import { v4 as uuid } from "uuid"; import { rpc, Submission } from "src/lib"; +import { submission } from "@prisma/client"; import { trpc } from "src/lib/trpc"; export interface RoundDetailsStore { fetching: boolean; evaluation?: any; - submissions?: Submission[]; - evaluation_field?: any; + submissions?: submission[]; evaluationID?: string; userID?: string; githubHandle?: string; load: (userID: string, evaluationID: string, githubHandle: string) => void; deleteSubmission: (submissionID: string) => void; - createSubmission: () => Promise; + createSubmission: () => Promise; } export const useRoundDetailsStore = create()((set, get) => ({ @@ -31,9 +31,8 @@ export const useRoundDetailsStore = create()((set, get) => ({ } set({ - evaluation: data.evaluation[0], + evaluation: data.evaluation, submissions: data.submissions, - evaluation_field: data.evaluation_field, evaluationID: evaluationID, userID: userID, githubHandle: githubHandle, @@ -60,7 +59,7 @@ export const useRoundDetailsStore = create()((set, get) => ({ } }); }, - createSubmission: async (): Promise => { + createSubmission: async (): Promise => { const evaluationID = get().evaluationID; const userID = get().userID; const githubHandle = get().githubHandle; diff --git a/src/components/shared/Submission.tsx b/src/components/shared/Submission.tsx index 82b6662..78b4658 100644 --- a/src/components/shared/Submission.tsx +++ b/src/components/shared/Submission.tsx @@ -1,8 +1,8 @@ import Add from "public/images/svg/Add"; import Delete from "public/images/svg/Delete"; import Edit from "public/images/svg/Edit"; -import { Submission as SubmissionType, SubmissionFormInputs } from "src/lib"; -import { SubmissionFormLinkInputs } from "src/lib"; +import { SubmissionFormInputs, SubmissionFormLinkInputs } from "src/lib"; +import { submission } from "@prisma/client"; type SubmissionProps = { store: any; @@ -11,7 +11,7 @@ type SubmissionProps = { setFormInputs: any; isGithubHandleChecked?: boolean; setIsGithubHandleChecked?: any; - submission?: SubmissionType; + submission?: submission; submission_id: string | string[] | undefined; }; @@ -229,8 +229,8 @@ export default function Submission({ className="text-base appearance-none border border-gray rounded-lg w-full py-2 px-3 mt-1 font-medium disabled:text-gray focus:outline-none" type="text" name="githubHandle" - value={formInputs.githubHandle || ""} - onChange={(e) => handleFormChange(e, "githubHandle")} + value={formInputs.github_handle || ""} + onChange={(e) => handleFormChange(e, "github_handle")} onBlur={ !submission ? (e) => store.setGithubHandle(e.target.value) diff --git a/src/components/submission/SubmissionPage.tsx b/src/components/submission/SubmissionPage.tsx index 25e252f..f763a75 100644 --- a/src/components/submission/SubmissionPage.tsx +++ b/src/components/submission/SubmissionPage.tsx @@ -47,7 +47,8 @@ export default function SubmissionPage() { specs: store.submission?.description.specs, github_link: store.submission?.github_link, links: store.submission?.links || [], - githubHandle: isGithubHandleChecked ? store.submission?.github_handle : "", + github_handle: isGithubHandleChecked ? store.submission?.github_handle : "", + user_id: store.submission?.user_id, }); const handleSubmitModal = () => { @@ -83,7 +84,8 @@ export default function SubmissionPage() { specs: store.submission?.description.specs, github_link: store.submission?.github_link, links: store.submission?.links, - githubHandle: store.submission?.github_handle, + github_handle: store.submission?.github_handle, + user_id: store.submission?.user_id, }); if (githubHandleFromProfile) setIsGithubHandleChecked(githubHandleFromProfile === store.submission?.github_handle); }, [store.submission]); @@ -91,7 +93,7 @@ export default function SubmissionPage() { const isSubmitButtonDisabled = !formInputs.name || !formInputs.github_link || - (isGithubHandleChecked ? !githubHandleFromProfile : !formInputs.githubHandle) || + (isGithubHandleChecked ? !githubHandleFromProfile : !formInputs.github_handle) || (formInputs.links && formInputs.links.some((link) => !link.name || !link.value)) || !submissionFormCustomFieldsCheck(formInputs, submission_id); diff --git a/src/components/submission/SubmissionStore.tsx b/src/components/submission/SubmissionStore.tsx index b0be617..6f4b5d7 100644 --- a/src/components/submission/SubmissionStore.tsx +++ b/src/components/submission/SubmissionStore.tsx @@ -2,6 +2,8 @@ import { create } from "zustand"; import { v4 as uuid } from "uuid"; import { Evaluation, rpc, Submission } from "src/lib"; import { trpc } from "src/lib/trpc"; +import { submission } from "@prisma/client"; + export interface SubmissionStore { fetching: boolean; submission?: any; @@ -17,7 +19,7 @@ export interface SubmissionStore { createSubmissionLink: () => void; deleteSubmissionLink: (index: number) => void; setSubmission: () => void; - createSubmission: (evaluation_id: string, user_id: string) => Promise; + createSubmission: (evaluation_id: string, user_id: string) => Promise; } export const useSubmissionStore = create()((set, get) => ({ @@ -320,7 +322,7 @@ export const useSubmissionStore = create()((set, get) => ({ } }); }, - createSubmission: async (evaluation_id: string, user_id: string): Promise => { + createSubmission: async (evaluation_id: string, user_id: string): Promise => { const githubHandle = get().githubHandle; if (get().fetching || !githubHandle) { @@ -328,13 +330,9 @@ export const useSubmissionStore = create()((set, get) => ({ } const newSubmission = Submission.init({ - description: "", evaluation_id: evaluation_id, - name: "", user_id: user_id, github_handle: githubHandle, - github_link: "", - links: [], }); const res = await trpc().user.createSubmission.mutate(newSubmission); diff --git a/src/components/voting/VotingStore.tsx b/src/components/voting/VotingStore.tsx index f9f491f..b5e2133 100644 --- a/src/components/voting/VotingStore.tsx +++ b/src/components/voting/VotingStore.tsx @@ -1,6 +1,7 @@ import { create } from "zustand"; -import { Submission, rpc } from "src/lib"; +import { rpc, Submission } from "src/lib"; import { trpc } from "src/lib/trpc"; +import { submission } from "@prisma/client"; function calculateAvailableCredits(votes: SubmissionVotes) { let usedCredits = 0; @@ -17,7 +18,7 @@ export type VotingStore = { votes: SubmissionVotes; evaluator: { id: string; voice_credits: number } | null; evaluation: any | null; - submissions: Submission[]; + submissions: submission[]; expandedSubmissions: { [submissionId: string]: boolean }; availableCredits: number; allocatedCredits: number; @@ -50,7 +51,7 @@ export const useVotingStore = create()((set, get) => ({ console.error(`ERROR -- rpc call getVotingStore failed. evaluation_id: ${evaluation_id}`, data); return; } - + console.log("data", data); data.submissions.sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase())); set({ diff --git a/src/components/voting/VotingTableBody.tsx b/src/components/voting/VotingTableBody.tsx index 20756a1..bda32a8 100644 --- a/src/components/voting/VotingTableBody.tsx +++ b/src/components/voting/VotingTableBody.tsx @@ -10,7 +10,9 @@ type VotingTableBodyProps = { }; export default function VotingTableBody({ idx, project, submissions, search }: VotingTableBodyProps) { - project.fields.sort((a: any, b: any) => a.field_order - b.field_order); + project.submission_field.sort((a: any, b: any) => a.field_order - b.field_order); + + const linksArray = Array.isArray(project.links) ? project.links : []; return (
- {project.fields.map((field: any) => ( -
+ {project.submission_field.map((field: any) => ( +
{field.heading}

{field.field_body}

))}
-
{`Project Link${project.links.length > 1 ? "s" : ""}`}
-
- - {project.links - ? project.links.map((link: any, idx: any) => ( -
- -
- )) - : null} -
+
{linksArray.length > 1 ? "Project Links" : "Project Link"}
+ {linksArray.length > 0 && ( +
+ {project.github_link ? : null} + {linksArray.map((link: any, idx: number) => ( +
+ +
+ ))} +
+ )}
diff --git a/src/lib/UserProfileStore.tsx b/src/lib/UserProfileStore.tsx index b35c250..cff80e0 100644 --- a/src/lib/UserProfileStore.tsx +++ b/src/lib/UserProfileStore.tsx @@ -1,9 +1,10 @@ import { create } from "zustand"; -import { rpc, UserProfile } from "../lib"; +import { rpc } from "../lib"; +import { user } from "@prisma/client"; import { trpc } from "../lib/trpc"; export type UserProfileStore = { - profile?: UserProfile; + profile?: user; login: () => void; logout: () => void; isAdmin: () => boolean; diff --git a/src/lib/types.ts b/src/lib/types.ts index 46fb393..585fec3 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -1,4 +1,5 @@ import { v4 as uuid } from "uuid"; +import { submission, evaluation } from "@prisma/client"; export interface AppError { error: string; @@ -19,22 +20,22 @@ export interface UserProfile { role: string; } -export interface Evaluation { - id: string; - name: string; - status: string; - description: string; - start_time: string | null; - end_time: string | null; - form_description: string; -} +// export interface Evaluation { +// id: string; +// name: string; +// status: string; +// description: string; +// start_time: string | null; +// end_time: string | null; +// form_description: string; +// } -export interface DashboardEvaluation extends Evaluation { +export interface DashboardEvaluation extends evaluation { is_submitted: boolean; } export namespace Evaluation { - export function init(): Evaluation { + export function init(): evaluation { return { id: uuid(), name: "", @@ -66,8 +67,22 @@ export interface Submission { links: any; } -export interface VotingTableBodySubmission extends Submission { - fields: VotingTableBodySubmissionFields[]; +// export interface VotingTableBodySubmission extends Submission { +// submission_field: VotingTableBodySubmissionFields[]; +// } + +// type VotingTableBodySubmissionFields = { +// char_count: number; +// field_body: string; +// field_id: string; +// heading: string; +// placeholder: string; +// subheading: string; +// submission_field_id: string; +// }; + +export interface VotingTableBodySubmission extends submission { + submission_field: VotingTableBodySubmissionFields[]; } type VotingTableBodySubmissionFields = { @@ -88,29 +103,31 @@ export interface RoundStatus { } export namespace Submission { - export function init(params: Omit): Submission { + export function init(params: Partial> & Pick): submission { return { - ...params, + description: "", + name: "", + user_id: null, + github_handle: "", + github_link: "", + links: [], id: uuid(), + is_submitted: false, + contract_id: null, + ...params, }; } } -export interface RoundDetailsData extends Submission { +export interface RoundDetailsData extends submission { user: { github_handle: string }; } - -export interface SubmissionFormInputs { - name: string; +export type SubmissionFormInputs = Omit & { evaluation_field: SubmissionFormFieldInputs[]; - description: string; summary: string; specs: string; - github_link: string; links?: SubmissionFormLinkInputs[]; - githubHandle: string; - user_id?: string; -} +}; export interface SubmissionFormLinkInputs { name: string; diff --git a/src/lib/utils.ts b/src/lib/utils.ts index 472ca3c..e88d28b 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -1,4 +1,4 @@ -export function returnTime(date: string | null) { +export function returnTime(date: Date | null) { if (date === null) { return; } @@ -6,7 +6,7 @@ export function returnTime(date: string | null) { return time.slice(17, 22) + " UTC"; } -export function returnDate(date: string | null) { +export function returnDate(date: Date | null) { if (date === null) { return; } diff --git a/src/server/routes/admin/createEvaluation.ts b/src/server/routes/admin/createEvaluation.ts index 295c531..c86947f 100644 --- a/src/server/routes/admin/createEvaluation.ts +++ b/src/server/routes/admin/createEvaluation.ts @@ -5,12 +5,12 @@ export const createEvaluation = adminProcedure .input( z.object({ id: z.string(), - name: z.string(), + name: z.string().nullable(), status: z.string(), description: z.string(), - start_time: z.string().nullish(), - end_time: z.string().nullish(), - form_description: z.string(), + start_time: z.date().nullish(), + end_time: z.date().nullish(), + form_description: z.string().nullable(), }), ) .mutation(async ({ ctx: { db }, input }) => { diff --git a/src/server/routes/admin/getStatusStore.ts b/src/server/routes/admin/getStatusStore.ts index 26d1b2f..30395e4 100644 --- a/src/server/routes/admin/getStatusStore.ts +++ b/src/server/routes/admin/getStatusStore.ts @@ -1,12 +1,36 @@ import { adminProcedure } from "../../trpc"; -export const getStatusStore = adminProcedure.query(async ({ ctx: { supabase, auth }, input }) => { - const { data, error } = await supabase.rpc("get_status_store"); +export const getStatusStore = adminProcedure.query(async ({ ctx: { db }, input }) => { + try { + const evaluations = await db.evaluation.findMany(); - if (error) { + const results = []; + for (let evaluationItem of evaluations) { + const evaluators = await db.evaluator.findMany({ + where: { + evaluation_id: evaluationItem.id, + user: { + NOT: { + role: "admin", + }, + }, + }, + }); + + const num_evaluators = evaluators.length; + const num_submitted = evaluators.filter((e) => e.is_submitted).length; + + results.push({ + name: evaluationItem.name, + status: evaluationItem.status, + num_evaluators, + num_submitted, + }); + } + + return results; + } catch (error) { console.error(error); - return new Error(`ERROR -- get_status_store failed. message: ${error.message}`); + return new Error(`ERROR -- get_status_store failed.`); } - //TODO typing? - return data as any; }); diff --git a/src/server/routes/admin/getSubmissions.ts b/src/server/routes/admin/getSubmissions.ts index 88ad166..05ff620 100644 --- a/src/server/routes/admin/getSubmissions.ts +++ b/src/server/routes/admin/getSubmissions.ts @@ -1,6 +1,6 @@ -import { Submission } from "src/lib"; import { adminProcedure } from "src/server/trpc"; import { z } from "zod"; +import { submission } from "@prisma/client"; export const getSubmissions = adminProcedure .input( @@ -21,10 +21,10 @@ export const getSubmissions = adminProcedure console.error(error); // return new Error(`ERROR -- get_submissions failed. evaluation_id: ${input.evaluation_id}`); } - + console.log("data", data); const d: any = data; return { - submissions: (d.submissions || []) as Submission[], + submissions: (d.submissions || []) as submission[], }; }); diff --git a/src/server/routes/admin/setEvaluationEndTime.ts b/src/server/routes/admin/setEvaluationEndTime.ts index 5c4cfcb..0f03561 100644 --- a/src/server/routes/admin/setEvaluationEndTime.ts +++ b/src/server/routes/admin/setEvaluationEndTime.ts @@ -12,7 +12,7 @@ export const setEvaluationEndTime = adminProcedure try { await db.evaluation.update({ where: { id: input.id }, - data: { end_time: new Date(input.time) }, + data: { end_time: input.time }, }); } catch (error) { console.error(error); diff --git a/src/server/routes/admin/setEvaluationStartTime.ts b/src/server/routes/admin/setEvaluationStartTime.ts index af3ea76..f555e6f 100644 --- a/src/server/routes/admin/setEvaluationStartTime.ts +++ b/src/server/routes/admin/setEvaluationStartTime.ts @@ -12,7 +12,7 @@ export const setEvaluationStartTime = adminProcedure try { await db.evaluation.update({ where: { id: input.id }, - data: { start_time: new Date(input.time) }, + data: { start_time: input.time }, }); } catch (error) { console.error(error); diff --git a/src/server/routes/user/createSubmission.ts b/src/server/routes/user/createSubmission.ts index 231a820..8b163bc 100644 --- a/src/server/routes/user/createSubmission.ts +++ b/src/server/routes/user/createSubmission.ts @@ -1,63 +1,76 @@ import { isAdmin } from "src/lib/rpc"; import { userProcedure } from "../../trpc"; import { z } from "zod"; +import { v4 as uuid } from "uuid"; export const createSubmission = userProcedure .input( z.object({ id: z.string(), - user_id: z.string().nullish(), + user_id: z.string().optional().nullish(), name: z.string(), - github_link: z.string(), - github_handle: z.string().nullish(), - evaluation_id: z.string().nullish(), + github_link: z.string().optional().nullish(), + github_handle: z.string().optional().nullish(), + evaluation_id: z.string(), description: z.any(), links: z.any(), + is_submitted: z.boolean().optional(), + contract_id: z.string().optional().nullish(), }), ) - .mutation(async ({ ctx: { supabase, auth }, input }) => { + .mutation(async ({ ctx: { db, auth }, input }) => { if (!isAdmin(auth) && input.user_id != auth.user_id) { return new Error(`Unauthorized`); } - const { data, error } = await supabase.rpc("create_submission", input); + try { + const newSubmission = await db.submission.create({ + data: { + id: input.id, + user_id: input.user_id || null, + name: input.name, + github_link: input.github_link, + github_handle: input.github_handle, + evaluation_id: input.evaluation_id, + description: input.description, + links: input.links, + is_submitted: false, + }, - if (error) { + select: { + id: true, + user_id: true, + name: true, + github_link: true, + github_handle: true, + evaluation_id: true, + description: true, + links: true, + is_submitted: true, + }, + }); + + const evaluationFields = await db.evaluation_field.findMany({ + where: { + evaluation_id: input.evaluation_id, + }, + }); + + const submissionFields = await db.submission_field.createMany({ + data: evaluationFields.map((field) => ({ + id: uuid(), + field_body: "", + submission_id: newSubmission.id, + fields_id: field.id, + })), + }); + console.log("submissionFields", submissionFields); + + return { + submission: newSubmission, + fields: submissionFields, + }; + } catch (error) { console.error(error); return new Error(`ERROR -- failed to insert submission.`); } }); -// export const createSubmission = userProcedure -// .input( -// z.object({ -// id: z.string(), -// user_id: z.string().nullish(), -// name: z.string(), -// github_link: z.string(), -// github_handle: z.string(), -// evaluation_id: z.string(), -// description: z.any(), -// links: z.any(), -// }), -// ) -// .mutation(async ({ ctx: { db, auth }, input }) => { -// if (!isAdmin(auth) && input.user_id != auth.user_id) { -// return new Error(`Unauthorized`); -// } -// try { -// await db.submission.create({ -// data: { -// id: input.id, -// user_id: input.user_id || null, -// name: input.name, -// github_link: input.github_link, -// github_handle: input.github_handle, -// evaluation_id: input.evaluation_id, -// description: input.description, -// links: input.links, -// }, -// }); -// } catch (error) { -// console.error(error); -// return new Error(`ERROR -- failed to insert submission.`); -// } -// }); diff --git a/src/server/routes/user/getDashboardStore.ts b/src/server/routes/user/getDashboardStore.ts index d5a8677..ff2db72 100644 --- a/src/server/routes/user/getDashboardStore.ts +++ b/src/server/routes/user/getDashboardStore.ts @@ -12,3 +12,41 @@ export const getDashboardStore = userProcedure.query(async ({ ctx: { supabase, a return (data as any) || []; }); + +// export const getDashboardStore = userProcedure.query(async ({ ctx: { db, auth } }) => { +// try { +// const evaluations = await db.evaluator.findMany({ +// where: { +// user_id: auth.user_id, +// }, +// select: { +// evaluation: { +// select: { +// id: true, +// name: true, +// status: true, +// description: true, +// start_time: true, +// end_time: true, +// form_description: true, +// evaluation_field: true, +// evaluator: true, +// invitation: true, +// submission: true, +// }, +// }, +// is_submitted: true, +// }, +// }); + +// const transformedData = evaluations.map((item) => ({ +// ...item.evaluation, +// is_submitted: item.is_submitted, +// })); + +// return transformedData; +// } catch (error) { +// console.error(error); +// return new Error(`ERROR -- get_dashboard_store failed. user_id: ${auth.user_id}`); +// } +// }); diff --git a/src/server/routes/user/getRoundDetailsStore.ts b/src/server/routes/user/getRoundDetailsStore.ts index ee3373d..de15628 100644 --- a/src/server/routes/user/getRoundDetailsStore.ts +++ b/src/server/routes/user/getRoundDetailsStore.ts @@ -7,16 +7,52 @@ export const getRoundDetailsStore = userProcedure evaluation_id: z.string(), }), ) - .query(async ({ ctx: { supabase, auth }, input }) => { - const { data, error } = await supabase.rpc("get_round_details_store", { - user_id: auth.user_id, - evaluation_id: input.evaluation_id, - }); + .query(async ({ ctx: { db, auth }, input }) => { + try { + const submissions = await db.submission.findMany({ + where: { + evaluation_id: input.evaluation_id, + user_id: auth.user_id, + }, + select: { + id: true, + name: true, + github_link: true, + description: true, + links: true, + github_handle: true, + user_id: true, + is_submitted: true, + contract_id: true, + evaluation_id: true, + submission_field: { + select: { + id: true, + field_body: true, + fields_id: true, + evaluation_field: { + select: { + heading: true, + subheading: true, + char_count: true, + placeholder: true, + }, + }, + }, + }, + }, + }); - if (error) { + const evaluation = await db.evaluation.findUnique({ + where: { id: input.evaluation_id }, + }); + + return { + submissions, + evaluation, + }; + } catch (error) { console.error(error); - return new Error(`ERROR -- getRoundDetailsStore failed. message: ${error.message}`); + throw new Error(`ERROR -- getRoundDetailsStore failed. evaluation_id: ${input.evaluation_id}`); } - - return data as any; }); diff --git a/src/server/routes/user/getUserProfile.ts b/src/server/routes/user/getUserProfile.ts index 50607fd..aee2f19 100644 --- a/src/server/routes/user/getUserProfile.ts +++ b/src/server/routes/user/getUserProfile.ts @@ -3,7 +3,7 @@ import { isAdmin } from "src/lib/rpc"; export const getUserProfile = userProcedure.query(async ({ ctx: { db, auth } }) => { if (!isAdmin(auth)) { - return new Error("Not authorized"); + // return new Error("Not authorized"); } try { @@ -12,12 +12,12 @@ export const getUserProfile = userProcedure.query(async ({ ctx: { db, auth } }) }); if (!user) { - return new Error(`ERROR -- getUserProfile failed. user_id: ${auth.user_id}`); + // return new Error(`ERROR -- getUserProfile failed. user_id: ${auth.user_id}`); } return user; } catch (error) { console.error("An error occurred while fetching the user profile:", error); - return new Error(`ERROR -- getUserProfile failed. user_id: ${auth.user_id}`); + // return new Error(`ERROR -- getUserProfile failed. user_id: ${auth.user_id}`); } }); diff --git a/src/server/routes/user/getVotingStore.ts b/src/server/routes/user/getVotingStore.ts index c213527..6935a43 100644 --- a/src/server/routes/user/getVotingStore.ts +++ b/src/server/routes/user/getVotingStore.ts @@ -2,6 +2,7 @@ import { isAdmin } from "src/lib/rpc"; import { Evaluation, Evaluator, Submission } from "src/lib"; import { userProcedure } from "../../trpc"; import { z } from "zod"; +import { submission, evaluation } from "@prisma/client"; export const getVotingStore = userProcedure .input( @@ -11,7 +12,7 @@ export const getVotingStore = userProcedure ) .query(async ({ ctx: { supabase, auth }, input }) => { if (!isAdmin(auth)) { - return new Error(`Unauthorized`); + // return new Error(`Unauthorized`); } // User has to be evaluator for the evaluation for get_voting_store to work @@ -33,9 +34,84 @@ export const getVotingStore = userProcedure const d: any = data; return { - submissions: d.submissions || [], + submissions: (d.submissions || []) as submission[], evaluator: d.evaluator, - evaluation: d.evaluation, + evaluation: d.evaluation as evaluation, votes: d.votes || {}, }; }); + +// export const getVotingStore = userProcedure +// .input( +// z.object({ +// evaluation_id: z.string(), +// }), +// ) +// .query(async ({ ctx: { db, auth }, input }) => { +// if (!isAdmin(auth)) { +// return new Error(`Unauthorized`); +// } + +// const evaluator = await db.evaluator.findFirst({ +// where: { +// user_id: auth.user_id, +// evaluation_id: input.evaluation_id, +// }, +// select: { +// id: true, +// voice_credits: true, +// }, +// }); + +// if (!evaluator) { +// throw new Error(`User is not assigned as evaluator for evaluation. evaluation_id: ${input.evaluation_id}`); +// } + +// const evaluation = await db.evaluation.findUnique({ +// where: { id: input.evaluation_id }, +// }); + +// const submissions = await db.submission.findMany({ +// where: { evaluation_id: input.evaluation_id }, +// select: { +// submission_field: { +// select: { +// id: true, +// field_body: true, +// fields_id: true, +// evaluation_field: { +// select: { +// heading: true, +// subheading: true, +// char_count: true, +// placeholder: true, +// }, +// }, +// }, +// }, +// }, +// }); + +// const votesArray = await db.votes.findMany({ +// where: { +// evaluator_id: evaluator.id, +// submission: { evaluation_id: input.evaluation_id }, +// }, +// select: { +// submission_id: true, +// votes: true, +// }, +// }); + +// const votes = votesArray.reduce>((acc, vote) => { +// acc[vote.submission_id] = vote.votes; +// return acc; +// }, {}); + +// return { +// submissions, +// evaluator, +// evaluation, +// votes, +// }; +// }); diff --git a/src/server/routes/user/setResetVotes.ts b/src/server/routes/user/setResetVotes.ts index 278cc04..6ebc9f9 100644 --- a/src/server/routes/user/setResetVotes.ts +++ b/src/server/routes/user/setResetVotes.ts @@ -2,24 +2,48 @@ import { isAdmin, getIsUserEvaluator } from "src/lib/rpc"; import { userProcedure } from "src/server/trpc"; import { z } from "zod"; +// export const setResetVotes = userProcedure +// .input( +// z.object({ +// in_evaluator_id: z.string(), +// }), +// ) +// .mutation(async ({ ctx: { supabase, auth }, input }) => { +// const isUserEvaluator = await getIsUserEvaluator(supabase, auth.user_id, input.in_evaluator_id); + +// if (!isAdmin(auth) && !isUserEvaluator) { +// return new Error(`Unauthorized`); +// } + +// const { data, error } = await supabase.rpc("reset", { +// in_evaluator_id: input.in_evaluator_id, +// }); + +// if (error) { +// console.error(error); +// return new Error(`ERROR -- failed to reset votes`); +// } +// }); + export const setResetVotes = userProcedure .input( z.object({ in_evaluator_id: z.string(), }), ) - .mutation(async ({ ctx: { supabase, auth }, input }) => { + .mutation(async ({ ctx: { db, auth, supabase }, input }) => { const isUserEvaluator = await getIsUserEvaluator(supabase, auth.user_id, input.in_evaluator_id); if (!isAdmin(auth) && !isUserEvaluator) { return new Error(`Unauthorized`); } - const { data, error } = await supabase.rpc("reset", { - in_evaluator_id: input.in_evaluator_id, - }); - - if (error) { + try { + await db.votes.updateMany({ + where: { evaluator_id: input.in_evaluator_id }, + data: { votes: 0 }, + }); + } catch (error) { console.error(error); return new Error(`ERROR -- failed to reset votes`); } diff --git a/src/server/routes/user/setVote.ts b/src/server/routes/user/setVote.ts index 75b33ef..712ca17 100644 --- a/src/server/routes/user/setVote.ts +++ b/src/server/routes/user/setVote.ts @@ -2,6 +2,33 @@ import { isAdmin, getIsUserEvaluator } from "src/lib/rpc"; import { userProcedure } from "src/server/trpc"; import { z } from "zod"; +// export const setVote = userProcedure +// .input( +// z.object({ +// in_evaluator_id: z.string(), +// in_submission_id: z.string(), +// vote_count: z.number(), +// }), +// ) +// .mutation(async ({ ctx: { supabase, auth }, input }) => { +// const isUserEvaluator = await getIsUserEvaluator(supabase, auth.user_id, input.in_evaluator_id); + +// if (!isAdmin(auth) && !isUserEvaluator) { +// return new Error(`Unauthorized`); +// } + +// const { error } = await supabase.rpc("upsertvote", { +// in_evaluator_id: input.in_evaluator_id, +// in_submission_id: input.in_submission_id, +// vote_count: input.vote_count, +// }); + +// if (error) { +// console.error(error); +// return new Error(`ERROR -- failed to upsert vote`); +// } +// }); + export const setVote = userProcedure .input( z.object({ @@ -10,20 +37,31 @@ export const setVote = userProcedure vote_count: z.number(), }), ) - .mutation(async ({ ctx: { supabase, auth }, input }) => { + .mutation(async ({ ctx: { db, supabase, auth }, input }) => { const isUserEvaluator = await getIsUserEvaluator(supabase, auth.user_id, input.in_evaluator_id); if (!isAdmin(auth) && !isUserEvaluator) { return new Error(`Unauthorized`); } - const { error } = await supabase.rpc("upsertvote", { - in_evaluator_id: input.in_evaluator_id, - in_submission_id: input.in_submission_id, - vote_count: input.vote_count, - }); - - if (error) { + try { + await db.votes.upsert({ + where: { + evaluator_id_submission_id: { + evaluator_id: input.in_evaluator_id, + submission_id: input.in_submission_id, + }, + }, + create: { + evaluator_id: input.in_evaluator_id, + submission_id: input.in_submission_id, + votes: input.vote_count, + }, + update: { + votes: input.vote_count, + }, + }); + } catch (error) { console.error(error); return new Error(`ERROR -- failed to upsert vote`); }