29 lines
763 B
TypeScript
29 lines
763 B
TypeScript
import { Player } from "@prisma/client"
|
|
import errors from "../errors"
|
|
import prisma from "../../prisma"
|
|
import sendError from "./sendError"
|
|
import { NextApiRequest, NextApiResponse } from "next"
|
|
|
|
export default async function getPlayerByNameDB(
|
|
req: NextApiRequest,
|
|
res: NextApiResponse,
|
|
username: string,
|
|
next: (player: Player) => void
|
|
) {
|
|
// Find Player in DB if it still exists (just to make sure)
|
|
const player = await Promise.any([
|
|
prisma.player.findUniqueOrThrow({
|
|
where: {
|
|
username: username,
|
|
},
|
|
}),
|
|
prisma.player.findUniqueOrThrow({
|
|
where: {
|
|
email: username,
|
|
},
|
|
}),
|
|
]).catch(() => null)
|
|
if (player === null) return sendError(req, res, errors.playerNotFound)
|
|
|
|
return next(player)
|
|
}
|