import prisma from "../../prisma"

export default async function getPlayerByNameDB<T>(
  payload: T & { username: string }
) {
  const { username } = payload
  // Find Player in DB if it still exists (just to make sure)
  const player = await Promise.any([
    prisma.player.findUnique({
      where: {
        username: username,
      },
    }),
    prisma.player.findUnique({
      where: {
        email: username,
      },
    }),
  ])
  if (!player) {
    return Promise.reject({
      message: "Player not found in DB!",
      statusCode: 401,
      solved: false,
    })
  }

  return {
    ...payload,
    player,
  }
}
