diff --git a/modules/core/src/api/errors/index.ts b/modules/core/src/api/errors/index.ts index 6601beee..123c1df7 100644 --- a/modules/core/src/api/errors/index.ts +++ b/modules/core/src/api/errors/index.ts @@ -1,3 +1,4 @@ export * from "./domain-validation-error"; export * from "./error-mapper"; +export * from "./validation-api-error"; export * from "./validation-error-collection"; diff --git a/modules/core/src/api/infrastructure/express/middlewares/validate-request.ts b/modules/core/src/api/infrastructure/express/middlewares/validate-request.ts index b17d61a4..6165cd25 100644 --- a/modules/core/src/api/infrastructure/express/middlewares/validate-request.ts +++ b/modules/core/src/api/infrastructure/express/middlewares/validate-request.ts @@ -1,5 +1,5 @@ // src/common/middlewares/validate-dto.ts -import { ValidationApiError } from "@erp/core/api"; +import { ExpressController, ValidationApiError } from "@erp/core/api"; import { RequestHandler } from "express"; import { ZodSchema } from "zod/v4"; @@ -40,17 +40,22 @@ export const validateRequest = ( options: ValidateRequestWithSchemaOptions = { sanitize: true } ): RequestHandler => { return async (req, res, next) => { - console.debug(`Validating request ${source} with schema:`, schema); + console.debug(`Validating request ${source} with schema.`); const result = schema.safeParse(req[source]); if (!result.success) { // Construye errores detallados - const validationErrors = result.error.errors.map((err) => ({ + const validationErrors = result.error.issues.map((err) => ({ field: err.path.join("."), message: err.message, })); - return new ValidationApiError("Validation failed", validationErrors); + console.debug(validationErrors); + + return ExpressController.errorResponse( + new ValidationApiError("Validation failed", validationErrors), + res + ); } // Si pasa la validación, opcionalmente reescribe req.body diff --git a/modules/customer-invoices/src/api/controllers/create-customer-invoice/create-customer-invoice.ts b/modules/customer-invoices/src/api/controllers/create-customer-invoice/create-customer-invoice.ts index a233afc9..437dcef6 100644 --- a/modules/customer-invoices/src/api/controllers/create-customer-invoice/create-customer-invoice.ts +++ b/modules/customer-invoices/src/api/controllers/create-customer-invoice/create-customer-invoice.ts @@ -1,10 +1,9 @@ import { ExpressController, errorMapper } from "@erp/core/api"; import { CreateCustomerInvoiceCommandDTO } from "../../../common/dto"; +import { CreateCustomerInvoiceUseCase } from "../../application"; export class CreateCustomerInvoiceController extends ExpressController { - public constructor( - private readonly createCustomerInvoice: any // Replace with actual type - ) { + public constructor(private readonly createCustomerInvoice: CreateCustomerInvoiceUseCase) { super(); } @@ -25,6 +24,7 @@ export class CreateCustomerInvoiceController extends ExpressController { const result = await this.createCustomerInvoice.execute(dto); if (result.isFailure) { + console.log(result.error); const apiError = errorMapper.toApiError(result.error); return this.handleApiError(apiError); } diff --git a/modules/customer-invoices/src/api/domain/repositories/customer-invoice-repository.interface.ts b/modules/customer-invoices/src/api/domain/repositories/customer-invoice-repository.interface.ts index 8b9e05af..aec94265 100644 --- a/modules/customer-invoices/src/api/domain/repositories/customer-invoice-repository.interface.ts +++ b/modules/customer-invoices/src/api/domain/repositories/customer-invoice-repository.interface.ts @@ -45,5 +45,4 @@ export interface ICustomerInvoiceRepository { * @returns Result */ deleteById(id: UniqueID, transaction: any): Promise>; - - \ No newline at end of file +} 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 e6634c79..4baacde9 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 @@ -5,7 +5,10 @@ import { CreateCustomerInvoiceCommandSchema, ListCustomerInvoicesQuerySchema, } from "../../../common/dto"; -import { buildListCustomerInvoicesController } from "../../controllers"; +import { + buildCreateCustomerInvoicesController, + buildListCustomerInvoicesController, +} from "../../controllers"; export const customerInvoicesRouter = (params: ModuleParams) => { const { app, database, baseRoutePath, logger } = params as { @@ -43,7 +46,7 @@ export const customerInvoicesRouter = (params: ModuleParams) => { //checkUser, validateRequest(CreateCustomerInvoiceCommandSchema), (req: Request, res: Response, next: NextFunction) => { - buildCreateCustomerInvoiceController(database).execute(req, res, next); + buildCreateCustomerInvoicesController(database).execute(req, res, next); } ); diff --git a/modules/customer-invoices/src/common/dto/customer-invoices.schemas.ts b/modules/customer-invoices/src/common/dto/customer-invoices.schemas.ts index f4385bd0..4445e4a9 100644 --- a/modules/customer-invoices/src/common/dto/customer-invoices.schemas.ts +++ b/modules/customer-invoices/src/common/dto/customer-invoices.schemas.ts @@ -1,7 +1,7 @@ import * as z from "zod/v4"; export const ICreateCustomerInvoiceRequestSchema = z.object({ - id: z.string().uuid(), + id: z.uuid(), customerInvoice_number: z.string().min(1), customerInvoice_series: z.string().min(1), issue_date: z.string().refine((date) => {