refactor racing use cases
This commit is contained in:
@@ -8,96 +8,128 @@
|
||||
import type { ISponsorshipPricingRepository } from '../../domain/repositories/ISponsorshipPricingRepository';
|
||||
import type { ISponsorshipRequestRepository } from '../../domain/repositories/ISponsorshipRequestRepository';
|
||||
import type { ISeasonSponsorshipRepository } from '../../domain/repositories/ISeasonSponsorshipRepository';
|
||||
import type { AsyncUseCase, Logger } from '@core/shared/application';
|
||||
import type { SponsorshipPricing, SponsorshipSlotConfig } from '../../domain/value-objects/SponsorshipPricing';
|
||||
import type { Logger } from '@core/shared/application';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { GetEntitySponsorshipPricingInputPort } from '../ports/input/GetEntitySponsorshipPricingInputPort';
|
||||
import type { GetEntitySponsorshipPricingOutputPort } from '../ports/output/GetEntitySponsorshipPricingOutputPort';
|
||||
|
||||
export class GetEntitySponsorshipPricingUseCase
|
||||
implements AsyncUseCase<GetEntitySponsorshipPricingInputPort, GetEntitySponsorshipPricingOutputPort | null, 'REPOSITORY_ERROR'>
|
||||
{
|
||||
export type SponsorshipEntityType = 'season' | 'league' | 'team';
|
||||
|
||||
export type GetEntitySponsorshipPricingInput = {
|
||||
entityType: SponsorshipEntityType;
|
||||
entityId: string;
|
||||
};
|
||||
|
||||
export type SponsorshipPricingTier = {
|
||||
name: string;
|
||||
price: SponsorshipPricing['mainSlot'] extends SponsorshipSlotConfig
|
||||
? SponsorshipSlotConfig['price']
|
||||
: SponsorshipPricing['secondarySlots'] extends SponsorshipSlotConfig
|
||||
? SponsorshipSlotConfig['price']
|
||||
: never;
|
||||
benefits: string[];
|
||||
};
|
||||
|
||||
export type GetEntitySponsorshipPricingResult = {
|
||||
entityType: SponsorshipEntityType;
|
||||
entityId: string;
|
||||
acceptingApplications: boolean;
|
||||
customRequirements?: string;
|
||||
tiers: SponsorshipPricingTier[];
|
||||
};
|
||||
|
||||
export type GetEntitySponsorshipPricingErrorCode =
|
||||
| 'ENTITY_NOT_FOUND'
|
||||
| 'PRICING_NOT_CONFIGURED'
|
||||
| 'REPOSITORY_ERROR';
|
||||
|
||||
export class GetEntitySponsorshipPricingUseCase {
|
||||
constructor(
|
||||
private readonly sponsorshipPricingRepo: ISponsorshipPricingRepository,
|
||||
private readonly sponsorshipRequestRepo: ISponsorshipRequestRepository,
|
||||
private readonly seasonSponsorshipRepo: ISeasonSponsorshipRepository,
|
||||
private readonly logger: Logger,
|
||||
private readonly output: UseCaseOutputPort<GetEntitySponsorshipPricingResult>,
|
||||
) {}
|
||||
|
||||
async execute(dto: GetEntitySponsorshipPricingInputPort): Promise<Result<GetEntitySponsorshipPricingOutputPort | null, ApplicationErrorCode<'REPOSITORY_ERROR', { message: string }>>> {
|
||||
this.logger.debug(`Executing GetEntitySponsorshipPricingUseCase for entityType: ${dto.entityType}, entityId: ${dto.entityId}`);
|
||||
async execute(
|
||||
input: GetEntitySponsorshipPricingInput,
|
||||
): Promise<
|
||||
Result<void, ApplicationErrorCode<GetEntitySponsorshipPricingErrorCode, { message: string }>>
|
||||
> {
|
||||
this.logger.debug(
|
||||
`Executing GetEntitySponsorshipPricingUseCase for entityType: ${input.entityType}, entityId: ${input.entityId}`,
|
||||
);
|
||||
|
||||
try {
|
||||
const pricing = await this.sponsorshipPricingRepo.findByEntity(dto.entityType, dto.entityId);
|
||||
const pricing = await this.sponsorshipPricingRepo.findByEntity(
|
||||
input.entityType,
|
||||
input.entityId,
|
||||
);
|
||||
|
||||
if (!pricing) {
|
||||
this.logger.info(`No pricing found for entityType: ${dto.entityType}, entityId: ${dto.entityId}`);
|
||||
return Result.ok(null);
|
||||
this.logger.info(
|
||||
`No pricing configured for entityType: ${input.entityType}, entityId: ${input.entityId}`,
|
||||
);
|
||||
|
||||
return Result.err({
|
||||
code: 'PRICING_NOT_CONFIGURED',
|
||||
details: {
|
||||
message: `No sponsorship pricing configured for entityType: ${input.entityType}, entityId: ${input.entityId}`,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Count pending requests by tier
|
||||
const pendingRequests = await this.sponsorshipRequestRepo.findPendingByEntity(
|
||||
dto.entityType,
|
||||
dto.entityId,
|
||||
);
|
||||
const pendingMainCount = pendingRequests.filter(r => r.tier === 'main').length;
|
||||
const pendingSecondaryCount = pendingRequests.filter(r => r.tier === 'secondary').length;
|
||||
const tiers: SponsorshipPricingTier[] = [];
|
||||
|
||||
// Count filled slots (for seasons, check SeasonSponsorship table)
|
||||
let filledMainSlots = 0;
|
||||
let filledSecondarySlots = 0;
|
||||
|
||||
if (dto.entityType === 'season') {
|
||||
const sponsorships = await this.seasonSponsorshipRepo.findBySeasonId(dto.entityId);
|
||||
const activeSponsorships = sponsorships.filter(s => s.isActive());
|
||||
filledMainSlots = activeSponsorships.filter(s => s.tier === 'main').length;
|
||||
filledSecondarySlots = activeSponsorships.filter(s => s.tier === 'secondary').length;
|
||||
if (pricing.mainSlot) {
|
||||
tiers.push({
|
||||
name: 'main',
|
||||
price: pricing.mainSlot.price,
|
||||
benefits: pricing.mainSlot.benefits,
|
||||
});
|
||||
}
|
||||
|
||||
const result: GetEntitySponsorshipPricingOutputPort = {
|
||||
entityType: dto.entityType,
|
||||
entityId: dto.entityId,
|
||||
if (pricing.secondarySlots) {
|
||||
tiers.push({
|
||||
name: 'secondary',
|
||||
price: pricing.secondarySlots.price,
|
||||
benefits: pricing.secondarySlots.benefits,
|
||||
});
|
||||
}
|
||||
|
||||
const result: GetEntitySponsorshipPricingResult = {
|
||||
entityType: input.entityType,
|
||||
entityId: input.entityId,
|
||||
acceptingApplications: pricing.acceptingApplications,
|
||||
...(pricing.customRequirements !== undefined
|
||||
? { customRequirements: pricing.customRequirements }
|
||||
: {}),
|
||||
tiers,
|
||||
};
|
||||
|
||||
if (pricing.mainSlot) {
|
||||
const mainMaxSlots = pricing.mainSlot.maxSlots;
|
||||
result.mainSlot = {
|
||||
tier: 'main',
|
||||
price: pricing.mainSlot.price.amount,
|
||||
currency: pricing.mainSlot.price.currency,
|
||||
formattedPrice: pricing.mainSlot.price.format(),
|
||||
benefits: pricing.mainSlot.benefits,
|
||||
available: pricing.mainSlot.available && filledMainSlots < mainMaxSlots,
|
||||
maxSlots: mainMaxSlots,
|
||||
filledSlots: filledMainSlots,
|
||||
pendingRequests: pendingMainCount,
|
||||
};
|
||||
}
|
||||
this.logger.info(
|
||||
`Successfully retrieved sponsorship pricing for entityType: ${input.entityType}, entityId: ${input.entityId}`,
|
||||
);
|
||||
this.output.present(result);
|
||||
|
||||
if (pricing.secondarySlots) {
|
||||
const secondaryMaxSlots = pricing.secondarySlots.maxSlots;
|
||||
result.secondarySlot = {
|
||||
tier: 'secondary',
|
||||
price: pricing.secondarySlots.price.amount,
|
||||
currency: pricing.secondarySlots.price.currency,
|
||||
formattedPrice: pricing.secondarySlots.price.format(),
|
||||
benefits: pricing.secondarySlots.benefits,
|
||||
available:
|
||||
pricing.secondarySlots.available && filledSecondarySlots < secondaryMaxSlots,
|
||||
maxSlots: secondaryMaxSlots,
|
||||
filledSlots: filledSecondarySlots,
|
||||
pendingRequests: pendingSecondaryCount,
|
||||
};
|
||||
}
|
||||
|
||||
this.logger.info(`Successfully retrieved sponsorship pricing for entityType: ${dto.entityType}, entityId: ${dto.entityId}`);
|
||||
return Result.ok(result);
|
||||
return Result.ok(undefined);
|
||||
} catch (error) {
|
||||
this.logger.error('Error executing GetEntitySponsorshipPricingUseCase', error instanceof Error ? error : new Error(String(error)));
|
||||
return Result.err({ code: 'REPOSITORY_ERROR', details: { message: error instanceof Error ? error.message : 'Unknown error occurred' } });
|
||||
this.logger.error(
|
||||
'Error executing GetEntitySponsorshipPricingUseCase',
|
||||
error instanceof Error ? error : new Error(String(error)),
|
||||
);
|
||||
|
||||
return Result.err({
|
||||
code: 'REPOSITORY_ERROR',
|
||||
details: {
|
||||
message:
|
||||
error instanceof Error
|
||||
? error.message
|
||||
: 'Failed to load sponsorship pricing',
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user