66 lines
1.9 KiB
TypeScript
66 lines
1.9 KiB
TypeScript
import { type SetupParams, buildCatalogs } from "@erp/core/api";
|
|
import type { TINNumber, UniqueID } from "@repo/rdx-ddd";
|
|
import { Result } from "@repo/rdx-utils";
|
|
|
|
import {
|
|
type ICustomerPublicServices,
|
|
type ICustomerServicesContext,
|
|
buildCustomerCreator,
|
|
buildCustomerFinder,
|
|
} from "../../application";
|
|
import type { ICustomerCreateProps } from "../../domain";
|
|
|
|
import { buildCustomerPersistenceMappers } from "./customer-persistence-mappers.di";
|
|
import { buildCustomerRepository } from "./customer-repositories.di";
|
|
import type { CustomersInternalDeps } from "./customers.di";
|
|
|
|
export function buildCustomerPublicServices(
|
|
params: SetupParams,
|
|
deps: CustomersInternalDeps
|
|
): ICustomerPublicServices {
|
|
const { database } = params;
|
|
const catalogs = buildCatalogs();
|
|
|
|
// Infrastructure
|
|
const persistenceMappers = buildCustomerPersistenceMappers(catalogs);
|
|
const repository = buildCustomerRepository({ database, mappers: persistenceMappers });
|
|
|
|
const finder = buildCustomerFinder({ repository });
|
|
const creator = buildCustomerCreator({ repository });
|
|
|
|
return {
|
|
findCustomerByTIN: async (tin: TINNumber, context: ICustomerServicesContext) => {
|
|
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,
|
|
context: ICustomerServicesContext
|
|
) => {
|
|
const { companyId, transaction } = context;
|
|
|
|
const customerResult = await creator.create({
|
|
companyId,
|
|
id,
|
|
props,
|
|
transaction,
|
|
});
|
|
|
|
if (customerResult.isFailure) {
|
|
return Result.fail(customerResult.error);
|
|
}
|
|
|
|
return Result.ok(customerResult.data);
|
|
},
|
|
};
|
|
}
|