Skip to content

Commit

Permalink
refact: allow all none process fields to custom fields
Browse files Browse the repository at this point in the history
  • Loading branch information
kaioken committed Jul 9, 2024
1 parent edc98d4 commit a38d8d3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public function parseLead(array $request, array $template): array
{
$parsedData = [];
$customFields = [];
$processFields = [];

// Initialize people structure with placeholders
$peopleStructure = [
Expand All @@ -106,6 +107,19 @@ public function parseLead(array $request, array $template): array
'function' => $this->mapFunctionType($parsedData, $request, $info, $name),
default => null
};

$processFields[$name] = $value;
}

if (! empty($request['company']) || ! empty($request['organization'])) {
$parsedData['organization'] = $request['company'] ?? $request['organization'];
}

// Add remaining unprocessed fields to custom fields
foreach ($request as $key => $value) {
if (! array_key_exists($key, $processFields) && ! array_key_exists($key, $customFields)) {
$customFields[$key] = $value;
}
}

$parsedData['custom_fields'] = $customFields;
Expand Down
71 changes: 71 additions & 0 deletions tests/Guild/Integration/LeadReceiverParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,77 @@ public function testSimpleLeadParser(): void
$this->assertEquals('lpr2230', $leadStructure['custom_fields']['member']);
}

public function testExtraLeadParser(): void
{
$leadTemplate = '
{
"Member": {
"name": "member",
"type": "customField"
},
"firstname": {
"name": "firstname",
"type": "string"
},
"lastname": {
"name": "lastname",
"type": "string"
},
"phone": {
"name": "phone",
"type": "string"
},
"email": {
"name": "email",
"type": "string"
}
}';

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

$leadReceived = json_encode([
'firstname' => $name,
'lastname' => $lastname,
'phone' => $phone,
'email' => $email,
'Member' => 'lpr2230',
'URL' => $url,
'credit_score' => 'Poor',
'SMS_Opt_Out' => '1',
'CRE_Estimated_Property_Value' => '1',
'CRE_Estimated_1st_Mortgage' => '1',
'CRE_Loan_Purpose' => '1',
'CRE_Amount_of_Loan_Request' => '1',
'amount_requested' => '1',
'business_name' => '1',
'compay' => '1',
]);

$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('lpr2230', $leadStructure['custom_fields']['member']);
$this->assertEquals('1', $leadStructure['custom_fields']['CRE_Estimated_1st_Mortgage']);
}

public function testComplexLearParser(): void
{
$leadTemplate = '
Expand Down

0 comments on commit a38d8d3

Please sign in to comment.