Uecko_ERP/modules/customer-invoices/src/api/infrastructure/issued-invoices-dependencies.ts

176 lines
5.5 KiB
TypeScript
Raw Normal View History

2025-11-17 17:20:18 +00:00
// modules/invoice/infrastructure/invoice-dependencies.factory.ts
import { type JsonTaxCatalogProvider, SpainTaxCatalogProvider } from "@erp/core";
2025-11-19 16:05:01 +00:00
import type {
IMapperRegistry,
IPresenterRegistry,
2026-01-13 18:41:14 +00:00
IRendererRegistry,
2025-11-19 16:05:01 +00:00
ModuleParams,
} from "@erp/core/api";
2025-11-17 17:20:18 +00:00
import {
2026-01-13 18:41:14 +00:00
FastReportExecutableResolver,
FastReportProcessRunner,
FastReportTemplateResolver,
FileSystemReportStorage,
2025-11-17 17:20:18 +00:00
InMemoryMapperRegistry,
InMemoryPresenterRegistry,
2026-01-13 18:41:14 +00:00
InMemoryRendererRegistry,
2025-11-17 17:20:18 +00:00
SequelizeTransactionManager,
} from "@erp/core/api";
import {
CustomerInvoiceApplicationService,
GetIssuedInvoiceUseCase,
IssuedInvoiceFullPresenter,
IssuedInvoiceItemsFullPresenter,
IssuedInvoiceItemsReportPresenter,
IssuedInvoiceListPresenter,
IssuedInvoiceRecipientFullPresenter,
IssuedInvoiceReportPresenter,
IssuedInvoiceTaxesReportPresenter,
IssuedInvoiceVerifactuFullPresenter,
ListIssuedInvoicesUseCase,
ReportIssuedInvoiceUseCase,
} from "../application";
import { CustomerInvoiceDomainMapper, CustomerInvoiceListMapper } from "./mappers";
2026-01-13 18:41:14 +00:00
import { IssuedInvoiceReportPDFRenderer } from "./renderers/issued-invoice-report-pdf.renderer";
2025-11-17 17:20:18 +00:00
import { CustomerInvoiceRepository } from "./sequelize";
import { SequelizeInvoiceNumberGenerator } from "./services";
export type IssuedInvoicesDeps = {
transactionManager: SequelizeTransactionManager;
mapperRegistry: IMapperRegistry;
presenterRegistry: IPresenterRegistry;
2026-01-13 18:41:14 +00:00
rendererRegistry: IRendererRegistry;
2025-11-17 17:20:18 +00:00
repo: CustomerInvoiceRepository;
appService: CustomerInvoiceApplicationService;
catalogs: {
taxes: JsonTaxCatalogProvider;
};
useCases: {
list_issued_invoices: () => ListIssuedInvoicesUseCase;
get_issued_invoice: () => GetIssuedInvoiceUseCase;
report_issued_invoice: () => ReportIssuedInvoiceUseCase;
};
};
export function buildIssuedInvoicesDependencies(params: ModuleParams): IssuedInvoicesDeps {
2026-01-13 18:41:14 +00:00
const { database, env } = params;
const templateRootPath = env.TEMPLATES_PATH;
const documentRootPath = env.DOCUMENTS_PATH;
2025-11-17 17:20:18 +00:00
/** Dominio */
const catalogs = { taxes: SpainTaxCatalogProvider() };
/** Infraestructura */
const transactionManager = new SequelizeTransactionManager(database);
const mapperRegistry = new InMemoryMapperRegistry();
mapperRegistry
.registerDomainMapper(
{ resource: "customer-invoice" },
new CustomerInvoiceDomainMapper({ taxCatalog: catalogs.taxes })
)
.registerQueryMappers([
{
key: { resource: "customer-invoice", query: "LIST" },
mapper: new CustomerInvoiceListMapper(),
},
]);
// Repository & Services
const repository = new CustomerInvoiceRepository({ mapperRegistry, database });
const numberGenerator = new SequelizeInvoiceNumberGenerator();
2026-01-13 18:41:14 +00:00
// Renderers Registry
const frExecResolver = new FastReportExecutableResolver();
const frProcessRunner = new FastReportProcessRunner();
const frTemplateResolver = new FastReportTemplateResolver(templateRootPath);
const rendererRegistry = new InMemoryRendererRegistry();
const reportStorage = new FileSystemReportStorage(documentRootPath);
rendererRegistry.registerRenderers([
{
key: { resource: "issued-invoice", format: "PDF" },
renderer: new IssuedInvoiceReportPDFRenderer(
frExecResolver,
frProcessRunner,
frTemplateResolver,
reportStorage
),
},
]);
2025-11-17 17:20:18 +00:00
/** Aplicación */
const appService = new CustomerInvoiceApplicationService(repository, numberGenerator);
// Presenter Registry
const presenterRegistry = new InMemoryPresenterRegistry();
presenterRegistry.registerPresenters([
// FULL
{
key: { resource: "issued-invoice-items", projection: "FULL" },
presenter: new IssuedInvoiceItemsFullPresenter(presenterRegistry),
},
{
key: { resource: "issued-invoice-recipient", projection: "FULL" },
presenter: new IssuedInvoiceRecipientFullPresenter(presenterRegistry),
},
{
key: { resource: "issued-invoice-verifactu", projection: "FULL" },
presenter: new IssuedInvoiceVerifactuFullPresenter(presenterRegistry),
},
{
key: { resource: "issued-invoice", projection: "FULL" },
presenter: new IssuedInvoiceFullPresenter(presenterRegistry),
},
// LIST
{
key: { resource: "issued-invoice", projection: "LIST" },
presenter: new IssuedInvoiceListPresenter(presenterRegistry),
},
// REPORT
{
2026-01-13 18:41:14 +00:00
key: { resource: "issued-invoice", projection: "REPORT", format: "DTO" },
2025-11-17 17:20:18 +00:00
presenter: new IssuedInvoiceReportPresenter(presenterRegistry),
},
{
2026-01-13 18:41:14 +00:00
key: { resource: "issued-invoice-taxes", projection: "REPORT", format: "DTO" },
2025-11-17 17:20:18 +00:00
presenter: new IssuedInvoiceTaxesReportPresenter(presenterRegistry),
},
{
2026-01-13 18:41:14 +00:00
key: { resource: "issued-invoice-items", projection: "REPORT", format: "DTO" },
2025-11-17 17:20:18 +00:00
presenter: new IssuedInvoiceItemsReportPresenter(presenterRegistry),
},
]);
const useCases: IssuedInvoicesDeps["useCases"] = {
// Issue Invoices
list_issued_invoices: () =>
new ListIssuedInvoicesUseCase(appService, transactionManager, presenterRegistry),
get_issued_invoice: () =>
new GetIssuedInvoiceUseCase(appService, transactionManager, presenterRegistry),
report_issued_invoice: () =>
2026-01-13 18:41:14 +00:00
new ReportIssuedInvoiceUseCase(
appService,
transactionManager,
presenterRegistry,
rendererRegistry
),
2025-11-17 17:20:18 +00:00
};
return {
transactionManager,
repo: repository,
mapperRegistry,
presenterRegistry,
2026-01-13 18:41:14 +00:00
rendererRegistry,
2025-11-17 17:20:18 +00:00
appService,
catalogs,
useCases,
};
}