2026-03-16 17:45:45 +00:00
|
|
|
import { mockUser, requireAuthenticated, requireCompanyContext } from "@erp/auth/api";
|
|
|
|
|
import { type ModuleParams, type RequestWithAuth, validateRequest } from "@erp/core/api";
|
|
|
|
|
import type { ProformaPublicServices } from "@erp/customer-invoices/api";
|
|
|
|
|
import type { CustomerPublicServices } from "@erp/customers/api";
|
2026-03-18 16:38:40 +00:00
|
|
|
import { CreateProformaFromFactugesRequestSchema } from "@erp/factuges/common";
|
2026-03-16 17:45:45 +00:00
|
|
|
import { type NextFunction, type Request, type Response, Router } from "express";
|
|
|
|
|
|
|
|
|
|
import type { FactugesInternalDeps } from "../di/factuges.di";
|
|
|
|
|
|
|
|
|
|
import { CreateProformaFromFactugesController } from "./controllers";
|
|
|
|
|
|
|
|
|
|
export const factugesRouter = (
|
|
|
|
|
params: ModuleParams,
|
|
|
|
|
deps: FactugesInternalDeps,
|
|
|
|
|
publicServices: {
|
|
|
|
|
customerServices: CustomerPublicServices;
|
|
|
|
|
proformaServices: ProformaPublicServices;
|
|
|
|
|
}
|
|
|
|
|
) => {
|
|
|
|
|
const { app, config } = params;
|
|
|
|
|
|
|
|
|
|
const router: Router = Router({ mergeParams: true });
|
|
|
|
|
|
|
|
|
|
// 🔐 Autenticación + Tenancy para TODO el router
|
|
|
|
|
if (process.env.NODE_ENV === "development" || process.env.NODE_ENV === "production") {
|
|
|
|
|
router.use(
|
|
|
|
|
(req: Request, res: Response, next: NextFunction) =>
|
|
|
|
|
mockUser(req as RequestWithAuth, res, next) // Debe ir antes de las rutas protegidas
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//router.use(/*authenticateJWT(),*/ enforceTenant() /*checkTabContext*/);
|
|
|
|
|
router.use([
|
|
|
|
|
(req: Request, res: Response, next: NextFunction) =>
|
|
|
|
|
requireAuthenticated()(req as RequestWithAuth, res, next), // Debe ir antes de las rutas protegidas
|
|
|
|
|
|
|
|
|
|
(req: Request, res: Response, next: NextFunction) =>
|
|
|
|
|
requireCompanyContext()(req as RequestWithAuth, res, next), // Debe ir antes de las rutas protegidas
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------
|
|
|
|
|
|
|
|
|
|
router.post(
|
|
|
|
|
"/",
|
|
|
|
|
//checkTabContext,
|
|
|
|
|
validateRequest(CreateProformaFromFactugesRequestSchema, "body"),
|
|
|
|
|
(req: Request, res: Response, next: NextFunction) => {
|
|
|
|
|
const useCase = deps.useCases.createProforma(publicServices);
|
|
|
|
|
const controller = new CreateProformaFromFactugesController(useCase);
|
|
|
|
|
return controller.execute(req, res, next);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
app.use(`${config.server.apiBasePath}/factuges`, router);
|
|
|
|
|
};
|