view data fixes
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
import type { LeagueStandingsViewData, StandingEntryData, DriverData, LeagueMembershipData } from '@/lib/view-data/LeagueStandingsViewData';
|
||||
'use client';
|
||||
|
||||
import type { LeagueStandingsViewData } from '@/lib/view-data/LeagueStandingsViewData';
|
||||
import type { LeagueStandingDTO } from '@/lib/types/generated/LeagueStandingDTO';
|
||||
import type { LeagueMemberDTO } from '@/lib/types/generated/LeagueMemberDTO';
|
||||
import type { ViewDataBuilder } from '@/lib/contracts/builders/ViewDataBuilder';
|
||||
|
||||
interface LeagueStandingsApiDto {
|
||||
standings: LeagueStandingDTO[];
|
||||
@@ -10,39 +13,34 @@ interface LeagueMembershipsApiDto {
|
||||
members: LeagueMemberDTO[];
|
||||
}
|
||||
|
||||
/**
|
||||
* LeagueStandingsViewDataBuilder
|
||||
*
|
||||
* Transforms API DTOs into LeagueStandingsViewData for server-side rendering.
|
||||
* Deterministic; side-effect free; no HTTP calls.
|
||||
*/
|
||||
import { ViewDataBuilder } from "../../contracts/builders/ViewDataBuilder";
|
||||
type LeagueStandingsInputDTO = {
|
||||
standingsDto: LeagueStandingsApiDto;
|
||||
membershipsDto: LeagueMembershipsApiDto;
|
||||
leagueId: string;
|
||||
isTeamChampionship?: boolean;
|
||||
}
|
||||
|
||||
export class LeagueStandingsViewDataBuilder implements ViewDataBuilder<any, any> {
|
||||
build(input: any): any {
|
||||
return LeagueStandingsViewDataBuilder.build(input);
|
||||
}
|
||||
|
||||
static build(
|
||||
static build(
|
||||
standingsDto: LeagueStandingsApiDto,
|
||||
membershipsDto: LeagueMembershipsApiDto,
|
||||
leagueId: string,
|
||||
isTeamChampionship: boolean = false
|
||||
): LeagueStandingsViewData {
|
||||
export class LeagueStandingsViewDataBuilder {
|
||||
public static build(apiDto: LeagueStandingsInputDTO): LeagueStandingsViewData {
|
||||
const { standingsDto, membershipsDto, leagueId, isTeamChampionship = false } = apiDto;
|
||||
const standings = standingsDto.standings || [];
|
||||
const members = membershipsDto.members || [];
|
||||
|
||||
// Convert LeagueStandingDTO to StandingEntryData
|
||||
const standingData: StandingEntryData[] = standings.map(standing => ({
|
||||
const standingData: LeagueStandingsViewData['standings'] = standings.map(standing => ({
|
||||
driverId: standing.driverId,
|
||||
position: standing.position,
|
||||
points: standing.points,
|
||||
totalPoints: standing.points,
|
||||
races: standing.races,
|
||||
racesFinished: standing.races,
|
||||
racesStarted: standing.races,
|
||||
avgFinish: null, // Not in DTO
|
||||
penaltyPoints: 0, // Not in DTO
|
||||
bonusPoints: 0, // Not in DTO
|
||||
leaderPoints: 0, // Not in DTO
|
||||
nextPoints: 0, // Not in DTO
|
||||
currentUserId: null, // Not in DTO
|
||||
// New fields from Phase 3
|
||||
positionChange: standing.positionChange || 0,
|
||||
lastRacePoints: standing.lastRacePoints || 0,
|
||||
@@ -52,7 +50,7 @@ export class LeagueStandingsViewDataBuilder implements ViewDataBuilder<any, any>
|
||||
}));
|
||||
|
||||
// Extract unique drivers from standings
|
||||
const driverMap = new Map<string, DriverData>();
|
||||
const driverMap = new Map<string, LeagueStandingsViewData['drivers'][number]>();
|
||||
standings.forEach(standing => {
|
||||
if (standing.driver && !driverMap.has(standing.driverId)) {
|
||||
const driver = standing.driver;
|
||||
@@ -66,13 +64,13 @@ export class LeagueStandingsViewDataBuilder implements ViewDataBuilder<any, any>
|
||||
});
|
||||
}
|
||||
});
|
||||
const driverData: DriverData[] = Array.from(driverMap.values());
|
||||
const driverData = Array.from(driverMap.values());
|
||||
|
||||
// Convert LeagueMemberDTO to LeagueMembershipData
|
||||
const membershipData: LeagueMembershipData[] = members.map(member => ({
|
||||
const membershipData: LeagueStandingsViewData['memberships'] = members.map(member => ({
|
||||
driverId: member.driverId,
|
||||
leagueId: leagueId,
|
||||
role: (member.role as LeagueMembershipData['role']) || 'member',
|
||||
role: (member.role as any) || 'member',
|
||||
joinedAt: member.joinedAt,
|
||||
status: 'active' as const,
|
||||
}));
|
||||
@@ -87,4 +85,6 @@ export class LeagueStandingsViewDataBuilder implements ViewDataBuilder<any, any>
|
||||
isTeamChampionship: isTeamChampionship,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LeagueStandingsViewDataBuilder satisfies ViewDataBuilder<LeagueStandingsInputDTO, LeagueStandingsViewData>;
|
||||
Reference in New Issue
Block a user