86 lines
3.0 KiB
TypeScript
86 lines
3.0 KiB
TypeScript
/**
|
|
* Use Case: AcceptSponsorshipRequestUseCase
|
|
*
|
|
* Allows an entity owner to accept a sponsorship request.
|
|
* This creates an active sponsorship and notifies the sponsor.
|
|
*/
|
|
|
|
import type { ISponsorshipRequestRepository } from '../../domain/repositories/ISponsorshipRequestRepository';
|
|
import type { ISeasonSponsorshipRepository } from '../../domain/repositories/ISeasonSponsorshipRepository';
|
|
import type { ISeasonRepository } from '../../domain/repositories/ISeasonRepository';
|
|
import { SeasonSponsorship } from '../../domain/entities/SeasonSponsorship';
|
|
import type { AsyncUseCase } from '@gridpilot/shared/application';
|
|
|
|
export interface AcceptSponsorshipRequestDTO {
|
|
requestId: string;
|
|
respondedBy: string; // driverId of the person accepting
|
|
}
|
|
|
|
export interface AcceptSponsorshipRequestResultDTO {
|
|
requestId: string;
|
|
sponsorshipId: string;
|
|
status: 'accepted';
|
|
acceptedAt: Date;
|
|
platformFee: number;
|
|
netAmount: number;
|
|
}
|
|
|
|
export class AcceptSponsorshipRequestUseCase
|
|
implements AsyncUseCase<AcceptSponsorshipRequestDTO, AcceptSponsorshipRequestResultDTO> {
|
|
constructor(
|
|
private readonly sponsorshipRequestRepo: ISponsorshipRequestRepository,
|
|
private readonly seasonSponsorshipRepo: ISeasonSponsorshipRepository,
|
|
private readonly seasonRepository: ISeasonRepository,
|
|
) {}
|
|
|
|
async execute(dto: AcceptSponsorshipRequestDTO): Promise<AcceptSponsorshipRequestResultDTO> {
|
|
// Find the request
|
|
const request = await this.sponsorshipRequestRepo.findById(dto.requestId);
|
|
if (!request) {
|
|
throw new Error('Sponsorship request not found');
|
|
}
|
|
|
|
if (!request.isPending()) {
|
|
throw new Error(`Cannot accept a ${request.status} sponsorship request`);
|
|
}
|
|
|
|
// Accept the request
|
|
const acceptedRequest = request.accept(dto.respondedBy);
|
|
await this.sponsorshipRequestRepo.update(acceptedRequest);
|
|
|
|
// If this is a season sponsorship, create the SeasonSponsorship record
|
|
let sponsorshipId = `spons_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
|
|
|
|
if (request.entityType === 'season') {
|
|
const season = await this.seasonRepository.findById(request.entityId);
|
|
if (!season) {
|
|
throw new Error('Season not found for sponsorship request');
|
|
}
|
|
|
|
const sponsorship = SeasonSponsorship.create({
|
|
id: sponsorshipId,
|
|
seasonId: season.id,
|
|
leagueId: season.leagueId,
|
|
sponsorId: request.sponsorId,
|
|
tier: request.tier,
|
|
pricing: request.offeredAmount,
|
|
status: 'active',
|
|
});
|
|
await this.seasonSponsorshipRepo.create(sponsorship);
|
|
}
|
|
|
|
// TODO: In a real implementation, we would:
|
|
// 1. Create notification for the sponsor
|
|
// 2. Process payment
|
|
// 3. Update wallet balances
|
|
|
|
return {
|
|
requestId: acceptedRequest.id,
|
|
sponsorshipId,
|
|
status: 'accepted',
|
|
acceptedAt: acceptedRequest.respondedAt!,
|
|
platformFee: acceptedRequest.getPlatformFee().amount,
|
|
netAmount: acceptedRequest.getNetAmount().amount,
|
|
};
|
|
}
|
|
} |