2026-03-09 20:23:48 +00:00
|
|
|
import { mockUser, requireAuthenticated, requireCompanyContext } from "@erp/auth/api";
|
2026-03-29 19:00:07 +00:00
|
|
|
import { type RequestWithAuth, type StartParams, validateRequest } from "@erp/core/api";
|
2026-03-07 21:39:21 +00:00
|
|
|
import { type NextFunction, type Request, type Response, Router } from "express";
|
2025-12-04 18:31:40 +00:00
|
|
|
|
2025-09-14 10:50:29 +00:00
|
|
|
import {
|
2026-03-09 20:23:48 +00:00
|
|
|
CreateCustomerRequestSchema,
|
2026-03-07 22:11:39 +00:00
|
|
|
CustomerListRequestSchema,
|
2026-03-09 20:23:48 +00:00
|
|
|
GetCustomerByIdRequestSchema,
|
2026-03-16 17:45:45 +00:00
|
|
|
UpdateCustomerByIdParamsRequestSchema,
|
|
|
|
|
UpdateCustomerByIdRequestSchema,
|
2025-09-14 10:50:29 +00:00
|
|
|
} from "../../../common/dto";
|
2026-03-07 21:39:21 +00:00
|
|
|
import type { CustomersInternalDeps } from "../di";
|
2025-12-04 18:31:40 +00:00
|
|
|
|
2025-09-14 10:50:29 +00:00
|
|
|
import {
|
2026-03-09 20:23:48 +00:00
|
|
|
CreateCustomerController,
|
2026-03-07 22:11:39 +00:00
|
|
|
GetCustomerController,
|
2026-03-09 20:23:48 +00:00
|
|
|
ListCustomersController,
|
2026-03-16 17:45:45 +00:00
|
|
|
UpdateCustomerController,
|
2025-09-14 10:50:29 +00:00
|
|
|
} from "./controllers";
|
2025-08-11 17:49:52 +00:00
|
|
|
|
2026-03-29 19:00:07 +00:00
|
|
|
export const customersRouter = (params: StartParams) => {
|
|
|
|
|
const { app, config, getInternal } = params;
|
|
|
|
|
|
|
|
|
|
// Recuperamos el dominio interno del módulo
|
|
|
|
|
const deps = getInternal<CustomersInternalDeps>("customers");
|
2025-08-11 17:49:52 +00:00
|
|
|
|
2025-08-26 18:55:59 +00:00
|
|
|
const router: Router = Router({ mergeParams: true });
|
|
|
|
|
|
2026-03-29 19:00:07 +00:00
|
|
|
// ----------------------------------------------
|
|
|
|
|
|
2025-08-14 14:58:13 +00:00
|
|
|
// 🔐 Autenticación + Tenancy para TODO el router
|
2025-12-04 18:31:40 +00:00
|
|
|
if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "production") {
|
2025-09-03 18:04:09 +00:00
|
|
|
router.use(
|
|
|
|
|
(req: Request, res: Response, next: NextFunction) =>
|
|
|
|
|
mockUser(req as RequestWithAuth, res, next) // Debe ir antes de las rutas protegidas
|
|
|
|
|
);
|
2025-08-26 18:55:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//router.use(/*authenticateJWT(),*/ enforceTenant() /*checkTabContext*/);
|
2025-09-03 18:04:09 +00:00
|
|
|
router.use([
|
|
|
|
|
(req: Request, res: Response, next: NextFunction) =>
|
2026-01-13 18:41:14 +00:00
|
|
|
requireAuthenticated()(req as RequestWithAuth, res, next), // Debe ir antes de las rutas protegidas
|
2025-09-03 18:04:09 +00:00
|
|
|
|
|
|
|
|
(req: Request, res: Response, next: NextFunction) =>
|
2026-01-13 18:41:14 +00:00
|
|
|
requireCompanyContext()(req as RequestWithAuth, res, next), // Debe ir antes de las rutas protegidas
|
2025-09-03 18:04:09 +00:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------
|
2025-08-14 14:58:13 +00:00
|
|
|
|
|
|
|
|
router.get(
|
2025-08-11 17:49:52 +00:00
|
|
|
"/",
|
|
|
|
|
//checkTabContext,
|
2025-08-14 14:58:13 +00:00
|
|
|
|
2025-08-12 11:23:50 +00:00
|
|
|
validateRequest(CustomerListRequestSchema, "params"),
|
2025-08-11 17:49:52 +00:00
|
|
|
(req: Request, res: Response, next: NextFunction) => {
|
2026-03-07 21:39:21 +00:00
|
|
|
const useCase = deps.useCases.listCustomers();
|
2025-08-14 14:58:13 +00:00
|
|
|
const controller = new ListCustomersController(useCase /*, deps.presenters.list */);
|
|
|
|
|
return controller.execute(req, res, next);
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2026-03-09 20:23:48 +00:00
|
|
|
router.get(
|
2025-09-02 08:57:41 +00:00
|
|
|
"/:customer_id",
|
2025-08-11 17:49:52 +00:00
|
|
|
//checkTabContext,
|
2025-08-14 14:58:13 +00:00
|
|
|
|
2025-08-12 11:23:50 +00:00
|
|
|
validateRequest(GetCustomerByIdRequestSchema, "params"),
|
2025-08-11 17:49:52 +00:00
|
|
|
(req: Request, res: Response, next: NextFunction) => {
|
2026-03-07 22:11:39 +00:00
|
|
|
const useCase = deps.useCases.getCustomerById();
|
2025-08-14 14:58:13 +00:00
|
|
|
const controller = new GetCustomerController(useCase);
|
|
|
|
|
return controller.execute(req, res, next);
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
2026-03-07 22:11:39 +00:00
|
|
|
);
|
2025-08-11 17:49:52 +00:00
|
|
|
|
2026-03-09 20:23:48 +00:00
|
|
|
router.post(
|
2025-08-11 17:49:52 +00:00
|
|
|
"/",
|
|
|
|
|
//checkTabContext,
|
2025-08-14 14:58:13 +00:00
|
|
|
|
2025-09-02 08:57:41 +00:00
|
|
|
validateRequest(CreateCustomerRequestSchema, "body"),
|
2025-08-11 17:49:52 +00:00
|
|
|
(req: Request, res: Response, next: NextFunction) => {
|
2026-03-09 20:23:48 +00:00
|
|
|
const useCase = deps.useCases.createCustomer();
|
2025-08-14 14:58:13 +00:00
|
|
|
const controller = new CreateCustomerController(useCase);
|
|
|
|
|
return controller.execute(req, res, next);
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
2026-03-09 20:23:48 +00:00
|
|
|
);
|
2025-08-11 17:49:52 +00:00
|
|
|
|
2026-03-16 17:45:45 +00:00
|
|
|
router.put(
|
2025-09-02 08:57:41 +00:00
|
|
|
"/:customer_id",
|
2025-09-01 16:38:00 +00:00
|
|
|
//checkTabContext,
|
2025-09-19 16:55:30 +00:00
|
|
|
|
2025-09-02 10:55:45 +00:00
|
|
|
validateRequest(UpdateCustomerByIdParamsRequestSchema, "params"),
|
|
|
|
|
validateRequest(UpdateCustomerByIdRequestSchema, "body"),
|
2025-08-11 17:49:52 +00:00
|
|
|
(req: Request, res: Response, next: NextFunction) => {
|
2026-03-16 17:45:45 +00:00
|
|
|
const useCase = deps.useCases.updateCustomer();
|
2025-09-01 16:38:00 +00:00
|
|
|
const controller = new UpdateCustomerController(useCase);
|
|
|
|
|
return controller.execute(req, res, next);
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
2025-09-01 16:38:00 +00:00
|
|
|
);
|
2026-03-16 17:45:45 +00:00
|
|
|
|
2025-09-19 16:55:30 +00:00
|
|
|
/*router.delete(
|
2025-09-02 08:57:41 +00:00
|
|
|
"/:customer_id",
|
2025-08-11 17:49:52 +00:00
|
|
|
//checkTabContext,
|
2025-08-14 14:58:13 +00:00
|
|
|
|
2025-08-12 11:23:50 +00:00
|
|
|
validateRequest(DeleteCustomerByIdRequestSchema, "params"),
|
2025-08-11 17:49:52 +00:00
|
|
|
(req: Request, res: Response, next: NextFunction) => {
|
2025-08-14 14:58:13 +00:00
|
|
|
const useCase = deps.build.delete();
|
|
|
|
|
const controller = new DeleteCustomerController(useCase);
|
|
|
|
|
return controller.execute(req, res, next);
|
2025-08-11 17:49:52 +00:00
|
|
|
}
|
2025-09-19 16:55:30 +00:00
|
|
|
);*/
|
2025-08-11 17:49:52 +00:00
|
|
|
|
2026-03-07 21:39:21 +00:00
|
|
|
app.use(`${config.server.apiBasePath}/customers`, router);
|
2025-08-11 17:49:52 +00:00
|
|
|
};
|