-
-
Notifications
You must be signed in to change notification settings - Fork 106
Expand file tree
/
Copy pathUpsertUserFromAnystackContact.php
More file actions
75 lines (58 loc) · 2.51 KB
/
Copy pathUpsertUserFromAnystackContact.php
File metadata and controls
75 lines (58 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
namespace App\Jobs;
use App\Models\User;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;
class UpsertUserFromAnystackContact implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public int $maxExceptions = 1;
public function __construct(
public array $contactData
) {}
public function handle(): void
{
$users = $this->matchingUsers();
if ($users->count() > 1) {
$userIds = $users->pluck('id')->implode(', ');
throw new Exception("Multiple users [$userIds] found for contact by id [{$this->contactData['id']}] or email [{$this->contactData['email']}]");
}
$user = $users->first() ?? new User;
$this->assertUserAttributesAreValid($user);
Log::debug(($user->exists ? "Updating user [{$user->id}]" : 'Creating user')." from anystack contact [{$this->contactData['id']}].");
$user->anystack_contact_id ??= $this->contactData['id'];
$user->email ??= $this->contactData['email'];
$user->name ??= $this->contactData['full_name'];
$user->created_at ??= $this->contactData['created_at'];
$user->updated_at ??= $this->contactData['updated_at'];
$user->password ??= Hash::make(Str::random(72));
$user->save();
}
protected function matchingUsers(): Collection
{
return User::query()
->where('email', $this->contactData['email'])
->orWhere('anystack_contact_id', $this->contactData['id'])
->get();
}
protected function assertUserAttributesAreValid(User $user): void
{
if (! $user->exists) {
return;
}
if (filled($user->anystack_contact_id) && $user->anystack_contact_id !== $this->contactData['id']) {
throw new Exception("User [{$user->id}] already exists but the user's anystack_contact_id [{$user->anystack_contact_id}] does not match the id [{$this->contactData['id']}].");
}
if ($user->email !== $this->contactData['email']) {
throw new Exception("User [{$user->id}] already exists but the user's email [{$user->email}] does not match the email [{$this->contactData['email']}].");
}
}
}