Skip to content

Commit b0769ce

Browse files
Merge pull request #127 from frankhereford/query-on-userId
Query-on-userId
2 parents d43e440 + bdd767a commit b0769ce

File tree

6 files changed

+74
-29
lines changed

6 files changed

+74
-29
lines changed

src/server/trpc/router/availablePenta.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ export const availablePentaRouter = router({
5252

5353
}),
5454

55+
5556
count: publicProcedure.query(async ({ ctx }) => {
5657
const pentas = await ctx.prisma.availablePenta.findMany();
5758
return pentas.length

src/server/trpc/router/block.ts

Lines changed: 51 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,25 @@
1-
import { router, publicProcedure, protectedProcedure } from "../trpc";
1+
import { router, protectedProcedure } from "../trpc";
22
import { z } from "zod";
33

4+
import { isBlockOwner } from "../../../utils/database";
5+
46
export const blockRouter = router({
5-
list: protectedProcedure.query(async ({ ctx, input }) => {
6-
return await ctx.prisma.block.findMany({ // this should filter on user id - any many other places!
7-
include: {
8-
piece: {
9-
include: {
10-
color: true
11-
}
12-
}
13-
}
14-
});
15-
}),
16-
17-
get: protectedProcedure
18-
.input(z.object({ id: z.string() }))
19-
.query(async ({ ctx, input }) => {
20-
const block = await ctx.prisma.block.findUnique({
21-
where: {
22-
id: input.id
23-
}
24-
});
25-
return block;
26-
}),
277

288
set_rotation: protectedProcedure
299
.input(z.object({ id: z.string(), clockwise: z.number() }))
3010
.mutation(async ({ ctx, input }) => {
11+
12+
const blockOriginal = await ctx.prisma.block.findUnique({
13+
where: {
14+
id: input.id
15+
},
16+
include: {
17+
penta: true
18+
}
19+
})
20+
21+
if (!isBlockOwner(blockOriginal, ctx.session.user.id)) { return false }
22+
3123
const block = await ctx.prisma.block.update({
3224
where: {
3325
id: input.id
@@ -44,6 +36,18 @@ export const blockRouter = router({
4436
set_reflection: protectedProcedure
4537
.input(z.object({ id: z.string(), reflection: z.boolean() }))
4638
.mutation(async ({ ctx, input }) => {
39+
40+
const blockOriginal = await ctx.prisma.block.findUnique({
41+
where: {
42+
id: input.id
43+
},
44+
include: {
45+
penta: true
46+
}
47+
})
48+
49+
if (!isBlockOwner(blockOriginal, ctx.session.user.id)) { return false }
50+
4751
const block = await ctx.prisma.block.update({
4852
where: {
4953
id: input.id
@@ -58,6 +62,18 @@ export const blockRouter = router({
5862
set_translation: protectedProcedure
5963
.input(z.object({ id: z.string(), translation: z.any() }))
6064
.mutation(async ({ ctx, input }) => {
65+
66+
const blockOriginal = await ctx.prisma.block.findUnique({
67+
where: {
68+
id: input.id
69+
},
70+
include: {
71+
penta: true
72+
}
73+
})
74+
75+
if (!isBlockOwner(blockOriginal, ctx.session.user.id)) { return false }
76+
6177
const block = await ctx.prisma.block.update({
6278
where: {
6379
id: input.id
@@ -72,6 +88,18 @@ export const blockRouter = router({
7288
set_visibility: protectedProcedure
7389
.input(z.object({ id: z.string(), visible: z.boolean() }))
7490
.mutation(async ({ ctx, input }) => {
91+
92+
const blockOriginal = await ctx.prisma.block.findUnique({
93+
where: {
94+
id: input.id
95+
},
96+
include: {
97+
penta: true
98+
}
99+
})
100+
101+
if (!isBlockOwner(blockOriginal, ctx.session.user.id)) { return false }
102+
75103
const block = await ctx.prisma.block.update({
76104
where: {
77105
id: input.id

src/server/trpc/router/color.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
1-
import { Color } from '@prisma/client'
1+
import { type Color } from '@prisma/client'
22
import { router, publicProcedure } from "../trpc";
33

44
export const colorRouter = router({
55

66
randomColor: publicProcedure
7-
//.input(z.object({ text: z.string().nullish() }).nullish())
87
.query(async ({ ctx }) => {
98
const randomColors = await ctx.prisma.$queryRaw<Color[]>`SELECT * FROM colors order by random() limit 1;`
109
return randomColors[0]
1110
}),
1211

1312
getColorLookup: publicProcedure
14-
//.input(z.object({ text: z.string().nullish() }).nullish())
1513
.query(async ({ ctx }) => {
1614
const colors = await ctx.prisma.color.findMany()
1715

src/server/trpc/router/penta.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
import { z } from "zod";
22
import { router, protectedProcedure } from "../trpc";
3+
import { isPentaOwner } from "../../../utils/database";
34

45
export const pentaRouter = router({
56

67
setComplete: protectedProcedure
78
.input(z.object({ id: z.string() }))
89
.mutation(async ({ ctx, input }) => {
9-
const penta = await ctx.prisma.penta.update({
10+
11+
const pentaOriginal = await ctx.prisma.penta.findUnique({
1012
where: {
1113
id: input.id
1214
},
15+
})
16+
17+
if (!isPentaOwner(pentaOriginal, ctx.session.user.id)) { return false }
18+
19+
const penta = await ctx.prisma.penta.update({
20+
where: {
21+
id: input.id,
22+
},
1323
data: {
1424
completed: true
1525
}
@@ -25,7 +35,8 @@ export const pentaRouter = router({
2535
availablePenta: true
2636
},
2737
where: {
28-
completed: true
38+
completed: true,
39+
userId: ctx.session.user.id
2940
}
3041
});
3142
return pentas.map((penta) => penta.availablePenta.id) || []

src/server/trpc/router/piece.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import { router, publicProcedure } from "../trpc";
44
export const pieceRouter = router({
55

66
randomPiece: publicProcedure
7-
//.input(z.object({ text: z.string().nullish() }).nullish())
87
.query(async ({ ctx }) => {
98
const pieces = await ctx.prisma.piece.findMany({ include: { color: true } })
109
// select a random piece from pieces array

src/utils/database.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
export function isBlockOwner(block: any, userId: string) {
3+
return block.penta.userId === userId;
4+
}
5+
6+
export function isPentaOwner(penta: any, userId: string) {
7+
return penta.userId === userId;
8+
}

0 commit comments

Comments
 (0)