Skip to content

Commit

Permalink
Merge pull request #2718 from bakaphp/hotfix/branch-listing
Browse files Browse the repository at this point in the history
htofix: parser
  • Loading branch information
kaioken authored Dec 23, 2024
2 parents 244906c + 700ae75 commit 05ab5f6
Show file tree
Hide file tree
Showing 2 changed files with 249 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,17 +87,17 @@ public function parseLead(array $request, array $template): array
$parsedData = [];
$customFields = [];
$processFields = [];

// Initialize people structure with placeholders
$peopleStructure = [
'firstname' => null,
'lastname' => null,
'contacts' => [],
];

// Iterate through the template and map values accordingly
foreach ($template as $path => $info) {
// Fetch the value and use the default if the value is empty
$value = $this->getValueFromPath($request, $path);
$value = ! empty($value) ? $value : ($info['default'] ?? null);

$name = $info['name'];
$type = $info['type'];

Expand Down Expand Up @@ -161,27 +161,20 @@ private function mapFunctionType(array &$parsedData, array $request, array $info

public function getValueFromPath(array $array, string $path): string
{
$values = [];
$paths = explode(' ', $path);
foreach ($paths as $p) {
$keys = explode('.', $p);
$tempArray = $array;
foreach ($keys as $key) {
if (isset($tempArray[$key])) {
$tempArray = $tempArray[$key];
} else {
$tempArray = null;

break;
}
}
// Check if the value is a string before appending
if (is_string($tempArray) || is_numeric($tempArray)) {
$values[] = $tempArray;
$keys = explode('.', $path); // Use dot notation for hierarchical keys
$tempArray = $array;

foreach ($keys as $key) {
$key = trim($key); // Remove any unnecessary spaces
if (isset($tempArray[$key])) {
$tempArray = $tempArray[$key];
} else {
return ''; // Return an empty string if the key does not exist
}
}

return implode(' ', $values);
// Ensure the value is a string or numeric
return is_string($tempArray) || is_numeric($tempArray) ? (string) $tempArray : '';
}

/**
Expand Down
235 changes: 235 additions & 0 deletions tests/Guild/Integration/LeadReceiverParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,234 @@ public function testExtraLeadParser(): void
$this->assertEquals('1', $leadStructure['custom_fields']['CRE_Estimated_1st_Mortgage']);
}

public function testExtraLeaSpacingParser(): void
{
$leadTemplate = '
{
"First Name": {
"name": "firstname",
"type": "string"
},
"Last Name": {
"name": "lastname",
"type": "string"
},
"Phone": {
"name": "phone",
"type": "string"
},
"Email": {
"name": "email",
"type": "string"
},
"Company": {
"name": "Company",
"type": "customField"
},
"City": {
"name": "city",
"type": "customField"
},
"State": {
"name": "state",
"type": "customField"
},
"Zip Code": {
"name": "zip",
"type": "customField"
},
"Type of Incorporation": {
"name": "type_of_incorporation",
"type": "customField"
},
"Industry": {
"name": "industry",
"type": "customField"
},
"Business Founded": {
"name": "business_founded",
"type": "customField"
},
"SubID": {
"name": "sub_id",
"type": "customField"
},
"Credit Score": {
"name": "Credit_Score",
"type": "customField"
},
"Amount Requested": {
"name": "amount_requested",
"type": "customField"
},
"Annual Revenue": {
"name": "annual_revenue",
"type": "decimal"
}
}';

$name = fake()->name;
$phone = fake()->phoneNumber;
$email = fake()->email;
$lastname = fake()->lastName;
$url = fake()->url;

$leadReceived = json_encode([
'First Name' => $name,
'Last Name' => $lastname,
'Phone' => $phone,
'Email' => $email,
'Company' => 'TEST, LLC',
'City' => 'aa BB',
'State' => 'PA',
'Zip Code' => '19053',
'Type of Incorporation' => 'soleProprietorship',
'Industry' => 'real_estate',
'Business Founded' => '2004-05-01T00:00:00',
'SubID' => '272da453-ed2c-4fa7-9ec0-c3efc6f55c87;cf3e6255ba55da60765e9d108',
'Credit Score' => 'Excellent (720+)',
'Amount Requested' => '1150000',
'Annual Revenue' => '70000',
]);

$parseTemplate = new ConvertJsonTemplateToLeadStructureAction(
json_decode($leadTemplate, true),
json_decode($leadReceived, true)
);

$leadStructure = $parseTemplate->execute();

$this->assertIsArray($leadStructure);
$this->assertArrayHasKey('custom_fields', $leadStructure);
$this->assertArrayHasKey('people', $leadStructure);
$this->assertArrayHasKey('firstname', $leadStructure['people']);
$this->assertArrayHasKey('lastname', $leadStructure['people']);
$this->assertArrayHasKey('contacts', $leadStructure['people']);
$this->assertEquals($name, $leadStructure['people']['firstname']);
$this->assertEquals($lastname, $leadStructure['people']['lastname']);
$this->assertEquals($phone, $leadStructure['people']['contacts'][0]['value']);
$this->assertEquals($email, $leadStructure['people']['contacts'][1]['value']);
$this->assertEquals('Excellent (720+)', $leadStructure['custom_fields']['Credit_Score']);
$this->assertEquals('1150000', $leadStructure['custom_fields']['Amount Requested']);
}

public function testExtraLeaDefaultValueParser(): void
{
$leadTemplate = '
{
"First Name": {
"name": "firstname",
"type": "string"
},
"Last Name": {
"name": "lastname",
"type": "string"
},
"Phone": {
"name": "phone",
"type": "string"
},
"Member": {
"name": "member",
"type": "customField",
"default": "lpr2230"
},
"Email": {
"name": "email",
"type": "string"
},
"Company": {
"name": "Company",
"type": "customField"
},
"City": {
"name": "city",
"type": "customField"
},
"State": {
"name": "state",
"type": "customField"
},
"Zip Code": {
"name": "zip",
"type": "customField"
},
"Type of Incorporation": {
"name": "type_of_incorporation",
"type": "customField"
},
"Industry": {
"name": "industry",
"type": "customField"
},
"Business Founded": {
"name": "business_founded",
"type": "customField"
},
"SubID": {
"name": "sub_id",
"type": "customField"
},
"Credit Score": {
"name": "Credit_Score",
"type": "customField"
},
"Amount Requested": {
"name": "amount_requested",
"type": "customField"
},
"Annual Revenue": {
"name": "annual_revenue",
"type": "decimal"
}
}';

$name = fake()->name;
$phone = fake()->phoneNumber;
$email = fake()->email;
$lastname = fake()->lastName;
$url = fake()->url;

$leadReceived = json_encode([
'First Name' => $name,
'Last Name' => $lastname,
'Phone' => $phone,
'Email' => $email,
'Company' => 'TEST, LLC',
'City' => 'aa BB',
'State' => 'PA',
'Zip Code' => '19053',
'Type of Incorporation' => 'soleProprietorship',
'Industry' => 'real_estate',
'Business Founded' => '2004-05-01T00:00:00',
'SubID' => '272da453-ed2c-4fa7-9ec0-c3efc6f55c87;cf3e6255ba55da60765e9d108',
'Credit Score' => 'Excellent (720+)',
'Amount Requested' => '1150000',
'Annual Revenue' => '70000',
]);

$parseTemplate = new ConvertJsonTemplateToLeadStructureAction(
json_decode($leadTemplate, true),
json_decode($leadReceived, true)
);

$leadStructure = $parseTemplate->execute();

$this->assertIsArray($leadStructure);
$this->assertArrayHasKey('custom_fields', $leadStructure);
$this->assertArrayHasKey('people', $leadStructure);
$this->assertArrayHasKey('firstname', $leadStructure['people']);
$this->assertArrayHasKey('lastname', $leadStructure['people']);
$this->assertArrayHasKey('contacts', $leadStructure['people']);
$this->assertEquals($name, $leadStructure['people']['firstname']);
$this->assertEquals($lastname, $leadStructure['people']['lastname']);
$this->assertEquals($phone, $leadStructure['people']['contacts'][0]['value']);
$this->assertEquals($email, $leadStructure['people']['contacts'][1]['value']);
$this->assertEquals('Excellent (720+)', $leadStructure['custom_fields']['Credit_Score']);
$this->assertEquals('1150000', $leadStructure['custom_fields']['Amount Requested']);
$this->assertEquals('lpr2230', $leadStructure['custom_fields']['member']);
}

public function testComplexLearParser(): void
{
$leadTemplate = '
Expand All @@ -167,6 +395,11 @@ public function testComplexLearParser(): void
"name": "business_founded",
"type": "customField"
},
"Member": {
"name": "member",
"type": "customField",
"default": "lpr2230"
},
"business.use_of_proceeds": {
"name": "industry",
"type": "customField"
Expand Down Expand Up @@ -309,5 +542,7 @@ public function testComplexLearParser(): void
$this->assertArrayHasKey('firstname', $leadStructure['people']);
$this->assertArrayHasKey('lastname', $leadStructure['people']);
$this->assertArrayHasKey('contacts', $leadStructure['people']);
$this->assertArrayHasKey('member', $leadStructure['custom_fields']);
$this->assertEquals('lpr2230', $leadStructure['custom_fields']['member']);
}
}

0 comments on commit 05ab5f6

Please sign in to comment.