wip
This commit is contained in:
@@ -11,6 +11,10 @@ import type { ISponsorshipRequestRepository } from '../../domain/repositories/IS
|
||||
import type { ISponsorshipPricingRepository } from '../../domain/repositories/ISponsorshipPricingRepository';
|
||||
import type { ISponsorRepository } from '../../domain/repositories/ISponsorRepository';
|
||||
import { Money, type Currency } from '../../domain/value-objects/Money';
|
||||
import {
|
||||
EntityNotFoundError,
|
||||
BusinessRuleViolationError,
|
||||
} from '../errors/RacingApplicationError';
|
||||
|
||||
export interface ApplyForSponsorshipDTO {
|
||||
sponsorId: string;
|
||||
@@ -39,23 +43,23 @@ export class ApplyForSponsorshipUseCase {
|
||||
// Validate sponsor exists
|
||||
const sponsor = await this.sponsorRepo.findById(dto.sponsorId);
|
||||
if (!sponsor) {
|
||||
throw new Error('Sponsor not found');
|
||||
throw new EntityNotFoundError({ entity: 'sponsor', id: dto.sponsorId });
|
||||
}
|
||||
|
||||
// Check if entity accepts sponsorship applications
|
||||
const pricing = await this.sponsorshipPricingRepo.findByEntity(dto.entityType, dto.entityId);
|
||||
if (!pricing) {
|
||||
throw new Error('This entity has not set up sponsorship pricing');
|
||||
throw new BusinessRuleViolationError('This entity has not set up sponsorship pricing');
|
||||
}
|
||||
|
||||
if (!pricing.acceptingApplications) {
|
||||
throw new Error('This entity is not currently accepting sponsorship applications');
|
||||
throw new RacingApplicationError('This entity is not currently accepting sponsorship applications');
|
||||
}
|
||||
|
||||
// Check if the requested tier slot is available
|
||||
const slotAvailable = pricing.isSlotAvailable(dto.tier);
|
||||
if (!slotAvailable) {
|
||||
throw new Error(`No ${dto.tier} sponsorship slots are available`);
|
||||
throw new RacingApplicationError(`No ${dto.tier} sponsorship slots are available`);
|
||||
}
|
||||
|
||||
// Check if sponsor already has a pending request for this entity
|
||||
@@ -65,13 +69,13 @@ export class ApplyForSponsorshipUseCase {
|
||||
dto.entityId
|
||||
);
|
||||
if (hasPending) {
|
||||
throw new Error('You already have a pending sponsorship request for this entity');
|
||||
throw new RacingApplicationError('You already have a pending sponsorship request for this entity');
|
||||
}
|
||||
|
||||
// Validate offered amount meets minimum price
|
||||
const minPrice = pricing.getPrice(dto.tier);
|
||||
if (minPrice && dto.offeredAmount < minPrice.amount) {
|
||||
throw new Error(`Offered amount must be at least ${minPrice.format()}`);
|
||||
throw new RacingApplicationError(`Offered amount must be at least ${minPrice.format()}`);
|
||||
}
|
||||
|
||||
// Create the sponsorship request
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { ISeasonRepository } from '../../domain/repositories/ISeasonReposit
|
||||
import type { ILeagueScoringConfigRepository } from '../../domain/repositories/ILeagueScoringConfigRepository';
|
||||
import type { IGameRepository } from '../../domain/repositories/IGameRepository';
|
||||
import type { ILeagueFullConfigPresenter, LeagueFullConfigData } from '../presenters/ILeagueFullConfigPresenter';
|
||||
import { EntityNotFoundError } from '../errors/RacingApplicationError';
|
||||
|
||||
/**
|
||||
* Use Case for retrieving a league's full configuration.
|
||||
@@ -22,7 +23,7 @@ export class GetLeagueFullConfigUseCase {
|
||||
|
||||
const league = await this.leagueRepository.findById(leagueId);
|
||||
if (!league) {
|
||||
throw new Error(`League ${leagueId} not found`);
|
||||
throw new EntityNotFoundError({ entity: 'league', id: leagueId });
|
||||
}
|
||||
|
||||
const seasons = await this.seasonRepository.findByLeagueId(leagueId);
|
||||
|
||||
@@ -3,6 +3,10 @@ import type { ILeagueRepository } from '../../domain/repositories/ILeagueReposit
|
||||
import type { IResultRepository } from '../../domain/repositories/IResultRepository';
|
||||
import type { IStandingRepository } from '../../domain/repositories/IStandingRepository';
|
||||
import { Result } from '../../domain/entities/Result';
|
||||
import {
|
||||
BusinessRuleViolationError,
|
||||
EntityNotFoundError,
|
||||
} from '../errors/RacingApplicationError';
|
||||
import type {
|
||||
IImportRaceResultsPresenter,
|
||||
ImportRaceResultsSummaryViewModel,
|
||||
@@ -37,17 +41,17 @@ export class ImportRaceResultsUseCase {
|
||||
|
||||
const race = await this.raceRepository.findById(raceId);
|
||||
if (!race) {
|
||||
throw new Error('Race not found');
|
||||
throw new EntityNotFoundError({ entity: 'race', id: raceId });
|
||||
}
|
||||
|
||||
const league = await this.leagueRepository.findById(race.leagueId);
|
||||
if (!league) {
|
||||
throw new Error('League not found');
|
||||
throw new EntityNotFoundError({ entity: 'league', id: race.leagueId });
|
||||
}
|
||||
|
||||
const existing = await this.resultRepository.existsByRaceId(raceId);
|
||||
if (existing) {
|
||||
throw new Error('Results already exist for this race');
|
||||
throw new BusinessRuleViolationError('Results already exist for this race');
|
||||
}
|
||||
|
||||
const entities = results.map((dto) =>
|
||||
|
||||
@@ -7,6 +7,7 @@ import type {
|
||||
MembershipStatus,
|
||||
} from '@gridpilot/racing/domain/entities/LeagueMembership';
|
||||
import type { JoinLeagueCommandDTO } from '../dto/JoinLeagueCommandDTO';
|
||||
import { BusinessRuleViolationError } from '../errors/RacingApplicationError';
|
||||
|
||||
export class JoinLeagueUseCase {
|
||||
constructor(private readonly membershipRepository: ILeagueMembershipRepository) {}
|
||||
@@ -23,7 +24,7 @@ export class JoinLeagueUseCase {
|
||||
|
||||
const existing = await this.membershipRepository.getMembership(leagueId, driverId);
|
||||
if (existing) {
|
||||
throw new Error('Already a member or have a pending request');
|
||||
throw new BusinessRuleViolationError('Already a member or have a pending request');
|
||||
}
|
||||
|
||||
const membership: LeagueMembership = {
|
||||
|
||||
@@ -6,6 +6,10 @@ import type {
|
||||
TeamRole,
|
||||
} from '../../domain/entities/Team';
|
||||
import type { JoinTeamCommandDTO } from '../dto/TeamCommandAndQueryDTO';
|
||||
import {
|
||||
BusinessRuleViolationError,
|
||||
EntityNotFoundError,
|
||||
} from '../errors/RacingApplicationError';
|
||||
|
||||
export class JoinTeamUseCase {
|
||||
constructor(
|
||||
@@ -20,17 +24,17 @@ export class JoinTeamUseCase {
|
||||
driverId,
|
||||
);
|
||||
if (existingActive) {
|
||||
throw new Error('Driver already belongs to a team');
|
||||
throw new BusinessRuleViolationError('Driver already belongs to a team');
|
||||
}
|
||||
|
||||
const existingMembership = await this.membershipRepository.getMembership(teamId, driverId);
|
||||
if (existingMembership) {
|
||||
throw new Error('Already a member or have a pending request');
|
||||
throw new BusinessRuleViolationError('Already a member or have a pending request');
|
||||
}
|
||||
|
||||
const team = await this.teamRepository.findById(teamId);
|
||||
if (!team) {
|
||||
throw new Error('Team not found');
|
||||
throw new EntityNotFoundError({ entity: 'team', id: teamId });
|
||||
}
|
||||
|
||||
const membership: TeamMembership = {
|
||||
|
||||
@@ -2,6 +2,10 @@ import type { IRaceRegistrationRepository } from '@gridpilot/racing/domain/repos
|
||||
import type { ILeagueMembershipRepository } from '@gridpilot/racing/domain/repositories/ILeagueMembershipRepository';
|
||||
import type { RaceRegistration } from '@gridpilot/racing/domain/entities/RaceRegistration';
|
||||
import type { RegisterForRaceCommandDTO } from '../dto/RegisterForRaceCommandDTO';
|
||||
import {
|
||||
BusinessRuleViolationError,
|
||||
PermissionDeniedError,
|
||||
} from '../errors/RacingApplicationError';
|
||||
|
||||
export class RegisterForRaceUseCase {
|
||||
constructor(
|
||||
@@ -20,12 +24,12 @@ export class RegisterForRaceUseCase {
|
||||
|
||||
const alreadyRegistered = await this.registrationRepository.isRegistered(raceId, driverId);
|
||||
if (alreadyRegistered) {
|
||||
throw new Error('Already registered for this race');
|
||||
throw new BusinessRuleViolationError('Already registered for this race');
|
||||
}
|
||||
|
||||
const membership = await this.membershipRepository.getMembership(leagueId, driverId);
|
||||
if (!membership || membership.status !== 'active') {
|
||||
throw new Error('Must be an active league member to register for races');
|
||||
throw new PermissionDeniedError('NOT_ACTIVE_MEMBER', 'Must be an active league member to register for races');
|
||||
}
|
||||
|
||||
const registration: RaceRegistration = {
|
||||
|
||||
@@ -7,6 +7,10 @@
|
||||
import type { IProtestRepository } from '../../domain/repositories/IProtestRepository';
|
||||
import type { IRaceRepository } from '../../domain/repositories/IRaceRepository';
|
||||
import type { ILeagueMembershipRepository } from '../../domain/repositories/ILeagueMembershipRepository';
|
||||
import {
|
||||
EntityNotFoundError,
|
||||
PermissionDeniedError,
|
||||
} from '../errors/RacingApplicationError';
|
||||
|
||||
export interface ReviewProtestCommand {
|
||||
protestId: string;
|
||||
@@ -26,13 +30,13 @@ export class ReviewProtestUseCase {
|
||||
// Load the protest
|
||||
const protest = await this.protestRepository.findById(command.protestId);
|
||||
if (!protest) {
|
||||
throw new Error('Protest not found');
|
||||
throw new EntityNotFoundError({ entity: 'protest', id: command.protestId });
|
||||
}
|
||||
|
||||
// Load the race to get league ID
|
||||
const race = await this.raceRepository.findById(protest.raceId);
|
||||
if (!race) {
|
||||
throw new Error('Race not found');
|
||||
throw new EntityNotFoundError({ entity: 'race', id: protest.raceId });
|
||||
}
|
||||
|
||||
// Validate steward has authority (owner or admin of the league)
|
||||
@@ -42,7 +46,10 @@ export class ReviewProtestUseCase {
|
||||
);
|
||||
|
||||
if (!stewardMembership || (stewardMembership.role !== 'owner' && stewardMembership.role !== 'admin')) {
|
||||
throw new Error('Only league owners and admins can review protests');
|
||||
throw new PermissionDeniedError(
|
||||
'NOT_LEAGUE_ADMIN',
|
||||
'Only league owners and admins can review protests',
|
||||
);
|
||||
}
|
||||
|
||||
// Apply the decision
|
||||
|
||||
Reference in New Issue
Block a user