way better auth middleware, enroll team server fn and pb fn
This commit is contained in:
@@ -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;
|
||||
}
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user