refactor use cases

This commit is contained in:
2026-01-08 15:34:51 +01:00
parent d984ab24a8
commit 52e9a2f6a7
362 changed files with 5192 additions and 8409 deletions

View File

@@ -73,11 +73,7 @@ describe('AcceptSponsorshipRequestUseCase', () => {
};
});
it('should send notification to sponsor, process payment, update wallets, and present result when accepting season sponsorship', async () => {
const output = {
present: vi.fn(),
};
it('should send notification to sponsor, process payment, update wallets, and return result when accepting season sponsorship', async () => {
const useCase = new AcceptSponsorshipRequestUseCase(
mockSponsorshipRequestRepo as unknown as ISponsorshipRequestRepository,
mockSeasonSponsorshipRepo as unknown as ISeasonSponsorshipRepository,
@@ -87,7 +83,6 @@ describe('AcceptSponsorshipRequestUseCase', () => {
mockWalletRepo as unknown as IWalletRepository,
mockLeagueWalletRepo as unknown as ILeagueWalletRepository,
mockLogger as unknown as Logger,
output,
);
const request = SponsorshipRequest.create({
@@ -140,7 +135,13 @@ describe('AcceptSponsorshipRequestUseCase', () => {
});
expect(result.isOk()).toBe(true);
expect(result.unwrap()).toBeUndefined();
const successResult = result.unwrap();
expect(successResult.requestId).toBe('req1');
expect(successResult.status).toBe('accepted');
expect(successResult.sponsorshipId).toBeDefined();
expect(successResult.acceptedAt).toBeInstanceOf(Date);
expect(successResult.platformFee).toBeDefined();
expect(successResult.netAmount).toBeDefined();
expect(mockNotificationService.sendNotification).toHaveBeenCalledWith({
recipientId: 'sponsor1',
@@ -189,14 +190,61 @@ describe('AcceptSponsorshipRequestUseCase', () => {
expect(asString(updatedLeagueWalletId)).toBe('league1');
expect(updatedLeagueWalletBalanceAmount).toBe(1400);
expect(output.present).toHaveBeenCalledWith({
requestId: 'req1',
sponsorshipId: expect.any(String),
status: 'accepted',
acceptedAt: expect.any(Date),
platformFee: expect.any(Number),
netAmount: expect.any(Number),
});
});
});
it('should return error when sponsorship request not found', async () => {
const useCase = new AcceptSponsorshipRequestUseCase(
mockSponsorshipRequestRepo as unknown as ISponsorshipRequestRepository,
mockSeasonSponsorshipRepo as unknown as ISeasonSponsorshipRepository,
mockSeasonRepo as unknown as ISeasonRepository,
mockNotificationService as unknown as NotificationService,
processPayment,
mockWalletRepo as unknown as IWalletRepository,
mockLeagueWalletRepo as unknown as ILeagueWalletRepository,
mockLogger as unknown as Logger,
);
mockSponsorshipRequestRepo.findById.mockResolvedValue(null);
const result = await useCase.execute({
requestId: 'req1',
respondedBy: 'driver1',
});
expect(result.isErr()).toBe(true);
expect(result.unwrapErr().code).toBe('SPONSORSHIP_REQUEST_NOT_FOUND');
});
it('should return error when sponsorship request is not pending', async () => {
const useCase = new AcceptSponsorshipRequestUseCase(
mockSponsorshipRequestRepo as unknown as ISponsorshipRequestRepository,
mockSeasonSponsorshipRepo as unknown as ISeasonSponsorshipRepository,
mockSeasonRepo as unknown as ISeasonRepository,
mockNotificationService as unknown as NotificationService,
processPayment,
mockWalletRepo as unknown as IWalletRepository,
mockLeagueWalletRepo as unknown as ILeagueWalletRepository,
mockLogger as unknown as Logger,
);
const request = SponsorshipRequest.create({
id: 'req1',
sponsorId: 'sponsor1',
entityId: 'season1',
entityType: 'season',
tier: 'main',
offeredAmount: Money.create(1000),
status: 'accepted',
});
mockSponsorshipRequestRepo.findById.mockResolvedValue(request);
const result = await useCase.execute({
requestId: 'req1',
respondedBy: 'driver1',
});
expect(result.isErr()).toBe(true);
expect(result.unwrapErr().code).toBe('SPONSORSHIP_REQUEST_NOT_PENDING');
});
});