leaky-ships/leaky-ships/lib/backend/components/getPlayerByNameDB.ts
2023-04-11 20:07:27 +02:00

27 lines
718 B
TypeScript

import prisma from "../../prisma"
import { rejectionErrors } from "../errors"
import sendError, { API } from "./sendError"
import type { Player } from "@prisma/client"
export default async function getPlayerByNameDB<T>(
context: API<T>,
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(context, rejectionErrors.playerNotFound)
return next(player)
}