Files
cfw-autumn/shared/utils/cusUtils/fullCusUtils/enrichFullCustomer.ts
2026-01-21 12:50:19 +00:00

47 lines
1.3 KiB
TypeScript

import { InternalError } from "@api/errors/base/InternalError.js";
import type { Entity } from "@models/cusModels/entityModels/entityModels.js";
import type { FullCustomer } from "@models/cusModels/fullCusModel.js";
type FullCustomerWithEntity = FullCustomer & { entity: Entity };
// Overload: errorOnNotFound = true → guaranteed entity
export function enrichFullCustomerWithEntity(params: {
fullCustomer: FullCustomer;
internalEntityId: string | null;
errorOnNotFound: true;
}): FullCustomerWithEntity;
// Overload: errorOnNotFound = false/undefined → entity may be undefined
export function enrichFullCustomerWithEntity(params: {
fullCustomer: FullCustomer;
internalEntityId: string | null;
errorOnNotFound?: false;
}): FullCustomer;
// Implementation
export function enrichFullCustomerWithEntity({
fullCustomer,
internalEntityId,
errorOnNotFound,
}: {
fullCustomer: FullCustomer;
internalEntityId: string | null;
errorOnNotFound?: boolean;
}): FullCustomer | FullCustomerWithEntity {
if (internalEntityId === null) {
fullCustomer.entity = undefined;
} else {
fullCustomer.entity = fullCustomer.entities?.find(
(e) => e.internal_id === internalEntityId,
);
}
if (errorOnNotFound && !fullCustomer.entity) {
throw new InternalError({
message: `Entity not found for internal_id: ${internalEntityId}`,
});
}
return fullCustomer;
}