Files
gridpilot.gg/apps/website/hooks/sponsor/useSponsorshipRequestsPageData.ts
2026-01-07 12:40:52 +01:00

120 lines
4.1 KiB
TypeScript

import { usePageData, usePageMutation } from '@/lib/page/usePageData';
import { useInject } from '@/lib/di/hooks/useInject';
import {
SPONSORSHIP_SERVICE_TOKEN,
DRIVER_SERVICE_TOKEN,
LEAGUE_SERVICE_TOKEN,
TEAM_SERVICE_TOKEN,
LEAGUE_MEMBERSHIP_SERVICE_TOKEN
} from '@/lib/di/tokens';
import { LeagueRoleUtility } from '@/lib/utilities/LeagueRoleUtility';
export function useSponsorshipRequestsPageData(currentDriverId: string | null | undefined) {
const sponsorshipService = useInject(SPONSORSHIP_SERVICE_TOKEN);
const driverService = useInject(DRIVER_SERVICE_TOKEN);
const leagueService = useInject(LEAGUE_SERVICE_TOKEN);
const teamService = useInject(TEAM_SERVICE_TOKEN);
const leagueMembershipService = useInject(LEAGUE_MEMBERSHIP_SERVICE_TOKEN);
const queryResult = usePageData({
queryKey: ['sponsorshipRequests', 'all', currentDriverId || ''],
queryFn: async () => {
if (!currentDriverId) return [];
const allSections: any[] = [];
// 1. Driver's own sponsorship requests
const driverRequests = await sponsorshipService.getPendingSponsorshipRequests({
entityType: 'driver',
entityId: currentDriverId,
});
if (driverRequests.length > 0) {
const driverProfile = await driverService.getDriverProfile(currentDriverId);
allSections.push({
entityType: 'driver',
entityId: currentDriverId,
entityName: driverProfile?.currentDriver?.name ?? 'Your Profile',
requests: driverRequests,
});
}
// 2. Leagues where the user is admin/owner
const allLeagues = await leagueService.getAllLeagues();
for (const league of allLeagues) {
const membership = await leagueMembershipService.getMembership(league.id, currentDriverId);
if (membership && LeagueRoleUtility.isLeagueAdminOrHigherRole(membership.role)) {
try {
const leagueRequests = await sponsorshipService.getPendingSponsorshipRequests({
entityType: 'season',
entityId: league.id,
});
if (leagueRequests.length > 0) {
allSections.push({
entityType: 'season',
entityId: league.id,
entityName: league.name,
requests: leagueRequests,
});
}
} catch (err) {
// Silently skip if no requests found
}
}
}
// 3. Teams where the user is owner/manager
const allTeams = await teamService.getAllTeams();
for (const team of allTeams) {
const membership = await teamService.getMembership(team.id, currentDriverId);
if (membership && (membership.role === 'owner' || membership.role === 'manager')) {
const teamRequests = await sponsorshipService.getPendingSponsorshipRequests({
entityType: 'team',
entityId: team.id,
});
if (teamRequests.length > 0) {
allSections.push({
entityType: 'team',
entityId: team.id,
entityName: team.name,
requests: teamRequests,
});
}
}
}
return allSections;
},
enabled: !!currentDriverId,
});
return queryResult;
}
export function useSponsorshipRequestMutations(currentDriverId: string | null | undefined, refetch: () => void) {
const sponsorshipService = useInject(SPONSORSHIP_SERVICE_TOKEN);
const acceptMutation = usePageMutation(
async ({ requestId }: { requestId: string }) => {
if (!currentDriverId) throw new Error('No driver ID');
await sponsorshipService.acceptSponsorshipRequest(requestId, currentDriverId);
},
{
onSuccess: () => refetch(),
}
);
const rejectMutation = usePageMutation(
async ({ requestId, reason }: { requestId: string; reason?: string }) => {
if (!currentDriverId) throw new Error('No driver ID');
await sponsorshipService.rejectSponsorshipRequest(requestId, currentDriverId, reason);
},
{
onSuccess: () => refetch(),
}
);
return { acceptMutation, rejectMutation };
}