import type { ITeamMembershipRepository } from '../../domain/repositories/ITeamMembershipRepository'; import type { TeamMembership, TeamMembershipStatus, TeamRole, TeamJoinRequest, } from '../../domain/types/TeamMembership'; import type { ApproveTeamJoinRequestCommandDTO } from '../dto/TeamCommandAndQueryDTO'; import type { AsyncUseCase } from '@gridpilot/shared/application'; export class ApproveTeamJoinRequestUseCase implements AsyncUseCase { constructor( private readonly membershipRepository: ITeamMembershipRepository, ) {} async execute(command: ApproveTeamJoinRequestCommandDTO): Promise { const { requestId } = command; // There is no repository method to look up a single request by ID, // so we rely on the repository implementation to surface all relevant // requests via getJoinRequests and search by ID here. const allRequests: TeamJoinRequest[] = await this.membershipRepository.getJoinRequests( // For the in-memory fake used in tests, the teamId argument is ignored // and all requests are returned. '' as string, ); const request = allRequests.find((r) => r.id === requestId); if (!request) { throw new Error('Join request not found'); } const membership: TeamMembership = { teamId: request.teamId, driverId: request.driverId, role: 'driver' as TeamRole, status: 'active' as TeamMembershipStatus, joinedAt: new Date(), }; await this.membershipRepository.saveMembership(membership); await this.membershipRepository.removeJoinRequest(requestId); } }