2024-05-26 17:09:43 +00:00
|
|
|
import { IUseCase, IUseCaseError, UseCaseError } from "@/contexts/common/application";
|
|
|
|
|
import { IRepositoryManager } from "@/contexts/common/domain";
|
|
|
|
|
import { IInfrastructureError } from "@/contexts/common/infrastructure";
|
|
|
|
|
import { ISequelizeAdapter } from "@/contexts/common/infrastructure/sequelize";
|
|
|
|
|
import {
|
|
|
|
|
Collection,
|
|
|
|
|
Currency,
|
|
|
|
|
Description,
|
|
|
|
|
DomainError,
|
|
|
|
|
ICreateQuote_Request_DTO,
|
|
|
|
|
IDomainError,
|
|
|
|
|
Language,
|
2024-07-01 17:12:15 +00:00
|
|
|
Note,
|
2024-05-26 17:09:43 +00:00
|
|
|
Quantity,
|
|
|
|
|
Result,
|
|
|
|
|
UTCDateValue,
|
|
|
|
|
UniqueID,
|
|
|
|
|
UnitPrice,
|
|
|
|
|
ensureIdIsValid,
|
|
|
|
|
} from "@shared/contexts";
|
2024-07-01 17:12:15 +00:00
|
|
|
import { IQuoteRepository, Quote, QuoteCustomer, QuoteItem, QuoteStatus } from "../../domain";
|
2024-05-26 17:09:43 +00:00
|
|
|
|
|
|
|
|
export type CreateQuoteResponseOrError =
|
|
|
|
|
| Result<never, IUseCaseError> // Misc errors (value objects)
|
|
|
|
|
| Result<Quote, never>; // Success!
|
|
|
|
|
|
|
|
|
|
export class CreateQuoteUseCase
|
|
|
|
|
implements IUseCase<ICreateQuote_Request_DTO, Promise<CreateQuoteResponseOrError>>
|
|
|
|
|
{
|
|
|
|
|
private _adapter: ISequelizeAdapter;
|
|
|
|
|
private _repositoryManager: IRepositoryManager;
|
|
|
|
|
|
|
|
|
|
constructor(props: { adapter: ISequelizeAdapter; repositoryManager: IRepositoryManager }) {
|
|
|
|
|
this._adapter = props.adapter;
|
|
|
|
|
this._repositoryManager = props.repositoryManager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async execute(request: ICreateQuote_Request_DTO) {
|
|
|
|
|
const { id } = request;
|
|
|
|
|
|
|
|
|
|
// Validaciones de datos
|
|
|
|
|
const idOrError = ensureIdIsValid(id);
|
|
|
|
|
if (idOrError.isFailure) {
|
|
|
|
|
const message = idOrError.error.message; //`Quote ID ${quoteDTO.id} is not valid`;
|
|
|
|
|
return Result.fail(
|
|
|
|
|
UseCaseError.create(UseCaseError.INVALID_INPUT_DATA, message, [{ path: "id" }])
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Comprobar que no existe un usuario previo con esos datos
|
|
|
|
|
const quoteRepository = this._getQuoteRepository();
|
|
|
|
|
|
|
|
|
|
const idExists = await quoteRepository().exists(idOrError.object);
|
|
|
|
|
if (idExists) {
|
|
|
|
|
const message = `Another quote with same ID exists`;
|
|
|
|
|
return Result.fail(
|
|
|
|
|
UseCaseError.create(UseCaseError.RESOURCE_ALREADY_EXITS, message, {
|
|
|
|
|
path: "id",
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Crear quote
|
|
|
|
|
const quoteOrError = this._tryCreateQuoteInstance(request, idOrError.object);
|
|
|
|
|
|
|
|
|
|
if (quoteOrError.isFailure) {
|
|
|
|
|
const { error: domainError } = quoteOrError;
|
|
|
|
|
let errorCode = "";
|
|
|
|
|
let message = "";
|
|
|
|
|
|
|
|
|
|
switch (domainError.code) {
|
|
|
|
|
case DomainError.INVALID_INPUT_DATA:
|
|
|
|
|
errorCode = UseCaseError.INVALID_INPUT_DATA;
|
|
|
|
|
message = "El usuario tiene algún dato erróneo.";
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
errorCode = UseCaseError.UNEXCEPTED_ERROR;
|
|
|
|
|
message = domainError.message;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Result.fail(UseCaseError.create(errorCode, message, domainError));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return this._saveQuote(quoteOrError.object);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async _saveQuote(quote: Quote) {
|
|
|
|
|
// Guardar el contacto
|
|
|
|
|
const transaction = this._adapter.startTransaction();
|
|
|
|
|
const quoteRepository = this._getQuoteRepository();
|
|
|
|
|
let quoteRepo: IQuoteRepository;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
await transaction.complete(async (t) => {
|
|
|
|
|
quoteRepo = quoteRepository({ transaction: t });
|
|
|
|
|
await quoteRepo.create(quote);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return Result.ok<Quote>(quote);
|
|
|
|
|
} catch (error: unknown) {
|
|
|
|
|
const _error = error as IInfrastructureError;
|
|
|
|
|
return Result.fail(UseCaseError.create(UseCaseError.REPOSITORY_ERROR, _error.message));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _tryCreateQuoteInstance(
|
|
|
|
|
quoteDTO: ICreateQuote_Request_DTO,
|
|
|
|
|
quoteId: UniqueID
|
|
|
|
|
): Result<Quote, IDomainError> {
|
|
|
|
|
const statusOrError = QuoteStatus.create(quoteDTO.status);
|
|
|
|
|
if (statusOrError.isFailure) {
|
|
|
|
|
return Result.fail(statusOrError.error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const dateOrError = UTCDateValue.create(quoteDTO.date);
|
|
|
|
|
if (dateOrError.isFailure) {
|
|
|
|
|
return Result.fail(dateOrError.error);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-01 17:12:15 +00:00
|
|
|
const referenceOrError = QuoteStatus.create(quoteDTO.reference);
|
|
|
|
|
if (referenceOrError.isFailure) {
|
|
|
|
|
return Result.fail(referenceOrError.error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const languageOrError = Language.createFromCode(quoteDTO.lang_code);
|
2024-05-26 17:09:43 +00:00
|
|
|
if (languageOrError.isFailure) {
|
|
|
|
|
return Result.fail(languageOrError.error);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-01 17:12:15 +00:00
|
|
|
const customerOrError = QuoteCustomer.create(quoteDTO.customer_information);
|
|
|
|
|
if (customerOrError.isFailure) {
|
|
|
|
|
return Result.fail(customerOrError.error);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-26 17:09:43 +00:00
|
|
|
const currencyOrError = Currency.createFromCode(quoteDTO.currency_code);
|
|
|
|
|
if (currencyOrError.isFailure) {
|
|
|
|
|
return Result.fail(currencyOrError.error);
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-01 17:12:15 +00:00
|
|
|
const paymentOrError = Note.create(quoteDTO.payment_method);
|
|
|
|
|
if (paymentOrError.isFailure) {
|
|
|
|
|
return Result.fail(paymentOrError.error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const notesOrError = Note.create(quoteDTO.notes);
|
|
|
|
|
if (notesOrError.isFailure) {
|
|
|
|
|
return Result.fail(notesOrError.error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const validityOrError = Note.create(quoteDTO.validity);
|
|
|
|
|
if (validityOrError.isFailure) {
|
|
|
|
|
return Result.fail(validityOrError.error);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-26 17:09:43 +00:00
|
|
|
const items = new Collection<QuoteItem>(
|
|
|
|
|
quoteDTO.items?.map(
|
|
|
|
|
(item) =>
|
|
|
|
|
QuoteItem.create({
|
|
|
|
|
description: Description.create(item.description).object,
|
2024-07-01 17:12:15 +00:00
|
|
|
quantity: Quantity.create({ amount: item.quantity, precision: 4 }).object,
|
2024-05-26 17:09:43 +00:00
|
|
|
unitPrice: UnitPrice.create({
|
|
|
|
|
amount: item.unit_price.amount,
|
|
|
|
|
currencyCode: item.unit_price.currency,
|
|
|
|
|
precision: item.unit_price.precision,
|
|
|
|
|
}).object,
|
|
|
|
|
}).object
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return Quote.create(
|
|
|
|
|
{
|
|
|
|
|
status: statusOrError.object,
|
|
|
|
|
date: dateOrError.object,
|
2024-07-01 17:12:15 +00:00
|
|
|
reference: referenceOrError.object,
|
2024-05-26 17:09:43 +00:00
|
|
|
language: languageOrError.object,
|
2024-07-01 17:12:15 +00:00
|
|
|
customer: customerOrError.object,
|
2024-05-26 17:09:43 +00:00
|
|
|
currency: currencyOrError.object,
|
2024-07-01 17:12:15 +00:00
|
|
|
paymentMethod: paymentOrError.object,
|
|
|
|
|
notes: notesOrError.object,
|
|
|
|
|
validity: validityOrError.object,
|
|
|
|
|
|
2024-05-26 17:09:43 +00:00
|
|
|
items,
|
|
|
|
|
},
|
|
|
|
|
quoteId
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private _getQuoteRepository() {
|
|
|
|
|
return this._repositoryManager.getRepository<IQuoteRepository>("Quote");
|
|
|
|
|
}
|
|
|
|
|
}
|