61 lines
2.4 KiB
TypeScript
61 lines
2.4 KiB
TypeScript
import { mockUser, requireAuthenticated, requireCompanyContext } from "@erp/auth/api";
|
|
import { type RequestWithAuth, type StartParams, validateRequest } from "@erp/core/api";
|
|
import type { IProformaPublicServices } from "@erp/customer-invoices/api";
|
|
import type { ICustomerPublicServices } from "@erp/customers/api";
|
|
import { CreateProformaFromFactugesRequestSchema } from "@erp/factuges/common";
|
|
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: StartParams) => {
|
|
const { app, config, getInternal, getService } = params;
|
|
|
|
// Recuperamos el dominio interno del módulo
|
|
const deps = getInternal<FactugesInternalDeps>("factuges");
|
|
|
|
// Recuperamos servicios externos que necesitemos
|
|
const customerServices = getService<ICustomerPublicServices>("customers:general");
|
|
const proformaServices = getService<IProformaPublicServices>("customer-invoices:proformas");
|
|
|
|
const publicServices = {
|
|
customerServices,
|
|
proformaServices,
|
|
};
|
|
|
|
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);
|
|
};
|