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