Files
gridpilot.gg/apps/website/lib/services/teams/TeamService.ts
2026-01-16 11:13:42 +01:00

113 lines
4.9 KiB
TypeScript

import { TeamsApiClient } from '@/lib/api/teams/TeamsApiClient';
import type { TeamListItemDTO } from '@/lib/types/generated/TeamListItemDTO';
import type { CreateTeamInputDTO } from '@/lib/types/generated/CreateTeamInputDTO';
import type { CreateTeamOutputDTO } from '@/lib/types/generated/CreateTeamOutputDTO';
import type { UpdateTeamInputDTO } from '@/lib/types/generated/UpdateTeamInputDTO';
import type { UpdateTeamOutputDTO } from '@/lib/types/generated/UpdateTeamOutputDTO';
import type { GetDriverTeamOutputDTO } from '@/lib/types/generated/GetDriverTeamOutputDTO';
import type { GetTeamMembershipOutputDTO } from '@/lib/types/generated/GetTeamMembershipOutputDTO';
import type { GetTeamJoinRequestsOutputDTO } from '@/lib/types/generated/GetTeamJoinRequestsOutputDTO';
import { TeamMemberViewModel } from '@/lib/view-models/TeamMemberViewModel';
import { Result } from '@/lib/contracts/Result';
import { DomainError, Service } from '@/lib/contracts/services/Service';
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
import { isProductionEnvironment } from '@/lib/config/env';
/**
* Team Service - DTO Only
*
* Returns raw API DTOs. No ViewModels or UX logic.
* All client-side presentation logic must be handled by hooks/components.
*/
export class TeamService implements Service {
private apiClient: TeamsApiClient;
constructor() {
const baseUrl = getWebsiteApiBaseUrl();
const logger = new ConsoleLogger();
const errorReporter = new EnhancedErrorReporter(logger, {
showUserNotifications: true,
logToConsole: true,
reportToExternal: isProductionEnvironment(),
});
this.apiClient = new TeamsApiClient(baseUrl, errorReporter, logger);
}
async getAllTeams(): Promise<Result<TeamListItemDTO[], DomainError>> {
try {
const result = await this.apiClient.getAll();
return Result.ok(result.teams);
} catch (error: unknown) {
return Result.err({ type: 'unknown', message: (error as Error).message || 'Failed to fetch teams' });
}
}
async getTeamDetails(teamId: string, _: string): Promise<Result<any, DomainError>> {
try {
const result = await this.apiClient.getDetails(teamId);
if (!result) {
return Result.err({ type: 'notFound', message: 'Team not found' });
}
return Result.ok(result);
} catch (error: unknown) {
return Result.err({ type: 'unknown', message: (error as Error).message || 'Failed to fetch team details' });
}
}
async getTeamMembers(teamId: string, currentDriverId: string, ownerId: string): Promise<Result<TeamMemberViewModel[], DomainError>> {
try {
const result = await this.apiClient.getMembers(teamId);
return Result.ok(result.members.map(member => new TeamMemberViewModel(member, currentDriverId, ownerId)));
} catch (error: unknown) {
return Result.err({ type: 'unknown', message: (error as Error).message || 'Failed to fetch team members' });
}
}
async getTeamJoinRequests(teamId: string): Promise<Result<GetTeamJoinRequestsOutputDTO, DomainError>> {
try {
const result = await this.apiClient.getJoinRequests(teamId);
return Result.ok(result);
} catch (error: unknown) {
return Result.err({ type: 'unknown', message: (error as Error).message || 'Failed to fetch team join requests' });
}
}
async createTeam(input: CreateTeamInputDTO): Promise<Result<CreateTeamOutputDTO, DomainError>> {
try {
const result = await this.apiClient.create(input);
return Result.ok(result);
} catch (error: unknown) {
return Result.err({ type: 'unknown', message: (error as Error).message || 'Failed to create team' });
}
}
async updateTeam(teamId: string, input: UpdateTeamInputDTO): Promise<Result<UpdateTeamOutputDTO, DomainError>> {
try {
const result = await this.apiClient.update(teamId, input);
return Result.ok(result);
} catch (error: unknown) {
return Result.err({ type: 'unknown', message: (error as Error).message || 'Failed to update team' });
}
}
async getDriverTeam(driverId: string): Promise<Result<GetDriverTeamOutputDTO | null, DomainError>> {
try {
const result = await this.apiClient.getDriverTeam(driverId);
return Result.ok(result);
} catch (error: unknown) {
return Result.err({ type: 'unknown', message: (error as Error).message || 'Failed to fetch driver team' });
}
}
async getMembership(teamId: string, driverId: string): Promise<Result<GetTeamMembershipOutputDTO | null, DomainError>> {
try {
const result = await this.apiClient.getMembership(teamId, driverId);
return Result.ok(result);
} catch (error: unknown) {
return Result.err({ type: 'unknown', message: (error as Error).message || 'Failed to fetch team membership' });
}
}
}