2024-05-21 16:48:40 +00:00
|
|
|
import {
|
|
|
|
|
IUseCase,
|
|
|
|
|
IUseCaseError,
|
|
|
|
|
IUseCaseRequest,
|
|
|
|
|
UseCaseError,
|
|
|
|
|
} from "@/contexts/common/application/useCases";
|
|
|
|
|
import { IRepositoryManager } from "@/contexts/common/domain";
|
|
|
|
|
import { ISequelizeAdapter } from "@/contexts/common/infrastructure/sequelize";
|
|
|
|
|
import { Result, UniqueID } from "@shared/contexts";
|
2024-05-26 17:09:43 +00:00
|
|
|
import { IQuoteRepository } from "../../domain";
|
2024-05-21 16:48:40 +00:00
|
|
|
|
|
|
|
|
import { IInfrastructureError } from "@/contexts/common/infrastructure";
|
2024-05-26 17:09:43 +00:00
|
|
|
import { Quote } from "../../domain/entities/Quote";
|
2024-05-21 16:48:40 +00:00
|
|
|
|
2024-05-26 17:09:43 +00:00
|
|
|
export interface IGetQuoteUseCaseRequest extends IUseCaseRequest {
|
2024-05-21 16:48:40 +00:00
|
|
|
id: UniqueID;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-26 17:09:43 +00:00
|
|
|
export type GetQuoteResponseOrError =
|
2024-05-21 16:48:40 +00:00
|
|
|
| Result<never, IUseCaseError> // Misc errors (value objects)
|
2024-05-26 17:09:43 +00:00
|
|
|
| Result<Quote, never>; // Success!
|
2024-05-21 16:48:40 +00:00
|
|
|
|
2024-05-26 17:09:43 +00:00
|
|
|
export class GetQuoteUseCase
|
|
|
|
|
implements IUseCase<IGetQuoteUseCaseRequest, Promise<GetQuoteResponseOrError>>
|
2024-05-21 16:48:40 +00:00
|
|
|
{
|
|
|
|
|
private _adapter: ISequelizeAdapter;
|
|
|
|
|
private _repositoryManager: IRepositoryManager;
|
|
|
|
|
|
|
|
|
|
constructor(props: { adapter: ISequelizeAdapter; repositoryManager: IRepositoryManager }) {
|
|
|
|
|
this._adapter = props.adapter;
|
|
|
|
|
this._repositoryManager = props.repositoryManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getRepositoryByName<T>(name: string) {
|
|
|
|
|
return this._repositoryManager.getRepository<T>(name);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-26 17:09:43 +00:00
|
|
|
async execute(request: IGetQuoteUseCaseRequest): Promise<GetQuoteResponseOrError> {
|
2024-05-21 16:48:40 +00:00
|
|
|
const { id } = request;
|
|
|
|
|
|
|
|
|
|
// Validación de datos
|
|
|
|
|
// No hay en este caso
|
|
|
|
|
|
2024-05-26 17:09:43 +00:00
|
|
|
return await this.findQuote(id);
|
2024-05-21 16:48:40 +00:00
|
|
|
}
|
|
|
|
|
|
2024-05-26 17:09:43 +00:00
|
|
|
private async findQuote(id: UniqueID) {
|
2024-05-21 16:48:40 +00:00
|
|
|
const transaction = this._adapter.startTransaction();
|
2024-05-26 17:09:43 +00:00
|
|
|
const QuoteRepoBuilder = this._getQuoteRepository();
|
2024-05-21 16:48:40 +00:00
|
|
|
|
2024-05-26 17:09:43 +00:00
|
|
|
let Quote: Quote | null = null;
|
2024-05-21 16:48:40 +00:00
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await transaction.complete(async (t) => {
|
2024-05-26 17:09:43 +00:00
|
|
|
const QuoteRepo = QuoteRepoBuilder({ transaction: t });
|
|
|
|
|
Quote = await QuoteRepo.getById(id);
|
2024-05-21 16:48:40 +00:00
|
|
|
});
|
|
|
|
|
|
2024-05-26 17:09:43 +00:00
|
|
|
if (!Quote) {
|
|
|
|
|
return Result.fail(UseCaseError.create(UseCaseError.NOT_FOUND_ERROR, "Quote not found"));
|
2024-05-21 16:48:40 +00:00
|
|
|
}
|
|
|
|
|
|
2024-05-26 17:09:43 +00:00
|
|
|
return Result.ok<Quote>(Quote!);
|
2024-05-21 16:48:40 +00:00
|
|
|
} catch (error: unknown) {
|
|
|
|
|
const _error = error as IInfrastructureError;
|
|
|
|
|
return Result.fail(
|
|
|
|
|
UseCaseError.create(UseCaseError.REPOSITORY_ERROR, "Error al consultar el usuario", _error)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-26 17:09:43 +00:00
|
|
|
|
|
|
|
|
private _getQuoteRepository() {
|
|
|
|
|
return this._repositoryManager.getRepository<IQuoteRepository>("Quote");
|
|
|
|
|
}
|
2024-05-21 16:48:40 +00:00
|
|
|
}
|