refactor
This commit is contained in:
2
core/payments/application/index.ts
Normal file
2
core/payments/application/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './presenters';
|
||||
export * from './use-cases';
|
||||
16
core/payments/application/presenters/IAwardPrizePresenter.ts
Normal file
16
core/payments/application/presenters/IAwardPrizePresenter.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Presenter Interface: IAwardPrizePresenter
|
||||
*/
|
||||
|
||||
import type { Presenter } from '@gridpilot/shared/presentation/Presenter';
|
||||
import type { PrizeDto } from './IGetPrizesPresenter';
|
||||
|
||||
export interface AwardPrizeResultDTO {
|
||||
prize: PrizeDto;
|
||||
}
|
||||
|
||||
export interface AwardPrizeViewModel {
|
||||
prize: PrizeDto;
|
||||
}
|
||||
|
||||
export interface IAwardPrizePresenter extends Presenter<AwardPrizeResultDTO, AwardPrizeViewModel> {}
|
||||
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Presenter Interface: ICreatePaymentPresenter
|
||||
*/
|
||||
|
||||
import type { Presenter } from '@gridpilot/shared/presentation/Presenter';
|
||||
import type { PaymentDto } from './IGetPaymentsPresenter';
|
||||
|
||||
export interface CreatePaymentResultDTO {
|
||||
payment: PaymentDto;
|
||||
}
|
||||
|
||||
export interface CreatePaymentViewModel {
|
||||
payment: PaymentDto;
|
||||
}
|
||||
|
||||
export interface ICreatePaymentPresenter extends Presenter<CreatePaymentResultDTO, CreatePaymentViewModel> {}
|
||||
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Presenter Interface: ICreatePrizePresenter
|
||||
*/
|
||||
|
||||
import type { Presenter } from '@gridpilot/shared/presentation/Presenter';
|
||||
import type { PrizeDto } from './IGetPrizesPresenter';
|
||||
|
||||
export interface CreatePrizeResultDTO {
|
||||
prize: PrizeDto;
|
||||
}
|
||||
|
||||
export interface CreatePrizeViewModel {
|
||||
prize: PrizeDto;
|
||||
}
|
||||
|
||||
export interface ICreatePrizePresenter extends Presenter<CreatePrizeResultDTO, CreatePrizeViewModel> {}
|
||||
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Presenter Interface: IDeletePrizePresenter
|
||||
*/
|
||||
|
||||
import type { Presenter } from '@gridpilot/shared/presentation/Presenter';
|
||||
|
||||
export interface DeletePrizeResultDTO {
|
||||
success: boolean;
|
||||
}
|
||||
|
||||
export interface DeletePrizeViewModel {
|
||||
success: boolean;
|
||||
}
|
||||
|
||||
export interface IDeletePrizePresenter extends Presenter<DeletePrizeResultDTO, DeletePrizeViewModel> {}
|
||||
@@ -0,0 +1,42 @@
|
||||
/**
|
||||
* Presenter Interface: IGetMembershipFeesPresenter
|
||||
*/
|
||||
|
||||
import type { Presenter } from '@gridpilot/shared/presentation/Presenter';
|
||||
import type { MembershipFeeType } from '../../domain/entities/MembershipFee';
|
||||
import type { MemberPaymentStatus } from '../../domain/entities/MemberPayment';
|
||||
|
||||
export interface MembershipFeeDto {
|
||||
id: string;
|
||||
leagueId: string;
|
||||
seasonId?: string;
|
||||
type: MembershipFeeType;
|
||||
amount: number;
|
||||
enabled: boolean;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
|
||||
export interface MemberPaymentDto {
|
||||
id: string;
|
||||
feeId: string;
|
||||
driverId: string;
|
||||
amount: number;
|
||||
platformFee: number;
|
||||
netAmount: number;
|
||||
status: MemberPaymentStatus;
|
||||
dueDate: Date;
|
||||
paidAt?: Date;
|
||||
}
|
||||
|
||||
export interface GetMembershipFeesResultDTO {
|
||||
fee: MembershipFeeDto | null;
|
||||
payments: MemberPaymentDto[];
|
||||
}
|
||||
|
||||
export interface GetMembershipFeesViewModel {
|
||||
fee: MembershipFeeDto | null;
|
||||
payments: MemberPaymentDto[];
|
||||
}
|
||||
|
||||
export interface IGetMembershipFeesPresenter extends Presenter<GetMembershipFeesResultDTO, GetMembershipFeesViewModel> {}
|
||||
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Presenter Interface: IGetPaymentsPresenter
|
||||
*/
|
||||
|
||||
import type { Presenter } from '@gridpilot/shared/presentation/Presenter';
|
||||
import type { PaymentType, PayerType, PaymentStatus } from '../../domain/entities/Payment';
|
||||
|
||||
export interface PaymentDto {
|
||||
id: string;
|
||||
type: PaymentType;
|
||||
amount: number;
|
||||
platformFee: number;
|
||||
netAmount: number;
|
||||
payerId: string;
|
||||
payerType: PayerType;
|
||||
leagueId: string;
|
||||
seasonId?: string;
|
||||
status: PaymentStatus;
|
||||
createdAt: Date;
|
||||
completedAt?: Date;
|
||||
}
|
||||
|
||||
export interface GetPaymentsResultDTO {
|
||||
payments: PaymentDto[];
|
||||
}
|
||||
|
||||
export interface GetPaymentsViewModel {
|
||||
payments: PaymentDto[];
|
||||
}
|
||||
|
||||
export interface IGetPaymentsPresenter extends Presenter<GetPaymentsResultDTO, GetPaymentsViewModel> {}
|
||||
31
core/payments/application/presenters/IGetPrizesPresenter.ts
Normal file
31
core/payments/application/presenters/IGetPrizesPresenter.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
/**
|
||||
* Presenter Interface: IGetPrizesPresenter
|
||||
*/
|
||||
|
||||
import type { Presenter } from '@gridpilot/shared/presentation/Presenter';
|
||||
import type { PrizeType } from '../../domain/entities/Prize';
|
||||
|
||||
export interface PrizeDto {
|
||||
id: string;
|
||||
leagueId: string;
|
||||
seasonId: string;
|
||||
position: number;
|
||||
name: string;
|
||||
amount: number;
|
||||
type: PrizeType;
|
||||
description?: string;
|
||||
awarded: boolean;
|
||||
awardedTo?: string;
|
||||
awardedAt?: Date;
|
||||
createdAt: Date;
|
||||
}
|
||||
|
||||
export interface GetPrizesResultDTO {
|
||||
prizes: PrizeDto[];
|
||||
}
|
||||
|
||||
export interface GetPrizesViewModel {
|
||||
prizes: PrizeDto[];
|
||||
}
|
||||
|
||||
export interface IGetPrizesPresenter extends Presenter<GetPrizesResultDTO, GetPrizesViewModel> {}
|
||||
40
core/payments/application/presenters/IGetWalletPresenter.ts
Normal file
40
core/payments/application/presenters/IGetWalletPresenter.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* Presenter Interface: IGetWalletPresenter
|
||||
*/
|
||||
|
||||
import type { Presenter } from '@gridpilot/shared/presentation/Presenter';
|
||||
import type { TransactionType, ReferenceType } from '../../domain/entities/Wallet';
|
||||
|
||||
export interface WalletDto {
|
||||
id: string;
|
||||
leagueId: string;
|
||||
balance: number;
|
||||
totalRevenue: number;
|
||||
totalPlatformFees: number;
|
||||
totalWithdrawn: number;
|
||||
currency: string;
|
||||
createdAt: Date;
|
||||
}
|
||||
|
||||
export interface TransactionDto {
|
||||
id: string;
|
||||
walletId: string;
|
||||
type: TransactionType;
|
||||
amount: number;
|
||||
description: string;
|
||||
referenceId?: string;
|
||||
referenceType?: ReferenceType;
|
||||
createdAt: Date;
|
||||
}
|
||||
|
||||
export interface GetWalletResultDTO {
|
||||
wallet: WalletDto;
|
||||
transactions: TransactionDto[];
|
||||
}
|
||||
|
||||
export interface GetWalletViewModel {
|
||||
wallet: WalletDto;
|
||||
transactions: TransactionDto[];
|
||||
}
|
||||
|
||||
export interface IGetWalletPresenter extends Presenter<GetWalletResultDTO, GetWalletViewModel> {}
|
||||
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Presenter Interface: IProcessWalletTransactionPresenter
|
||||
*/
|
||||
|
||||
import type { Presenter } from '@gridpilot/shared/presentation/Presenter';
|
||||
import type { WalletDto, TransactionDto } from './IGetWalletPresenter';
|
||||
|
||||
export interface ProcessWalletTransactionResultDTO {
|
||||
wallet: WalletDto;
|
||||
transaction: TransactionDto;
|
||||
}
|
||||
|
||||
export interface ProcessWalletTransactionViewModel {
|
||||
wallet: WalletDto;
|
||||
transaction: TransactionDto;
|
||||
}
|
||||
|
||||
export interface IProcessWalletTransactionPresenter extends Presenter<ProcessWalletTransactionResultDTO, ProcessWalletTransactionViewModel> {}
|
||||
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Presenter Interface: IUpdateMemberPaymentPresenter
|
||||
*/
|
||||
|
||||
import type { Presenter } from '@gridpilot/shared/presentation/Presenter';
|
||||
import type { MemberPaymentDto } from './IGetMembershipFeesPresenter';
|
||||
|
||||
export interface UpdateMemberPaymentResultDTO {
|
||||
payment: MemberPaymentDto;
|
||||
}
|
||||
|
||||
export interface UpdateMemberPaymentViewModel {
|
||||
payment: MemberPaymentDto;
|
||||
}
|
||||
|
||||
export interface IUpdateMemberPaymentPresenter extends Presenter<UpdateMemberPaymentResultDTO, UpdateMemberPaymentViewModel> {}
|
||||
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Presenter Interface: IUpdatePaymentStatusPresenter
|
||||
*/
|
||||
|
||||
import type { Presenter } from '@gridpilot/shared/presentation/Presenter';
|
||||
import type { PaymentDto } from './IGetPaymentsPresenter';
|
||||
|
||||
export interface UpdatePaymentStatusResultDTO {
|
||||
payment: PaymentDto;
|
||||
}
|
||||
|
||||
export interface UpdatePaymentStatusViewModel {
|
||||
payment: PaymentDto;
|
||||
}
|
||||
|
||||
export interface IUpdatePaymentStatusPresenter extends Presenter<UpdatePaymentStatusResultDTO, UpdatePaymentStatusViewModel> {}
|
||||
@@ -0,0 +1,16 @@
|
||||
/**
|
||||
* Presenter Interface: IUpsertMembershipFeePresenter
|
||||
*/
|
||||
|
||||
import type { Presenter } from '@gridpilot/shared/presentation/Presenter';
|
||||
import type { MembershipFeeDto } from './IGetMembershipFeesPresenter';
|
||||
|
||||
export interface UpsertMembershipFeeResultDTO {
|
||||
fee: MembershipFeeDto;
|
||||
}
|
||||
|
||||
export interface UpsertMembershipFeeViewModel {
|
||||
fee: MembershipFeeDto;
|
||||
}
|
||||
|
||||
export interface IUpsertMembershipFeePresenter extends Presenter<UpsertMembershipFeeResultDTO, UpsertMembershipFeeViewModel> {}
|
||||
12
core/payments/application/presenters/index.ts
Normal file
12
core/payments/application/presenters/index.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export * from './IGetPaymentsPresenter';
|
||||
export * from './ICreatePaymentPresenter';
|
||||
export * from './IUpdatePaymentStatusPresenter';
|
||||
export * from './IGetMembershipFeesPresenter';
|
||||
export * from './IUpsertMembershipFeePresenter';
|
||||
export * from './IUpdateMemberPaymentPresenter';
|
||||
export * from './IGetPrizesPresenter';
|
||||
export * from './ICreatePrizePresenter';
|
||||
export * from './IAwardPrizePresenter';
|
||||
export * from './IDeletePrizePresenter';
|
||||
export * from './IGetWalletPresenter';
|
||||
export * from './IProcessWalletTransactionPresenter';
|
||||
67
core/payments/application/use-cases/AwardPrizeUseCase.ts
Normal file
67
core/payments/application/use-cases/AwardPrizeUseCase.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
/**
|
||||
* Application Use Case: AwardPrizeUseCase
|
||||
*
|
||||
* Awards a prize to a driver.
|
||||
*/
|
||||
|
||||
import type { IPrizeRepository } from '../../domain/repositories/IPrizeRepository';
|
||||
import type {
|
||||
IAwardPrizePresenter,
|
||||
AwardPrizeResultDTO,
|
||||
AwardPrizeViewModel,
|
||||
} from '../presenters/IAwardPrizePresenter';
|
||||
import type { UseCase } from '@gridpilot/shared/application/UseCase';
|
||||
|
||||
export interface AwardPrizeInput {
|
||||
prizeId: string;
|
||||
driverId: string;
|
||||
}
|
||||
|
||||
export class AwardPrizeUseCase
|
||||
implements UseCase<AwardPrizeInput, AwardPrizeResultDTO, AwardPrizeViewModel, IAwardPrizePresenter>
|
||||
{
|
||||
constructor(private readonly prizeRepository: IPrizeRepository) {}
|
||||
|
||||
async execute(
|
||||
input: AwardPrizeInput,
|
||||
presenter: IAwardPrizePresenter,
|
||||
): Promise<void> {
|
||||
presenter.reset();
|
||||
|
||||
const { prizeId, driverId } = input;
|
||||
|
||||
const prize = await this.prizeRepository.findById(prizeId);
|
||||
if (!prize) {
|
||||
throw new Error('Prize not found');
|
||||
}
|
||||
|
||||
if (prize.awarded) {
|
||||
throw new Error('Prize has already been awarded');
|
||||
}
|
||||
|
||||
prize.awarded = true;
|
||||
prize.awardedTo = driverId;
|
||||
prize.awardedAt = new Date();
|
||||
|
||||
const updatedPrize = await this.prizeRepository.update(prize);
|
||||
|
||||
const dto: AwardPrizeResultDTO = {
|
||||
prize: {
|
||||
id: updatedPrize.id,
|
||||
leagueId: updatedPrize.leagueId,
|
||||
seasonId: updatedPrize.seasonId,
|
||||
position: updatedPrize.position,
|
||||
name: updatedPrize.name,
|
||||
amount: updatedPrize.amount,
|
||||
type: updatedPrize.type,
|
||||
description: updatedPrize.description,
|
||||
awarded: updatedPrize.awarded,
|
||||
awardedTo: updatedPrize.awardedTo,
|
||||
awardedAt: updatedPrize.awardedAt,
|
||||
createdAt: updatedPrize.createdAt,
|
||||
},
|
||||
};
|
||||
|
||||
presenter.present(dto);
|
||||
}
|
||||
}
|
||||
79
core/payments/application/use-cases/CreatePaymentUseCase.ts
Normal file
79
core/payments/application/use-cases/CreatePaymentUseCase.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* Application Use Case: CreatePaymentUseCase
|
||||
*
|
||||
* Creates a new payment.
|
||||
*/
|
||||
|
||||
import type { IPaymentRepository } from '../../domain/repositories/IPaymentRepository';
|
||||
import type { PaymentType, PayerType, PaymentStatus, Payment } from '../../domain/entities/Payment';
|
||||
import type {
|
||||
ICreatePaymentPresenter,
|
||||
CreatePaymentResultDTO,
|
||||
CreatePaymentViewModel,
|
||||
} from '../presenters/ICreatePaymentPresenter';
|
||||
import type { UseCase } from '@gridpilot/shared/application/UseCase';
|
||||
|
||||
const PLATFORM_FEE_RATE = 0.10;
|
||||
|
||||
export interface CreatePaymentInput {
|
||||
type: PaymentType;
|
||||
amount: number;
|
||||
payerId: string;
|
||||
payerType: PayerType;
|
||||
leagueId: string;
|
||||
seasonId?: string;
|
||||
}
|
||||
|
||||
export class CreatePaymentUseCase
|
||||
implements UseCase<CreatePaymentInput, CreatePaymentResultDTO, CreatePaymentViewModel, ICreatePaymentPresenter>
|
||||
{
|
||||
constructor(private readonly paymentRepository: IPaymentRepository) {}
|
||||
|
||||
async execute(
|
||||
input: CreatePaymentInput,
|
||||
presenter: ICreatePaymentPresenter,
|
||||
): Promise<void> {
|
||||
presenter.reset();
|
||||
|
||||
const { type, amount, payerId, payerType, leagueId, seasonId } = input;
|
||||
|
||||
const platformFee = amount * PLATFORM_FEE_RATE;
|
||||
const netAmount = amount - platformFee;
|
||||
|
||||
const id = `payment-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
||||
const payment: Payment = {
|
||||
id,
|
||||
type,
|
||||
amount,
|
||||
platformFee,
|
||||
netAmount,
|
||||
payerId,
|
||||
payerType,
|
||||
leagueId,
|
||||
seasonId,
|
||||
status: 'pending' as PaymentStatus,
|
||||
createdAt: new Date(),
|
||||
};
|
||||
|
||||
const createdPayment = await this.paymentRepository.create(payment);
|
||||
|
||||
const dto: CreatePaymentResultDTO = {
|
||||
payment: {
|
||||
id: createdPayment.id,
|
||||
type: createdPayment.type,
|
||||
amount: createdPayment.amount,
|
||||
platformFee: createdPayment.platformFee,
|
||||
netAmount: createdPayment.netAmount,
|
||||
payerId: createdPayment.payerId,
|
||||
payerType: createdPayment.payerType,
|
||||
leagueId: createdPayment.leagueId,
|
||||
seasonId: createdPayment.seasonId,
|
||||
status: createdPayment.status,
|
||||
createdAt: createdPayment.createdAt,
|
||||
completedAt: createdPayment.completedAt,
|
||||
},
|
||||
};
|
||||
|
||||
presenter.present(dto);
|
||||
}
|
||||
}
|
||||
79
core/payments/application/use-cases/CreatePrizeUseCase.ts
Normal file
79
core/payments/application/use-cases/CreatePrizeUseCase.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* Application Use Case: CreatePrizeUseCase
|
||||
*
|
||||
* Creates a new prize.
|
||||
*/
|
||||
|
||||
import type { IPrizeRepository } from '../../domain/repositories/IPrizeRepository';
|
||||
import type { PrizeType, Prize } from '../../domain/entities/Prize';
|
||||
import type {
|
||||
ICreatePrizePresenter,
|
||||
CreatePrizeResultDTO,
|
||||
CreatePrizeViewModel,
|
||||
} from '../presenters/ICreatePrizePresenter';
|
||||
import type { UseCase } from '@gridpilot/shared/application/UseCase';
|
||||
|
||||
export interface CreatePrizeInput {
|
||||
leagueId: string;
|
||||
seasonId: string;
|
||||
position: number;
|
||||
name: string;
|
||||
amount: number;
|
||||
type: PrizeType;
|
||||
description?: string;
|
||||
}
|
||||
|
||||
export class CreatePrizeUseCase
|
||||
implements UseCase<CreatePrizeInput, CreatePrizeResultDTO, CreatePrizeViewModel, ICreatePrizePresenter>
|
||||
{
|
||||
constructor(private readonly prizeRepository: IPrizeRepository) {}
|
||||
|
||||
async execute(
|
||||
input: CreatePrizeInput,
|
||||
presenter: ICreatePrizePresenter,
|
||||
): Promise<void> {
|
||||
presenter.reset();
|
||||
|
||||
const { leagueId, seasonId, position, name, amount, type, description } = input;
|
||||
|
||||
const existingPrize = await this.prizeRepository.findByPosition(leagueId, seasonId, position);
|
||||
if (existingPrize) {
|
||||
throw new Error(`Prize for position ${position} already exists`);
|
||||
}
|
||||
|
||||
const id = `prize-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
||||
const prize: Prize = {
|
||||
id,
|
||||
leagueId,
|
||||
seasonId,
|
||||
position,
|
||||
name,
|
||||
amount,
|
||||
type,
|
||||
description,
|
||||
awarded: false,
|
||||
createdAt: new Date(),
|
||||
};
|
||||
|
||||
const createdPrize = await this.prizeRepository.create(prize);
|
||||
|
||||
const dto: CreatePrizeResultDTO = {
|
||||
prize: {
|
||||
id: createdPrize.id,
|
||||
leagueId: createdPrize.leagueId,
|
||||
seasonId: createdPrize.seasonId,
|
||||
position: createdPrize.position,
|
||||
name: createdPrize.name,
|
||||
amount: createdPrize.amount,
|
||||
type: createdPrize.type,
|
||||
description: createdPrize.description,
|
||||
awarded: createdPrize.awarded,
|
||||
awardedTo: createdPrize.awardedTo,
|
||||
awardedAt: createdPrize.awardedAt,
|
||||
createdAt: createdPrize.createdAt,
|
||||
},
|
||||
};
|
||||
|
||||
presenter.present(dto);
|
||||
}
|
||||
}
|
||||
49
core/payments/application/use-cases/DeletePrizeUseCase.ts
Normal file
49
core/payments/application/use-cases/DeletePrizeUseCase.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
/**
|
||||
* Application Use Case: DeletePrizeUseCase
|
||||
*
|
||||
* Deletes a prize.
|
||||
*/
|
||||
|
||||
import type { IPrizeRepository } from '../../domain/repositories/IPrizeRepository';
|
||||
import type {
|
||||
IDeletePrizePresenter,
|
||||
DeletePrizeResultDTO,
|
||||
DeletePrizeViewModel,
|
||||
} from '../presenters/IDeletePrizePresenter';
|
||||
import type { UseCase } from '@gridpilot/shared/application/UseCase';
|
||||
|
||||
export interface DeletePrizeInput {
|
||||
prizeId: string;
|
||||
}
|
||||
|
||||
export class DeletePrizeUseCase
|
||||
implements UseCase<DeletePrizeInput, DeletePrizeResultDTO, DeletePrizeViewModel, IDeletePrizePresenter>
|
||||
{
|
||||
constructor(private readonly prizeRepository: IPrizeRepository) {}
|
||||
|
||||
async execute(
|
||||
input: DeletePrizeInput,
|
||||
presenter: IDeletePrizePresenter,
|
||||
): Promise<void> {
|
||||
presenter.reset();
|
||||
|
||||
const { prizeId } = input;
|
||||
|
||||
const prize = await this.prizeRepository.findById(prizeId);
|
||||
if (!prize) {
|
||||
throw new Error('Prize not found');
|
||||
}
|
||||
|
||||
if (prize.awarded) {
|
||||
throw new Error('Cannot delete an awarded prize');
|
||||
}
|
||||
|
||||
await this.prizeRepository.delete(prizeId);
|
||||
|
||||
const dto: DeletePrizeResultDTO = {
|
||||
success: true,
|
||||
};
|
||||
|
||||
presenter.present(dto);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
/**
|
||||
* Application Use Case: GetMembershipFeesUseCase
|
||||
*
|
||||
* Retrieves membership fees and member payments.
|
||||
*/
|
||||
|
||||
import type { IMembershipFeeRepository, IMemberPaymentRepository } from '../../domain/repositories/IMembershipFeeRepository';
|
||||
import type {
|
||||
IGetMembershipFeesPresenter,
|
||||
GetMembershipFeesResultDTO,
|
||||
GetMembershipFeesViewModel,
|
||||
} from '../presenters/IGetMembershipFeesPresenter';
|
||||
import type { UseCase } from '@gridpilot/shared/application/UseCase';
|
||||
|
||||
export interface GetMembershipFeesInput {
|
||||
leagueId: string;
|
||||
driverId?: string;
|
||||
}
|
||||
|
||||
export class GetMembershipFeesUseCase
|
||||
implements UseCase<GetMembershipFeesInput, GetMembershipFeesResultDTO, GetMembershipFeesViewModel, IGetMembershipFeesPresenter>
|
||||
{
|
||||
constructor(
|
||||
private readonly membershipFeeRepository: IMembershipFeeRepository,
|
||||
private readonly memberPaymentRepository: IMemberPaymentRepository,
|
||||
) {}
|
||||
|
||||
async execute(
|
||||
input: GetMembershipFeesInput,
|
||||
presenter: IGetMembershipFeesPresenter,
|
||||
): Promise<void> {
|
||||
presenter.reset();
|
||||
|
||||
const { leagueId, driverId } = input;
|
||||
|
||||
if (!leagueId) {
|
||||
throw new Error('leagueId is required');
|
||||
}
|
||||
|
||||
const fee = await this.membershipFeeRepository.findByLeagueId(leagueId);
|
||||
|
||||
let payments: any[] = [];
|
||||
if (driverId && fee) {
|
||||
const memberPayments = await this.memberPaymentRepository.findByLeagueIdAndDriverId(leagueId, driverId, this.membershipFeeRepository);
|
||||
payments = memberPayments.map(p => ({
|
||||
id: p.id,
|
||||
feeId: p.feeId,
|
||||
driverId: p.driverId,
|
||||
amount: p.amount,
|
||||
platformFee: p.platformFee,
|
||||
netAmount: p.netAmount,
|
||||
status: p.status,
|
||||
dueDate: p.dueDate,
|
||||
paidAt: p.paidAt,
|
||||
}));
|
||||
}
|
||||
|
||||
const dto: GetMembershipFeesResultDTO = {
|
||||
fee: fee ? {
|
||||
id: fee.id,
|
||||
leagueId: fee.leagueId,
|
||||
seasonId: fee.seasonId,
|
||||
type: fee.type,
|
||||
amount: fee.amount,
|
||||
enabled: fee.enabled,
|
||||
createdAt: fee.createdAt,
|
||||
updatedAt: fee.updatedAt,
|
||||
} : null,
|
||||
payments,
|
||||
};
|
||||
|
||||
presenter.present(dto);
|
||||
}
|
||||
}
|
||||
58
core/payments/application/use-cases/GetPaymentsUseCase.ts
Normal file
58
core/payments/application/use-cases/GetPaymentsUseCase.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
/**
|
||||
* Application Use Case: GetPaymentsUseCase
|
||||
*
|
||||
* Retrieves payments based on filters.
|
||||
*/
|
||||
|
||||
import type { IPaymentRepository } from '../../domain/repositories/IPaymentRepository';
|
||||
import type { PaymentType } from '../../domain/entities/Payment';
|
||||
import type {
|
||||
IGetPaymentsPresenter,
|
||||
GetPaymentsResultDTO,
|
||||
GetPaymentsViewModel,
|
||||
} from '../presenters/IGetPaymentsPresenter';
|
||||
import type { UseCase } from '@gridpilot/shared/application/UseCase';
|
||||
|
||||
export interface GetPaymentsInput {
|
||||
leagueId?: string;
|
||||
payerId?: string;
|
||||
type?: PaymentType;
|
||||
}
|
||||
|
||||
export class GetPaymentsUseCase
|
||||
implements UseCase<GetPaymentsInput, GetPaymentsResultDTO, GetPaymentsViewModel, IGetPaymentsPresenter>
|
||||
{
|
||||
constructor(private readonly paymentRepository: IPaymentRepository) {}
|
||||
|
||||
async execute(
|
||||
input: GetPaymentsInput,
|
||||
presenter: IGetPaymentsPresenter,
|
||||
): Promise<void> {
|
||||
presenter.reset();
|
||||
|
||||
const payments = await this.paymentRepository.findByFilters({
|
||||
leagueId: input.leagueId,
|
||||
payerId: input.payerId,
|
||||
type: input.type,
|
||||
});
|
||||
|
||||
const dto: GetPaymentsResultDTO = {
|
||||
payments: payments.map(payment => ({
|
||||
id: payment.id,
|
||||
type: payment.type,
|
||||
amount: payment.amount,
|
||||
platformFee: payment.platformFee,
|
||||
netAmount: payment.netAmount,
|
||||
payerId: payment.payerId,
|
||||
payerType: payment.payerType,
|
||||
leagueId: payment.leagueId,
|
||||
seasonId: payment.seasonId,
|
||||
status: payment.status,
|
||||
createdAt: payment.createdAt,
|
||||
completedAt: payment.completedAt,
|
||||
})),
|
||||
};
|
||||
|
||||
presenter.present(dto);
|
||||
}
|
||||
}
|
||||
61
core/payments/application/use-cases/GetPrizesUseCase.ts
Normal file
61
core/payments/application/use-cases/GetPrizesUseCase.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* Application Use Case: GetPrizesUseCase
|
||||
*
|
||||
* Retrieves prizes for a league or season.
|
||||
*/
|
||||
|
||||
import type { IPrizeRepository } from '../../domain/repositories/IPrizeRepository';
|
||||
import type {
|
||||
IGetPrizesPresenter,
|
||||
GetPrizesResultDTO,
|
||||
GetPrizesViewModel,
|
||||
} from '../presenters/IGetPrizesPresenter';
|
||||
import type { UseCase } from '@gridpilot/shared/application/UseCase';
|
||||
|
||||
export interface GetPrizesInput {
|
||||
leagueId: string;
|
||||
seasonId?: string;
|
||||
}
|
||||
|
||||
export class GetPrizesUseCase
|
||||
implements UseCase<GetPrizesInput, GetPrizesResultDTO, GetPrizesViewModel, IGetPrizesPresenter>
|
||||
{
|
||||
constructor(private readonly prizeRepository: IPrizeRepository) {}
|
||||
|
||||
async execute(
|
||||
input: GetPrizesInput,
|
||||
presenter: IGetPrizesPresenter,
|
||||
): Promise<void> {
|
||||
presenter.reset();
|
||||
|
||||
const { leagueId, seasonId } = input;
|
||||
|
||||
let prizes;
|
||||
if (seasonId) {
|
||||
prizes = await this.prizeRepository.findByLeagueIdAndSeasonId(leagueId, seasonId);
|
||||
} else {
|
||||
prizes = await this.prizeRepository.findByLeagueId(leagueId);
|
||||
}
|
||||
|
||||
prizes.sort((a, b) => a.position - b.position);
|
||||
|
||||
const dto: GetPrizesResultDTO = {
|
||||
prizes: prizes.map(prize => ({
|
||||
id: prize.id,
|
||||
leagueId: prize.leagueId,
|
||||
seasonId: prize.seasonId,
|
||||
position: prize.position,
|
||||
name: prize.name,
|
||||
amount: prize.amount,
|
||||
type: prize.type,
|
||||
description: prize.description,
|
||||
awarded: prize.awarded,
|
||||
awardedTo: prize.awardedTo,
|
||||
awardedAt: prize.awardedAt,
|
||||
createdAt: prize.createdAt,
|
||||
})),
|
||||
};
|
||||
|
||||
presenter.present(dto);
|
||||
}
|
||||
}
|
||||
85
core/payments/application/use-cases/GetWalletUseCase.ts
Normal file
85
core/payments/application/use-cases/GetWalletUseCase.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* Application Use Case: GetWalletUseCase
|
||||
*
|
||||
* Retrieves wallet information and transactions.
|
||||
*/
|
||||
|
||||
import type { IWalletRepository, ITransactionRepository } from '../../domain/repositories/IWalletRepository';
|
||||
import type { Wallet } from '../../domain/entities/Wallet';
|
||||
import type {
|
||||
IGetWalletPresenter,
|
||||
GetWalletResultDTO,
|
||||
GetWalletViewModel,
|
||||
} from '../presenters/IGetWalletPresenter';
|
||||
import type { UseCase } from '@gridpilot/shared/application/UseCase';
|
||||
|
||||
export interface GetWalletInput {
|
||||
leagueId: string;
|
||||
}
|
||||
|
||||
export class GetWalletUseCase
|
||||
implements UseCase<GetWalletInput, GetWalletResultDTO, GetWalletViewModel, IGetWalletPresenter>
|
||||
{
|
||||
constructor(
|
||||
private readonly walletRepository: IWalletRepository,
|
||||
private readonly transactionRepository: ITransactionRepository,
|
||||
) {}
|
||||
|
||||
async execute(
|
||||
input: GetWalletInput,
|
||||
presenter: IGetWalletPresenter,
|
||||
): Promise<void> {
|
||||
presenter.reset();
|
||||
|
||||
const { leagueId } = input;
|
||||
|
||||
if (!leagueId) {
|
||||
throw new Error('LeagueId is required');
|
||||
}
|
||||
|
||||
let wallet = await this.walletRepository.findByLeagueId(leagueId);
|
||||
|
||||
if (!wallet) {
|
||||
const id = `wallet-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
||||
const newWallet: Wallet = {
|
||||
id,
|
||||
leagueId,
|
||||
balance: 0,
|
||||
totalRevenue: 0,
|
||||
totalPlatformFees: 0,
|
||||
totalWithdrawn: 0,
|
||||
currency: 'USD',
|
||||
createdAt: new Date(),
|
||||
};
|
||||
wallet = await this.walletRepository.create(newWallet);
|
||||
}
|
||||
|
||||
const transactions = await this.transactionRepository.findByWalletId(wallet.id);
|
||||
transactions.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
|
||||
|
||||
const dto: GetWalletResultDTO = {
|
||||
wallet: {
|
||||
id: wallet.id,
|
||||
leagueId: wallet.leagueId,
|
||||
balance: wallet.balance,
|
||||
totalRevenue: wallet.totalRevenue,
|
||||
totalPlatformFees: wallet.totalPlatformFees,
|
||||
totalWithdrawn: wallet.totalWithdrawn,
|
||||
currency: wallet.currency,
|
||||
createdAt: wallet.createdAt,
|
||||
},
|
||||
transactions: transactions.map(t => ({
|
||||
id: t.id,
|
||||
walletId: t.walletId,
|
||||
type: t.type,
|
||||
amount: t.amount,
|
||||
description: t.description,
|
||||
referenceId: t.referenceId,
|
||||
referenceType: t.referenceType,
|
||||
createdAt: t.createdAt,
|
||||
})),
|
||||
};
|
||||
|
||||
presenter.present(dto);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
/**
|
||||
* Application Use Case: ProcessWalletTransactionUseCase
|
||||
*
|
||||
* Processes a wallet transaction (deposit or withdrawal).
|
||||
*/
|
||||
|
||||
import type { IWalletRepository, ITransactionRepository } from '../../domain/repositories/IWalletRepository';
|
||||
import type { Wallet, Transaction, TransactionType, ReferenceType } from '../../domain/entities/Wallet';
|
||||
import type {
|
||||
IProcessWalletTransactionPresenter,
|
||||
ProcessWalletTransactionResultDTO,
|
||||
ProcessWalletTransactionViewModel,
|
||||
} from '../presenters/IProcessWalletTransactionPresenter';
|
||||
import type { UseCase } from '@gridpilot/shared/application/UseCase';
|
||||
|
||||
export interface ProcessWalletTransactionInput {
|
||||
leagueId: string;
|
||||
type: TransactionType;
|
||||
amount: number;
|
||||
description: string;
|
||||
referenceId?: string;
|
||||
referenceType?: ReferenceType;
|
||||
}
|
||||
|
||||
export class ProcessWalletTransactionUseCase
|
||||
implements UseCase<ProcessWalletTransactionInput, ProcessWalletTransactionResultDTO, ProcessWalletTransactionViewModel, IProcessWalletTransactionPresenter>
|
||||
{
|
||||
constructor(
|
||||
private readonly walletRepository: IWalletRepository,
|
||||
private readonly transactionRepository: ITransactionRepository,
|
||||
) {}
|
||||
|
||||
async execute(
|
||||
input: ProcessWalletTransactionInput,
|
||||
presenter: IProcessWalletTransactionPresenter,
|
||||
): Promise<void> {
|
||||
presenter.reset();
|
||||
|
||||
const { leagueId, type, amount, description, referenceId, referenceType } = input;
|
||||
|
||||
if (!leagueId || !type || amount === undefined || !description) {
|
||||
throw new Error('Missing required fields: leagueId, type, amount, description');
|
||||
}
|
||||
|
||||
if (type !== ('deposit' as TransactionType) && type !== ('withdrawal' as TransactionType)) {
|
||||
throw new Error('Type must be "deposit" or "withdrawal"');
|
||||
}
|
||||
|
||||
let wallet = await this.walletRepository.findByLeagueId(leagueId);
|
||||
|
||||
if (!wallet) {
|
||||
const id = `wallet-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
||||
const newWallet: Wallet = {
|
||||
id,
|
||||
leagueId,
|
||||
balance: 0,
|
||||
totalRevenue: 0,
|
||||
totalPlatformFees: 0,
|
||||
totalWithdrawn: 0,
|
||||
currency: 'USD',
|
||||
createdAt: new Date(),
|
||||
};
|
||||
wallet = await this.walletRepository.create(newWallet);
|
||||
}
|
||||
|
||||
if (type === ('withdrawal' as TransactionType)) {
|
||||
if (amount > wallet.balance) {
|
||||
throw new Error('Insufficient balance');
|
||||
}
|
||||
}
|
||||
|
||||
const transactionId = `txn-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
||||
const transaction: Transaction = {
|
||||
id: transactionId,
|
||||
walletId: wallet.id,
|
||||
type,
|
||||
amount,
|
||||
description,
|
||||
referenceId,
|
||||
referenceType,
|
||||
createdAt: new Date(),
|
||||
};
|
||||
|
||||
const createdTransaction = await this.transactionRepository.create(transaction);
|
||||
|
||||
if (type === ('deposit' as TransactionType)) {
|
||||
wallet.balance += amount;
|
||||
wallet.totalRevenue += amount;
|
||||
} else {
|
||||
wallet.balance -= amount;
|
||||
wallet.totalWithdrawn += amount;
|
||||
}
|
||||
|
||||
const updatedWallet = await this.walletRepository.update(wallet);
|
||||
|
||||
const dto: ProcessWalletTransactionResultDTO = {
|
||||
wallet: {
|
||||
id: updatedWallet.id,
|
||||
leagueId: updatedWallet.leagueId,
|
||||
balance: updatedWallet.balance,
|
||||
totalRevenue: updatedWallet.totalRevenue,
|
||||
totalPlatformFees: updatedWallet.totalPlatformFees,
|
||||
totalWithdrawn: updatedWallet.totalWithdrawn,
|
||||
currency: updatedWallet.currency,
|
||||
createdAt: updatedWallet.createdAt,
|
||||
},
|
||||
transaction: {
|
||||
id: createdTransaction.id,
|
||||
walletId: createdTransaction.walletId,
|
||||
type: createdTransaction.type,
|
||||
amount: createdTransaction.amount,
|
||||
description: createdTransaction.description,
|
||||
referenceId: createdTransaction.referenceId,
|
||||
referenceType: createdTransaction.referenceType,
|
||||
createdAt: createdTransaction.createdAt,
|
||||
},
|
||||
};
|
||||
|
||||
presenter.present(dto);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
/**
|
||||
* Application Use Case: UpdateMemberPaymentUseCase
|
||||
*
|
||||
* Updates a member payment record.
|
||||
*/
|
||||
|
||||
import type { IMembershipFeeRepository, IMemberPaymentRepository } from '../../domain/repositories/IMembershipFeeRepository';
|
||||
import type { MemberPaymentStatus, MemberPayment } from '../../domain/entities/MemberPayment';
|
||||
import type {
|
||||
IUpdateMemberPaymentPresenter,
|
||||
UpdateMemberPaymentResultDTO,
|
||||
UpdateMemberPaymentViewModel,
|
||||
} from '../presenters/IUpdateMemberPaymentPresenter';
|
||||
import type { UseCase } from '@gridpilot/shared/application/UseCase';
|
||||
|
||||
const PLATFORM_FEE_RATE = 0.10;
|
||||
|
||||
export interface UpdateMemberPaymentInput {
|
||||
feeId: string;
|
||||
driverId: string;
|
||||
status?: MemberPaymentStatus;
|
||||
paidAt?: Date | string;
|
||||
}
|
||||
|
||||
export class UpdateMemberPaymentUseCase
|
||||
implements UseCase<UpdateMemberPaymentInput, UpdateMemberPaymentResultDTO, UpdateMemberPaymentViewModel, IUpdateMemberPaymentPresenter>
|
||||
{
|
||||
constructor(
|
||||
private readonly membershipFeeRepository: IMembershipFeeRepository,
|
||||
private readonly memberPaymentRepository: IMemberPaymentRepository,
|
||||
) {}
|
||||
|
||||
async execute(
|
||||
input: UpdateMemberPaymentInput,
|
||||
presenter: IUpdateMemberPaymentPresenter,
|
||||
): Promise<void> {
|
||||
presenter.reset();
|
||||
|
||||
const { feeId, driverId, status, paidAt } = input;
|
||||
|
||||
const fee = await this.membershipFeeRepository.findById(feeId);
|
||||
if (!fee) {
|
||||
throw new Error('Membership fee configuration not found');
|
||||
}
|
||||
|
||||
let payment = await this.memberPaymentRepository.findByFeeIdAndDriverId(feeId, driverId);
|
||||
|
||||
if (!payment) {
|
||||
const platformFee = fee.amount * PLATFORM_FEE_RATE;
|
||||
const netAmount = fee.amount - platformFee;
|
||||
|
||||
const paymentId = `mp-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
||||
const newPayment: MemberPayment = {
|
||||
id: paymentId,
|
||||
feeId,
|
||||
driverId,
|
||||
amount: fee.amount,
|
||||
platformFee,
|
||||
netAmount,
|
||||
status: 'pending' as MemberPaymentStatus,
|
||||
dueDate: new Date(),
|
||||
};
|
||||
payment = await this.memberPaymentRepository.create(newPayment);
|
||||
}
|
||||
|
||||
if (status) {
|
||||
payment.status = status;
|
||||
}
|
||||
if (paidAt || status === ('paid' as MemberPaymentStatus)) {
|
||||
payment.paidAt = paidAt ? new Date(paidAt as string) : new Date();
|
||||
}
|
||||
|
||||
const updatedPayment = await this.memberPaymentRepository.update(payment);
|
||||
|
||||
const dto: UpdateMemberPaymentResultDTO = {
|
||||
payment: {
|
||||
id: updatedPayment.id,
|
||||
feeId: updatedPayment.feeId,
|
||||
driverId: updatedPayment.driverId,
|
||||
amount: updatedPayment.amount,
|
||||
platformFee: updatedPayment.platformFee,
|
||||
netAmount: updatedPayment.netAmount,
|
||||
status: updatedPayment.status,
|
||||
dueDate: updatedPayment.dueDate,
|
||||
paidAt: updatedPayment.paidAt,
|
||||
},
|
||||
};
|
||||
|
||||
presenter.present(dto);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/**
|
||||
* Application Use Case: UpdatePaymentStatusUseCase
|
||||
*
|
||||
* Updates the status of a payment.
|
||||
*/
|
||||
|
||||
import type { IPaymentRepository } from '../../domain/repositories/IPaymentRepository';
|
||||
import type { PaymentStatus } from '../../domain/entities/Payment';
|
||||
import type {
|
||||
IUpdatePaymentStatusPresenter,
|
||||
UpdatePaymentStatusResultDTO,
|
||||
UpdatePaymentStatusViewModel,
|
||||
} from '../presenters/IUpdatePaymentStatusPresenter';
|
||||
import type { UseCase } from '@gridpilot/shared/application/UseCase';
|
||||
|
||||
export interface UpdatePaymentStatusInput {
|
||||
paymentId: string;
|
||||
status: PaymentStatus;
|
||||
}
|
||||
|
||||
export class UpdatePaymentStatusUseCase
|
||||
implements UseCase<UpdatePaymentStatusInput, UpdatePaymentStatusResultDTO, UpdatePaymentStatusViewModel, IUpdatePaymentStatusPresenter>
|
||||
{
|
||||
constructor(private readonly paymentRepository: IPaymentRepository) {}
|
||||
|
||||
async execute(
|
||||
input: UpdatePaymentStatusInput,
|
||||
presenter: IUpdatePaymentStatusPresenter,
|
||||
): Promise<void> {
|
||||
presenter.reset();
|
||||
|
||||
const { paymentId, status } = input;
|
||||
|
||||
const payment = await this.paymentRepository.findById(paymentId);
|
||||
if (!payment) {
|
||||
throw new Error('Payment not found');
|
||||
}
|
||||
|
||||
payment.status = status;
|
||||
if (status === ('completed' as PaymentStatus)) {
|
||||
payment.completedAt = new Date();
|
||||
}
|
||||
|
||||
const updatedPayment = await this.paymentRepository.update(payment);
|
||||
|
||||
const dto: UpdatePaymentStatusResultDTO = {
|
||||
payment: {
|
||||
id: updatedPayment.id,
|
||||
type: updatedPayment.type,
|
||||
amount: updatedPayment.amount,
|
||||
platformFee: updatedPayment.platformFee,
|
||||
netAmount: updatedPayment.netAmount,
|
||||
payerId: updatedPayment.payerId,
|
||||
payerType: updatedPayment.payerType,
|
||||
leagueId: updatedPayment.leagueId,
|
||||
seasonId: updatedPayment.seasonId,
|
||||
status: updatedPayment.status,
|
||||
createdAt: updatedPayment.createdAt,
|
||||
completedAt: updatedPayment.completedAt,
|
||||
},
|
||||
};
|
||||
|
||||
presenter.present(dto);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* Application Use Case: UpsertMembershipFeeUseCase
|
||||
*
|
||||
* Creates or updates membership fee configuration.
|
||||
*/
|
||||
|
||||
import type { IMembershipFeeRepository } from '../../domain/repositories/IMembershipFeeRepository';
|
||||
import type { MembershipFeeType, MembershipFee } from '../../domain/entities/MembershipFee';
|
||||
import type {
|
||||
IUpsertMembershipFeePresenter,
|
||||
UpsertMembershipFeeResultDTO,
|
||||
UpsertMembershipFeeViewModel,
|
||||
} from '../presenters/IUpsertMembershipFeePresenter';
|
||||
import type { UseCase } from '@gridpilot/shared/application/UseCase';
|
||||
|
||||
export interface UpsertMembershipFeeInput {
|
||||
leagueId: string;
|
||||
seasonId?: string;
|
||||
type: MembershipFeeType;
|
||||
amount: number;
|
||||
}
|
||||
|
||||
export class UpsertMembershipFeeUseCase
|
||||
implements UseCase<UpsertMembershipFeeInput, UpsertMembershipFeeResultDTO, UpsertMembershipFeeViewModel, IUpsertMembershipFeePresenter>
|
||||
{
|
||||
constructor(private readonly membershipFeeRepository: IMembershipFeeRepository) {}
|
||||
|
||||
async execute(
|
||||
input: UpsertMembershipFeeInput,
|
||||
presenter: IUpsertMembershipFeePresenter,
|
||||
): Promise<void> {
|
||||
presenter.reset();
|
||||
|
||||
const { leagueId, seasonId, type, amount } = input;
|
||||
|
||||
let existingFee = await this.membershipFeeRepository.findByLeagueId(leagueId);
|
||||
|
||||
let fee: MembershipFee;
|
||||
if (existingFee) {
|
||||
existingFee.type = type;
|
||||
existingFee.amount = amount;
|
||||
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,
|
||||
seasonId,
|
||||
type,
|
||||
amount,
|
||||
enabled: amount > 0,
|
||||
createdAt: new Date(),
|
||||
updatedAt: new Date(),
|
||||
};
|
||||
fee = await this.membershipFeeRepository.create(newFee);
|
||||
}
|
||||
|
||||
const dto: UpsertMembershipFeeResultDTO = {
|
||||
fee: {
|
||||
id: fee.id,
|
||||
leagueId: fee.leagueId,
|
||||
seasonId: fee.seasonId,
|
||||
type: fee.type,
|
||||
amount: fee.amount,
|
||||
enabled: fee.enabled,
|
||||
createdAt: fee.createdAt,
|
||||
updatedAt: fee.updatedAt,
|
||||
},
|
||||
};
|
||||
|
||||
presenter.present(dto);
|
||||
}
|
||||
}
|
||||
12
core/payments/application/use-cases/index.ts
Normal file
12
core/payments/application/use-cases/index.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
export * from './GetPaymentsUseCase';
|
||||
export * from './CreatePaymentUseCase';
|
||||
export * from './UpdatePaymentStatusUseCase';
|
||||
export * from './GetMembershipFeesUseCase';
|
||||
export * from './UpsertMembershipFeeUseCase';
|
||||
export * from './UpdateMemberPaymentUseCase';
|
||||
export * from './GetPrizesUseCase';
|
||||
export * from './CreatePrizeUseCase';
|
||||
export * from './AwardPrizeUseCase';
|
||||
export * from './DeletePrizeUseCase';
|
||||
export * from './GetWalletUseCase';
|
||||
export * from './ProcessWalletTransactionUseCase';
|
||||
21
core/payments/domain/entities/MemberPayment.ts
Normal file
21
core/payments/domain/entities/MemberPayment.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Domain Entity: MemberPayment
|
||||
*/
|
||||
|
||||
export enum MemberPaymentStatus {
|
||||
PENDING = 'pending',
|
||||
PAID = 'paid',
|
||||
OVERDUE = 'overdue',
|
||||
}
|
||||
|
||||
export interface MemberPayment {
|
||||
id: string;
|
||||
feeId: string;
|
||||
driverId: string;
|
||||
amount: number;
|
||||
platformFee: number;
|
||||
netAmount: number;
|
||||
status: MemberPaymentStatus;
|
||||
dueDate: Date;
|
||||
paidAt?: Date;
|
||||
}
|
||||
20
core/payments/domain/entities/MembershipFee.ts
Normal file
20
core/payments/domain/entities/MembershipFee.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
/**
|
||||
* Domain Entity: MembershipFee
|
||||
*/
|
||||
|
||||
export enum MembershipFeeType {
|
||||
SEASON = 'season',
|
||||
MONTHLY = 'monthly',
|
||||
PER_RACE = 'per_race',
|
||||
}
|
||||
|
||||
export interface MembershipFee {
|
||||
id: string;
|
||||
leagueId: string;
|
||||
seasonId?: string;
|
||||
type: MembershipFeeType;
|
||||
amount: number;
|
||||
enabled: boolean;
|
||||
createdAt: Date;
|
||||
updatedAt: Date;
|
||||
}
|
||||
35
core/payments/domain/entities/Payment.ts
Normal file
35
core/payments/domain/entities/Payment.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
/**
|
||||
* Domain Entity: Payment
|
||||
*/
|
||||
|
||||
export enum PaymentType {
|
||||
SPONSORSHIP = 'sponsorship',
|
||||
MEMBERSHIP_FEE = 'membership_fee',
|
||||
}
|
||||
|
||||
export enum PayerType {
|
||||
SPONSOR = 'sponsor',
|
||||
DRIVER = 'driver',
|
||||
}
|
||||
|
||||
export enum PaymentStatus {
|
||||
PENDING = 'pending',
|
||||
COMPLETED = 'completed',
|
||||
FAILED = 'failed',
|
||||
REFUNDED = 'refunded',
|
||||
}
|
||||
|
||||
export interface Payment {
|
||||
id: string;
|
||||
type: PaymentType;
|
||||
amount: number;
|
||||
platformFee: number;
|
||||
netAmount: number;
|
||||
payerId: string;
|
||||
payerType: PayerType;
|
||||
leagueId: string;
|
||||
seasonId?: string;
|
||||
status: PaymentStatus;
|
||||
createdAt: Date;
|
||||
completedAt?: Date;
|
||||
}
|
||||
24
core/payments/domain/entities/Prize.ts
Normal file
24
core/payments/domain/entities/Prize.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
/**
|
||||
* Domain Entity: Prize
|
||||
*/
|
||||
|
||||
export enum PrizeType {
|
||||
CASH = 'cash',
|
||||
MERCHANDISE = 'merchandise',
|
||||
OTHER = 'other',
|
||||
}
|
||||
|
||||
export interface Prize {
|
||||
id: string;
|
||||
leagueId: string;
|
||||
seasonId: string;
|
||||
position: number;
|
||||
name: string;
|
||||
amount: number;
|
||||
type: PrizeType;
|
||||
description?: string;
|
||||
awarded: boolean;
|
||||
awardedTo?: string;
|
||||
awardedAt?: Date;
|
||||
createdAt: Date;
|
||||
}
|
||||
37
core/payments/domain/entities/Wallet.ts
Normal file
37
core/payments/domain/entities/Wallet.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
/**
|
||||
* Domain Entity: Wallet
|
||||
*/
|
||||
|
||||
export interface Wallet {
|
||||
id: string;
|
||||
leagueId: string;
|
||||
balance: number;
|
||||
totalRevenue: number;
|
||||
totalPlatformFees: number;
|
||||
totalWithdrawn: number;
|
||||
currency: string;
|
||||
createdAt: Date;
|
||||
}
|
||||
|
||||
export enum TransactionType {
|
||||
DEPOSIT = 'deposit',
|
||||
WITHDRAWAL = 'withdrawal',
|
||||
PLATFORM_FEE = 'platform_fee',
|
||||
}
|
||||
|
||||
export enum ReferenceType {
|
||||
SPONSORSHIP = 'sponsorship',
|
||||
MEMBERSHIP_FEE = 'membership_fee',
|
||||
PRIZE = 'prize',
|
||||
}
|
||||
|
||||
export interface Transaction {
|
||||
id: string;
|
||||
walletId: string;
|
||||
type: TransactionType;
|
||||
amount: number;
|
||||
description: string;
|
||||
referenceId?: string;
|
||||
referenceType?: ReferenceType;
|
||||
createdAt: Date;
|
||||
}
|
||||
5
core/payments/domain/entities/index.ts
Normal file
5
core/payments/domain/entities/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
export * from './Payment';
|
||||
export * from './MembershipFee';
|
||||
export * from './MemberPayment';
|
||||
export * from './Prize';
|
||||
export * from './Wallet';
|
||||
2
core/payments/domain/index.ts
Normal file
2
core/payments/domain/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './entities';
|
||||
export * from './repositories';
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Repository Interface: IMembershipFeeRepository
|
||||
*/
|
||||
|
||||
import type { MembershipFee } from '../entities/MembershipFee';
|
||||
import type { MemberPayment } from '../entities/MemberPayment';
|
||||
|
||||
export interface IMembershipFeeRepository {
|
||||
findById(id: string): Promise<MembershipFee | null>;
|
||||
findByLeagueId(leagueId: string): Promise<MembershipFee | null>;
|
||||
create(fee: MembershipFee): Promise<MembershipFee>;
|
||||
update(fee: MembershipFee): Promise<MembershipFee>;
|
||||
}
|
||||
|
||||
export interface IMemberPaymentRepository {
|
||||
findById(id: string): Promise<MemberPayment | null>;
|
||||
findByFeeIdAndDriverId(feeId: string, driverId: string): Promise<MemberPayment | null>;
|
||||
findByLeagueIdAndDriverId(leagueId: string, driverId: string, membershipFeeRepo: IMembershipFeeRepository): Promise<MemberPayment[]>;
|
||||
create(payment: MemberPayment): Promise<MemberPayment>;
|
||||
update(payment: MemberPayment): Promise<MemberPayment>;
|
||||
}
|
||||
15
core/payments/domain/repositories/IPaymentRepository.ts
Normal file
15
core/payments/domain/repositories/IPaymentRepository.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Repository Interface: IPaymentRepository
|
||||
*/
|
||||
|
||||
import type { Payment, PaymentType } from '../entities/Payment';
|
||||
|
||||
export interface IPaymentRepository {
|
||||
findById(id: string): Promise<Payment | null>;
|
||||
findByLeagueId(leagueId: string): Promise<Payment[]>;
|
||||
findByPayerId(payerId: string): Promise<Payment[]>;
|
||||
findByType(type: PaymentType): Promise<Payment[]>;
|
||||
findByFilters(filters: { leagueId?: string; payerId?: string; type?: PaymentType }): Promise<Payment[]>;
|
||||
create(payment: Payment): Promise<Payment>;
|
||||
update(payment: Payment): Promise<Payment>;
|
||||
}
|
||||
15
core/payments/domain/repositories/IPrizeRepository.ts
Normal file
15
core/payments/domain/repositories/IPrizeRepository.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Repository Interface: IPrizeRepository
|
||||
*/
|
||||
|
||||
import type { Prize } from '../entities/Prize';
|
||||
|
||||
export interface IPrizeRepository {
|
||||
findById(id: string): Promise<Prize | null>;
|
||||
findByLeagueId(leagueId: string): Promise<Prize[]>;
|
||||
findByLeagueIdAndSeasonId(leagueId: string, seasonId: string): Promise<Prize[]>;
|
||||
findByPosition(leagueId: string, seasonId: string, position: number): Promise<Prize | null>;
|
||||
create(prize: Prize): Promise<Prize>;
|
||||
update(prize: Prize): Promise<Prize>;
|
||||
delete(id: string): Promise<void>;
|
||||
}
|
||||
18
core/payments/domain/repositories/IWalletRepository.ts
Normal file
18
core/payments/domain/repositories/IWalletRepository.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
/**
|
||||
* Repository Interface: IWalletRepository
|
||||
*/
|
||||
|
||||
import type { Wallet, Transaction } from '../entities/Wallet';
|
||||
|
||||
export interface IWalletRepository {
|
||||
findById(id: string): Promise<Wallet | null>;
|
||||
findByLeagueId(leagueId: string): Promise<Wallet | null>;
|
||||
create(wallet: Wallet): Promise<Wallet>;
|
||||
update(wallet: Wallet): Promise<Wallet>;
|
||||
}
|
||||
|
||||
export interface ITransactionRepository {
|
||||
findById(id: string): Promise<Transaction | null>;
|
||||
findByWalletId(walletId: string): Promise<Transaction[]>;
|
||||
create(transaction: Transaction): Promise<Transaction>;
|
||||
}
|
||||
4
core/payments/domain/repositories/index.ts
Normal file
4
core/payments/domain/repositories/index.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export * from './IPaymentRepository';
|
||||
export * from './IMembershipFeeRepository';
|
||||
export * from './IPrizeRepository';
|
||||
export * from './IWalletRepository';
|
||||
2
core/payments/index.ts
Normal file
2
core/payments/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './domain';
|
||||
export * from './application';
|
||||
Reference in New Issue
Block a user