61 lines
1.6 KiB
TypeScript
61 lines
1.6 KiB
TypeScript
/**
|
|
* Application Use Case: CreateSponsorUseCase
|
|
*
|
|
* Creates a new sponsor.
|
|
*/
|
|
|
|
import { Sponsor, type SponsorProps } from '../../domain/entities/Sponsor';
|
|
import type { ISponsorRepository } from '../../domain/repositories/ISponsorRepository';
|
|
import type {
|
|
ICreateSponsorPresenter,
|
|
CreateSponsorResultDTO,
|
|
CreateSponsorViewModel,
|
|
} from '../presenters/ICreateSponsorPresenter';
|
|
import type { UseCase } from '@gridpilot/shared/application/UseCase';
|
|
|
|
export interface CreateSponsorInput {
|
|
name: string;
|
|
contactEmail: string;
|
|
websiteUrl?: string;
|
|
logoUrl?: string;
|
|
}
|
|
|
|
export class CreateSponsorUseCase
|
|
implements UseCase<CreateSponsorInput, CreateSponsorResultDTO, CreateSponsorViewModel, ICreateSponsorPresenter>
|
|
{
|
|
constructor(
|
|
private readonly sponsorRepository: ISponsorRepository,
|
|
) {}
|
|
|
|
async execute(
|
|
input: CreateSponsorInput,
|
|
presenter: ICreateSponsorPresenter,
|
|
): Promise<void> {
|
|
presenter.reset();
|
|
|
|
const id = `sponsor-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
|
|
const sponsor = Sponsor.create({
|
|
id,
|
|
name: input.name,
|
|
contactEmail: input.contactEmail,
|
|
...(input.websiteUrl !== undefined ? { websiteUrl: input.websiteUrl } : {}),
|
|
...(input.logoUrl !== undefined ? { logoUrl: input.logoUrl } : {}),
|
|
} as any);
|
|
|
|
await this.sponsorRepository.create(sponsor);
|
|
|
|
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);
|
|
}
|
|
} |