This commit is contained in:
2025-12-16 18:17:48 +01:00
parent 362894d1a5
commit ec7c0b8f2a
94 changed files with 4240 additions and 983 deletions

View File

@@ -3,59 +3,90 @@
*
* Creates a new sponsor.
*/
import { v4 as uuidv4 } from 'uuid';
import { Sponsor } from '../../domain/entities/Sponsor';
import type { ISponsorRepository } from '../../domain/repositories/ISponsorRepository';
import type {
ICreateSponsorPresenter,
CreateSponsorResultDTO,
CreateSponsorViewModel,
} from '../presenters/ICreateSponsorPresenter';
import type { UseCase } from '@core/shared/application/UseCase';
export interface CreateSponsorInput {
name: string;
contactEmail: string;
websiteUrl?: string;
logoUrl?: string;
}
import type { AsyncUseCase } from '@core/shared/application';
import type { Logger } from '@core/shared/application';
import { Result } from '@core/shared/result/Result';
import { RacingDomainValidationError } from '../../domain/errors/RacingDomainError';
import type { CreateSponsorCommand } from './CreateSponsorCommand';
import type { CreateSponsorResultDTO } from '../dto/CreateSponsorResultDTO';
export class CreateSponsorUseCase
implements UseCase<CreateSponsorInput, CreateSponsorResultDTO, CreateSponsorViewModel, ICreateSponsorPresenter>
implements AsyncUseCase<CreateSponsorCommand, Result<CreateSponsorResultDTO, RacingDomainValidationError>>
{
constructor(
private readonly sponsorRepository: ISponsorRepository,
private readonly logger: Logger,
) {}
async execute(
input: CreateSponsorInput,
presenter: ICreateSponsorPresenter,
): Promise<void> {
presenter.reset();
command: CreateSponsorCommand,
): Promise<Result<CreateSponsorResultDTO, RacingDomainValidationError>> {
this.logger.debug('Executing CreateSponsorUseCase', { command });
const validation = this.validate(command);
if (validation.isErr()) {
return Result.err(validation.unwrapErr());
}
this.logger.info('Command validated successfully.');
try {
const sponsorId = uuidv4();
this.logger.debug(`Generated sponsorId: ${sponsorId}`);
const id = `sponsor-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
const sponsor = Sponsor.create({
id: sponsorId,
name: command.name,
contactEmail: command.contactEmail,
...(command.websiteUrl !== undefined ? { websiteUrl: command.websiteUrl } : {}),
...(command.logoUrl !== undefined ? { logoUrl: command.logoUrl } : {}),
});
const sponsor = Sponsor.create({
id,
name: input.name,
contactEmail: input.contactEmail,
...(input.websiteUrl !== undefined ? { websiteUrl: input.websiteUrl } : {}),
...(input.logoUrl !== undefined ? { logoUrl: input.logoUrl } : {}),
} as unknown);
await this.sponsorRepository.create(sponsor);
this.logger.info(`Sponsor ${sponsor.name} (${sponsor.id}) created successfully.`);
await this.sponsorRepository.create(sponsor);
const result: CreateSponsorResultDTO = {
sponsor: {
id: sponsor.id,
name: sponsor.name,
contactEmail: sponsor.contactEmail,
websiteUrl: sponsor.websiteUrl,
logoUrl: sponsor.logoUrl,
createdAt: sponsor.createdAt,
},
};
this.logger.debug('CreateSponsorUseCase completed successfully.', { result });
return Result.ok(result);
} catch (error) {
return Result.err(new RacingDomainValidationError(error instanceof Error ? error.message : 'Unknown error'));
}
}
const dto: CreateSponsorResultDTO = {
sponsor: {
id: sponsor.id,
name: sponsor.name,
contactEmail: sponsor.contactEmail,
websiteUrl: sponsor.websiteUrl,
logoUrl: sponsor.logoUrl,
createdAt: sponsor.createdAt,
},
};
presenter.present(dto);
private validate(command: CreateSponsorCommand): Result<void, RacingDomainValidationError> {
this.logger.debug('Validating CreateSponsorCommand', { command });
if (!command.name || command.name.trim().length === 0) {
this.logger.warn('Validation failed: Sponsor name is required', { command });
return Result.err(new RacingDomainValidationError('Sponsor name is required'));
}
if (!command.contactEmail || command.contactEmail.trim().length === 0) {
this.logger.warn('Validation failed: Sponsor contact email is required', { command });
return Result.err(new RacingDomainValidationError('Sponsor contact email is required'));
}
// Basic email validation
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(command.contactEmail)) {
this.logger.warn('Validation failed: Invalid sponsor contact email format', { command });
return Result.err(new RacingDomainValidationError('Invalid sponsor contact email format'));
}
if (command.websiteUrl && command.websiteUrl.trim().length > 0) {
try {
new URL(command.websiteUrl);
} catch {
this.logger.warn('Validation failed: Invalid sponsor website URL', { command });
return Result.err(new RacingDomainValidationError('Invalid sponsor website URL'));
}
}
this.logger.debug('Validation successful.');
return Result.ok(undefined);
}
}