Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add adminsOnlyMiddleware to procedures to ensure only admins can perform certain actions #78

Merged
merged 1 commit into from
Mar 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/server/api/routers/ticket/ticket-archive.procedure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { sendTicketArchivedEvent } from "@/contracts/events/ticket"
import { db } from "@/database"
import { tickets } from "@/database/schemas"
import waitForPredicate from "@/lib/wait-for-predicate"
import { adminsOnlyMiddleware } from "@/server/api/routers/middlewares/admins-only.middleware"
import { protectedProcedure } from "@/server/api/trpc"

const ArchiveTicketInput = z.object({
Expand All @@ -13,8 +14,8 @@ const ArchiveTicketInput = z.object({

export const archiveTicketProcedure = protectedProcedure
.input(ArchiveTicketInput)
.use(adminsOnlyMiddleware)
.mutation(async ({ input }) => {
// TODO: Check if user is allowed to archive ticket
await sendTicketArchivedEvent({ id: input.id })
try {
await waitForPredicate(
Expand Down
2 changes: 2 additions & 0 deletions src/server/api/routers/ticket/ticket-check-in.procedure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { sendTicketUpdatedEvent } from "@/contracts/events/ticket"
import { db } from "@/database"
import { tickets } from "@/database/schemas"
import waitForPredicate from "@/lib/wait-for-predicate"
import { adminsOnlyMiddleware } from "@/server/api/routers/middlewares/admins-only.middleware"
import { protectedProcedure } from "@/server/api/trpc"

const CheckInTicketInput = z.object({
Expand All @@ -13,6 +14,7 @@ const CheckInTicketInput = z.object({

export const checkInTicketProcedure = protectedProcedure
.input(CheckInTicketInput)
.use(adminsOnlyMiddleware)
.mutation(async ({ input }) => {
await sendTicketUpdatedEvent({ id: input.id, state: "checked-in" })
try {
Expand Down
3 changes: 2 additions & 1 deletion src/server/api/routers/ticket/ticket-create.procedure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { sendTicketCreatedEvent } from "@/contracts/events/ticket"
import { db } from "@/database"
import { tickets } from "@/database/schemas"
import waitForPredicate from "@/lib/wait-for-predicate"
import { adminsOnlyMiddleware } from "@/server/api/routers/middlewares/admins-only.middleware"
import { protectedProcedure } from "@/server/api/trpc"

const CreateTicketInput = z.object({
Expand All @@ -15,8 +16,8 @@ const CreateTicketInput = z.object({

export const createTicketProcedure = protectedProcedure
.input(CreateTicketInput)
.use(adminsOnlyMiddleware)
.mutation(async ({ ctx, input }) => {
// TODO: Check if user is allowed to create ticket
const userId = ctx.user.id
const ids: string[] = []
for (let i = 0; i < input.quantity; i++) {
Expand Down