Skip to content

Commit

Permalink
converting all db calls from supabase to prisma except for stored pro…
Browse files Browse the repository at this point in the history
…cedure calls
  • Loading branch information
cxjohn committed Aug 22, 2023
1 parent 3c548c8 commit 6bcbf9a
Show file tree
Hide file tree
Showing 31 changed files with 335 additions and 167 deletions.
48 changes: 38 additions & 10 deletions src/server/routes/admin/getEvaluationResult.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,54 @@ export const getEvaluationResult = adminProcedure
evaluation_id: z.string(),
}),
)
.query(async ({ ctx: { supabase, auth }, input }) => {
//TODO is this done right??
.query(async ({ ctx: { db, auth }, input }) => {
input.user_id = input.user_id ? input.user_id : auth.user_id;

if (!isAdmin(auth) && input.user_id != auth.user_id) {
return new Error(`Unauthorized`);
}

const { data, error } = await supabase
.rpc("get_evaluation_result_store", {
evaluation_id: input.evaluation_id,
})
.single();
try {
const evaluation_id = input.evaluation_id;

if (error) {
const votes = await db.votes.findMany({
where: { evaluator: { evaluation_id: evaluation_id } },
select: { evaluator_id: true, submission_id: true, votes: true },
});

const evaluation = await db.evaluation.findUnique({
where: { id: evaluation_id },
});

const submissions = await db.submission.findMany({
where: { evaluation_id },
});

const evaluators = await db.evaluator.findMany({
where: { evaluation_id },
select: {
id: true,
voice_credits: true,
user: {
select: { github_handle: true },
},
},
});

return {
evaluators: evaluators.map((evaluator) => ({
evaluator_id: evaluator.id,
github_handle: evaluator.user.github_handle,
voice_credits: evaluator.voice_credits,
})),
submissions,
votes,
evaluation,
};
} catch (error) {
console.error(error);
return new Error(
`ERROR -- get_evaluation_result_store failed. user_id: ${input.user_id}, evaluation_id: ${input.evaluation_id}`,
);
}

return data as any;
});
29 changes: 29 additions & 0 deletions src/server/routes/admin/getEvaluationStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,32 @@ export const getEvaluationStore = adminProcedure

return (data as any) || [];
});

// export const getEvaluationStore = adminProcedure
// .input(
// z.object({
// evaluation_id: z.string(),
// }),
// )
// .query(async ({ ctx: { db, auth }, input }) => {
// try {
// const data = await db.evaluation.findUnique({
// where: { id: input.evaluation_id },
// include: {
// evaluation_field: {
// include: { submission_field: true },
// },
// submission: true,
// evaluator: {
// include: { user: true },
// },
// invitation: true,
// },
// });

// return data || [];
// } catch (error) {
// console.error(error);
// return new Error(`ERROR -- failed to get Evaluation Store`);
// }
// });
11 changes: 7 additions & 4 deletions src/server/routes/admin/setEmail.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ export const setEmail = adminProcedure
id: z.string(),
}),
)
.mutation(async ({ ctx: { supabase, auth }, input }) => {
const { error } = await supabase.from("user").update({ preferred_email: input.email }).eq("id", input.id);

if (error) {
.mutation(async ({ ctx: { db }, input }) => {
try {
await db.user.update({
where: { id: input.id },
data: { preferred_email: input.email },
});
} catch (error) {
console.error(error);
return new Error(`ERROR -- failed to set user email. user id: ${input.id}`);
}
Expand Down
11 changes: 7 additions & 4 deletions src/server/routes/admin/setEvaluationDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ export const setEvaluationDescription = adminProcedure
description: z.string(),
}),
)
.mutation(async ({ ctx: { supabase, auth }, input }) => {
const { error } = await supabase.from("evaluation").update({ description: input.description }).eq("id", input.id);

if (error) {
.mutation(async ({ ctx: { db }, input }) => {
try {
await db.evaluation.update({
where: { id: input.id },
data: { description: input.description },
});
} catch (error) {
console.error(error);
return new Error(`ERROR -- failed to set evaluation description. evaluation id: ${input.id}`);
}
Expand Down
13 changes: 8 additions & 5 deletions src/server/routes/admin/setEvaluationEndTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ export const setEvaluationEndTime = adminProcedure
time: z.string(),
}),
)
.mutation(async ({ ctx: { supabase, auth }, input }) => {
const { error } = await supabase.from("evaluation").update({ end_time: input.time }).eq("id", input.id);

if (error) {
.mutation(async ({ ctx: { db }, input }) => {
try {
await db.evaluation.update({
where: { id: input.id },
data: { end_time: new Date(input.time) },
});
} catch (error) {
console.error(error);
return new Error(`ERROR -- failed to set evaluation start time. evaluation id: ${input.id}`);
return new Error(`ERROR -- failed to set evaluation end time. evaluation id: ${input.id}`);
}
});
11 changes: 7 additions & 4 deletions src/server/routes/admin/setEvaluationName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ export const setEvaluationName = adminProcedure
name: z.string(),
}),
)
.mutation(async ({ ctx: { supabase, auth }, input }) => {
const { error } = await supabase.from("evaluation").update({ name: input.name }).eq("id", input.id);

if (error) {
.mutation(async ({ ctx: { db }, input }) => {
try {
await db.evaluation.update({
where: { id: input.id },
data: { name: input.name },
});
} catch (error) {
console.error(error);
return new Error(`ERROR -- failed to set evaluation name. evaluation id: ${input.id}`);
}
Expand Down
11 changes: 7 additions & 4 deletions src/server/routes/admin/setEvaluationStartTime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ export const setEvaluationStartTime = adminProcedure
time: z.string(),
}),
)
.mutation(async ({ ctx: { supabase, auth }, input }) => {
const { error } = await supabase.from("evaluation").update({ start_time: input.time }).eq("id", input.id);

if (error) {
.mutation(async ({ ctx: { db }, input }) => {
try {
await db.evaluation.update({
where: { id: input.id },
data: { start_time: new Date(input.time) },
});
} catch (error) {
console.error(error);
return new Error(`ERROR -- failed to set evaluation start time. evaluation id: ${input.id}`);
}
Expand Down
11 changes: 7 additions & 4 deletions src/server/routes/admin/setEvaluationStatus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ export const setEvaluationStatus = adminProcedure
status: z.string(),
}),
)
.mutation(async ({ ctx: { supabase, auth }, input }) => {
const { error } = await supabase.from("evaluation").update({ status: input.status }).eq("id", input.id);

if (error) {
.mutation(async ({ ctx: { db }, input }) => {
try {
await db.evaluation.update({
where: { id: input.id },
data: { status: input.status },
});
} catch (error) {
console.error(error);
return new Error(`ERROR -- failed to set evaluation status. evaluation id: ${input.id}`);
}
Expand Down
13 changes: 7 additions & 6 deletions src/server/routes/admin/setFormDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ export const setFormDescription = adminProcedure
description: z.string(),
}),
)
.mutation(async ({ ctx: { supabase, auth }, input }) => {
const { error } = await supabase
.from("evaluation")
.update({ form_description: input.description })
.eq("id", input.id);
if (error) {
.mutation(async ({ ctx: { db }, input }) => {
try {
await db.evaluation.update({
where: { id: input.id },
data: { form_description: input.description },
});
} catch (error) {
console.error(error);
return new Error(`ERROR -- failed to set form description. evaluation id: ${input.id}`);
}
Expand Down
15 changes: 8 additions & 7 deletions src/server/routes/admin/setFormFieldCharCount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ export const setFormFieldCharCount = adminProcedure
char_count: z.number(),
}),
)
.mutation(async ({ ctx: { supabase, auth }, input }) => {
const { error } = await supabase
.from("evaluation_field")
.update({ char_count: input.char_count })
.eq("id", input.id);
if (error) {
.mutation(async ({ ctx: { db }, input }) => {
try {
await db.evaluation_field.update({
where: { id: input.id },
data: { char_count: input.char_count },
});
} catch (error) {
console.error(error);
return new Error(`ERROR -- failed to set form field character count. evaluation id: ${input.id}`);
return new Error(`ERROR -- failed to set form field character count. evaluation_field id: ${input.id}`);
}
});
13 changes: 8 additions & 5 deletions src/server/routes/admin/setFormFieldHeading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ export const setFormFieldHeading = adminProcedure
heading: z.string(),
}),
)
.mutation(async ({ ctx: { supabase, auth }, input }) => {
const { error } = await supabase.from("evaluation_field").update({ heading: input.heading }).eq("id", input.id);

if (error) {
.mutation(async ({ ctx: { db }, input }) => {
try {
await db.evaluation_field.update({
where: { id: input.id },
data: { heading: input.heading },
});
} catch (error) {
console.error(error);
return new Error(`ERROR -- failed to set form field heading. evaluation id: ${input.id}`);
return new Error(`ERROR -- failed to set form field heading. evaluation_field id: ${input.id}`);
}
});
16 changes: 8 additions & 8 deletions src/server/routes/admin/setFormFieldPlaceholder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ export const setFormFieldPlaceholder = adminProcedure
placeholder: z.string(),
}),
)
.mutation(async ({ ctx: { supabase, auth }, input }) => {
const { error } = await supabase
.from("evaluation_field")
.update({ placeholder: input.placeholder })
.eq("id", input.id);
console.log("hello?", input);
if (error) {
.mutation(async ({ ctx: { db }, input }) => {
try {
await db.evaluation_field.update({
where: { id: input.id },
data: { placeholder: input.placeholder },
});
} catch (error) {
console.error(error);
return new Error(`ERROR -- failed to set form field placeholder. evaluation id: ${input.id}`);
return new Error(`ERROR -- failed to set form field placeholder. evaluation_field id: ${input.id}`);
}
});
16 changes: 8 additions & 8 deletions src/server/routes/admin/setFormFieldSubheading.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ export const setFormFieldSubheading = adminProcedure
subheading: z.string(),
}),
)
.mutation(async ({ ctx: { supabase, auth }, input }) => {
const { error } = await supabase
.from("evaluation_field")
.update({ subheading: input.subheading })
.eq("id", input.id);

if (error) {
.mutation(async ({ ctx: { db }, input }) => {
try {
await db.evaluation_field.update({
where: { id: input.id },
data: { subheading: input.subheading },
});
} catch (error) {
console.error(error);
return new Error(`ERROR -- failed to set form field subheading. evaluation id: ${input.id}`);
return new Error(`ERROR -- failed to set form field subheading. evaluation_field id: ${input.id}`);
}
});
13 changes: 8 additions & 5 deletions src/server/routes/admin/setInvitationCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ export const setInvitationCode = adminProcedure
code: z.string(),
}),
)
.mutation(async ({ ctx: { supabase, auth }, input }) => {
const { error } = await supabase.from("invitation").update({ code: input.code }).eq("id", input.id);

if (error) {
.mutation(async ({ ctx: { db }, input }) => {
try {
await db.invitation.update({
where: { id: input.id },
data: { code: input.code },
});
} catch (error) {
console.error(error);
return new Error(`ERROR -- failed to set inviation code. invitation id: ${input.id}`);
return new Error(`ERROR -- failed to set invitation code. invitation id: ${input.id}`);
}
});
13 changes: 8 additions & 5 deletions src/server/routes/admin/setInvitationCredits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ export const setInvitationCredits = adminProcedure
credits: z.number(),
}),
)
.mutation(async ({ ctx: { supabase, auth }, input }) => {
const { error } = await supabase.from("invitation").update({ voice_credits: input.credits }).eq("id", input.id);

if (error) {
.mutation(async ({ ctx: { db }, input }) => {
try {
await db.invitation.update({
where: { id: input.id },
data: { voice_credits: input.credits },
});
} catch (error) {
console.error(error);
return new Error(`ERROR -- failed to set inviation credits. invitation id: ${input.id}`);
return new Error(`ERROR -- failed to set invitation credits. invitation id: ${input.id}`);
}
});
13 changes: 8 additions & 5 deletions src/server/routes/admin/setInvitationRemainingUses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ export const setInvitationRemainingUses = adminProcedure
uses: z.number(),
}),
)
.mutation(async ({ ctx: { supabase, auth }, input }) => {
const { error } = await supabase.from("invitation").update({ remaining_uses: input.uses }).eq("id", input.id);

if (error) {
.mutation(async ({ ctx: { db }, input }) => {
try {
await db.invitation.update({
where: { id: input.id },
data: { remaining_uses: input.uses },
});
} catch (error) {
console.error(error);
return new Error(`ERROR -- failed to set inviation remaining uses. invitation id: ${input.id}`);
return new Error(`ERROR -- failed to set invitation remaining uses. invitation id: ${input.id}`);
}
});
11 changes: 7 additions & 4 deletions src/server/routes/admin/setInvitationSubmissionRequired.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ export const setInvitationSubmissionRequired = adminProcedure
is_sme: z.boolean(),
}),
)
.mutation(async ({ ctx: { supabase, auth }, input }) => {
const { error } = await supabase.from("invitation").update({ is_sme: input.is_sme }).eq("id", input.id);

if (error) {
.mutation(async ({ ctx: { db }, input }) => {
try {
await db.invitation.update({
where: { id: input.id },
data: { is_sme: input.is_sme },
});
} catch (error) {
console.error(error);
return new Error(`ERROR -- failed to set invitation submission required. invitation id: ${input.id}`);
}
Expand Down
Loading

0 comments on commit 6bcbf9a

Please sign in to comment.