// modules/invoice/infrastructure/invoice-dependencies.factory.ts import { type JsonTaxCatalogProvider, SpainTaxCatalogProvider } from "@erp/core"; import type { IMapperRegistry, IPresenterRegistry, IRendererRegistry, ModuleParams, } from "@erp/core/api"; import { FastReportExecutableResolver, FastReportProcessRunner, FastReportTemplateResolver, FileSystemReportStorage, InMemoryMapperRegistry, InMemoryPresenterRegistry, InMemoryRendererRegistry, 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"; import { IssuedInvoiceReportJSONRenderer } from "./renderers"; import { IssuedInvoiceReportPDFRenderer } from "./renderers/issued-invoice-report-pdf.renderer"; import { CustomerInvoiceRepository } from "./sequelize"; import { SequelizeInvoiceNumberGenerator } from "./services"; export type IssuedInvoicesDeps = { transactionManager: SequelizeTransactionManager; mapperRegistry: IMapperRegistry; presenterRegistry: IPresenterRegistry; rendererRegistry: IRendererRegistry; 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 { const { database, env } = params; const templateRootPath = env.TEMPLATES_PATH; const documentRootPath = env.DOCUMENTS_PATH; /** 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(); // 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 ), }, { key: { resource: "issued-invoice", format: "JSON" }, renderer: new IssuedInvoiceReportJSONRenderer(), }, ]); /** 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 { key: { resource: "issued-invoice", projection: "REPORT", format: "DTO" }, presenter: new IssuedInvoiceReportPresenter(presenterRegistry), }, { key: { resource: "issued-invoice-taxes", projection: "REPORT", format: "DTO" }, presenter: new IssuedInvoiceTaxesReportPresenter(presenterRegistry), }, { key: { resource: "issued-invoice-items", projection: "REPORT", format: "DTO" }, 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: () => new ReportIssuedInvoiceUseCase( appService, transactionManager, presenterRegistry, rendererRegistry ), }; return { transactionManager, repo: repository, mapperRegistry, presenterRegistry, rendererRegistry, appService, catalogs, useCases, }; }