-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of getting match assignments from supabase
mock data (and mock events) are used. Really bad code. Please clean up for me Co-Authored-By: Amaar Chughtai <[email protected]>
- Loading branch information
1 parent
ed581e1
commit 04288b2
Showing
4 changed files
with
155 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { TRPCError } from "@trpc/server"; | ||
import { nanoid } from "nanoid"; | ||
import { z } from "zod"; | ||
|
||
import { desc, eq, schema } from "@acme/db"; | ||
import { matches } from "@acme/db/schema"; | ||
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../trpc"; | ||
|
||
export const scoutingRouter = createTRPCRouter({ | ||
// createMatch: publicProcedure.input(z.object({id:z.string({})})) | ||
// updateMatch: publicProcedure.input() | ||
matchesInEvent: publicProcedure | ||
.input(z.object({ event: z.string() })) | ||
.query(({ ctx, input }) => { | ||
return ctx.db | ||
.select() | ||
.from(matches) | ||
.where(eq(matches.eventId, input.event)); | ||
}), | ||
all: publicProcedure.query(({ ctx }) => { | ||
return ctx.db.query.post.findMany({ | ||
with: { author: true }, | ||
orderBy: desc(schema.post.id), | ||
limit: 10, | ||
}); | ||
}), | ||
|
||
byId: publicProcedure | ||
.input(z.object({ id: z.string() })) | ||
.query(({ ctx, input }) => { | ||
return ctx.db.query.post.findFirst({ | ||
with: { author: true }, | ||
where: eq(schema.post.id, input.id), | ||
}); | ||
}), | ||
|
||
create: protectedProcedure | ||
.input( | ||
z.object({ | ||
title: z.string().min(1), | ||
content: z.string().min(1), | ||
}), | ||
) | ||
.mutation(async ({ ctx, input }) => { | ||
function getNameFromUser() { | ||
const meta = ctx.user.user_metadata; | ||
if (typeof meta.name === "string") return meta.name; | ||
if (typeof meta.full_name === "string") return meta.full_name; | ||
if (typeof meta.user_name === "string") return meta.user_name; | ||
return "[redacted]"; | ||
} | ||
|
||
const authorId = await ctx.db.query.profile | ||
.findFirst({ | ||
where: eq(schema.profile.id, ctx.user.id), | ||
}) | ||
.then(async (profile) => { | ||
if (profile) return profile.id; | ||
const [newProfile] = await ctx.db | ||
.insert(schema.profile) | ||
.values({ | ||
id: ctx.user.id, | ||
name: getNameFromUser(), | ||
image: ctx.user.user_metadata.avatar_url as string | undefined, | ||
email: ctx.user.email, | ||
isLead: false, | ||
isMentor: false, | ||
role: "default", | ||
}) | ||
.returning(); | ||
|
||
return newProfile!.id; | ||
}); | ||
|
||
return ctx.db.insert(schema.post).values({ | ||
id: nanoid(), | ||
authorId, | ||
title: input.title, | ||
content: input.content, | ||
}); | ||
}), | ||
|
||
delete: protectedProcedure | ||
.input(z.string()) | ||
.mutation(async ({ ctx, input }) => { | ||
const post = await ctx.db.query.post.findFirst({ | ||
where: eq(schema.post.id, input), | ||
}); | ||
|
||
if (post?.authorId !== ctx.user.id) { | ||
throw new TRPCError({ | ||
code: "UNAUTHORIZED", | ||
message: "Only the author is allowed to delete the post", | ||
}); | ||
} | ||
|
||
return ctx.db.delete(schema.post).where(eq(schema.post.id, input)); | ||
}), | ||
}); |