refactor racing use cases
This commit is contained in:
@@ -4,64 +4,58 @@
|
||||
* Creates a new sponsor.
|
||||
*/
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
import { Sponsor } from '../../domain/entities/Sponsor';
|
||||
import { Sponsor } from '../../domain/entities/sponsor/Sponsor';
|
||||
import type { ISponsorRepository } from '../../domain/repositories/ISponsorRepository';
|
||||
import type { AsyncUseCase , Logger } from '@core/shared/application';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { CreateSponsorOutputPort } from '../ports/output/CreateSponsorOutputPort';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
|
||||
export interface CreateSponsorCommand {
|
||||
export interface CreateSponsorInput {
|
||||
name: string;
|
||||
contactEmail: string;
|
||||
websiteUrl?: string;
|
||||
logoUrl?: string;
|
||||
}
|
||||
|
||||
export class CreateSponsorUseCase
|
||||
implements AsyncUseCase<CreateSponsorCommand, CreateSponsorOutputPort, 'VALIDATION_ERROR' | 'REPOSITORY_ERROR'>
|
||||
{
|
||||
type CreateSponsorResult = {
|
||||
sponsor: Sponsor;
|
||||
};
|
||||
|
||||
export class CreateSponsorUseCase {
|
||||
constructor(
|
||||
private readonly sponsorRepository: ISponsorRepository,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<CreateSponsorResult>,
|
||||
) {}
|
||||
|
||||
async execute(
|
||||
command: CreateSponsorCommand,
|
||||
): Promise<Result<CreateSponsorOutputPort, ApplicationErrorCode<'VALIDATION_ERROR' | 'REPOSITORY_ERROR', { message: string }>>> {
|
||||
this.logger.debug('Executing CreateSponsorUseCase', { command });
|
||||
const validation = this.validate(command);
|
||||
input: CreateSponsorInput,
|
||||
): Promise<Result<void, 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('Command validated successfully.');
|
||||
this.logger.info('Input validated successfully.');
|
||||
try {
|
||||
const sponsorId = uuidv4();
|
||||
this.logger.debug(`Generated sponsorId: ${sponsorId}`);
|
||||
|
||||
const sponsor = Sponsor.create({
|
||||
id: sponsorId,
|
||||
name: command.name,
|
||||
contactEmail: command.contactEmail,
|
||||
...(command.websiteUrl !== undefined ? { websiteUrl: command.websiteUrl } : {}),
|
||||
...(command.logoUrl !== undefined ? { logoUrl: command.logoUrl } : {}),
|
||||
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: CreateSponsorOutputPort = {
|
||||
sponsor: {
|
||||
id: sponsor.id,
|
||||
name: sponsor.name,
|
||||
contactEmail: sponsor.contactEmail,
|
||||
createdAt: sponsor.createdAt,
|
||||
...(sponsor.websiteUrl !== undefined ? { websiteUrl: sponsor.websiteUrl } : {}),
|
||||
...(sponsor.logoUrl !== undefined ? { logoUrl: sponsor.logoUrl } : {}),
|
||||
},
|
||||
};
|
||||
this.logger.debug('CreateSponsorUseCase completed successfully.', { result });
|
||||
return Result.ok(result);
|
||||
this.output.present({ sponsor });
|
||||
this.logger.debug('CreateSponsorUseCase completed successfully.');
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
return Result.err({ code: 'REPOSITORY_ERROR', details: { message: error instanceof Error ? error.message : 'Unknown error' } });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user