Files
gridpilot.gg/packages/racing/application/use-cases/UpdateTeamUseCase.ts
2025-12-04 15:15:24 +01:00

32 lines
1.1 KiB
TypeScript

import type { ITeamRepository } from '../../domain/repositories/ITeamRepository';
import type { ITeamMembershipRepository } from '../../domain/repositories/ITeamMembershipRepository';
import type { Team } from '../../domain/entities/Team';
import type { UpdateTeamCommandDTO } from '../dto/TeamCommandAndQueryDTO';
export class UpdateTeamUseCase {
constructor(
private readonly teamRepository: ITeamRepository,
private readonly membershipRepository: ITeamMembershipRepository,
) {}
async execute(command: UpdateTeamCommandDTO): Promise<void> {
const { teamId, updates, updatedBy } = command;
const updaterMembership = await this.membershipRepository.getMembership(teamId, updatedBy);
if (!updaterMembership || (updaterMembership.role !== 'owner' && updaterMembership.role !== 'manager')) {
throw new Error('Only owners and managers can update team info');
}
const existing = await this.teamRepository.findById(teamId);
if (!existing) {
throw new Error('Team not found');
}
const updated: Team = {
...existing,
...updates,
};
await this.teamRepository.update(updated);
}
}