import { Token } from "@prisma/client"
import prisma from "../../prisma"

export default async function getPlayerByIdDB<T>(
  payload: T & { tokenDB: Token }
) {
  const { tokenDB } = payload
  // Find Host in DB if it still exists (just to make sure)
  const player = await prisma.player.findUnique({
    where: {
      id: tokenDB.ownerId,
    },
  })
  if (!player) {
    return Promise.reject({
      message: "Player not found in DB!",
      statusCode: 401,
      solved: false,
    })
  }

  return {
    ...payload,
    player,
  }
}
