45 lines
1.4 KiB
TypeScript
45 lines
1.4 KiB
TypeScript
import type { IDriverRepository } from '../../domain/repositories/IDriverRepository';
|
|
import { Result } from '@core/shared/application/Result';
|
|
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
|
|
import type { UseCaseOutputPort, UseCase } from '@core/shared/application';
|
|
|
|
/**
|
|
* Input type for retrieving total number of drivers.
|
|
*/
|
|
export type GetTotalDriversInput = {};
|
|
|
|
/**
|
|
* Domain result model for the total number of drivers.
|
|
*/
|
|
export type GetTotalDriversResult = {
|
|
totalDrivers: number;
|
|
};
|
|
|
|
export type GetTotalDriversErrorCode = 'REPOSITORY_ERROR';
|
|
|
|
export class GetTotalDriversUseCase implements UseCase<GetTotalDriversInput, void, GetTotalDriversErrorCode> {
|
|
constructor(
|
|
private readonly driverRepository: IDriverRepository,
|
|
private readonly output: UseCaseOutputPort<GetTotalDriversResult>,
|
|
) {}
|
|
|
|
async execute(
|
|
_input: GetTotalDriversInput,
|
|
): Promise<Result<void, ApplicationErrorCode<GetTotalDriversErrorCode, { message: string }>>> {
|
|
try {
|
|
const drivers = await this.driverRepository.findAll();
|
|
const result: GetTotalDriversResult = { totalDrivers: drivers.length };
|
|
|
|
this.output.present(result);
|
|
|
|
return Result.ok(undefined);
|
|
} catch (error) {
|
|
const message = (error as Error | undefined)?.message ?? 'Failed to compute total drivers';
|
|
|
|
return Result.err({
|
|
code: 'REPOSITORY_ERROR',
|
|
details: { message },
|
|
});
|
|
}
|
|
}
|
|
} |