Uecko_ERP/modules/customer-invoices/src/api/infrastructure/issued-invoices-dependencies.ts
2025-11-17 18:20:18 +01:00

149 lines
4.9 KiB
TypeScript

// modules/invoice/infrastructure/invoice-dependencies.factory.ts
import { type JsonTaxCatalogProvider, SpainTaxCatalogProvider } from "@erp/core";
import type { IMapperRegistry, IPresenterRegistry, ModuleParams } from "@erp/core/api";
import {
InMemoryMapperRegistry,
InMemoryPresenterRegistry,
SequelizeTransactionManager,
} from "@erp/core/api";
import {
CustomerInvoiceApplicationService,
GetIssuedInvoiceUseCase,
IssuedInvoiceFullPresenter,
IssuedInvoiceItemsFullPresenter,
IssuedInvoiceItemsReportPresenter,
IssuedInvoiceListPresenter,
IssuedInvoiceRecipientFullPresenter,
IssuedInvoiceReportPresenter,
IssuedInvoiceTaxesReportPresenter,
IssuedInvoiceVerifactuFullPresenter,
ListIssuedInvoicesUseCase,
ReportIssuedInvoiceUseCase,
} from "../application";
import {
IssuedInvoiceReportHTMLPresenter,
IssuedInvoiceReportPDFPresenter,
} from "../application/use-cases/issued-invoices/report-issued-invoices/reporter";
import { CustomerInvoiceDomainMapper, CustomerInvoiceListMapper } from "./mappers";
import { CustomerInvoiceRepository } from "./sequelize";
import { SequelizeInvoiceNumberGenerator } from "./services";
export type IssuedInvoicesDeps = {
transactionManager: SequelizeTransactionManager;
mapperRegistry: IMapperRegistry;
presenterRegistry: IPresenterRegistry;
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 } = params;
/** 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();
/** 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: "JSON" },
presenter: new IssuedInvoiceReportPresenter(presenterRegistry),
},
{
key: { resource: "issued-invoice-taxes", projection: "REPORT", format: "JSON" },
presenter: new IssuedInvoiceTaxesReportPresenter(presenterRegistry),
},
{
key: { resource: "issued-invoice-items", projection: "REPORT", format: "JSON" },
presenter: new IssuedInvoiceItemsReportPresenter(presenterRegistry),
},
{
key: { resource: "issued-invoice", projection: "REPORT", format: "HTML" },
presenter: new IssuedInvoiceReportHTMLPresenter(presenterRegistry),
},
{
key: { resource: "issued-invoice", projection: "REPORT", format: "PDF" },
presenter: new IssuedInvoiceReportPDFPresenter(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),
};
return {
transactionManager,
repo: repository,
mapperRegistry,
presenterRegistry,
appService,
catalogs,
useCases,
};
}