import type { SetupParams } from "@erp/core/api"; import type { TINNumber, UniqueID } from "@repo/rdx-ddd"; import { Result } from "@repo/rdx-utils"; import type { Transaction } from "sequelize"; import { buildCustomerCreator, buildCustomerFinder } from "../../application"; import type { Customer, ICustomerCreateProps } from "../../domain"; import { buildCustomerPersistenceMappers } from "./customer-persistence-mappers.di"; import { buildCustomerRepository } from "./customer-repositories.di"; import type { CustomersInternalDeps } from "./customers.di"; type CustomerServicesContext = { transaction: Transaction; companyId: UniqueID; }; export type CustomerPublicServices = { //listCustomers: (filters: unknown, context: unknown) => null; findCustomerByTIN: ( tin: TINNumber, context: CustomerServicesContext ) => Promise>; createCustomer: ( id: UniqueID, props: ICustomerCreateProps, context: CustomerServicesContext ) => Promise>; //generateCustomerReport: (id: unknown, options: unknown, context: unknown) => null; }; export function buildCustomerServices( params: SetupParams, deps: CustomersInternalDeps ): CustomerPublicServices { const { database } = params; // Infrastructure const persistenceMappers = buildCustomerPersistenceMappers(); const repository = buildCustomerRepository({ database, mappers: persistenceMappers }); const finder = buildCustomerFinder({ repository }); const creator = buildCustomerCreator({ repository }); return { findCustomerByTIN: async (tin: TINNumber, context: CustomerServicesContext) => { 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: CustomerServicesContext ) => { 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); }, }; }