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";
|
|
|
|
|
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";
|
2026-03-07 21:39:21 +00:00
|
|
|
import type { CustomersInternalDeps } from "./customers.di";
|
|
|
|
|
|
2026-03-16 17:45:45 +00:00
|
|
|
type CustomerServicesContext = {
|
|
|
|
|
transaction: Transaction;
|
|
|
|
|
companyId: UniqueID;
|
2026-03-07 21:39:21 +00:00
|
|
|
};
|
|
|
|
|
|
2026-03-16 17:45:45 +00:00
|
|
|
export type CustomerPublicServices = {
|
|
|
|
|
//listCustomers: (filters: unknown, context: unknown) => null;
|
|
|
|
|
findCustomerByTIN: (
|
|
|
|
|
tin: TINNumber,
|
|
|
|
|
context: CustomerServicesContext
|
|
|
|
|
) => Promise<Result<Customer, Error>>;
|
|
|
|
|
createCustomer: (
|
|
|
|
|
id: UniqueID,
|
|
|
|
|
props: ICustomerCreateProps,
|
|
|
|
|
context: CustomerServicesContext
|
|
|
|
|
) => Promise<Result<Customer, Error>>;
|
|
|
|
|
//generateCustomerReport: (id: unknown, options: unknown, context: unknown) => null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function buildCustomerServices(
|
|
|
|
|
params: SetupParams,
|
|
|
|
|
deps: CustomersInternalDeps
|
|
|
|
|
): CustomerPublicServices {
|
|
|
|
|
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-16 17:45:45 +00:00
|
|
|
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,
|
|
|
|
|
});
|
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
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
}
|