Skip to content

Commit

Permalink
fix: refactor trpc auth guard name + add two more
Browse files Browse the repository at this point in the history
  • Loading branch information
thomhickey committed Oct 7, 2024
1 parent c4607be commit be24b11
Show file tree
Hide file tree
Showing 9 changed files with 126 additions and 58 deletions.
4 changes: 2 additions & 2 deletions src/backend/routers/admin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { sql } from "kysely";
import { adminProcedure, router } from "../trpc";
import { hasAdmin, router } from "../trpc";

export const admin = router({
getPostgresInfo: adminProcedure.query(async (req) => {
getPostgresInfo: hasAdmin.query(async (req) => {
const result = await sql<{ version: string }>`SELECT version()`.execute(
req.ctx.db
);
Expand Down
22 changes: 11 additions & 11 deletions src/backend/routers/case_manager.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from "zod";
import { authenticatedProcedure, router } from "../trpc";
import { hasAuthenticated, router } from "../trpc";
import {
createPara,
assignParaToCaseManager,
Expand All @@ -10,7 +10,7 @@ export const case_manager = router({
/**
* Get all students assigned to the current user
*/
getMyStudents: authenticatedProcedure.query(async (req) => {
getMyStudents: hasAuthenticated.query(async (req) => {
const { userId } = req.ctx.auth;

const result = await req.ctx.db
Expand All @@ -22,7 +22,7 @@ export const case_manager = router({
return result;
}),

getMyStudentsAndIepInfo: authenticatedProcedure.query(async (req) => {
getMyStudentsAndIepInfo: hasAuthenticated.query(async (req) => {
const { userId } = req.ctx.auth;

const studentData = await req.ctx.db
Expand Down Expand Up @@ -50,7 +50,7 @@ export const case_manager = router({
* it doesn't already exist. Throws an error if the student is already
* assigned to another CM.
*/
addStudent: authenticatedProcedure
addStudent: hasAuthenticated
.input(
z.object({
first_name: z.string(),
Expand All @@ -72,7 +72,7 @@ export const case_manager = router({
/**
* Edits the given student in the CM's roster. Throws an error if the student was not found in the db.
*/
editStudent: authenticatedProcedure
editStudent: hasAuthenticated
.input(
z.object({
student_id: z.string(),
Expand Down Expand Up @@ -115,7 +115,7 @@ export const case_manager = router({
/**
* Removes the case manager associated with this student.
*/
removeStudent: authenticatedProcedure
removeStudent: hasAuthenticated
.input(
z.object({
student_id: z.string(),
Expand All @@ -131,7 +131,7 @@ export const case_manager = router({
.execute();
}),

getMyParas: authenticatedProcedure.query(async (req) => {
getMyParas: hasAuthenticated.query(async (req) => {
const { userId } = req.ctx.auth;

const result = await req.ctx.db
Expand All @@ -152,7 +152,7 @@ export const case_manager = router({
* Handles creation of para and assignment to user, attempts to send
* email but does not await email success
*/
addStaff: authenticatedProcedure
addStaff: hasAuthenticated
.input(
z.object({
first_name: z.string(),
Expand Down Expand Up @@ -180,7 +180,7 @@ export const case_manager = router({
/**
* Deprecated: use addStaff instead
*/
addPara: authenticatedProcedure
addPara: hasAuthenticated
.input(
z.object({
para_id: z.string(),
Expand All @@ -195,7 +195,7 @@ export const case_manager = router({
return;
}),

editPara: authenticatedProcedure
editPara: hasAuthenticated
.input(
z.object({
para_id: z.string(),
Expand Down Expand Up @@ -236,7 +236,7 @@ export const case_manager = router({
.executeTakeFirstOrThrow();
}),

removePara: authenticatedProcedure
removePara: hasAuthenticated
.input(
z.object({
para_id: z.string(),
Expand Down
12 changes: 6 additions & 6 deletions src/backend/routers/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ import {
GetObjectCommand,
} from "@aws-sdk/client-s3";
import { getSignedUrl } from "@aws-sdk/s3-request-presigner";
import { authenticatedProcedure, router } from "../trpc";
import { hasAuthenticated, router } from "../trpc";
import { randomUUID } from "crypto";
import { deleteFile } from "../lib/files";

export const file = router({
getMyFiles: authenticatedProcedure.query(async (req) => {
getMyFiles: hasAuthenticated.query(async (req) => {
return req.ctx.db
.selectFrom("file")
.selectAll()
.where("uploaded_by_user_id", "=", req.ctx.auth.userId)
.execute();
}),

getPresignedUrlForFileDownload: authenticatedProcedure
getPresignedUrlForFileDownload: hasAuthenticated
.input(
z.object({
file_id: z.string().uuid(),
Expand Down Expand Up @@ -50,7 +50,7 @@ export const file = router({
};
}),

getPresignedUrlForFileUpload: authenticatedProcedure
getPresignedUrlForFileUpload: hasAuthenticated
.input(
z.object({
type: z.string(),
Expand All @@ -71,7 +71,7 @@ export const file = router({
return { url, key };
}),

finishFileUpload: authenticatedProcedure
finishFileUpload: hasAuthenticated
.input(
z.object({
filename: z.string(),
Expand Down Expand Up @@ -99,7 +99,7 @@ export const file = router({
return file;
}),

deleteFile: authenticatedProcedure
deleteFile: hasAuthenticated
.input(
z.object({
file_id: z.string().uuid(),
Expand Down
36 changes: 18 additions & 18 deletions src/backend/routers/iep.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { z } from "zod";
import { authenticatedProcedure, router } from "../trpc";
import { hasAuthenticated, router } from "../trpc";
import { jsonArrayFrom } from "kysely/helpers/postgres";
import { deleteFile } from "../lib/files";
import { substituteTransactionOnContext } from "../lib/utils/context";
import { TRPCError } from "@trpc/server";

// TODO: define .output() schemas for all procedures
export const iep = router({
addGoal: authenticatedProcedure
addGoal: hasAuthenticated
.input(
z.object({
iep_id: z.string(),
Expand All @@ -31,7 +31,7 @@ export const iep = router({
return result;
}),

editGoal: authenticatedProcedure
editGoal: hasAuthenticated
.input(
z.object({
goal_id: z.string(),
Expand Down Expand Up @@ -70,7 +70,7 @@ export const iep = router({
return result;
}),

addSubgoal: authenticatedProcedure
addSubgoal: hasAuthenticated
.input(
z.object({
// current_level not included, should be calculated as trial data is collected
Expand Down Expand Up @@ -123,7 +123,7 @@ export const iep = router({
return result;
}),

addTask: authenticatedProcedure
addTask: hasAuthenticated
.input(
z.object({
subgoal_id: z.string(),
Expand All @@ -148,7 +148,7 @@ export const iep = router({

return result;
}),
assignTaskToParas: authenticatedProcedure
assignTaskToParas: hasAuthenticated
.input(
z.object({
subgoal_id: z.string().uuid(),
Expand All @@ -175,7 +175,7 @@ export const iep = router({
return result;
}),
//Temporary function to easily assign tasks to self for testing
tempAddTaskToSelf: authenticatedProcedure
tempAddTaskToSelf: hasAuthenticated
.input(
z.object({
subgoal_id: z.string(),
Expand Down Expand Up @@ -217,7 +217,7 @@ export const iep = router({
return result;
}),

addTrialData: authenticatedProcedure
addTrialData: hasAuthenticated
.input(
z.object({
task_id: z.string(),
Expand Down Expand Up @@ -246,7 +246,7 @@ export const iep = router({
return result;
}),

updateTrialData: authenticatedProcedure
updateTrialData: hasAuthenticated
.input(
z.object({
trial_data_id: z.string(),
Expand All @@ -271,7 +271,7 @@ export const iep = router({
.execute();
}),

getGoals: authenticatedProcedure
getGoals: hasAuthenticated
.input(
z.object({
iep_id: z.string(),
Expand All @@ -289,7 +289,7 @@ export const iep = router({
return result;
}),

getGoal: authenticatedProcedure
getGoal: hasAuthenticated
.input(
z.object({
goal_id: z.string(),
Expand All @@ -307,7 +307,7 @@ export const iep = router({
return result;
}),

getSubgoals: authenticatedProcedure
getSubgoals: hasAuthenticated
.input(
z.object({
goal_id: z.string(),
Expand All @@ -325,7 +325,7 @@ export const iep = router({
return result;
}),

getSubgoal: authenticatedProcedure
getSubgoal: hasAuthenticated
.input(
z.object({
subgoal_id: z.string(),
Expand All @@ -342,7 +342,7 @@ export const iep = router({
return result;
}),

getSubgoalsByAssignee: authenticatedProcedure
getSubgoalsByAssignee: hasAuthenticated
.input(
z.object({
assignee_id: z.string(),
Expand All @@ -361,7 +361,7 @@ export const iep = router({
return result;
}),

getSubgoalAndTrialData: authenticatedProcedure
getSubgoalAndTrialData: hasAuthenticated
.input(
z.object({
task_id: z.string(),
Expand Down Expand Up @@ -424,7 +424,7 @@ export const iep = router({
return result;
}),

markAsSeen: authenticatedProcedure
markAsSeen: hasAuthenticated
.input(
z.object({
task_id: z.string(),
Expand All @@ -442,7 +442,7 @@ export const iep = router({
.execute();
}),

attachFileToTrialData: authenticatedProcedure
attachFileToTrialData: hasAuthenticated
.input(
z.object({
trial_data_id: z.string(),
Expand All @@ -461,7 +461,7 @@ export const iep = router({
.execute();
}),

removeFileFromTrialDataAndDelete: authenticatedProcedure
removeFileFromTrialDataAndDelete: hasAuthenticated
.input(
z.object({
trial_data_id: z.string(),
Expand Down
10 changes: 5 additions & 5 deletions src/backend/routers/para.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { z } from "zod";
import { authenticatedProcedure, router } from "../trpc";
import { hasAuthenticated, router } from "../trpc";
import { createPara } from "../lib/db_helpers/case_manager";

export const para = router({
getParaById: authenticatedProcedure
getParaById: hasAuthenticated
.input(z.object({ user_id: z.string().uuid() }))
.query(async (req) => {
const { user_id } = req.input;
Expand All @@ -17,7 +17,7 @@ export const para = router({
return result;
}),

getParaByEmail: authenticatedProcedure
getParaByEmail: hasAuthenticated
.input(z.object({ email: z.string() }))
.query(async (req) => {
const { email } = req.input;
Expand All @@ -34,7 +34,7 @@ export const para = router({
/**
* Deprecated: use case_manager.addStaff instead
*/
createPara: authenticatedProcedure
createPara: hasAuthenticated
.input(
z.object({
first_name: z.string(),
Expand All @@ -61,7 +61,7 @@ export const para = router({
// TODO elsewhere: add "email_verified_at" timestamp when para first signs in with their email address (entered into db by cm)
}),

getMyTasks: authenticatedProcedure.query(async (req) => {
getMyTasks: hasAuthenticated.query(async (req) => {
const { userId } = req.ctx.auth;

const result = await req.ctx.db
Expand Down
4 changes: 2 additions & 2 deletions src/backend/routers/public.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { publicProcedure, router } from "../trpc";
import { noAuth, router } from "../trpc";

export const publicRouter = router({
healthCheck: publicProcedure.query(() => {
healthCheck: noAuth.query(() => {
return "Ok";
}),
});
Loading

0 comments on commit be24b11

Please sign in to comment.