import checkTokenIsValid from "./checkTokenIsValid"
import getPlayerByIdDB from "./getPlayerByIdDB"
import getTokenDB from "./getTokenDB"
import getTokenFromBody from "./getTokenFromBody"
import { API } from "./sendError"

async function getPlayer<T>(context: API<T>) {
  const accessToken = await getTokenFromBody(context)
  const token = await checkTokenIsValid(context, accessToken)
  const tokenDB = await getTokenDB(context, token)
  const player = await getPlayerByIdDB(context, tokenDB)
  return { player, tokenDB }
}

export default getPlayer
