2025-09-03 18:04:09 +00:00
|
|
|
import { RequestWithAuth, enforceTenant, enforceUser, mockUser } from "@erp/auth/api";
|
2025-06-24 18:38:57 +00:00
|
|
|
import { ILogger, ModuleParams, validateRequest } from "@erp/core/api";
|
2025-05-04 20:06:57 +00:00
|
|
|
import { Application, NextFunction, Request, Response, Router } from "express";
|
|
|
|
|
import { Sequelize } from "sequelize";
|
2025-06-24 18:38:57 +00:00
|
|
|
import {
|
2025-08-12 15:12:44 +00:00
|
|
|
CreateCustomerInvoiceRequestSchema,
|
2025-08-12 11:23:50 +00:00
|
|
|
CustomerInvoiceListRequestSchema,
|
2025-08-12 15:12:44 +00:00
|
|
|
GetCustomerInvoiceByIdRequestSchema,
|
2025-09-12 09:14:27 +00:00
|
|
|
ReportCustomerInvoiceByIdRequestSchema,
|
2025-06-24 18:38:57 +00:00
|
|
|
} from "../../../common/dto";
|
2025-09-13 18:45:55 +00:00
|
|
|
import { buildCustomerInvoiceDependencies } from "../dependencies";
|
2025-06-26 14:35:53 +00:00
|
|
|
import {
|
2025-08-14 14:58:13 +00:00
|
|
|
CreateCustomerInvoiceController,
|
|
|
|
|
GetCustomerInvoiceController,
|
|
|
|
|
ListCustomerInvoicesController,
|
2025-09-12 16:23:36 +00:00
|
|
|
ReportCustomerInvoiceController,
|
2025-08-14 14:58:13 +00:00
|
|
|
} from "./controllers";
|
2025-05-04 20:06:57 +00:00
|
|
|
|
2025-06-12 06:55:17 +00:00
|
|
|
export const customerInvoicesRouter = (params: ModuleParams) => {
|
2025-09-13 18:45:55 +00:00
|
|
|
const { app, baseRoutePath, logger } = params as {
|
2025-05-04 20:06:57 +00:00
|
|
|
app: Application;
|
|
|
|
|
database: Sequelize;
|
|
|
|
|
baseRoutePath: string;
|
2025-06-12 06:55:17 +00:00
|
|
|
logger: ILogger;
|
2025-05-04 20:06:57 +00:00
|
|
|
};
|
|
|
|
|
|
2025-09-13 18:45:55 +00:00
|
|
|
const deps = buildCustomerInvoiceDependencies(params);
|
2025-05-04 20:06:57 +00:00
|
|
|
|
2025-09-03 18:04:09 +00:00
|
|
|
const router: Router = Router({ mergeParams: true });
|
|
|
|
|
|
2025-08-14 14:58:13 +00:00
|
|
|
// 🔐 Autenticación + Tenancy para TODO el router
|
2025-09-03 18:04:09 +00:00
|
|
|
if (process.env.NODE_ENV === "development") {
|
|
|
|
|
router.use(
|
|
|
|
|
(req: Request, res: Response, next: NextFunction) =>
|
|
|
|
|
mockUser(req as RequestWithAuth, res, next) // Debe ir antes de las rutas protegidas
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
router.use([
|
|
|
|
|
(req: Request, res: Response, next: NextFunction) =>
|
|
|
|
|
enforceUser()(req as RequestWithAuth, res, next), // Debe ir antes de las rutas protegidas
|
|
|
|
|
|
|
|
|
|
(req: Request, res: Response, next: NextFunction) =>
|
|
|
|
|
enforceTenant()(req as RequestWithAuth, res, next), // Debe ir antes de las rutas protegidas
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------
|
2025-08-14 14:58:13 +00:00
|
|
|
|
|
|
|
|
router.get(
|
2025-05-04 20:06:57 +00:00
|
|
|
"/",
|
|
|
|
|
//checkTabContext,
|
2025-08-12 11:23:50 +00:00
|
|
|
validateRequest(CustomerInvoiceListRequestSchema, "params"),
|
2025-09-03 18:04:09 +00:00
|
|
|
async (req: Request, res: Response, next: NextFunction) => {
|
2025-08-14 14:58:13 +00:00
|
|
|
const useCase = deps.build.list();
|
|
|
|
|
const controller = new ListCustomerInvoicesController(useCase /*, deps.presenters.list */);
|
|
|
|
|
return controller.execute(req, res, next);
|
2025-05-04 20:06:57 +00:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2025-08-14 14:58:13 +00:00
|
|
|
router.get(
|
2025-09-10 10:27:07 +00:00
|
|
|
"/:invoice_id",
|
2025-05-04 20:06:57 +00:00
|
|
|
//checkTabContext,
|
2025-08-12 15:12:44 +00:00
|
|
|
validateRequest(GetCustomerInvoiceByIdRequestSchema, "params"),
|
2025-05-04 20:06:57 +00:00
|
|
|
(req: Request, res: Response, next: NextFunction) => {
|
2025-08-14 14:58:13 +00:00
|
|
|
const useCase = deps.build.get();
|
|
|
|
|
const controller = new GetCustomerInvoiceController(useCase);
|
|
|
|
|
return controller.execute(req, res, next);
|
2025-05-04 20:06:57 +00:00
|
|
|
}
|
2025-06-26 18:05:33 +00:00
|
|
|
);
|
2025-05-04 20:06:57 +00:00
|
|
|
|
2025-08-14 14:58:13 +00:00
|
|
|
router.post(
|
2025-05-04 20:06:57 +00:00
|
|
|
"/",
|
|
|
|
|
//checkTabContext,
|
2025-08-14 14:58:13 +00:00
|
|
|
|
2025-09-14 10:50:29 +00:00
|
|
|
validateRequest(CreateCustomerInvoiceRequestSchema, "body"),
|
2025-05-04 20:06:57 +00:00
|
|
|
(req: Request, res: Response, next: NextFunction) => {
|
2025-08-14 14:58:13 +00:00
|
|
|
const useCase = deps.build.create();
|
|
|
|
|
const controller = new CreateCustomerInvoiceController(useCase);
|
|
|
|
|
return controller.execute(req, res, next);
|
2025-05-04 20:06:57 +00:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2025-09-03 18:04:09 +00:00
|
|
|
/*router.put(
|
2025-09-10 10:27:07 +00:00
|
|
|
"/:invoice_id",
|
2025-09-03 18:04:09 +00:00
|
|
|
//checkTabContext,
|
|
|
|
|
validateRequest(UpdateCustomerInvoiceByIdParamsRequestSchema, "params"),
|
|
|
|
|
validateRequest(UpdateCustomerInvoiceByIdRequestSchema, "body"),
|
2025-05-04 20:06:57 +00:00
|
|
|
(req: Request, res: Response, next: NextFunction) => {
|
2025-09-03 18:04:09 +00:00
|
|
|
const useCase = deps.build.update();
|
|
|
|
|
const controller = new UpdateCustomerInvoiceController(useCase);
|
|
|
|
|
return controller.execute(req, res, next);
|
2025-05-04 20:06:57 +00:00
|
|
|
}
|
2025-06-26 18:25:38 +00:00
|
|
|
);*/
|
2025-05-04 20:06:57 +00:00
|
|
|
|
2025-09-13 18:45:55 +00:00
|
|
|
/*router.delete(
|
2025-09-10 10:27:07 +00:00
|
|
|
"/:invoice_id",
|
2025-06-26 18:25:38 +00:00
|
|
|
//checkTabContext,
|
2025-08-14 14:58:13 +00:00
|
|
|
|
2025-08-12 15:12:44 +00:00
|
|
|
validateRequest(DeleteCustomerInvoiceByIdRequestSchema, "params"),
|
2025-05-04 20:06:57 +00:00
|
|
|
(req: Request, res: Response, next: NextFunction) => {
|
2025-08-14 14:58:13 +00:00
|
|
|
const useCase = deps.build.delete();
|
|
|
|
|
const controller = new DeleteCustomerInvoiceController(useCase);
|
|
|
|
|
return controller.execute(req, res, next);
|
2025-05-04 20:06:57 +00:00
|
|
|
}
|
2025-09-13 18:45:55 +00:00
|
|
|
);*/
|
2025-06-24 18:38:57 +00:00
|
|
|
|
2025-09-12 09:14:27 +00:00
|
|
|
router.get(
|
|
|
|
|
"/:invoice_id/report",
|
|
|
|
|
//checkTabContext,
|
|
|
|
|
validateRequest(ReportCustomerInvoiceByIdRequestSchema, "params"),
|
|
|
|
|
(req: Request, res: Response, next: NextFunction) => {
|
|
|
|
|
const useCase = deps.build.report();
|
|
|
|
|
const controller = new ReportCustomerInvoiceController(useCase);
|
|
|
|
|
return controller.execute(req, res, next);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
2025-09-19 17:25:41 +00:00
|
|
|
app.use(`${baseRoutePath}/proforma-invoices`, router);
|
2025-05-04 20:06:57 +00:00
|
|
|
};
|