Alta de proformas
This commit is contained in:
parent
941ad25401
commit
bba38e67f2
@ -8,8 +8,7 @@
|
||||
"dev": "node --import=tsx --watch src/index.ts",
|
||||
"clean": "rimraf .turbo node_modules dist",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"lint": "biome check . && eslint .",
|
||||
"lint:fix": "biome check --write . && eslint . --fix",
|
||||
"lint": "biome lint --fix",
|
||||
"format": "biome format --write"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@ -41,13 +41,20 @@ import {
|
||||
*
|
||||
*/
|
||||
|
||||
export interface ICreateProformaInputMapper
|
||||
/*export interface ICreateProformaInputMapper
|
||||
extends IDTOInputToPropsMapper<
|
||||
CreateProformaRequestDTO,
|
||||
{ id: UniqueID; props: Omit<IProformaProps, "items"> & { items: IProformaItemProps[] } }
|
||||
> {}
|
||||
> {}*/
|
||||
|
||||
export class CreateProformaInputMapper implements ICreateProformaInputMapper {
|
||||
export interface ICreateProformaInputMapper {
|
||||
map(
|
||||
dto: CreateProformaRequestDTO,
|
||||
params: { companyId: UniqueID }
|
||||
): Result<{ id: UniqueID; props: IProformaProps }>;
|
||||
}
|
||||
|
||||
export class CreateProformaInputMapper /*implements ICreateProformaInputMapper*/ {
|
||||
private readonly taxCatalog: JsonTaxCatalogProvider;
|
||||
|
||||
constructor(params: { taxCatalog: JsonTaxCatalogProvider }) {
|
||||
@ -138,14 +145,14 @@ export class CreateProformaInputMapper implements ICreateProformaInputMapper {
|
||||
|
||||
const globalDiscountPercentage = extractOrPushError(
|
||||
Percentage.create({
|
||||
value: Number(dto.discount_percentage.value),
|
||||
scale: Number(dto.discount_percentage.scale),
|
||||
value: Number(dto.global_discount_percentage.value),
|
||||
scale: Number(dto.global_discount_percentage.scale),
|
||||
}),
|
||||
"discount_percentage",
|
||||
errors
|
||||
);
|
||||
|
||||
const items = this.mapItems(dto, {
|
||||
const itemsProps = this.mapItemsProps(dto, {
|
||||
languageCode: languageCode!,
|
||||
currencyCode: currencyCode!,
|
||||
globalDiscountPercentage: globalDiscountPercentage!,
|
||||
@ -158,7 +165,7 @@ export class CreateProformaInputMapper implements ICreateProformaInputMapper {
|
||||
);
|
||||
}
|
||||
|
||||
const props: Omit<IProformaProps, "items"> & { items: IProformaItemProps[] } = {
|
||||
const props: IProformaProps = {
|
||||
companyId,
|
||||
status: defaultStatus,
|
||||
|
||||
@ -181,7 +188,7 @@ export class CreateProformaInputMapper implements ICreateProformaInputMapper {
|
||||
paymentMethod: paymentMethod!,
|
||||
globalDiscountPercentage: globalDiscountPercentage!,
|
||||
|
||||
items, // ← IProformaItemProps[]
|
||||
items: itemsProps, // ← IProformaItemProps[]
|
||||
};
|
||||
|
||||
return Result.ok({
|
||||
@ -193,7 +200,7 @@ export class CreateProformaInputMapper implements ICreateProformaInputMapper {
|
||||
}
|
||||
}
|
||||
|
||||
private mapItems(
|
||||
private mapItemsProps(
|
||||
dto: CreateProformaRequestDTO,
|
||||
params: {
|
||||
languageCode: LanguageCode;
|
||||
@ -224,12 +231,12 @@ export class CreateProformaInputMapper implements ICreateProformaInputMapper {
|
||||
);
|
||||
|
||||
const discountPercentage = extractOrPushError(
|
||||
maybeFromNullableResult(item.discount_percentage, (v) => DiscountPercentage.create(v)),
|
||||
maybeFromNullableResult(item.item_discount_percentage, (v) => DiscountPercentage.create(v)),
|
||||
`items[${index}].discount_percentage`,
|
||||
params.errors
|
||||
);
|
||||
|
||||
const taxes = this.mapTaxes(item.taxes, {
|
||||
const taxes = this.mapTaxesProps(item.taxes, {
|
||||
itemIndex: index,
|
||||
errors: params.errors,
|
||||
});
|
||||
@ -252,7 +259,7 @@ export class CreateProformaInputMapper implements ICreateProformaInputMapper {
|
||||
|
||||
/* Devuelve las propiedades de los impustos de una línea de detalle */
|
||||
|
||||
private mapTaxes(
|
||||
private mapTaxesProps(
|
||||
taxesDTO: Pick<CreateProformaItemRequestDTO, "taxes">["taxes"],
|
||||
params: { itemIndex: number; errors: ValidationErrorDetail[] }
|
||||
): ProformaItemTaxesProps {
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
import { InvoiceSerie, type ProformaPatchProps } from "@erp/customer-invoices/api/domain";
|
||||
import {
|
||||
CurrencyCode,
|
||||
DomainError,
|
||||
@ -14,6 +13,7 @@ import {
|
||||
import { Result, isNullishOrEmpty, toPatchField } from "@repo/rdx-utils";
|
||||
|
||||
import type { UpdateProformaByIdRequestDTO } from "../../../../../common/dto";
|
||||
import type { ProformaPatchProps } from "../../../../domain";
|
||||
|
||||
/**
|
||||
* UpdateProformaPropsMapper
|
||||
|
||||
@ -2,31 +2,31 @@ import type { UniqueID } from "@repo/rdx-ddd";
|
||||
import { Result } from "@repo/rdx-utils";
|
||||
import type { Transaction } from "sequelize";
|
||||
|
||||
import type { ICustomerInvoiceRepository } from "../../../domain";
|
||||
import type { CustomerInvoice, CustomerInvoiceProps } from "../../../domain/aggregates";
|
||||
import type { IProformaProps, Proforma } from "../../../domain";
|
||||
import type { IProformaFactory } from "../factories";
|
||||
import type { IProformaRepository } from "../repositories";
|
||||
|
||||
import type { IProformaNumberGenerator } from "./proforma-number-generator.interface";
|
||||
|
||||
export interface IProformaCreator {
|
||||
create(
|
||||
companyId: UniqueID,
|
||||
id: UniqueID,
|
||||
props: CustomerInvoiceProps,
|
||||
transaction: Transaction
|
||||
): Promise<Result<CustomerInvoice, Error>>;
|
||||
create(params: {
|
||||
companyId: UniqueID;
|
||||
id: UniqueID;
|
||||
props: IProformaProps;
|
||||
transaction: Transaction;
|
||||
}): Promise<Result<Proforma, Error>>;
|
||||
}
|
||||
|
||||
type ProformaCreatorDeps = {
|
||||
numberService: IProformaNumberGenerator;
|
||||
factory: IProformaFactory;
|
||||
repository: ICustomerInvoiceRepository;
|
||||
repository: IProformaRepository;
|
||||
};
|
||||
|
||||
export class ProformaCreator implements IProformaCreator {
|
||||
private readonly numberService: IProformaNumberGenerator;
|
||||
private readonly factory: IProformaFactory;
|
||||
private readonly repository: ICustomerInvoiceRepository;
|
||||
private readonly repository: IProformaRepository;
|
||||
|
||||
constructor(deps: ProformaCreatorDeps) {
|
||||
this.numberService = deps.numberService;
|
||||
@ -34,12 +34,14 @@ export class ProformaCreator implements IProformaCreator {
|
||||
this.repository = deps.repository;
|
||||
}
|
||||
|
||||
async create(
|
||||
companyId: UniqueID,
|
||||
id: UniqueID,
|
||||
props: CustomerInvoiceProps,
|
||||
transaction: Transaction
|
||||
): Promise<Result<CustomerInvoice, Error>> {
|
||||
async create(params: {
|
||||
companyId: UniqueID;
|
||||
id: UniqueID;
|
||||
props: IProformaProps;
|
||||
transaction: Transaction;
|
||||
}): Promise<Result<Proforma, Error>> {
|
||||
const { companyId, id, props, transaction } = params;
|
||||
|
||||
// 1. Obtener siguiente número
|
||||
const { series } = props;
|
||||
const numberResult = await this.numberService.getNextForCompany(companyId, series, transaction);
|
||||
|
||||
@ -36,16 +36,16 @@ export class CreateProformaUseCase {
|
||||
const { dto, companyId } = params;
|
||||
|
||||
// 1) Mapear DTO → props de dominio
|
||||
const mappedResult = this.dtoMapper.map(dto, companyId);
|
||||
if (mappedResult.isFailure) {
|
||||
return Result.fail(mappedResult.error);
|
||||
const mappedPropsResult = this.dtoMapper.map(dto, { companyId });
|
||||
if (mappedPropsResult.isFailure) {
|
||||
return Result.fail(mappedPropsResult.error);
|
||||
}
|
||||
|
||||
const { props, id } = mappedResult.data;
|
||||
const { props, id } = mappedPropsResult.data;
|
||||
|
||||
return this.transactionManager.complete(async (transaction) => {
|
||||
try {
|
||||
const createResult = await this.creator.create(companyId, id, props, transaction);
|
||||
const createResult = await this.creator.create({ companyId, id, props, transaction });
|
||||
|
||||
if (createResult.isFailure) {
|
||||
return Result.fail(createResult.error);
|
||||
|
||||
@ -23,10 +23,9 @@ import {
|
||||
import {
|
||||
type IProformaItemProps,
|
||||
type IProformaItems,
|
||||
type IProformaItemsProps,
|
||||
ProformaItem,
|
||||
ProformaItems,
|
||||
} from "../entities/proforma-items";
|
||||
} from "../entities";
|
||||
import { ProformaItemMismatch } from "../errors";
|
||||
import { type IProformaTaxTotals, ProformaTaxesCalculator } from "../services";
|
||||
import { ProformaItemTaxes } from "../value-objects";
|
||||
@ -53,7 +52,7 @@ export interface IProformaProps {
|
||||
|
||||
paymentMethod: Maybe<InvoicePaymentMethod>;
|
||||
|
||||
items: IProformaItemsProps[];
|
||||
items: IProformaItemProps[];
|
||||
globalDiscountPercentage: DiscountPercentage;
|
||||
}
|
||||
|
||||
@ -96,21 +95,19 @@ export interface IProforma {
|
||||
|
||||
paymentMethod: Maybe<InvoicePaymentMethod>;
|
||||
|
||||
items: IProformaItems;
|
||||
items: IProformaItems; // <- Colección
|
||||
taxes(): Collection<IProformaTaxTotals>;
|
||||
totals(): IProformaTotals;
|
||||
}
|
||||
|
||||
export type ProformaPatchProps = Partial<Omit<IProformaProps, "companyId" | "items">> & {
|
||||
items?: ProformaItems;
|
||||
//items?: ProformaItems;
|
||||
};
|
||||
|
||||
type CreateProformaProps = IProformaProps;
|
||||
type InternalProformaProps = Omit<IProformaProps, "items">;
|
||||
|
||||
export class Proforma extends AggregateRoot<InternalProformaProps> implements IProforma {
|
||||
private readonly _items: ProformaItems;
|
||||
|
||||
// Creación funcional
|
||||
static create(props: CreateProformaProps, id?: UniqueID): Result<Proforma, Error> {
|
||||
const { items, ...internalProps } = props;
|
||||
@ -136,6 +133,8 @@ export class Proforma extends AggregateRoot<InternalProformaProps> implements IP
|
||||
return new Proforma(props, id);
|
||||
}
|
||||
|
||||
private readonly _items: ProformaItems;
|
||||
|
||||
protected constructor(props: InternalProformaProps, id?: UniqueID) {
|
||||
super(props, id);
|
||||
|
||||
@ -147,36 +146,7 @@ export class Proforma extends AggregateRoot<InternalProformaProps> implements IP
|
||||
});
|
||||
}
|
||||
|
||||
// Mutabilidad
|
||||
public update(
|
||||
partialProforma: Partial<Omit<IProformaProps, "companyId">>
|
||||
): Result<Proforma, Error> {
|
||||
const updatedProps = {
|
||||
...this.props,
|
||||
...partialProforma,
|
||||
} as IProformaProps;
|
||||
|
||||
return Proforma.create(updatedProps, this.id);
|
||||
}
|
||||
|
||||
public issue(): Result<void, Error> {
|
||||
if (!this.props.status.canTransitionTo("ISSUED")) {
|
||||
return Result.fail(
|
||||
new DomainValidationError(
|
||||
"INVALID_STATE",
|
||||
"status",
|
||||
"Proforma cannot be issued from current state"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// Falta
|
||||
//this.props.status = this.props.status.canTransitionTo("ISSUED");
|
||||
return Result.ok();
|
||||
}
|
||||
|
||||
// Getters
|
||||
|
||||
public get companyId(): UniqueID {
|
||||
return this.props.companyId;
|
||||
}
|
||||
@ -249,6 +219,34 @@ export class Proforma extends AggregateRoot<InternalProformaProps> implements IP
|
||||
return this.paymentMethod.isSome();
|
||||
}
|
||||
|
||||
// Mutabilidad
|
||||
public update(
|
||||
partialProforma: Partial<Omit<IProformaProps, "companyId">>
|
||||
): Result<Proforma, Error> {
|
||||
const updatedProps = {
|
||||
...this.props,
|
||||
...partialProforma,
|
||||
} as IProformaProps;
|
||||
|
||||
return Proforma.create(updatedProps, this.id);
|
||||
}
|
||||
|
||||
public issue(): Result<void, Error> {
|
||||
if (!this.props.status.canTransitionTo("ISSUED")) {
|
||||
return Result.fail(
|
||||
new DomainValidationError(
|
||||
"INVALID_STATE",
|
||||
"status",
|
||||
"Proforma cannot be issued from current state"
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// Falta
|
||||
//this.props.status = this.props.status.canTransitionTo("ISSUED");
|
||||
return Result.ok();
|
||||
}
|
||||
|
||||
// Cálculos
|
||||
|
||||
/**
|
||||
|
||||
@ -217,6 +217,60 @@ export class ProformaItem extends DomainEntity<InternalProformaItemProps> implem
|
||||
return this.taxes.retention.map((tax) => tax.percentage);
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary Cálculo centralizado de todos los valores intermedios.
|
||||
* @returns Devuelve un objeto inmutable con todos los valores necesarios:
|
||||
* - subtotal
|
||||
* - itemDiscount
|
||||
* - globalDiscount
|
||||
* - totalDiscount
|
||||
* - taxableAmount
|
||||
* - ivaAmount
|
||||
* - recAmount
|
||||
* - retentionAmount
|
||||
* - taxesAmount
|
||||
* - totalAmount
|
||||
*
|
||||
*/
|
||||
public totals(): IProformaItemTotals {
|
||||
const subtotalAmount = this._calculateSubtotalAmount();
|
||||
|
||||
const itemDiscountAmount = this._calculateItemDiscountAmount(subtotalAmount);
|
||||
const globalDiscountAmount = this._calculateGlobalDiscountAmount(
|
||||
subtotalAmount,
|
||||
itemDiscountAmount
|
||||
);
|
||||
const totalDiscountAmount = this._calculateTotalDiscountAmount(
|
||||
itemDiscountAmount,
|
||||
globalDiscountAmount
|
||||
);
|
||||
|
||||
const taxableAmount = subtotalAmount.subtract(totalDiscountAmount);
|
||||
|
||||
// Calcular impuestos individuales a partir de la base imponible
|
||||
const { ivaAmount, recAmount, retentionAmount } = this.taxes.totals(taxableAmount);
|
||||
|
||||
const taxesAmount = ivaAmount.add(recAmount).add(retentionAmount);
|
||||
const totalAmount = taxableAmount.add(taxesAmount);
|
||||
|
||||
return {
|
||||
subtotalAmount,
|
||||
|
||||
itemDiscountAmount,
|
||||
globalDiscountAmount,
|
||||
totalDiscountAmount,
|
||||
|
||||
taxableAmount,
|
||||
|
||||
ivaAmount,
|
||||
recAmount,
|
||||
retentionAmount,
|
||||
|
||||
taxesAmount,
|
||||
totalAmount,
|
||||
};
|
||||
}
|
||||
|
||||
// Cálculos / Ayudantes
|
||||
|
||||
/**
|
||||
@ -276,58 +330,4 @@ export class ProformaItem extends DomainEntity<InternalProformaItemProps> implem
|
||||
) {
|
||||
return itemDiscountAmount.add(globalDiscountAmount);
|
||||
}
|
||||
|
||||
/**
|
||||
* @summary Cálculo centralizado de todos los valores intermedios.
|
||||
* @returns Devuelve un objeto inmutable con todos los valores necesarios:
|
||||
* - subtotal
|
||||
* - itemDiscount
|
||||
* - globalDiscount
|
||||
* - totalDiscount
|
||||
* - taxableAmount
|
||||
* - ivaAmount
|
||||
* - recAmount
|
||||
* - retentionAmount
|
||||
* - taxesAmount
|
||||
* - totalAmount
|
||||
*
|
||||
*/
|
||||
public totals(): IProformaItemTotals {
|
||||
const subtotalAmount = this._calculateSubtotalAmount();
|
||||
|
||||
const itemDiscountAmount = this._calculateItemDiscountAmount(subtotalAmount);
|
||||
const globalDiscountAmount = this._calculateGlobalDiscountAmount(
|
||||
subtotalAmount,
|
||||
itemDiscountAmount
|
||||
);
|
||||
const totalDiscountAmount = this._calculateTotalDiscountAmount(
|
||||
itemDiscountAmount,
|
||||
globalDiscountAmount
|
||||
);
|
||||
|
||||
const taxableAmount = subtotalAmount.subtract(totalDiscountAmount);
|
||||
|
||||
// Calcular impuestos individuales a partir de la base imponible
|
||||
const { ivaAmount, recAmount, retentionAmount } = this.taxes.totals(taxableAmount);
|
||||
|
||||
const taxesAmount = ivaAmount.add(recAmount).add(retentionAmount);
|
||||
const totalAmount = taxableAmount.add(taxesAmount);
|
||||
|
||||
return {
|
||||
subtotalAmount,
|
||||
|
||||
itemDiscountAmount,
|
||||
globalDiscountAmount,
|
||||
totalDiscountAmount,
|
||||
|
||||
taxableAmount,
|
||||
|
||||
ivaAmount,
|
||||
recAmount,
|
||||
retentionAmount,
|
||||
|
||||
taxesAmount,
|
||||
totalAmount,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@ -6,14 +6,14 @@ import { ProformaItemMismatch } from "../../errors";
|
||||
import { ProformaItemsTotalsCalculator } from "../../services/proforma-items-totals-calculator";
|
||||
|
||||
import type {
|
||||
ICreateProformaItemProps,
|
||||
IProformaItem,
|
||||
IProformaItemProps,
|
||||
IProformaItemTotals,
|
||||
ProformaItem,
|
||||
} from "./proforma-item.entity";
|
||||
|
||||
export interface IProformaItemsProps {
|
||||
items?: ICreateProformaItemProps[];
|
||||
items: IProformaItemProps[];
|
||||
|
||||
// Estos campos vienen de la cabecera,
|
||||
// pero se necesitan para cálculos y representaciones de la línea.
|
||||
@ -22,6 +22,9 @@ export interface IProformaItemsProps {
|
||||
currencyCode: CurrencyCode; // Para cálculos y formateos de moneda
|
||||
}
|
||||
|
||||
type CreateProformaProps = IProformaItemsProps;
|
||||
type InternalProformaProps = Omit<IProformaItemsProps, "items">;
|
||||
|
||||
export interface IProformaItems {
|
||||
// OJO, no extendemos de Collection<IProformaItem> para no exponer
|
||||
// públicamente métodos para manipular la colección.
|
||||
@ -35,12 +38,16 @@ export interface IProformaItems {
|
||||
}
|
||||
|
||||
export class ProformaItems extends Collection<ProformaItem> implements IProformaItems {
|
||||
static create(props: CreateProformaProps): ProformaItems {
|
||||
return new ProformaItems(props);
|
||||
}
|
||||
|
||||
public readonly languageCode!: LanguageCode;
|
||||
public readonly currencyCode!: CurrencyCode;
|
||||
public readonly globalDiscountPercentage!: DiscountPercentage;
|
||||
|
||||
constructor(props: IProformaItemsProps) {
|
||||
super(props.items ?? []);
|
||||
protected constructor(props: InternalProformaProps) {
|
||||
super([]);
|
||||
this.languageCode = props.languageCode;
|
||||
this.currencyCode = props.currencyCode;
|
||||
this.globalDiscountPercentage = props.globalDiscountPercentage;
|
||||
@ -48,8 +55,15 @@ export class ProformaItems extends Collection<ProformaItem> implements IProforma
|
||||
this.ensureSameCurrencyAndLanguage(this.items);
|
||||
}
|
||||
|
||||
public static create(props: IProformaItemsProps): ProformaItems {
|
||||
return new ProformaItems(props);
|
||||
public add(item: ProformaItem): boolean {
|
||||
const same =
|
||||
this.languageCode.equals(item.languageCode) &&
|
||||
this.currencyCode.equals(item.currencyCode) &&
|
||||
this.globalDiscountPercentage.equals(item.globalDiscountPercentage);
|
||||
|
||||
if (!same) return false;
|
||||
|
||||
return super.add(item);
|
||||
}
|
||||
|
||||
public valued(): IProformaItem[] {
|
||||
|
||||
@ -7,9 +7,10 @@ import {
|
||||
buildIssuedInvoicesDependencies,
|
||||
buildProformaServices,
|
||||
buildProformasDependencies,
|
||||
issuedInvoicesRouter,
|
||||
models,
|
||||
proformasRouter,
|
||||
} from "./infrastructure";
|
||||
import { issuedInvoicesRouter, proformasRouter } from "./infrastructure/express";
|
||||
|
||||
export const customerInvoicesAPIModule: IModuleServer = {
|
||||
name: "customer-invoices",
|
||||
|
||||
@ -1,2 +0,0 @@
|
||||
export * from "./issued-invoices";
|
||||
export * from "./proformas";
|
||||
@ -1,2 +0,0 @@
|
||||
export * from "../../issued-invoices/express/controllers";
|
||||
export * from "../../issued-invoices/express/issued-invoices.routes";
|
||||
@ -1,4 +0,0 @@
|
||||
export * from "../../proformas/express/controllers";
|
||||
|
||||
export * from "./proformas.routes";
|
||||
export * from "./proformas-api-error-mapper";
|
||||
@ -7,12 +7,12 @@ import {
|
||||
|
||||
import { GetIssuedInvoiceByIdResponseSchema } from "../../../../../common/index.ts";
|
||||
import type { GetIssuedInvoiceByIdUseCase } from "../../../../application/issued-invoices/index.ts";
|
||||
import { customerInvoicesApiErrorMapper } from "../../../express/proformas/proformas-api-error-mapper.ts";
|
||||
import { proformasApiErrorMapper } from "../../../proformas/express/proformas-api-error-mapper.ts";
|
||||
|
||||
export class GetIssuedInvoiceByIdController extends ExpressController {
|
||||
public constructor(private readonly useCase: GetIssuedInvoiceByIdUseCase) {
|
||||
super();
|
||||
this.errorMapper = customerInvoicesApiErrorMapper;
|
||||
this.errorMapper = proformasApiErrorMapper;
|
||||
|
||||
// 🔐 Reutiliza guards de auth/tenant y prohíbe 'companyId' en query
|
||||
this.registerGuards(
|
||||
|
||||
@ -8,12 +8,12 @@ import { Criteria } from "@repo/rdx-criteria/server";
|
||||
|
||||
import { ListIssuedInvoicesResponseSchema } from "../../../../../common/index.ts";
|
||||
import type { ListIssuedInvoicesUseCase } from "../../../../application/issued-invoices/index.ts";
|
||||
import { customerInvoicesApiErrorMapper } from "../../../express/proformas/proformas-api-error-mapper.ts";
|
||||
import { proformasApiErrorMapper } from "../../../proformas/express/proformas-api-error-mapper.ts";
|
||||
|
||||
export class ListIssuedInvoicesController extends ExpressController {
|
||||
public constructor(private readonly useCase: ListIssuedInvoicesUseCase) {
|
||||
super();
|
||||
this.errorMapper = customerInvoicesApiErrorMapper;
|
||||
this.errorMapper = proformasApiErrorMapper;
|
||||
|
||||
// 🔐 Reutiliza guards de auth/tenant y prohíbe 'companyId' en query
|
||||
this.registerGuards(
|
||||
|
||||
@ -8,12 +8,12 @@ import {
|
||||
|
||||
import type { ReportIssueInvoiceByIdQueryRequestDTO } from "../../../../../common";
|
||||
import type { ReportIssuedInvoiceUseCase } from "../../../../application/index.ts";
|
||||
import { customerInvoicesApiErrorMapper } from "../../../express/proformas/proformas-api-error-mapper.ts";
|
||||
import { proformasApiErrorMapper } from "../../../proformas/express/proformas-api-error-mapper.ts";
|
||||
|
||||
export class ReportIssuedInvoiceController extends ExpressController {
|
||||
public constructor(private readonly useCase: ReportIssuedInvoiceUseCase) {
|
||||
super();
|
||||
this.errorMapper = customerInvoicesApiErrorMapper;
|
||||
this.errorMapper = proformasApiErrorMapper;
|
||||
|
||||
// 🔐 Reutiliza guards de auth/tenant y prohíbe 'companyId' en query
|
||||
this.registerGuards(
|
||||
|
||||
@ -18,7 +18,7 @@ import {
|
||||
isInvalidProformaTransitionError,
|
||||
isProformaCannotBeConvertedToInvoiceError,
|
||||
isProformaCannotBeDeletedError,
|
||||
} from "../../domain";
|
||||
} from "../../../domain";
|
||||
|
||||
// Crea una regla específica (prioridad alta para sobreescribir mensajes)
|
||||
const invoiceDuplicateRule: ErrorToApiRule = {
|
||||
@ -1,4 +1,9 @@
|
||||
import type { ICatalogs, IProformaDomainMapper, IProformaListMapper } from "../../../application";
|
||||
import {
|
||||
CreateProformaInputMapper,
|
||||
type ICatalogs,
|
||||
type IProformaDomainMapper,
|
||||
type IProformaListMapper,
|
||||
} from "../../../application";
|
||||
import { SequelizeProformaDomainMapper, SequelizeProformaListMapper } from "../persistence";
|
||||
|
||||
export interface IProformaPersistenceMappers {
|
||||
|
||||
@ -7,12 +7,12 @@ import {
|
||||
|
||||
import type { CreateProformaRequestDTO } from "../../../../../common/dto/index.ts";
|
||||
import type { CreateProformaUseCase } from "../../../../application/index.ts";
|
||||
import { customerInvoicesApiErrorMapper } from "../../../express/proformas/proformas-api-error-mapper.ts";
|
||||
import { proformasApiErrorMapper } from "../proformas-api-error-mapper.ts";
|
||||
|
||||
export class CreateProformaController extends ExpressController {
|
||||
public constructor(private readonly useCase: CreateProformaUseCase) {
|
||||
super();
|
||||
this.errorMapper = customerInvoicesApiErrorMapper;
|
||||
this.errorMapper = proformasApiErrorMapper;
|
||||
|
||||
// 🔐 Reutiliza guards de auth/tenant y prohíbe 'companyId' en query
|
||||
this.registerGuards(
|
||||
|
||||
@ -6,12 +6,12 @@ import {
|
||||
} from "@erp/core/api";
|
||||
|
||||
import type { DeleteProformaUseCase } from "../../../../application/index.ts";
|
||||
import { customerInvoicesApiErrorMapper } from "../../../express/proformas/proformas-api-error-mapper.ts";
|
||||
import { proformasApiErrorMapper } from "../proformas-api-error-mapper.ts";
|
||||
|
||||
export class DeleteProformaController extends ExpressController {
|
||||
public constructor(private readonly useCase: DeleteProformaUseCase) {
|
||||
super();
|
||||
this.errorMapper = customerInvoicesApiErrorMapper;
|
||||
this.errorMapper = proformasApiErrorMapper;
|
||||
|
||||
// 🔐 Reutiliza guards de auth/tenant y prohíbe 'companyId' en query
|
||||
this.registerGuards(
|
||||
|
||||
@ -5,13 +5,13 @@ import {
|
||||
requireCompanyContextGuard,
|
||||
} from "@erp/core/api";
|
||||
|
||||
import type { GetProformaUseCase } from "../../../../application/index.ts";
|
||||
import { customerInvoicesApiErrorMapper } from "../../../express/proformas/proformas-api-error-mapper.ts";
|
||||
import type { GetProformaByIdUseCase } from "../../../../application";
|
||||
import { proformasApiErrorMapper } from "../proformas-api-error-mapper.ts";
|
||||
|
||||
export class GetProformaController extends ExpressController {
|
||||
public constructor(private readonly useCase: GetProformaUseCase) {
|
||||
public constructor(private readonly useCase: GetProformaByIdUseCase) {
|
||||
super();
|
||||
this.errorMapper = customerInvoicesApiErrorMapper;
|
||||
this.errorMapper = proformasApiErrorMapper;
|
||||
|
||||
// 🔐 Reutiliza guards de auth/tenant y prohíbe 'companyId' en query
|
||||
this.registerGuards(
|
||||
|
||||
@ -6,12 +6,12 @@ import {
|
||||
} from "@erp/core/api";
|
||||
|
||||
import type { IssueProformaUseCase } from "../../../../application/index.ts";
|
||||
import { customerInvoicesApiErrorMapper } from "../../../express/proformas/proformas-api-error-mapper.ts";
|
||||
import { proformasApiErrorMapper } from "../proformas-api-error-mapper.ts";
|
||||
|
||||
export class IssueProformaController extends ExpressController {
|
||||
public constructor(private readonly useCase: IssueProformaUseCase) {
|
||||
super();
|
||||
this.errorMapper = customerInvoicesApiErrorMapper;
|
||||
this.errorMapper = proformasApiErrorMapper;
|
||||
|
||||
// 🔐 Reutiliza guards de auth/tenant y prohíbe 'companyId' en query
|
||||
this.registerGuards(
|
||||
|
||||
@ -7,12 +7,12 @@ import {
|
||||
import { Criteria } from "@repo/rdx-criteria/server";
|
||||
|
||||
import type { ListProformasUseCase } from "../../../../application/index.ts";
|
||||
import { customerInvoicesApiErrorMapper } from "../../../express/proformas/proformas-api-error-mapper.ts";
|
||||
import { proformasApiErrorMapper } from "../proformas-api-error-mapper.ts";
|
||||
|
||||
export class ListProformasController extends ExpressController {
|
||||
public constructor(private readonly useCase: ListProformasUseCase) {
|
||||
super();
|
||||
this.errorMapper = customerInvoicesApiErrorMapper;
|
||||
this.errorMapper = proformasApiErrorMapper;
|
||||
|
||||
// 🔐 Reutiliza guards de auth/tenant y prohíbe 'companyId' en query
|
||||
this.registerGuards(
|
||||
|
||||
@ -8,12 +8,12 @@ import {
|
||||
|
||||
import type { ReportProformaByIdQueryRequestDTO } from "../../../../../common";
|
||||
import type { ReportProformaUseCase } from "../../../../application/index.ts";
|
||||
import { customerInvoicesApiErrorMapper } from "../../../express/proformas/proformas-api-error-mapper.ts";
|
||||
import { proformasApiErrorMapper } from "../proformas-api-error-mapper.ts";
|
||||
|
||||
export class ReportProformaController extends ExpressController {
|
||||
public constructor(private readonly useCase: ReportProformaUseCase) {
|
||||
super();
|
||||
this.errorMapper = customerInvoicesApiErrorMapper;
|
||||
this.errorMapper = proformasApiErrorMapper;
|
||||
|
||||
// 🔐 Reutiliza guards de auth/tenant y prohíbe 'companyId' en query
|
||||
this.registerGuards(
|
||||
|
||||
@ -7,12 +7,12 @@ import {
|
||||
|
||||
import type { UpdateProformaByIdRequestDTO } from "../../../../../common/dto/index.ts";
|
||||
import type { UpdateProformaUseCase } from "../../../../application/index.ts";
|
||||
import { customerInvoicesApiErrorMapper } from "../../../express/proformas/proformas-api-error-mapper.ts";
|
||||
import { proformasApiErrorMapper } from "../proformas-api-error-mapper.ts";
|
||||
|
||||
export class UpdateProformaController extends ExpressController {
|
||||
public constructor(private readonly useCase: UpdateProformaUseCase) {
|
||||
super();
|
||||
this.errorMapper = customerInvoicesApiErrorMapper;
|
||||
this.errorMapper = proformasApiErrorMapper;
|
||||
|
||||
// 🔐 Reutiliza guards de auth/tenant y prohíbe 'companyId' en query
|
||||
this.registerGuards(
|
||||
|
||||
@ -1 +1,2 @@
|
||||
export * from "./controllers";
|
||||
export * from "./proformas.routes";
|
||||
|
||||
@ -134,8 +134,8 @@ export class CreateProformaRequestMapper {
|
||||
|
||||
const globalDiscountPercentage = extractOrPushError(
|
||||
Percentage.create({
|
||||
value: Number(dto.discount_percentage.value),
|
||||
scale: Number(dto.discount_percentage.scale),
|
||||
value: Number(dto.global_discount_percentage.value),
|
||||
scale: Number(dto.global_discount_percentage.scale),
|
||||
}),
|
||||
"discount_percentage",
|
||||
this.errors
|
||||
@ -209,7 +209,7 @@ export class CreateProformaRequestMapper {
|
||||
);
|
||||
|
||||
const discountPercentage = extractOrPushError(
|
||||
maybeFromNullableResult(item.discount_percentage, (value) =>
|
||||
maybeFromNullableResult(item.item_discount_percentage, (value) =>
|
||||
ItemDiscountPercentage.create(value)
|
||||
),
|
||||
"discount_percentage",
|
||||
|
||||
@ -1,6 +1,3 @@
|
||||
// Ejemplo: regla específica para Billing → InvoiceIdAlreadyExistsError
|
||||
// (si defines un error más ubicuo dentro del BC con su propia clase)
|
||||
|
||||
import {
|
||||
ApiErrorMapper,
|
||||
ConflictApiError,
|
||||
@ -23,7 +20,7 @@ import {
|
||||
} from "../../../domain";
|
||||
|
||||
// Crea una regla específica (prioridad alta para sobreescribir mensajes)
|
||||
const invoiceDuplicateRule: ErrorToApiRule = {
|
||||
const proformaDuplicateRule: ErrorToApiRule = {
|
||||
priority: 120,
|
||||
matches: (e) => isCustomerInvoiceIdAlreadyExistsError(e),
|
||||
build: (e) =>
|
||||
@ -81,8 +78,8 @@ const proformaCannotBeDeletedRule: ErrorToApiRule = {
|
||||
};
|
||||
|
||||
// Cómo aplicarla: crea una nueva instancia del mapper con la regla extra
|
||||
export const customerInvoicesApiErrorMapper: ApiErrorMapper = ApiErrorMapper.default()
|
||||
.register(invoiceDuplicateRule)
|
||||
export const proformasApiErrorMapper: ApiErrorMapper = ApiErrorMapper.default()
|
||||
.register(proformaDuplicateRule)
|
||||
.register(proformaItemMismatchError)
|
||||
.register(entityIsNotProformaError)
|
||||
.register(proformaConversionRule)
|
||||
@ -3,17 +3,21 @@ import { type ModuleParams, type RequestWithAuth, validateRequest } from "@erp/c
|
||||
import { type NextFunction, type Request, type Response, Router } from "express";
|
||||
|
||||
import {
|
||||
GetProformaController,
|
||||
ListProformasController,
|
||||
type ProformasInternalDeps,
|
||||
ReportProformaController,
|
||||
} from "..";
|
||||
|
||||
import {
|
||||
CreateProformaRequestSchema,
|
||||
GetProformaByIdRequestSchema,
|
||||
ListProformasRequestSchema,
|
||||
ReportProformaByIdParamsRequestSchema,
|
||||
ReportProformaByIdQueryRequestSchema,
|
||||
} from "../../../../common";
|
||||
import {
|
||||
GetProformaController,
|
||||
ListProformasController,
|
||||
type ProformasInternalDeps,
|
||||
ReportProformaController,
|
||||
} from "../../proformas";
|
||||
|
||||
import { CreateProformaController } from "./controllers/create-proforma.controller";
|
||||
|
||||
export const proformasRouter = (params: ModuleParams, deps: ProformasInternalDeps) => {
|
||||
const { app, config } = params;
|
||||
@ -73,18 +77,19 @@ export const proformasRouter = (params: ModuleParams, deps: ProformasInternalDep
|
||||
}
|
||||
);
|
||||
|
||||
/*router.post(
|
||||
router.post(
|
||||
"/",
|
||||
//checkTabContext,
|
||||
|
||||
validateRequest(CreateProformaRequestSchema, "body"),
|
||||
(req: Request, res: Response, next: NextFunction) => {
|
||||
const useCase = deps.useCases.create_proforma();
|
||||
const useCase = deps.useCases.createProforma();
|
||||
const controller = new CreateProformaController(useCase);
|
||||
return controller.execute(req, res, next);
|
||||
}
|
||||
);
|
||||
|
||||
/*
|
||||
router.put(
|
||||
"/:proforma_id",
|
||||
//checkTabContext,
|
||||
@ -7,7 +7,10 @@ export const CreateProformaItemRequestSchema = z.object({
|
||||
description: z.string().default(""),
|
||||
quantity: NumericStringSchema.default(""),
|
||||
unit_amount: NumericStringSchema.default(""),
|
||||
discount_percentage: NumericStringSchema.default(""),
|
||||
item_discount_percentage: PercentageSchema.default({
|
||||
value: "0",
|
||||
scale: "2",
|
||||
}),
|
||||
taxes: z.string().default(""),
|
||||
});
|
||||
export type CreateProformaItemRequestDTO = z.infer<typeof CreateProformaItemRequestSchema>;
|
||||
@ -29,7 +32,7 @@ export const CreateProformaRequestSchema = z.object({
|
||||
language_code: z.string().toLowerCase().default("es"),
|
||||
currency_code: z.string().toUpperCase().default("EUR"),
|
||||
|
||||
discount_percentage: PercentageSchema.default({
|
||||
global_discount_percentage: PercentageSchema.default({
|
||||
value: "0",
|
||||
scale: "2",
|
||||
}),
|
||||
|
||||
@ -13,8 +13,6 @@
|
||||
"dev": "turbo dev",
|
||||
"dev:server": "turbo dev --filter=server",
|
||||
"dev:client": "turbo dev --filter=client",
|
||||
"lint": "turbo run lint",
|
||||
"lint:fix": "turbo run lint:fix",
|
||||
"format-and-lint": "biome check .",
|
||||
"format-and-lint:fix": "biome check . --write",
|
||||
"ui:add": "pnpm --filter @repo/shadcn-ui ui:add",
|
||||
@ -28,7 +26,6 @@
|
||||
"@typescript-eslint/eslint-plugin": "^8.56.1",
|
||||
"@typescript-eslint/parser": "^8.56.1",
|
||||
"change-case": "^5.4.4",
|
||||
"eslint": "^10.0.2",
|
||||
"inquirer": "^12.10.0",
|
||||
"plop": "^4.0.4",
|
||||
"rimraf": "^5.0.5",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user