62 lines
1.9 KiB
TypeScript
62 lines
1.9 KiB
TypeScript
/**
|
|
* Application Use Case: UpsertMembershipFeeUseCase
|
|
*
|
|
* Creates or updates membership fee configuration.
|
|
*/
|
|
|
|
import type { UseCase } from '@core/shared/application/UseCase';
|
|
import { Result } from '@core/shared/domain/Result';
|
|
import type { MembershipFee, MembershipFeeType } from '../../domain/entities/MembershipFee';
|
|
import type { MembershipFeeRepository } from '../../domain/repositories/MembershipFeeRepository';
|
|
|
|
export interface UpsertMembershipFeeInput {
|
|
leagueId: string;
|
|
seasonId?: string;
|
|
type: MembershipFeeType;
|
|
amount: number;
|
|
}
|
|
|
|
export interface UpsertMembershipFeeResult {
|
|
fee: MembershipFee;
|
|
}
|
|
|
|
export type UpsertMembershipFeeErrorCode = never;
|
|
|
|
export class UpsertMembershipFeeUseCase
|
|
implements UseCase<UpsertMembershipFeeInput, UpsertMembershipFeeResult, UpsertMembershipFeeErrorCode>
|
|
{
|
|
constructor(
|
|
private readonly membershipFeeRepository: MembershipFeeRepository,
|
|
) {}
|
|
|
|
async execute(input: UpsertMembershipFeeInput): Promise<Result<UpsertMembershipFeeResult, never>> {
|
|
const { leagueId, seasonId, type, amount } = input;
|
|
|
|
let existingFee = await this.membershipFeeRepository.findByLeagueId(leagueId);
|
|
|
|
let fee: MembershipFee;
|
|
if (existingFee) {
|
|
existingFee.type = type;
|
|
existingFee.amount = amount;
|
|
if (seasonId !== undefined) existingFee.seasonId = seasonId;
|
|
existingFee.enabled = amount > 0;
|
|
existingFee.updatedAt = new Date();
|
|
fee = await this.membershipFeeRepository.update(existingFee);
|
|
} else {
|
|
const id = `fee-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
const newFee: MembershipFee = {
|
|
id,
|
|
leagueId,
|
|
type,
|
|
amount,
|
|
enabled: amount > 0,
|
|
createdAt: new Date(),
|
|
updatedAt: new Date(),
|
|
...(seasonId !== undefined ? { seasonId } : {}),
|
|
};
|
|
fee = await this.membershipFeeRepository.create(newFee);
|
|
}
|
|
|
|
return Result.ok({ fee });
|
|
}
|
|
} |