From 700ae753419dfd3e7c1f95de083817fc4061ced5 Mon Sep 17 00:00:00 2001 From: kaioken Date: Mon, 23 Dec 2024 12:41:02 -0400 Subject: [PATCH] htofix: parser --- ...nvertJsonTemplateToLeadStructureAction.php | 35 ++- .../Integration/LeadReceiverParserTest.php | 235 ++++++++++++++++++ 2 files changed, 249 insertions(+), 21 deletions(-) diff --git a/src/Domains/Guild/Leads/Actions/ConvertJsonTemplateToLeadStructureAction.php b/src/Domains/Guild/Leads/Actions/ConvertJsonTemplateToLeadStructureAction.php index 73ee9ab2a..28aa9416d 100644 --- a/src/Domains/Guild/Leads/Actions/ConvertJsonTemplateToLeadStructureAction.php +++ b/src/Domains/Guild/Leads/Actions/ConvertJsonTemplateToLeadStructureAction.php @@ -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']; @@ -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 : ''; } /** diff --git a/tests/Guild/Integration/LeadReceiverParserTest.php b/tests/Guild/Integration/LeadReceiverParserTest.php index 53a02bef9..fe022d5bb 100644 --- a/tests/Guild/Integration/LeadReceiverParserTest.php +++ b/tests/Guild/Integration/LeadReceiverParserTest.php @@ -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 = ' @@ -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" @@ -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']); } }