.
This commit is contained in:
parent
26bc608131
commit
321c27fce9
@ -5,19 +5,37 @@ const generateQuoteReferenceForDealer = async (
|
|||||||
quoteRepository: IQuoteRepository
|
quoteRepository: IQuoteRepository
|
||||||
): Promise<QuoteReference> => {
|
): Promise<QuoteReference> => {
|
||||||
// Obtener la última referencia del dealer
|
// Obtener la última referencia del dealer
|
||||||
const lastQuote = await quoteRepository.findLastQuoteByDealerId(dealer.id);
|
let lastQuoteReference = await quoteRepository.findLastReferenceByDealerId(dealer.id);
|
||||||
|
let isUnique = false;
|
||||||
|
|
||||||
// Generar la nueva referencia usando el Value Object
|
// Referencia temporal
|
||||||
const quoteReferenceResult = QuoteReference.fromPrevious(
|
let newQuoteReference: QuoteReference = QuoteReference.create(undefined).object;
|
||||||
lastQuote ? lastQuote.reference : null,
|
|
||||||
dealer
|
|
||||||
);
|
|
||||||
|
|
||||||
if (quoteReferenceResult.isFailure) {
|
while (!isUnique) {
|
||||||
throw new Error("Error al crear la referencia de presupuesto");
|
// Generar la nueva referencia usando el Value Object
|
||||||
|
const quoteReferenceOrError = QuoteReference.fromPrevious(
|
||||||
|
lastQuoteReference ? lastQuoteReference : null,
|
||||||
|
dealer
|
||||||
|
);
|
||||||
|
|
||||||
|
if (quoteReferenceOrError.isFailure) {
|
||||||
|
throw new Error("Error al crear la referencia de presupuesto");
|
||||||
|
}
|
||||||
|
|
||||||
|
newQuoteReference = quoteReferenceOrError.object;
|
||||||
|
|
||||||
|
// Verificar si la referencia ya existe
|
||||||
|
const existingQuote = await quoteRepository.existsByReference(newQuoteReference);
|
||||||
|
|
||||||
|
if (!existingQuote) {
|
||||||
|
isUnique = true;
|
||||||
|
} else {
|
||||||
|
// Utilizamos la última referencia encontrada para continuar la generación
|
||||||
|
lastQuoteReference = newQuoteReference;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return quoteReferenceResult.object;
|
return newQuoteReference;
|
||||||
};
|
};
|
||||||
|
|
||||||
export { generateQuoteReferenceForDealer };
|
export { generateQuoteReferenceForDealer };
|
||||||
|
|||||||
@ -1,10 +1,12 @@
|
|||||||
/* eslint-disable no-unused-vars */
|
/* eslint-disable no-unused-vars */
|
||||||
import { IRepository } from "@/contexts/common/domain/repositories";
|
import { IRepository } from "@/contexts/common/domain/repositories";
|
||||||
import { ICollection, IQueryCriteria, UniqueID } from "@shared/contexts";
|
import { ICollection, IQueryCriteria, UniqueID } from "@shared/contexts";
|
||||||
import { Quote } from "../entities";
|
import { Quote, QuoteReference } from "../entities";
|
||||||
|
|
||||||
export interface IQuoteRepository extends IRepository<Quote> {
|
export interface IQuoteRepository extends IRepository<Quote> {
|
||||||
exists(id: UniqueID): Promise<boolean>;
|
exists(id: UniqueID): Promise<boolean>;
|
||||||
|
existsByReference(reference: QuoteReference): Promise<boolean>;
|
||||||
|
|
||||||
create(quote: Quote): Promise<void>;
|
create(quote: Quote): Promise<void>;
|
||||||
update(quote: Quote): Promise<void>;
|
update(quote: Quote): Promise<void>;
|
||||||
|
|
||||||
@ -14,4 +16,5 @@ export interface IQuoteRepository extends IRepository<Quote> {
|
|||||||
removeById(id: UniqueID): Promise<void>;
|
removeById(id: UniqueID): Promise<void>;
|
||||||
|
|
||||||
findLastQuoteByDealerId(dealerId: UniqueID): Promise<Quote | null>;
|
findLastQuoteByDealerId(dealerId: UniqueID): Promise<Quote | null>;
|
||||||
|
findLastReferenceByDealerId(dealerId: UniqueID): Promise<QuoteReference | null>;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -3,7 +3,7 @@ import { ICollection, IQueryCriteria, UniqueID } from "@shared/contexts";
|
|||||||
import { ModelDefined, Transaction } from "sequelize";
|
import { ModelDefined, Transaction } from "sequelize";
|
||||||
|
|
||||||
import { IQuoteRepository } from "../domain";
|
import { IQuoteRepository } from "../domain";
|
||||||
import { Quote } from "../domain/entities";
|
import { Quote, QuoteReference } from "../domain/entities";
|
||||||
import { ISalesContext } from "./Sales.context";
|
import { ISalesContext } from "./Sales.context";
|
||||||
import { IQuoteMapper, createQuoteMapper } from "./mappers/quote.mapper";
|
import { IQuoteMapper, createQuoteMapper } from "./mappers/quote.mapper";
|
||||||
|
|
||||||
@ -29,6 +29,10 @@ export class QuoteRepository extends SequelizeRepository<Quote> implements IQuot
|
|||||||
return this._exists("Quote_Model", "id", id.toPrimitive());
|
return this._exists("Quote_Model", "id", id.toPrimitive());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async existsByReference(reference: QuoteReference): Promise<boolean> {
|
||||||
|
return this._exists("Quote_Model", "reference", reference.toPrimitive());
|
||||||
|
}
|
||||||
|
|
||||||
public async create(user: Quote): Promise<void> {
|
public async create(user: Quote): Promise<void> {
|
||||||
const userData = this.mapper.mapToPersistence(user);
|
const userData = this.mapper.mapToPersistence(user);
|
||||||
await this._save("Quote_Model", user.id, userData);
|
await this._save("Quote_Model", user.id, userData);
|
||||||
@ -134,6 +138,11 @@ export class QuoteRepository extends SequelizeRepository<Quote> implements IQuot
|
|||||||
|
|
||||||
return this.mapper.mapToDomain(rawQuote);
|
return this.mapper.mapToDomain(rawQuote);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async findLastReferenceByDealerId(dealerId: UniqueID): Promise<QuoteReference | null> {
|
||||||
|
const quote = await this.findLastQuoteByDealerId(dealerId);
|
||||||
|
return quote ? quote.reference : null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const registerQuoteRepository = (context: ISalesContext) => {
|
export const registerQuoteRepository = (context: ISalesContext) => {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user