89 lines
3.4 KiB
TypeScript
89 lines
3.4 KiB
TypeScript
/**
|
|
* Application Use Case: CreateSponsorUseCase
|
|
*
|
|
* Creates a new sponsor.
|
|
*/
|
|
import { ApplicationErrorCode } from '@/shared/errors/ApplicationErrorCode';
|
|
import type { Logger } from '@core/shared/domain/Logger';
|
|
import { Result } from '@core/shared/domain/Result';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
import { Sponsor } from '../../domain/entities/sponsor/Sponsor';
|
|
|
|
export interface CreateSponsorInput {
|
|
name: string;
|
|
contactEmail: string;
|
|
websiteUrl?: string;
|
|
logoUrl?: string;
|
|
}
|
|
|
|
export type CreateSponsorResult = {
|
|
sponsor: Sponsor;
|
|
};
|
|
|
|
export class CreateSponsorUseCase {
|
|
constructor(
|
|
private readonly sponsorRepository: SponsorRepository,
|
|
private readonly logger: Logger,
|
|
) {}
|
|
|
|
async execute(
|
|
input: CreateSponsorInput,
|
|
): Promise<Result<CreateSponsorResult, ApplicationErrorCode<'VALIDATION_ERROR' | 'REPOSITORY_ERROR', { message: string }>>> {
|
|
this.logger.debug('Executing CreateSponsorUseCase', { input });
|
|
const validation = this.validate(input);
|
|
if (validation.isErr()) {
|
|
return Result.err(validation.unwrapErr());
|
|
}
|
|
this.logger.info('Input validated successfully.');
|
|
try {
|
|
const sponsorId = uuidv4();
|
|
this.logger.debug(`Generated sponsorId: ${sponsorId}`);
|
|
|
|
const sponsor = Sponsor.create({
|
|
id: sponsorId,
|
|
name: input.name,
|
|
contactEmail: input.contactEmail,
|
|
...(input.websiteUrl !== undefined ? { websiteUrl: input.websiteUrl } : {}),
|
|
...(input.logoUrl !== undefined ? { logoUrl: input.logoUrl } : {}),
|
|
});
|
|
|
|
await this.sponsorRepository.create(sponsor);
|
|
this.logger.info(`Sponsor ${sponsor.name} (${sponsor.id}) created successfully.`);
|
|
|
|
const result: CreateSponsorResult = { sponsor };
|
|
this.logger.debug('CreateSponsorUseCase completed successfully.');
|
|
return Result.ok(result);
|
|
} catch (error) {
|
|
return Result.err({ code: 'REPOSITORY_ERROR', details: { message: error instanceof Error ? error.message : 'Unknown error' } });
|
|
}
|
|
}
|
|
|
|
private validate(input: CreateSponsorInput): Result<void, ApplicationErrorCode<'VALIDATION_ERROR', { message: string }>> {
|
|
this.logger.debug('Validating CreateSponsorInput', { input });
|
|
if (!input.name || input.name.trim().length === 0) {
|
|
this.logger.warn('Validation failed: Sponsor name is required', { input });
|
|
return Result.err({ code: 'VALIDATION_ERROR', details: { message: 'Sponsor name is required' } });
|
|
}
|
|
if (!input.contactEmail || input.contactEmail.trim().length === 0) {
|
|
this.logger.warn('Validation failed: Sponsor contact email is required', { input });
|
|
return Result.err({ code: 'VALIDATION_ERROR', details: { message: 'Sponsor contact email is required' } });
|
|
}
|
|
// Basic email validation
|
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
if (!emailRegex.test(input.contactEmail)) {
|
|
this.logger.warn('Validation failed: Invalid sponsor contact email format', { input });
|
|
return Result.err({ code: 'VALIDATION_ERROR', details: { message: 'Invalid sponsor contact email format' } });
|
|
}
|
|
if (input.websiteUrl && input.websiteUrl.trim().length > 0) {
|
|
try {
|
|
new URL(input.websiteUrl);
|
|
} catch {
|
|
this.logger.warn('Validation failed: Invalid sponsor website URL', { input });
|
|
return Result.err({ code: 'VALIDATION_ERROR', details: { message: 'Invalid sponsor website URL' } });
|
|
}
|
|
}
|
|
this.logger.debug('Validation successful.');
|
|
return Result.ok(undefined);
|
|
}
|
|
}
|