Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #215 predict email #216

Merged
merged 2 commits into from
Aug 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions app/Console/Commands/ContactEmailPredictCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

declare(strict_types=1);

namespace App\Console\Commands;

use App\Helpers\PredictEmail;
use App\Models\Contact;
use Illuminate\Console\Command;

class ContactEmailPredictCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'crm:contact:email-predict';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Contact email completition';

/**
* Execute the console command.
*/
public function handle()
{
// Counter for contacts modified
$contactsModified = 0;

// Get all contacts where the company has a URL and the contact's email is empty
$contacts = Contact::with('lead')->whereHas('lead', function ($query) {
$query->whereNotNull('website'); // Assuming the company's URL is stored in the 'url' column
})
->whereNull('email')
->get();

$predictEmail = new PredictEmail();

foreach ($contacts as $contact) {

$predictedEmail = $predictEmail->predict(
$contact->first_name,
(string) $contact->last_name,
$contact->lead->website // Assuming the company's URL is stored in the 'url' column
);

if ($predictedEmail) {
$contact->email = $predictedEmail;
$contact->updated_at = now();
$contact->save();
$contactsModified++;
}
}

// Display the counter
$this->info("Number of contacts modified: $contactsModified");

return Command::SUCCESS;
}
}
1 change: 1 addition & 0 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Kernel extends ConsoleKernel
Commands\ScheduleNotificationReminder::class,
Commands\CrmDevCheck::class,
Commands\EmailValidatorCommand::class,
Commands\ContactEmailPredictCommand::class,
];

/**
Expand Down
71 changes: 71 additions & 0 deletions app/Helpers/PredictEmail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace App\Helpers;

use Illuminate\Support\Facades\Validator;

class PredictEmail
{
public function predict(string $name, string $last_name, string $company_url): string|false
{
$name = str_replace(' ', '', trim(strtolower($name)));
$last_name = str_replace(' ', '', trim(strtolower($last_name)));
$email = false;
$host = str_replace(['https://', 'http://', 'www.'], '', $company_url);

// online
if ($socket = @fsockopen($host, 80, $errno, $errstr, 30)) {
$cases = [
'fullNameWithDots',
'firstLetterNameLastName',
'nameOnly',
'firstLettersOfNamesAndLastName',
];

foreach ($cases as $case) {
$email = $this->$case($name, $last_name, $host);
if ($this->isValid($email)) {
return $email;
}
}
}

return $email;
}

public function fullNameWithDots(string $name, string $last_name, string $host): string
{
return $name.'.'.$last_name.'@'.$host;
}

public function firstLetterNameLastName(string $name, string $last_name, string $host): string
{
return substr($name, 0, 1).$last_name.'@'.$host;
}

public function nameOnly(string $name, string $host): string
{
return substr($name, 0, 1).'@'.$host;
}

public function firstLettersOfNamesAndLastName(string $name, string $last_name, string $host): string
{
$firstLetters = '';
$names = explode(' ', $name);
foreach ($names as $n) {
$firstLetters .= substr($n, 0, 1);
}

return $firstLetters.$last_name.'@'.$host;
}

public function isValid(string $email): bool
{
$rules = ['email' => 'email:rfc,dns'];
$validator = Validator::make(['email' => $email], $rules);

return $validator->passes();
}
}
80 changes: 80 additions & 0 deletions tests/Unit/Helpers/PredictEmailTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace Tests\Unit\Helpers;

use App\Helpers\PredictEmail;
use Illuminate\Support\Facades\Validator;
use Tests\TestCase;

class PredictEmailTest extends TestCase
{
/** @test */
public function predict_valid_email()
{
$predictEmail = new PredictEmail();

// Mocking the connection to the host
$this->partialMock(PredictEmail::class, function ($mock) {
$mock->shouldReceive('fsockopen')->andReturn(true);
});

$email = $predictEmail->predict('John', 'Doe', 'https://www.example.com');
$this->assertEquals('john.doe@example.com', $email);
}

/** @test */
public function full_name_with_dots()
{
$predictEmail = new PredictEmail();
$email = $predictEmail->fullNameWithDots('John', 'Doe', 'example.com');
$this->assertEquals('john.doe@example.com', $email);
}

/** @test */
public function first_letter_name_last_name()
{
$predictEmail = new PredictEmail();
$email = $predictEmail->firstLetterNameLastName('John', 'Doe', 'example.com');
$this->assertEquals('jdoe@example.com', $email);
}

/** @test */
public function name_only()
{
$predictEmail = new PredictEmail();
$email = $predictEmail->nameOnly('John', 'example.com');
$this->assertEquals('j@example.com', $email);
}

/** @test */
public function first_letters_of_names_and_last_name()
{
$predictEmail = new PredictEmail();

// Testing with multiple parts in the name and last name
$email = $predictEmail->firstLettersOfNamesAndLastName('Juan Manuel', 'Perez Rodriguez', 'example.com');
$this->assertEquals('jmperezrodriguez@example.com', $email);

// Testing with a single name and last name
$email = $predictEmail->firstLettersOfNamesAndLastName('John', 'Doe', 'example.com');
$this->assertEquals('jdoe@example.com', $email);
}

/** @test */
public function is_valid()
{
$predictEmail = new PredictEmail();

// Mocking Laravel's Validator
Validator::shouldReceive('make')
->once()
->andReturnSelf()
->shouldReceive('passes')
->andReturn(true);

$isValid = $predictEmail->isValid('john.doe@example.com');
$this->assertTrue($isValid);
}
}