Files
gridpilot.gg/core/racing/application/use-cases/UpdateDriverProfileUseCase.ts
2025-12-19 14:08:27 +01:00

36 lines
1.3 KiB
TypeScript

import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
import type { Driver } from '../../domain/entities/Driver';
export interface UpdateDriverProfileInput {
driverId: string;
bio?: string;
country?: string;
}
/**
* Application use case responsible for updating basic driver profile details.
* Encapsulates domain entity mutation and returns the updated entity.
* Mapping to DTOs is handled by presenters in the presentation layer.
*/
export class UpdateDriverProfileUseCase {
constructor(private readonly driverRepository: IDriverRepository) {}
async execute(input: UpdateDriverProfileInput): Promise<Result<Driver, ApplicationErrorCode<'DRIVER_NOT_FOUND'>>> {
const { driverId, bio, country } = input;
const existing = await this.driverRepository.findById(driverId);
if (!existing) {
return Result.err({ code: 'DRIVER_NOT_FOUND' });
}
const updated = existing.update({
...(bio !== undefined ? { bio } : {}),
...(country !== undefined ? { country } : {}),
});
const persisted = await this.driverRepository.update(updated);
return Result.ok(persisted);
}
}