way better auth middleware, enroll team server fn and pb fn

This commit is contained in:
yohlo
2025-08-24 00:10:09 -05:00
parent 4427bd5494
commit 53276cc18e
6 changed files with 164 additions and 70 deletions

View File

@@ -41,3 +41,32 @@ export const getTournament = createServerFn()
const tournament = await pbAdmin.getTournament(tournamentId);
return tournament;
});
export const enrollTeam = createServerFn()
.validator(z.object({
tournamentId: z.string(),
teamId: z.string()
}))
.middleware([superTokensFunctionMiddleware])
.handler(async ({ data: { tournamentId, teamId }, context }) => {
try {
const userId = context.userAuthId;
const isAdmin = context.roles.includes("Admin");
const team = await pbAdmin.getTeam(teamId);
if (!team) { throw new Error('Team not found'); }
const isPlayerOnTeam = team.players?.some(player => player.id === userId);
if (!isPlayerOnTeam && !isAdmin) {
throw new Error('You do not have permission to enroll this team');
}
logger.info('Enrolling team in tournament', { tournamentId, teamId, userId });
const tournament = await pbAdmin.enrollTeam(tournamentId, teamId);
return tournament;
} catch (error) {
logger.error('Error enrolling team', error);
throw error;
}
});