Uecko_ERP/modules/customers/src/api/infrastructure/di/customer-public-services.ts

66 lines
1.9 KiB
TypeScript
Raw Normal View History

2026-03-25 09:34:17 +00:00
import { type SetupParams, buildCatalogs } from "@erp/core/api";
2026-03-16 17:45:45 +00:00
import type { TINNumber, UniqueID } from "@repo/rdx-ddd";
import { Result } from "@repo/rdx-utils";
2026-03-28 21:10:05 +00:00
import {
type ICustomerPublicServices,
type ICustomerServicesContext,
buildCustomerCreator,
buildCustomerFinder,
} from "../../application";
import type { ICustomerCreateProps } from "../../domain";
2026-03-16 17:45:45 +00:00
import { buildCustomerPersistenceMappers } from "./customer-persistence-mappers.di";
import { buildCustomerRepository } from "./customer-repositories.di";
2026-03-07 21:39:21 +00:00
import type { CustomersInternalDeps } from "./customers.di";
2026-03-28 21:10:05 +00:00
export function buildCustomerPublicServices(
2026-03-16 17:45:45 +00:00
params: SetupParams,
deps: CustomersInternalDeps
2026-03-28 21:10:05 +00:00
): ICustomerPublicServices {
2026-03-16 17:45:45 +00:00
const { database } = params;
2026-03-25 09:34:17 +00:00
const catalogs = buildCatalogs();
2026-03-16 17:45:45 +00:00
// Infrastructure
2026-03-25 09:34:17 +00:00
const persistenceMappers = buildCustomerPersistenceMappers(catalogs);
2026-03-16 17:45:45 +00:00
const repository = buildCustomerRepository({ database, mappers: persistenceMappers });
const finder = buildCustomerFinder({ repository });
const creator = buildCustomerCreator({ repository });
2026-03-07 21:39:21 +00:00
return {
2026-03-28 21:10:05 +00:00
findCustomerByTIN: async (tin: TINNumber, context: ICustomerServicesContext) => {
2026-03-16 17:45:45 +00:00
const { companyId, transaction } = context;
const customerResult = await finder.findCustomerByTIN(companyId, tin, transaction);
if (customerResult.isFailure) {
return Result.fail(customerResult.error);
}
return Result.ok(customerResult.data);
},
createCustomer: async (
id: UniqueID,
props: ICustomerCreateProps,
2026-03-28 21:10:05 +00:00
context: ICustomerServicesContext
2026-03-16 17:45:45 +00:00
) => {
const { companyId, transaction } = context;
const customerResult = await creator.create({
companyId,
id,
props,
transaction,
});
2026-03-07 21:39:21 +00:00
2026-03-16 17:45:45 +00:00
if (customerResult.isFailure) {
return Result.fail(customerResult.error);
}
2026-03-07 21:39:21 +00:00
2026-03-16 17:45:45 +00:00
return Result.ok(customerResult.data);
2026-03-07 21:39:21 +00:00
},
};
}