Skip to content

Commit

Permalink
Fix: edit players in existing registration
Browse files Browse the repository at this point in the history
  • Loading branch information
kearfy committed May 14, 2024
1 parent a511f11 commit 001b915
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
26 changes: 20 additions & 6 deletions src/app/[locale]/(public)/e/[event]/registration/[id]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import { Attends, RichAttends } from '@/schema/relations/attends';
import { Event } from '@/schema/resources/event';
import { Organisation } from '@/schema/resources/organisation';
import { linkToProfile } from '@/schema/resources/profile';
import { User } from '@/schema/resources/user';
import { User, UserAsRelatedUser } from '@/schema/resources/user';
import { useMutation, useQuery } from '@tanstack/react-query';
import { useTranslations } from 'next-intl';
import { useParams, useRouter } from 'next/navigation';
Expand Down Expand Up @@ -89,7 +89,7 @@ export default function Page() {
useMutation({
mutationKey: ['registration', 'update-player', slug, regId],
async mutationFn(players: Attends['players']) {
await surreal.merge(`attends:${slug}`, { players });
await surreal.merge(`attends:${regId}`, { players });
await refetch();
},
});
Expand Down Expand Up @@ -120,7 +120,13 @@ export default function Page() {
if (isPending) return <LoaderOverlay />;
if (!data) return <NotFoundScreen text={t('not-found')} />;

const { registration, tournament_path, is_player, can_manage } = data;
const {
registration,
tournament_path,
is_player,
can_manage,
all_team_players,
} = data;
const { out: event, in: registrant, players } = registration;

return (
Expand Down Expand Up @@ -251,7 +257,7 @@ export default function Page() {
{t('details.players.label')}
</h3>
<div className="flex flex-col gap-2">
{players.map((player) => (
{all_team_players.map((player) => (
<div
key={player.id}
className="flex items-center justify-between gap-4"
Expand Down Expand Up @@ -347,7 +353,14 @@ function useData({
throwOnError: true,
queryFn: async () => {
const result = await surreal.query<
[null, RichAttends | null, TournamentPath, boolean, boolean]
[
null,
RichAttends | null,
TournamentPath,
boolean,
boolean,
UserAsRelatedUser[],
]
>(
/* surql */ `
LET $registration = SELECT * FROM ONLY type::thing('attends', $regId)
Expand All @@ -358,6 +371,7 @@ function useData({
SELECT * FROM $registration.out.tournament_path ?? [];
$auth IN $registration.players.id;
$auth IN $registration.out.organiser.managers[?role IN ['owner', 'administrator', 'event_manager']].user;
SELECT * FROM fn::team::compute_eligable_players($registration.in.id, type::thing('event', $slug), true);
`,
{
slug,
Expand All @@ -366,13 +380,13 @@ function useData({
);

if (!result?.[1]) return null;
console.log(result[2]);

return {
registration: RichAttends.parse(result[1]),
tournament_path: TournamentPath.parse(result[2]),
is_player: !!result[3],
can_manage: !!result[4],
all_team_players: z.array(UserAsRelatedUser).parse(result[5]),
};
},
});
Expand Down
3 changes: 2 additions & 1 deletion src/schema/functions/team.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const team = /* surrealql */ `
DEFINE FUNCTION fn::team::compute_eligable_players(
$team: record<team | user> | array<record<user>>,
$event: record<event>,
$ignore_existing_registrations: option<bool>
) {
LET $min_age = $event.options.min_age;
LET $max_age = $event.options.max_age;
Expand All @@ -24,7 +25,7 @@ const team = /* surrealql */ `
-- Lastly, filter out players who previously signed up to this event
LET $players = $players[WHERE !$min_age OR (birthdate AND (duration::years(time::now() - birthdate) >= $min_age))];
LET $players = $players[WHERE !$max_age OR (birthdate AND (duration::years(time::now() - birthdate) <= $max_age))];
LET $players = $players[WHERE !fn::team::find_actor_registration(id, $event)];
LET $players = $players[WHERE $ignore_existing_registrations OR !fn::team::find_actor_registration(id, $event)];
RETURN $players
};
Expand Down

0 comments on commit 001b915

Please sign in to comment.