import { ApiMethods, ApiRoutes } from '../../model/api'; import { RingProps } from '../../model/components/ring'; import { RingParams, Ring } from '../../model/rings'; import authenticateRequest from '../api/authenticateRequest'; import checkAccess from './checkAccess'; import withUserSSR from './withSSRUser'; const getServerSidePropsRing = withUserSSR( async (ctx, user) => { const { id } = ctx.params; const ring = await authenticateRequest( ApiMethods.Get, `${ApiRoutes.GetRings}/${id}`, ); const { url } = ctx.req; const { data } = ring; if (!data) { return { redirect: { destination: '/', permanent: false, }, }; } if ( !data.training || !data.trivial || !data.closure || (!data.clinicCase && !data.challenge) ) { return { redirect: { destination: `/ringError/${data.title}`, permanent: false, }, }; } const access = checkAccess(user, data, url); if (!access) { return { redirect: { destination: `/ring/${data.id}`, permanent: false, }, }; } return { props: { ring: data, user, }, }; }, ); export default getServerSidePropsRing;