-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: select a new random person to play
- Loading branch information
Showing
6 changed files
with
143 additions
and
6 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
src/app/api/events/[event]/participants/[participantId]/select-new-random-player/route.ts
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,80 @@ | ||
import { NextRequest, NextResponse } from "next/server" | ||
import { getRandomInteger } from "@/lib" | ||
import { prisma } from "@/prisma" | ||
|
||
type SelectNewRandomPlayerInput = { | ||
params: { | ||
event: string | ||
participantId: string | ||
} | ||
} | ||
export async function POST( | ||
request: NextRequest, | ||
{ params }: SelectNewRandomPlayerInput, | ||
) { | ||
const { participantId, event } = params | ||
// TODO: Validar com Zod | ||
const { groupId }: { groupId: number } = await request.json() | ||
|
||
return prisma.$transaction(async (prisma) => { | ||
const usersThatWannaPlay = await prisma.user.findMany({ | ||
select: { | ||
id: true, | ||
}, | ||
where: { | ||
play: { | ||
every: { | ||
wannaPlay: true, | ||
gonnaPlay: false, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
if (usersThatWannaPlay.length === 0) { | ||
return NextResponse.json( | ||
{ | ||
message: | ||
"Selecione mais de 16 pessoas para participar antes de tentar trocar alguém.", | ||
}, | ||
{ | ||
status: 400, | ||
}, | ||
) | ||
} | ||
|
||
const rnd = getRandomInteger(usersThatWannaPlay.length - 1) | ||
const randomUserId = usersThatWannaPlay[rnd].id | ||
|
||
await prisma.play.update({ | ||
where: { | ||
userId_eventSlug: { | ||
userId: participantId, | ||
eventSlug: event, | ||
}, | ||
}, | ||
data: { | ||
wannaPlay: false, | ||
gonnaPlay: false, | ||
groupId: null, | ||
}, | ||
}) | ||
|
||
await prisma.play.update({ | ||
where: { | ||
userId_eventSlug: { | ||
userId: randomUserId, | ||
eventSlug: event, | ||
}, | ||
}, | ||
data: { | ||
groupId, | ||
gonnaPlay: true, | ||
}, | ||
}) | ||
|
||
return NextResponse.json({ | ||
status: 204, | ||
}) | ||
}) | ||
} |
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
File renamed without changes.
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,2 @@ | ||
export * from "./get-random-integer" | ||
export * from "./utils" |