/home/alixis5/integrate.sellofinance.com/src/Services
Edit: /home/alixis5/integrate.sellofinance.com/src/Services/CustomerBundleLoader.php (3952B)
*/
public function loadByProfileId(string $profileId): array
{
$profile = null;
try {
$profile = $this->client->getById('CCustomerProfile', $profileId);
} catch (EspoApiException) {
// treat missing as null
}
$employment = $this->findOneLinked('CCustomerEmployment', 'customerProfileId', $profileId, [
'id','customerProfileId','employmentType','employerCategory','employerName',
'salaryTransfer','monthsInBusiness','workSinceDate','status',
]);
$financial = $this->findOneLinked('CCustomerFinancialProfile', 'customerProfileId', $profileId, [
'id','customerProfileId','verifiedMonthlyIncome','declaredMonthlyIncome',
'dbtRatio','incomeStability','status',
]);
$address = $this->findOneLinked('CCustomerAddress', 'customerProfileId', $profileId, [
'id','customerProfileId','country','city','area','street','postalCode','addressType','status',
], extraWhere: [['type' => 'equals', 'attribute' => 'addressType', 'value' => 'home']]);
return [
'CCustomerProfile' => $profile,
'CCustomerEmployment' => $employment,
'CCustomerFinancialProfile' => $financial,
'CCustomerAddress' => $address,
// CScoringResult is always null until Phase 3 (scoring service not
// built yet). Impact on live users: any CEligibilityRule targeting
// creditScore will resolve to 'pending' for EVERY customer, even
// those who have granted credit consent, so products with a
// creditScore rule will show verdict 'conditional /
// profile_incomplete → step 3' when onboarding is actually
// complete. Acceptable for Phase 2B launch; must be resolved in
// Phase 3 before any creditScore rule becomes load-bearing.
'CScoringResult' => null,
];
}
/**
* @return array
|null
*/
private function findOneLinked(string $entity, string $fk, string $fkValue, array $select, array $extraWhere = []): ?array
{
try {
// Versioned-replace semantics: the newest row per customerProfileId is the
// current "active" one. A status='active' filter would be cleaner, but the
// KYC entities (CCustomerFinancialProfile/Employment/Address) don't have
// a status column on the EspoCRM schema yet — filtering on it returns HTTP
// 400 and leaves the bundle entity null, producing a spurious 'pending'
// verdict on every rule that sources from those entities. Fall back to
// "newest row wins" via orderBy=createdAt desc + maxSize=1.
$where = [
['type' => 'equals', 'attribute' => $fk, 'value' => $fkValue],
];
foreach ($extraWhere as $w) $where[] = $w;
$res = $this->client->get($entity, [
'select' => implode(',', $select),
'maxSize' => 1,
'orderBy' => 'createdAt',
'order' => 'desc',
'where' => $where,
]);
return $res['list'][0] ?? null;
} catch (EspoApiException) {
return null;
}
}
}