|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Console\Commands\Synchronize; |
|
4
|
|
|
|
|
5
|
|
|
use App\Models\Person; |
|
6
|
|
|
use App\Models\Tenant; |
|
7
|
|
|
use App\Models\TenantPerson; |
|
8
|
|
|
use Illuminate\Console\Command; |
|
9
|
|
|
|
|
10
|
|
|
class CopyPeopleFromTenancyToGenealogy extends Command |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* The name and signature of the console command. |
|
14
|
|
|
* |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $signature = 'synchronize:people'; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* The console command description. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $description = 'Copy people records from tenancy databases into main genealogy database'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Execute the console command. |
|
28
|
|
|
* |
|
29
|
|
|
* @return int |
|
30
|
|
|
*/ |
|
31
|
|
|
public function handle() |
|
32
|
|
|
{ |
|
33
|
|
|
$recordsSince = TenantPerson::orderBy('updated_at', 'desc')->first()?->updated_at; |
|
|
|
|
|
|
34
|
|
|
$this->info('Tenant people records syncronization since: '.($recordsSince ?: 'start')); |
|
35
|
|
|
|
|
36
|
|
|
tenancy()->query()->cursor()->each(function (Tenant $tenant) use ($recordsSince) { |
|
37
|
|
|
try { |
|
38
|
|
|
tenancy()->initialize($tenant); |
|
39
|
|
|
|
|
40
|
|
|
$tenantPersonQuery = Person::query(); |
|
41
|
|
|
|
|
42
|
|
|
if ($recordsSince) { |
|
43
|
|
|
$tenantPersonQuery->where('updated_at', '>=', $recordsSince); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$this->info('Processing tenant #'.$tenant->id.' records: '.(clone $tenantPersonQuery)->count()); |
|
|
|
|
|
|
47
|
|
|
$tenantPersonQuery->chunk(100, function ($people) use ($tenant) { |
|
48
|
|
|
// clear old records and push updated ones |
|
49
|
|
|
TenantPerson::where('tenant_id', $tenant->id) |
|
50
|
|
|
->whereIn('tenant_person_id', $people->pluck('id')) |
|
51
|
|
|
->delete(); |
|
52
|
|
|
|
|
53
|
|
|
foreach ($people as $person) { |
|
54
|
|
|
$personData = $person->toArray(); |
|
55
|
|
|
unset($personData['id']); |
|
56
|
|
|
$personData['tenant_id'] = $tenant->id; |
|
57
|
|
|
$personData['tenant_person_id'] = $person->id; |
|
58
|
|
|
|
|
59
|
|
|
(new TenantPerson($personData))->save(); |
|
60
|
|
|
} |
|
61
|
|
|
}); |
|
62
|
|
|
|
|
63
|
|
|
tenancy()->end(); |
|
64
|
|
|
} catch (\Exception) { |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
}); |
|
67
|
|
|
|
|
68
|
|
|
return Command::SUCCESS; |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.