Files
2026-09-09 16:56:22 +07:00

58 lines
1.9 KiB
PHP

<?php
namespace App\Integrations\Domain\Support;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
/**
* Resolves a source (tgs_domain_prod) `users.id` to a local `customers.id`, matched by email.
* Creates the customer if no match exists — customers.username/type/email_verified_at were
* added specifically to carry this data (see migration add_admin_zonedns_fields_to_customers_table).
*/
class DomainUserResolver
{
/** @var array<int, ?int> source user id => customer id */
private static array $cache = [];
public function resolve(int $sourceUserId): ?int
{
if (array_key_exists($sourceUserId, self::$cache)) {
return self::$cache[$sourceUserId];
}
$user = DB::connection('domain')->table('users')->where('id', $sourceUserId)->first();
if (! $user) {
return self::$cache[$sourceUserId] = null;
}
$email = strtolower($user->email);
$customer = DB::table('customers')->where('email', $email)->first();
if ($customer) {
DB::table('customers')->where('id', $customer->id)->update([
'username' => $customer->username ?? $user->username,
'type' => $user->type,
]);
return self::$cache[$sourceUserId] = $customer->id;
}
$customerId = DB::table('customers')->insertGetId([
'uuid' => (string) Str::uuid(),
'fullname' => $user->name,
'username' => $user->username,
'email' => $email,
'email_verified_at' => $user->email_verified_at,
// source hashes are Laravel bcrypt (self-contained, no APP_KEY needed), safe to copy as-is
'password' => $user->password,
'type' => $user->type,
'status' => 1,
'created_at' => $user->created_at,
'updated_at' => $user->updated_at,
]);
return self::$cache[$sourceUserId] = $customerId;
}
}