fix
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\User;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
|
||||
class SyncSurveyUsers extends Command
|
||||
{
|
||||
protected $signature = 'survey:sync-users {--token= : Approagency admin token}';
|
||||
protected $description = 'Sync user profile fields (name, age, gender) from approagency for all meditation users with missing data';
|
||||
|
||||
public function handle()
|
||||
{
|
||||
$token = $this->option('token') ?: config('services.approagency.token');
|
||||
|
||||
if (empty($token)) {
|
||||
$this->error('Provide --token=<approagency_admin_token> or set APPROAGENCY_ADMIN_TOKEN in .env');
|
||||
return 1;
|
||||
}
|
||||
|
||||
$users = User::whereNull('first_name')
|
||||
->whereNotNull('mobile')
|
||||
->get();
|
||||
|
||||
$this->info("Found {$users->count()} users with missing first_name. Syncing...");
|
||||
|
||||
$synced = 0;
|
||||
$failed = 0;
|
||||
|
||||
foreach ($users as $user) {
|
||||
try {
|
||||
$response = Http::withToken($token)
|
||||
->get('https://api.approagency.ir/api/admin/users', [
|
||||
'mobile' => $user->mobile,
|
||||
'per_page' => 1,
|
||||
]);
|
||||
|
||||
if ($response->successful()) {
|
||||
$data = $response->json('data.0');
|
||||
if ($data) {
|
||||
$user->update([
|
||||
'first_name' => $data['first_name'] ?? $user->first_name,
|
||||
'last_name' => $data['last_name'] ?? $user->last_name,
|
||||
'age' => $data['age'] ?? $user->age,
|
||||
'gender' => $data['gender'] ?? $user->gender,
|
||||
'birthday' => $data['birthday'] ?? $user->birthday,
|
||||
'identifier' => $data['uuid'] ?? $user->identifier,
|
||||
]);
|
||||
$this->line(" synced: {$user->mobile} → {$data['first_name'] ?? 'null'}");
|
||||
$synced++;
|
||||
}
|
||||
} else {
|
||||
$this->warn(" skip: {$user->mobile} (HTTP {$response->status()})");
|
||||
$failed++;
|
||||
}
|
||||
} catch (\Exception $e) {
|
||||
$this->error(" failed: {$user->mobile} — {$e->getMessage()}");
|
||||
$failed++;
|
||||
}
|
||||
|
||||
usleep(200000); // 200ms delay to avoid rate limits
|
||||
}
|
||||
|
||||
$this->info("Done. Synced: {$synced}, Failed: {$failed}");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user