Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 42 |
|
0.00% |
0 / 10 |
CRAP | |
0.00% |
0 / 1 |
| ProcessUserRoleAsyncJob | |
0.00% |
0 / 42 |
|
0.00% |
0 / 10 |
272 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| handle | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
| backoff | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| created | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| updated | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
| deleted | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| updateUserInfo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| validateUpdatedFields | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| parseFields | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| parseUserType | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Jobs; |
| 4 | |
| 5 | use Illuminate\Bus\Queueable; |
| 6 | use Illuminate\Contracts\Queue\ShouldQueue; |
| 7 | use Illuminate\Foundation\Bus\Dispatchable; |
| 8 | use Illuminate\Queue\InteractsWithQueue; |
| 9 | use Illuminate\Queue\SerializesModels; |
| 10 | use App\Http\Models\Auth\UserRole; |
| 11 | use App\Http\Models\UserInfo; |
| 12 | use App\Http\Models\Auth\Role; |
| 13 | |
| 14 | class ProcessUserRoleAsyncJob implements ShouldQueue |
| 15 | { |
| 16 | use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; |
| 17 | |
| 18 | public $tries = 5; |
| 19 | |
| 20 | public function __construct( |
| 21 | public UserRole $userRole, |
| 22 | public string $action |
| 23 | ) {} |
| 24 | |
| 25 | public function handle(): void |
| 26 | { |
| 27 | if ($this->action === 'created') { |
| 28 | $this->created($this->userRole); |
| 29 | } elseif ($this->action === 'updated') { |
| 30 | $this->updated($this->userRole); |
| 31 | } elseif ($this->action === 'deleted') { |
| 32 | $this->deleted($this->userRole); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | public function backoff() |
| 37 | { |
| 38 | return [10, 30, 60, 120, 300]; |
| 39 | } |
| 40 | |
| 41 | private function created(UserRole $userRole): void |
| 42 | { |
| 43 | $parsedFields = $this->parseFields($userRole); |
| 44 | |
| 45 | $this->updateUserInfo($parsedFields, $userRole); |
| 46 | } |
| 47 | |
| 48 | private function updated(UserRole $userRole): void |
| 49 | { |
| 50 | $updated = $userRole->getDirty(); |
| 51 | |
| 52 | if (!empty($updated)) { |
| 53 | $fields = $this->validateUpdatedFields($updated); |
| 54 | |
| 55 | if (!empty($fields)) { |
| 56 | $parsedFields = $this->parseFields($userRole); |
| 57 | |
| 58 | $this->updateUserInfo($parsedFields, $userRole); |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | private function deleted(UserRole $userRole): void |
| 64 | { |
| 65 | $this->updateUserInfo([], $userRole); |
| 66 | } |
| 67 | |
| 68 | private function updateUserInfo(array $data, UserRole $userRole) |
| 69 | { |
| 70 | UserInfo::where('user_id', $userRole->user_id)->update($data); |
| 71 | } |
| 72 | |
| 73 | private function validateUpdatedFields(array $updated): array |
| 74 | { |
| 75 | $hubspotFields = [ |
| 76 | 'user_id', |
| 77 | 'role_id', |
| 78 | ]; |
| 79 | |
| 80 | return array_filter($updated, fn($key) => in_array($key, $hubspotFields), ARRAY_FILTER_USE_KEY); |
| 81 | } |
| 82 | |
| 83 | private function parseFields(UserRole $userRole): array |
| 84 | { |
| 85 | $roles = UserRole::where('user_id', $userRole->user_id)->get(); |
| 86 | $roleNames = $roles->pluck('role_id')->toArray(); |
| 87 | $roleIds = $roles->pluck('role_id')->toArray(); |
| 88 | $role = implode(',', $roleNames); |
| 89 | |
| 90 | return [ |
| 91 | 'user_type' => $this->parseUserType($role), |
| 92 | 'role_ids' => $roleIds, |
| 93 | 'role_names' => $roleNames, |
| 94 | ]; |
| 95 | } |
| 96 | |
| 97 | private function parseUserType(string $roles) |
| 98 | { |
| 99 | if (empty($roles)) { |
| 100 | return Role::USER; |
| 101 | } |
| 102 | |
| 103 | $userRoles = explode(',', $roles); |
| 104 | |
| 105 | return implode(",", array_map(function ($role) { |
| 106 | return match ($role) { |
| 107 | 'Reporting Admin' => 'Reporting POC', |
| 108 | 'Group Admin' => 'Group Admin Manager', |
| 109 | default => $role, |
| 110 | }; |
| 111 | }, $userRoles)); |
| 112 | } |
| 113 | } |