api client refactor
This commit is contained in:
9
apps/website/lib/services/analytics/AnalyticsService.ts
Normal file
9
apps/website/lib/services/analytics/AnalyticsService.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { api as api } from '../../api';
|
||||
|
||||
export async function recordPageView(input: any): Promise<any> {
|
||||
return await api.analytics.recordPageView(input);
|
||||
}
|
||||
|
||||
export async function recordEngagement(input: any): Promise<any> {
|
||||
return await api.analytics.recordEngagement(input);
|
||||
}
|
||||
6
apps/website/lib/services/analytics/DashboardService.ts
Normal file
6
apps/website/lib/services/analytics/DashboardService.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { api as api } from '../../api';
|
||||
|
||||
export async function getDashboardOverview(): Promise<any> {
|
||||
// TODO: aggregate data
|
||||
return {};
|
||||
}
|
||||
17
apps/website/lib/services/auth/AuthService.ts
Normal file
17
apps/website/lib/services/auth/AuthService.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { api as api } from '../../api';
|
||||
|
||||
export async function signup(params: any): Promise<any> {
|
||||
return await api.auth.signup(params);
|
||||
}
|
||||
|
||||
export async function login(params: any): Promise<any> {
|
||||
return await api.auth.login(params);
|
||||
}
|
||||
|
||||
export async function logout(): Promise<void> {
|
||||
await api.auth.logout();
|
||||
}
|
||||
|
||||
export function getIracingAuthUrl(returnTo?: string): string {
|
||||
return api.auth.getIracingAuthUrl(returnTo);
|
||||
}
|
||||
9
apps/website/lib/services/auth/SessionService.ts
Normal file
9
apps/website/lib/services/auth/SessionService.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { api as api } from '../../api';
|
||||
import { SessionViewModel } from '../../view-models';
|
||||
|
||||
export async function getSession(): Promise<SessionViewModel | null> {
|
||||
const dto = await api.auth.getSession();
|
||||
if (!dto) return null;
|
||||
// TODO: presenter
|
||||
return dto as any;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import { api as api } from '../../api';
|
||||
import { presentDriverRegistrationStatus } from '../../presenters';
|
||||
import { DriverRegistrationStatusViewModel } from '../../view-models';
|
||||
|
||||
export async function getDriverRegistrationStatus(driverId: string): Promise<DriverRegistrationStatusViewModel> {
|
||||
// TODO: implement API call
|
||||
const dto = { driverId, status: 'pending' };
|
||||
return presentDriverRegistrationStatus(dto);
|
||||
}
|
||||
|
||||
export async function registerDriver(input: any): Promise<any> {
|
||||
// TODO: implement
|
||||
return {};
|
||||
}
|
||||
16
apps/website/lib/services/drivers/DriverService.ts
Normal file
16
apps/website/lib/services/drivers/DriverService.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { api as api } from '../../api';
|
||||
import { presentDriversLeaderboard } from '../../presenters';
|
||||
import { DriverLeaderboardViewModel } from '../../view-models';
|
||||
|
||||
export async function getDriverLeaderboard(): Promise<DriverLeaderboardViewModel> {
|
||||
const dto = await api.drivers.getLeaderboard();
|
||||
return presentDriversLeaderboard(dto);
|
||||
}
|
||||
|
||||
export async function completeDriverOnboarding(input: any): Promise<any> {
|
||||
return await api.drivers.completeOnboarding(input);
|
||||
}
|
||||
|
||||
export async function getCurrentDriver(): Promise<any> {
|
||||
return await api.drivers.getCurrent();
|
||||
}
|
||||
36
apps/website/lib/services/index.ts
Normal file
36
apps/website/lib/services/index.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
// Analytics Services
|
||||
export * from './analytics/AnalyticsService';
|
||||
export * from './analytics/DashboardService';
|
||||
|
||||
// Auth Services
|
||||
export * from './auth/AuthService';
|
||||
export * from './auth/SessionService';
|
||||
|
||||
// Driver Services
|
||||
export * from './drivers/DriverService';
|
||||
export * from './drivers/DriverRegistrationService';
|
||||
|
||||
// League Services
|
||||
export * from './leagues/LeagueService';
|
||||
export * from './leagues/LeagueMembershipService';
|
||||
|
||||
// Media Services
|
||||
export * from './media/MediaService';
|
||||
export * from './media/AvatarService';
|
||||
|
||||
// Payments Services
|
||||
export * from './payments/PaymentService';
|
||||
export * from './payments/WalletService';
|
||||
export * from './payments/MembershipFeeService';
|
||||
|
||||
// Race Services
|
||||
export * from './races/RaceService';
|
||||
export * from './races/RaceResultsService';
|
||||
|
||||
// Sponsor Services
|
||||
export * from './sponsors/SponsorService';
|
||||
export * from './sponsors/SponsorshipService';
|
||||
|
||||
// Team Services
|
||||
export * from './teams/TeamService';
|
||||
export * from './teams/TeamJoinService';
|
||||
12
apps/website/lib/services/leagues/LeagueMembershipService.ts
Normal file
12
apps/website/lib/services/leagues/LeagueMembershipService.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { api as api } from '../../api';
|
||||
import { presentLeagueMember } from '../../presenters';
|
||||
import { LeagueMemberViewModel } from '../../view-models';
|
||||
|
||||
export async function getLeagueMemberships(leagueId: string, currentUserId: string): Promise<LeagueMemberViewModel[]> {
|
||||
const dto = await api.leagues.getMemberships(leagueId);
|
||||
return dto.members.map(m => presentLeagueMember(m, currentUserId));
|
||||
}
|
||||
|
||||
export async function removeLeagueMember(leagueId: string, performerDriverId: string, targetDriverId: string): Promise<void> {
|
||||
await api.leagues.removeMember(leagueId, performerDriverId, targetDriverId);
|
||||
}
|
||||
28
apps/website/lib/services/leagues/LeagueService.ts
Normal file
28
apps/website/lib/services/leagues/LeagueService.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { api as api } from '../../api';
|
||||
import { presentLeagueSummaries, presentLeagueStandings } from '../../presenters';
|
||||
import { LeagueSummaryViewModel, LeagueStandingsViewModel } from '../../view-models';
|
||||
|
||||
export async function getAllLeagues(): Promise<LeagueSummaryViewModel[]> {
|
||||
const dto = await api.leagues.getAllWithCapacity();
|
||||
return presentLeagueSummaries(dto.leagues);
|
||||
}
|
||||
|
||||
export async function getLeagueStandings(leagueId: string, currentUserId?: string): Promise<LeagueStandingsViewModel> {
|
||||
const dto = await api.leagues.getStandings(leagueId);
|
||||
// TODO: include drivers and memberships in dto
|
||||
const dtoWithExtras = {
|
||||
...dto,
|
||||
drivers: [], // TODO: fetch drivers
|
||||
memberships: [], // TODO: fetch memberships
|
||||
};
|
||||
return presentLeagueStandings(dtoWithExtras, currentUserId || '');
|
||||
}
|
||||
|
||||
export async function createLeague(input: any): Promise<any> {
|
||||
return await api.leagues.create(input);
|
||||
}
|
||||
|
||||
export async function getLeagueAdminView(leagueId: string): Promise<any> {
|
||||
// TODO: implement
|
||||
return {};
|
||||
}
|
||||
5
apps/website/lib/services/media/AvatarService.ts
Normal file
5
apps/website/lib/services/media/AvatarService.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { api as api } from '../../api';
|
||||
|
||||
export async function requestAvatarGeneration(input: any): Promise<any> {
|
||||
return await api.media.requestAvatarGeneration(input);
|
||||
}
|
||||
6
apps/website/lib/services/media/MediaService.ts
Normal file
6
apps/website/lib/services/media/MediaService.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { api as api } from '../../api';
|
||||
|
||||
export async function uploadMedia(file: any): Promise<any> {
|
||||
// TODO: implement
|
||||
return {};
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import { api as api } from '../../api';
|
||||
import { presentMembershipFee } from '../../presenters';
|
||||
import { MembershipFeeViewModel } from '../../view-models';
|
||||
|
||||
export async function getMembershipFees(leagueId: string): Promise<MembershipFeeViewModel[]> {
|
||||
const dto = await api.payments.getMembershipFees(leagueId);
|
||||
return dto.fees.map(f => presentMembershipFee(f));
|
||||
}
|
||||
12
apps/website/lib/services/payments/PaymentService.ts
Normal file
12
apps/website/lib/services/payments/PaymentService.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { api as api } from '../../api';
|
||||
import { presentPayment } from '../../presenters';
|
||||
import { PaymentViewModel } from '../../view-models';
|
||||
|
||||
export async function getPayments(leagueId?: string, driverId?: string): Promise<PaymentViewModel[]> {
|
||||
const dto = await api.payments.getPayments(leagueId, driverId);
|
||||
return dto.payments.map(p => presentPayment(p));
|
||||
}
|
||||
|
||||
export async function createPayment(input: any): Promise<any> {
|
||||
return await api.payments.createPayment(input);
|
||||
}
|
||||
8
apps/website/lib/services/payments/WalletService.ts
Normal file
8
apps/website/lib/services/payments/WalletService.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import { api as api } from '../../api';
|
||||
import { presentWallet } from '../../presenters';
|
||||
import { WalletViewModel } from '../../view-models';
|
||||
|
||||
export async function getWallet(driverId: string): Promise<WalletViewModel> {
|
||||
const dto = await api.payments.getWallet(driverId);
|
||||
return presentWallet(dto);
|
||||
}
|
||||
121
apps/website/lib/services/races/RaceResultsService.test.ts
Normal file
121
apps/website/lib/services/races/RaceResultsService.test.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
||||
import { getRaceResults, getRaceSOF, importRaceResults } from './RaceResultsService';
|
||||
import type { RaceResultsDetailDto, RaceWithSOFDto, ImportRaceResultsSummaryDto } from '../../dtos';
|
||||
|
||||
// Mock the API client
|
||||
vi.mock('../../api', () => ({
|
||||
apiClient: {
|
||||
races: {
|
||||
getResultsDetail: vi.fn(),
|
||||
getWithSOF: vi.fn(),
|
||||
importResults: vi.fn(),
|
||||
},
|
||||
},
|
||||
}));
|
||||
|
||||
// Mock the presenter
|
||||
vi.mock('../../presenters', () => ({
|
||||
presentRaceResultsDetail: vi.fn(),
|
||||
}));
|
||||
|
||||
import { api } from '../../api';
|
||||
import { presentRaceResultsDetail } from '../../presenters';
|
||||
|
||||
describe('RaceResultsService', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('getRaceResults', () => {
|
||||
it('should call API and presenter with correct parameters', async () => {
|
||||
const mockDto: RaceResultsDetailDto = {
|
||||
id: 'race-1',
|
||||
name: 'Test Race',
|
||||
results: [],
|
||||
// ... other required fields
|
||||
} as RaceResultsDetailDto;
|
||||
|
||||
const mockViewModel = {
|
||||
id: 'race-1',
|
||||
name: 'Test Race',
|
||||
formattedResults: [],
|
||||
};
|
||||
|
||||
const raceId = 'race-123';
|
||||
const currentUserId = 'user-456';
|
||||
|
||||
// Mock API call
|
||||
vi.mocked(api.races.getResultsDetail).mockResolvedValue(mockDto);
|
||||
// Mock presenter
|
||||
vi.mocked(presentRaceResultsDetail).mockReturnValue(mockViewModel);
|
||||
|
||||
const result = await getRaceResults(raceId, currentUserId);
|
||||
|
||||
expect(api.races.getResultsDetail).toHaveBeenCalledWith(raceId);
|
||||
expect(presentRaceResultsDetail).toHaveBeenCalledWith(mockDto, currentUserId);
|
||||
expect(result).toBe(mockViewModel);
|
||||
});
|
||||
|
||||
it('should call presenter with undefined currentUserId when not provided', async () => {
|
||||
const mockDto: RaceResultsDetailDto = {
|
||||
id: 'race-1',
|
||||
name: 'Test Race',
|
||||
results: [],
|
||||
} as RaceResultsDetailDto;
|
||||
|
||||
const mockViewModel = {
|
||||
id: 'race-1',
|
||||
name: 'Test Race',
|
||||
formattedResults: [],
|
||||
};
|
||||
|
||||
const raceId = 'race-123';
|
||||
|
||||
vi.mocked(api.races.getResultsDetail).mockResolvedValue(mockDto);
|
||||
vi.mocked(presentRaceResultsDetail).mockReturnValue(mockViewModel);
|
||||
|
||||
await getRaceResults(raceId);
|
||||
|
||||
expect(presentRaceResultsDetail).toHaveBeenCalledWith(mockDto, undefined);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getRaceSOF', () => {
|
||||
it('should call API and return DTO directly', async () => {
|
||||
const mockDto: RaceWithSOFDto = {
|
||||
id: 'race-1',
|
||||
name: 'Test Race',
|
||||
sof: 1500,
|
||||
// ... other fields
|
||||
} as RaceWithSOFDto;
|
||||
|
||||
const raceId = 'race-123';
|
||||
|
||||
vi.mocked(api.races.getWithSOF).mockResolvedValue(mockDto);
|
||||
|
||||
const result = await getRaceSOF(raceId);
|
||||
|
||||
expect(api.races.getWithSOF).toHaveBeenCalledWith(raceId);
|
||||
expect(result).toBe(mockDto);
|
||||
});
|
||||
});
|
||||
|
||||
describe('importRaceResults', () => {
|
||||
it('should call API with correct parameters and return result', async () => {
|
||||
const mockInput = { results: [] };
|
||||
const mockSummary: ImportRaceResultsSummaryDto = {
|
||||
totalImported: 10,
|
||||
errors: [],
|
||||
};
|
||||
|
||||
const raceId = 'race-123';
|
||||
|
||||
vi.mocked(api.races.importResults).mockResolvedValue(mockSummary);
|
||||
|
||||
const result = await importRaceResults(raceId, mockInput);
|
||||
|
||||
expect(api.races.importResults).toHaveBeenCalledWith(raceId, mockInput);
|
||||
expect(result).toBe(mockSummary);
|
||||
});
|
||||
});
|
||||
});
|
||||
23
apps/website/lib/services/races/RaceResultsService.ts
Normal file
23
apps/website/lib/services/races/RaceResultsService.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { api as api } from '../../api';
|
||||
import { presentRaceResultsDetail } from '../../presenters';
|
||||
import { RaceResultsDetailViewModel } from '../../view-models';
|
||||
|
||||
export async function getRaceResults(
|
||||
raceId: string,
|
||||
currentUserId?: string
|
||||
): Promise<RaceResultsDetailViewModel> {
|
||||
const dto = await api.races.getResultsDetail(raceId);
|
||||
return presentRaceResultsDetail(dto, currentUserId);
|
||||
}
|
||||
|
||||
export async function getRaceSOF(raceId: string): Promise<any> {
|
||||
const dto = await api.races.getWithSOF(raceId);
|
||||
// TODO: use presenter
|
||||
return dto;
|
||||
}
|
||||
|
||||
export async function importRaceResults(raceId: string, input: any): Promise<any> {
|
||||
const dto = await api.races.importResults(raceId, input);
|
||||
// TODO: use presenter
|
||||
return dto;
|
||||
}
|
||||
22
apps/website/lib/services/races/RaceService.ts
Normal file
22
apps/website/lib/services/races/RaceService.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { api as api } from '../../api';
|
||||
import { presentRaceDetail } from '../../presenters';
|
||||
import { RaceDetailViewModel } from '../../view-models';
|
||||
|
||||
export async function getRaceDetail(
|
||||
raceId: string,
|
||||
driverId: string
|
||||
): Promise<RaceDetailViewModel> {
|
||||
const dto = await api.races.getDetail(raceId, driverId);
|
||||
return presentRaceDetail(dto);
|
||||
}
|
||||
|
||||
export async function getRacesPageData(): Promise<any> {
|
||||
const dto = await api.races.getPageData();
|
||||
// TODO: use presenter
|
||||
return dto;
|
||||
}
|
||||
|
||||
export async function getRacesTotal(): Promise<any> {
|
||||
const dto = await api.races.getTotal();
|
||||
return dto;
|
||||
}
|
||||
17
apps/website/lib/services/sponsors/SponsorService.ts
Normal file
17
apps/website/lib/services/sponsors/SponsorService.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
import { api as api } from '../../api';
|
||||
import { presentSponsor } from '../../presenters';
|
||||
import { SponsorViewModel } from '../../view-models';
|
||||
|
||||
export async function getAllSponsors(): Promise<SponsorViewModel[]> {
|
||||
const dto = await api.sponsors.getAll();
|
||||
return dto.sponsors.map(s => presentSponsor(s));
|
||||
}
|
||||
|
||||
export async function createSponsor(input: any): Promise<any> {
|
||||
return await api.sponsors.create(input);
|
||||
}
|
||||
|
||||
export async function getSponsorDashboard(sponsorId: string): Promise<any> {
|
||||
const dto = await api.sponsors.getDashboard(sponsorId);
|
||||
return dto;
|
||||
}
|
||||
11
apps/website/lib/services/sponsors/SponsorshipService.ts
Normal file
11
apps/website/lib/services/sponsors/SponsorshipService.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { api as api } from '../../api';
|
||||
|
||||
export async function getSponsorshipPricing(): Promise<any> {
|
||||
const dto = await api.sponsors.getPricing();
|
||||
return dto;
|
||||
}
|
||||
|
||||
export async function getSponsorSponsorships(sponsorId: string): Promise<any> {
|
||||
const dto = await api.sponsors.getSponsorships(sponsorId);
|
||||
return dto;
|
||||
}
|
||||
16
apps/website/lib/services/teams/TeamJoinService.ts
Normal file
16
apps/website/lib/services/teams/TeamJoinService.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { api as api } from '../../api';
|
||||
import { presentTeamJoinRequest } from '../../presenters';
|
||||
import { TeamJoinRequestViewModel } from '../../view-models';
|
||||
|
||||
export async function getTeamJoinRequests(teamId: string, currentUserId: string, isOwner: boolean): Promise<TeamJoinRequestViewModel[]> {
|
||||
const dto = await api.teams.getJoinRequests(teamId);
|
||||
return dto.requests.map(r => presentTeamJoinRequest(r, currentUserId, isOwner));
|
||||
}
|
||||
|
||||
export async function approveTeamJoinRequest(teamId: string, requestId: string): Promise<void> {
|
||||
// TODO: implement API call
|
||||
}
|
||||
|
||||
export async function rejectTeamJoinRequest(teamId: string, requestId: string): Promise<void> {
|
||||
// TODO: implement API call
|
||||
}
|
||||
30
apps/website/lib/services/teams/TeamService.ts
Normal file
30
apps/website/lib/services/teams/TeamService.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { api as api } from '../../api';
|
||||
import { presentTeamDetails, presentTeamMember, presentTeamSummary } from '../../presenters';
|
||||
import { TeamDetailsViewModel, TeamMemberViewModel, TeamSummaryViewModel } from '../../view-models';
|
||||
|
||||
export async function getAllTeams(): Promise<TeamSummaryViewModel[]> {
|
||||
const dto = await api.teams.getAll();
|
||||
return dto.teams.map(t => presentTeamSummary(t));
|
||||
}
|
||||
|
||||
export async function getTeamDetails(teamId: string): Promise<TeamDetailsViewModel | null> {
|
||||
const dto = await api.teams.getDetails(teamId);
|
||||
return dto ? presentTeamDetails(dto) : null;
|
||||
}
|
||||
|
||||
export async function getTeamMembers(teamId: string): Promise<TeamMemberViewModel[]> {
|
||||
const dto = await api.teams.getMembers(teamId);
|
||||
return dto.members.map(m => presentTeamMember(m));
|
||||
}
|
||||
|
||||
export async function createTeam(input: any): Promise<any> {
|
||||
return await api.teams.create(input);
|
||||
}
|
||||
|
||||
export async function updateTeam(teamId: string, input: any): Promise<any> {
|
||||
return await api.teams.update(teamId, input);
|
||||
}
|
||||
|
||||
export async function getDriverTeam(driverId: string): Promise<any> {
|
||||
return await api.teams.getDriverTeam(driverId);
|
||||
}
|
||||
Reference in New Issue
Block a user