refactor use cases
This commit is contained in:
@@ -2,7 +2,6 @@ import { vi, describe, it, expect, beforeEach } from 'vitest';
|
||||
import { ListUsersUseCase, ListUsersResult } from './ListUsersUseCase';
|
||||
import { IAdminUserRepository } from '../ports/IAdminUserRepository';
|
||||
import { AdminUser } from '../../domain/entities/AdminUser';
|
||||
import { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import { AuthorizationService } from '../../domain/services/AuthorizationService';
|
||||
|
||||
// Mock the authorization service
|
||||
@@ -20,11 +19,6 @@ const mockRepository = {
|
||||
delete: vi.fn(),
|
||||
} as unknown as IAdminUserRepository;
|
||||
|
||||
// Mock output port
|
||||
const mockOutputPort = {
|
||||
present: vi.fn(),
|
||||
} as unknown as UseCaseOutputPort<ListUsersResult>;
|
||||
|
||||
describe('ListUsersUseCase', () => {
|
||||
let useCase: ListUsersUseCase;
|
||||
let actor: AdminUser;
|
||||
@@ -41,7 +35,7 @@ describe('ListUsersUseCase', () => {
|
||||
// Setup default successful authorization
|
||||
vi.mocked(AuthorizationService.canListUsers).mockReturnValue(true);
|
||||
|
||||
useCase = new ListUsersUseCase(mockRepository, mockOutputPort);
|
||||
useCase = new ListUsersUseCase(mockRepository);
|
||||
|
||||
// Create actor (owner)
|
||||
actor = AdminUser.create({
|
||||
@@ -76,7 +70,8 @@ describe('ListUsersUseCase', () => {
|
||||
|
||||
// Assert
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(mockOutputPort.present).toHaveBeenCalledWith({
|
||||
const data = result.unwrap();
|
||||
expect(data).toEqual({
|
||||
users: [],
|
||||
total: 0,
|
||||
page: 1,
|
||||
@@ -120,13 +115,12 @@ describe('ListUsersUseCase', () => {
|
||||
|
||||
// Assert
|
||||
expect(result.isOk()).toBe(true);
|
||||
expect(mockOutputPort.present).toHaveBeenCalledWith({
|
||||
users: [user1, user2],
|
||||
total: 2,
|
||||
page: 1,
|
||||
limit: 10,
|
||||
totalPages: 1,
|
||||
});
|
||||
const data = result.unwrap();
|
||||
expect(data.users).toEqual([user1, user2]);
|
||||
expect(data.total).toBe(2);
|
||||
expect(data.page).toBe(1);
|
||||
expect(data.limit).toBe(10);
|
||||
expect(data.totalPages).toBe(1);
|
||||
});
|
||||
|
||||
it('should filter by role', async () => {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import { Result } from '@core/shared/application/Result';
|
||||
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
||||
import type { UseCaseOutputPort } from '@core/shared/application/UseCaseOutputPort';
|
||||
import type { IAdminUserRepository } from '../ports/IAdminUserRepository';
|
||||
import { AuthorizationService } from '../../domain/services/AuthorizationService';
|
||||
import { UserId } from '../../domain/value-objects/UserId';
|
||||
@@ -46,14 +45,13 @@ export type ListUsersApplicationError = ApplicationErrorCode<ListUsersErrorCode,
|
||||
export class ListUsersUseCase {
|
||||
constructor(
|
||||
private readonly adminUserRepository: IAdminUserRepository,
|
||||
private readonly output: UseCaseOutputPort<ListUsersResult>,
|
||||
) {}
|
||||
|
||||
async execute(
|
||||
input: ListUsersInput,
|
||||
): Promise<
|
||||
Result<
|
||||
void,
|
||||
ListUsersResult,
|
||||
ListUsersApplicationError
|
||||
>
|
||||
> {
|
||||
@@ -137,16 +135,15 @@ export class ListUsersUseCase {
|
||||
|
||||
const result = await this.adminUserRepository.list(query);
|
||||
|
||||
// Pass domain objects to output port
|
||||
this.output.present({
|
||||
const output: ListUsersResult = {
|
||||
users: result.users,
|
||||
total: result.total,
|
||||
page: result.page,
|
||||
limit: result.limit,
|
||||
totalPages: result.totalPages,
|
||||
});
|
||||
};
|
||||
|
||||
return Result.ok(undefined);
|
||||
return Result.ok(output);
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : 'Failed to list users';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user