Skip to content

Commit c05286c

Browse files
authored
Merge pull request #36 from tipoff/chx2/feature/31-attach-user-command
Add AttachContactUsers command #31
2 parents 2a442ea + bc43698 commit c05286c

File tree

3 files changed

+92
-0
lines changed

3 files changed

+92
-0
lines changed

src/Commands/AttachContactUsers.php

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tipoff\Forms\Commands;
6+
7+
use Illuminate\Console\Command;
8+
use Tipoff\Forms\Models\Contact;
9+
use Tipoff\TestSupport\Models\User;
10+
11+
class AttachContactUsers extends Command
12+
{
13+
/**
14+
* The name and signature of the console command.
15+
*
16+
* @var string
17+
*/
18+
protected $signature = 'attach:users';
19+
20+
/**
21+
* The console command description.
22+
*
23+
* @var string
24+
*/
25+
protected $description = 'Attach contacts to users with matching emails that have not yet been linked.';
26+
27+
/**
28+
* Create a new command instance.
29+
*
30+
* @return void
31+
*/
32+
public function __construct()
33+
{
34+
parent::__construct();
35+
}
36+
37+
/**
38+
* Execute the console command.
39+
*
40+
* @return void
41+
*/
42+
public function handle()
43+
{
44+
$contacts = Contact::whereNull('user_id')->get();
45+
foreach ($contacts as $contact) {
46+
$user = User::where('email', '=', $contact->email)->first();
47+
if ($user) {
48+
$contact->user_id = $user->id;
49+
$contact->save();
50+
}
51+
}
52+
}
53+
}

src/FormsServiceProvider.php

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Tipoff\Forms;
66

7+
use Tipoff\Forms\Commands\AttachContactUsers;
78
use Tipoff\Forms\Models\Contact;
89
use Tipoff\Forms\Policies\ContactPolicy;
910
use Tipoff\Support\TipoffPackage;
@@ -14,6 +15,9 @@ class FormsServiceProvider extends TipoffServiceProvider
1415
public function configureTipoffPackage(TipoffPackage $package): void
1516
{
1617
$package
18+
->hasCommands([
19+
AttachContactUsers::class,
20+
])
1721
->hasPolicies([
1822
Contact::class => ContactPolicy::class,
1923
])
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tipoff\Forms\Tests\Unit\Commands;
6+
7+
use Illuminate\Foundation\Testing\DatabaseTransactions;
8+
use Tipoff\Forms\Models\Contact;
9+
use Tipoff\Forms\Tests\TestCase;
10+
11+
class AttachContactUsersCommandTest extends TestCase
12+
{
13+
use DatabaseTransactions;
14+
15+
/** @test */
16+
public function no_users_found()
17+
{
18+
$this->artisan('attach:users');
19+
$this->assertDatabaseCount('contacts', 0);
20+
}
21+
22+
/** @test */
23+
public function user_found()
24+
{
25+
//Create a user and contact with same email, but not attached
26+
$user = randomOrCreate(app('user'));
27+
Contact::factory()->create([
28+
'email' => $user->email,
29+
]);
30+
31+
$this->artisan('attach:users');
32+
$contact = Contact::where('email', '=', $user->email)->first();
33+
$this->assertNotNull($contact->user);
34+
}
35+
}

0 commit comments

Comments
 (0)