Files
gridpilot.gg/apps/website/lib/presenters/TeamAdminPresenter.ts
2025-12-11 21:06:25 +01:00

145 lines
3.6 KiB
TypeScript

import type { DriverDTO } from '@gridpilot/racing/application/dto/DriverDTO';
import { EntityMappers } from '@gridpilot/racing/application/mappers/EntityMappers';
import {
getDriverRepository,
getGetTeamJoinRequestsUseCase,
getApproveTeamJoinRequestUseCase,
getRejectTeamJoinRequestUseCase,
getUpdateTeamUseCase,
} from '@/lib/di-container';
export interface TeamAdminJoinRequestViewModel {
id: string;
teamId: string;
driverId: string;
requestedAt: Date;
message?: string;
driver?: DriverDTO;
}
export interface TeamAdminTeamSummaryViewModel {
id: string;
name: string;
tag: string;
description: string;
ownerId: string;
}
export interface TeamAdminViewModel {
team: TeamAdminTeamSummaryViewModel;
requests: TeamAdminJoinRequestViewModel[];
}
/**
* Load join requests plus driver DTOs for a team.
*/
export async function loadTeamAdminViewModel(
team: TeamAdminTeamSummaryViewModel,
): Promise<TeamAdminViewModel> {
const requests = await loadTeamJoinRequests(team.id);
return {
team: {
id: team.id,
name: team.name,
tag: team.tag,
description: team.description,
ownerId: team.ownerId,
},
requests,
};
}
export async function loadTeamJoinRequests(
teamId: string,
): Promise<TeamAdminJoinRequestViewModel[]> {
const getRequestsUseCase = getGetTeamJoinRequestsUseCase();
const presenter = new (await import('./TeamJoinRequestsPresenter')).TeamJoinRequestsPresenter();
await getRequestsUseCase.execute({ teamId }, presenter);
const presenterVm = presenter.getViewModel();
if (!presenterVm) {
return [];
}
const driverRepo = getDriverRepository();
const allDrivers = await driverRepo.findAll();
const driversById: Record<string, DriverDTO> = {};
for (const driver of allDrivers) {
const dto = EntityMappers.toDriverDTO(driver);
if (dto) {
driversById[dto.id] = dto;
}
}
return presenterVm.requests.map((req: {
requestId: string;
teamId: string;
driverId: string;
requestedAt: string;
message?: string;
}): TeamAdminJoinRequestViewModel => {
const base: TeamAdminJoinRequestViewModel = {
id: req.requestId,
teamId: req.teamId,
driverId: req.driverId,
requestedAt: new Date(req.requestedAt),
};
const message = req.message;
const driver = driversById[req.driverId];
return {
...base,
...(message !== undefined ? { message } : {}),
...(driver !== undefined ? { driver } : {}),
};
});
}
/**
* Approve a team join request and return updated request view models.
*/
export async function approveTeamJoinRequestAndReload(
requestId: string,
teamId: string,
): Promise<TeamAdminJoinRequestViewModel[]> {
const useCase = getApproveTeamJoinRequestUseCase();
await useCase.execute({ requestId });
return loadTeamJoinRequests(teamId);
}
/**
* Reject a team join request and return updated request view models.
*/
export async function rejectTeamJoinRequestAndReload(
requestId: string,
teamId: string,
): Promise<TeamAdminJoinRequestViewModel[]> {
const useCase = getRejectTeamJoinRequestUseCase();
await useCase.execute({ requestId });
return loadTeamJoinRequests(teamId);
}
/**
* Update team basic details.
*/
export async function updateTeamDetails(params: {
teamId: string;
name: string;
tag: string;
description: string;
updatedByDriverId: string;
}): Promise<void> {
const useCase = getUpdateTeamUseCase();
await useCase.execute({
teamId: params.teamId,
updates: {
name: params.name,
tag: params.tag,
description: params.description,
},
updatedBy: params.updatedByDriverId,
});
}