From fa56473a008057b3052a464435c31bf157bf3570 Mon Sep 17 00:00:00 2001 From: david Date: Wed, 10 Sep 2025 12:27:07 +0200 Subject: [PATCH] Facturas de cliente --- .../list-customer-invoices.use-case.ts | 4 +- .../api/domain/aggregates/customer-invoice.ts | 12 ----- .../invoice-recipient/invoice-recipient.ts | 44 ++++++------------- .../express/customer-invoices.routes.ts | 6 +-- .../mappers/customer-invoice.mapper.ts | 10 +++-- .../sequelize/customer-invoice.repository.ts | 11 ++++- 6 files changed, 33 insertions(+), 54 deletions(-) diff --git a/modules/customer-invoices/src/api/application/list-customer-invoices/list-customer-invoices.use-case.ts b/modules/customer-invoices/src/api/application/list-customer-invoices/list-customer-invoices.use-case.ts index 2cc73afa..d9f7fd24 100644 --- a/modules/customer-invoices/src/api/application/list-customer-invoices/list-customer-invoices.use-case.ts +++ b/modules/customer-invoices/src/api/application/list-customer-invoices/list-customer-invoices.use-case.ts @@ -1,4 +1,3 @@ -import { JsonTaxCatalogProvider } from "@erp/core"; import { ITransactionManager } from "@erp/core/api"; import { Criteria } from "@repo/rdx-criteria/server"; import { UniqueID } from "@repo/rdx-ddd"; @@ -17,8 +16,7 @@ export class ListCustomerInvoicesUseCase { constructor( private readonly service: CustomerInvoiceService, private readonly transactionManager: ITransactionManager, - private readonly assembler: ListCustomerInvoicesAssembler, - private readonly taxCatalog: JsonTaxCatalogProvider + private readonly assembler: ListCustomerInvoicesAssembler ) {} public execute( diff --git a/modules/customer-invoices/src/api/domain/aggregates/customer-invoice.ts b/modules/customer-invoices/src/api/domain/aggregates/customer-invoice.ts index 091f1285..e8db9540 100644 --- a/modules/customer-invoices/src/api/domain/aggregates/customer-invoice.ts +++ b/modules/customer-invoices/src/api/domain/aggregates/customer-invoice.ts @@ -30,22 +30,10 @@ export interface CustomerInvoiceProps { operationDate: Maybe; customerId: UniqueID; - recipient: Maybe; notes: Maybe; - //dueDate: UtcDate; // ? --> depende de la forma de pago - - //tax: Tax; // ? --> detalles? - - //purchareOrderNumber: string; - - //senderId: UniqueID; - - //paymentInstructions: Note; - //paymentTerms: string; - languageCode: LanguageCode; currencyCode: CurrencyCode; diff --git a/modules/customer-invoices/src/api/domain/entities/invoice-recipient/invoice-recipient.ts b/modules/customer-invoices/src/api/domain/entities/invoice-recipient/invoice-recipient.ts index 324198a0..e5755705 100644 --- a/modules/customer-invoices/src/api/domain/entities/invoice-recipient/invoice-recipient.ts +++ b/modules/customer-invoices/src/api/domain/entities/invoice-recipient/invoice-recipient.ts @@ -1,15 +1,14 @@ import { - City, - Country, - Name, - PostalCode, - Province, - Street, - TINNumber, - ValueObject + City, + Country, + Name, + PostalCode, + Province, + Street, + TINNumber, + ValueObject, } from "@repo/rdx-ddd"; import { Maybe, Result } from "@repo/rdx-utils"; -import * as z from "zod/v4"; export interface InvoiceRecipientProps { name: Name; @@ -23,26 +22,15 @@ export interface InvoiceRecipientProps { } export class InvoiceRecipient extends ValueObject { - protected static validate(values: InvoiceRecipientProps) { - const schema = z.object({ - name: z.string(), - tin: z.string(), - street: z.string().optional(), - street2: z.string().optional(), - city: z.string().optional(), - postalCode: z.string().optional(), - province: z.string().optional(), - country: z.string().optional(), - }) - - return schema.safeParse(values); - } + protected static validate(values: InvoiceRecipientProps) { + return Result.ok(values); + } static create(values: InvoiceRecipientProps): Result { - const valueIsValid = InvoiceRecipient.validate(values) + const valueIsValid = InvoiceRecipient.validate(values); - if (!valueIsValid.success) { - return Result.fail(new Error(valueIsValid.error.issues[0].message)); + if (valueIsValid.isFailure) { + return Result.fail(valueIsValid.error); } return Result.ok(new InvoiceRecipient(values)); @@ -57,7 +45,6 @@ export class InvoiceRecipient extends ValueObject { return InvoiceRecipient.create(updatedProps); } - public get name(): Name { return this.props.name; } @@ -97,7 +84,4 @@ export class InvoiceRecipient extends ValueObject { toPrimitive() { return this.getProps(); } - } - - diff --git a/modules/customer-invoices/src/api/infrastructure/express/customer-invoices.routes.ts b/modules/customer-invoices/src/api/infrastructure/express/customer-invoices.routes.ts index 96dc06f8..afe13678 100644 --- a/modules/customer-invoices/src/api/infrastructure/express/customer-invoices.routes.ts +++ b/modules/customer-invoices/src/api/infrastructure/express/customer-invoices.routes.ts @@ -58,7 +58,7 @@ export const customerInvoicesRouter = (params: ModuleParams) => { ); router.get( - "/:id", + "/:invoice_id", //checkTabContext, validateRequest(GetCustomerInvoiceByIdRequestSchema, "params"), (req: Request, res: Response, next: NextFunction) => { @@ -81,7 +81,7 @@ export const customerInvoicesRouter = (params: ModuleParams) => { ); /*router.put( - "/:customer_id", + "/:invoice_id", //checkTabContext, validateRequest(UpdateCustomerInvoiceByIdParamsRequestSchema, "params"), validateRequest(UpdateCustomerInvoiceByIdRequestSchema, "body"), @@ -93,7 +93,7 @@ export const customerInvoicesRouter = (params: ModuleParams) => { );*/ router.delete( - "/:id", + "/:invoice_id", //checkTabContext, validateRequest(DeleteCustomerInvoiceByIdRequestSchema, "params"), diff --git a/modules/customer-invoices/src/api/infrastructure/mappers/customer-invoice.mapper.ts b/modules/customer-invoices/src/api/infrastructure/mappers/customer-invoice.mapper.ts index d286070e..cdd9ff12 100644 --- a/modules/customer-invoices/src/api/infrastructure/mappers/customer-invoice.mapper.ts +++ b/modules/customer-invoices/src/api/infrastructure/mappers/customer-invoice.mapper.ts @@ -121,10 +121,12 @@ export class CustomerInvoiceMapper ); const discountPercentage = extractOrPushError( - Percentage.create({ - value: source.discount_percentage_value, - scale: source.discount_percentage_scale, - }), + maybeFromNullableVO(source.discount_percentage_value, (value) => + Percentage.create({ + value: value, + scale: source.discount_percentage_scale, + }) + ), "discount_percentage", errors ); diff --git a/modules/customer-invoices/src/api/infrastructure/sequelize/customer-invoice.repository.ts b/modules/customer-invoices/src/api/infrastructure/sequelize/customer-invoice.repository.ts index 687b0e8f..363f43e4 100644 --- a/modules/customer-invoices/src/api/infrastructure/sequelize/customer-invoice.repository.ts +++ b/modules/customer-invoices/src/api/infrastructure/sequelize/customer-invoice.repository.ts @@ -114,8 +114,17 @@ export class CustomerInvoiceRepository transaction: Transaction ): Promise> { try { + const { CustomerModel } = this._database.models; + const row = await CustomerInvoiceModel.findOne({ where: { id: id.toString(), company_id: companyId.toString() }, + include: [ + { + model: CustomerModel, + as: "current_customer", + required: false, // false => LEFT JOIN + }, + ], transaction, }); @@ -169,8 +178,6 @@ export class CustomerInvoiceRepository transaction, }); - console.log(instances); - return this._mapper.mapArrayToDomain(instances); } catch (err: unknown) { return Result.fail(translateSequelizeError(err));