refactor
This commit is contained in:
@@ -11,8 +11,7 @@ import type { ISeasonSponsorshipRepository } from '../../domain/repositories/ISe
|
||||
import type { SponsorableEntityType } from '../../domain/entities/SponsorshipRequest';
|
||||
import type { SponsorshipTier } from '../../domain/entities/SeasonSponsorship';
|
||||
import type { IEntitySponsorshipPricingPresenter } from '../presenters/IEntitySponsorshipPricingPresenter';
|
||||
import type { AsyncUseCase } from '@gridpilot/shared/application';
|
||||
import type { Logger } from '../../../shared/src/logging/Logger';
|
||||
import type { UseCase } from '@gridpilot/shared/application/UseCase';
|
||||
|
||||
export interface GetEntitySponsorshipPricingDTO {
|
||||
entityType: SponsorableEntityType;
|
||||
@@ -41,35 +40,28 @@ export interface GetEntitySponsorshipPricingResultDTO {
|
||||
}
|
||||
|
||||
export class GetEntitySponsorshipPricingUseCase
|
||||
implements AsyncUseCase<GetEntitySponsorshipPricingDTO, void> {
|
||||
implements UseCase<GetEntitySponsorshipPricingDTO, GetEntitySponsorshipPricingResultDTO | null, GetEntitySponsorshipPricingResultDTO | null, IEntitySponsorshipPricingPresenter>
|
||||
{
|
||||
constructor(
|
||||
private readonly sponsorshipPricingRepo: ISponsorshipPricingRepository,
|
||||
private readonly sponsorshipRequestRepo: ISponsorshipRequestRepository,
|
||||
private readonly seasonSponsorshipRepo: ISeasonSponsorshipRepository,
|
||||
private readonly presenter: IEntitySponsorshipPricingPresenter,
|
||||
private readonly logger: Logger,
|
||||
) {}
|
||||
|
||||
async execute(dto: GetEntitySponsorshipPricingDTO): Promise<void> {
|
||||
this.logger.debug(
|
||||
`Executing GetEntitySponsorshipPricingUseCase for entityType: ${dto.entityType}, entityId: ${dto.entityId}`,
|
||||
{ dto },
|
||||
);
|
||||
async execute(
|
||||
dto: GetEntitySponsorshipPricingDTO,
|
||||
presenter: IEntitySponsorshipPricingPresenter,
|
||||
): Promise<void> {
|
||||
presenter.reset();
|
||||
|
||||
try {
|
||||
const pricing = await this.sponsorshipPricingRepo.findByEntity(dto.entityType, dto.entityId);
|
||||
|
||||
if (!pricing) {
|
||||
this.logger.warn(
|
||||
`No pricing found for entityType: ${dto.entityType}, entityId: ${dto.entityId}. Presenting null.`,
|
||||
{ dto },
|
||||
);
|
||||
this.presenter.present(null);
|
||||
presenter.present(null);
|
||||
return;
|
||||
}
|
||||
|
||||
this.logger.debug(`Found pricing for entityType: ${dto.entityType}, entityId: ${dto.entityId}`, { pricing });
|
||||
|
||||
// Count pending requests by tier
|
||||
const pendingRequests = await this.sponsorshipRequestRepo.findPendingByEntity(
|
||||
dto.entityType,
|
||||
@@ -78,10 +70,6 @@ export class GetEntitySponsorshipPricingUseCase
|
||||
const pendingMainCount = pendingRequests.filter(r => r.tier === 'main').length;
|
||||
const pendingSecondaryCount = pendingRequests.filter(r => r.tier === 'secondary').length;
|
||||
|
||||
this.logger.debug(
|
||||
`Pending requests counts: main=${pendingMainCount}, secondary=${pendingSecondaryCount}`,
|
||||
);
|
||||
|
||||
// Count filled slots (for seasons, check SeasonSponsorship table)
|
||||
let filledMainSlots = 0;
|
||||
let filledSecondarySlots = 0;
|
||||
@@ -91,9 +79,6 @@ export class GetEntitySponsorshipPricingUseCase
|
||||
const activeSponsorships = sponsorships.filter(s => s.isActive());
|
||||
filledMainSlots = activeSponsorships.filter(s => s.tier === 'main').length;
|
||||
filledSecondarySlots = activeSponsorships.filter(s => s.tier === 'secondary').length;
|
||||
this.logger.debug(
|
||||
`Filled slots for season: main=${filledMainSlots}, secondary=${filledSecondarySlots}`,
|
||||
);
|
||||
}
|
||||
|
||||
const result: GetEntitySponsorshipPricingResultDTO = {
|
||||
@@ -118,7 +103,6 @@ export class GetEntitySponsorshipPricingUseCase
|
||||
filledSlots: filledMainSlots,
|
||||
pendingRequests: pendingMainCount,
|
||||
};
|
||||
this.logger.debug(`Main slot pricing information processed`, { mainSlot: result.mainSlot });
|
||||
}
|
||||
|
||||
if (pricing.secondarySlots) {
|
||||
@@ -135,26 +119,10 @@ export class GetEntitySponsorshipPricingUseCase
|
||||
filledSlots: filledSecondarySlots,
|
||||
pendingRequests: pendingSecondaryCount,
|
||||
};
|
||||
this.logger.debug(`Secondary slot pricing information processed`, {
|
||||
secondarySlot: result.secondarySlot,
|
||||
});
|
||||
}
|
||||
|
||||
this.logger.info(
|
||||
`Successfully retrieved and processed entity sponsorship pricing for entityType: ${dto.entityType}, entityId: ${dto.entityId}`,
|
||||
{ result },
|
||||
);
|
||||
this.presenter.present(result);
|
||||
presenter.present(result);
|
||||
} catch (error: unknown) {
|
||||
let errorMessage = 'An unknown error occurred';
|
||||
if (error instanceof Error) {
|
||||
errorMessage = error.message;
|
||||
}
|
||||
this.logger.error(
|
||||
`Failed to get entity sponsorship pricing for entityType: ${dto.entityType}, entityId: ${dto.entityId}. Error: ${errorMessage}`,
|
||||
{ error, dto },
|
||||
);
|
||||
// Re-throw the error or present an error state if the presenter supports it
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user