fix : 테넌트 없는 회원 처리 및 테넌트 등록시 해당 테넌트 디폴트값 설정

This commit is contained in:
2025-08-14 20:19:51 +09:00
parent e9d1e42359
commit 9935bba84e
4 changed files with 37 additions and 21 deletions

View File

@@ -7,6 +7,7 @@
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Hash;
use App\Models\Members\User;
use App\Models\Tenants\Tenant;
use App\Models\Members\UserTenant;
class MemberService
@@ -49,16 +50,14 @@ public static function getMember(int $userNo)
public static function getMyInfo()
{
$apiUser = app('api_user');
$user = User::find($apiUser);
$data['user'] = $user;
$user = User::with([
'userTenant.tenant' => function($q) {
$q->select('id', 'company_name', 'code', 'email', 'phone', 'address', 'business_num', 'corp_reg_no', 'ceo_name', 'homepage', 'fax', 'logo', 'admin_memo', 'options',); // 원하는 컬럼만
}
])->find($apiUser);
$data=[
'user' => $user->userTenant->user,
'tenant' => $user->userTenant->tenant,
];
$tenantId = app('tenant_id');
if($tenantId){
$tenant = Tenant::find($tenantId);
$data['tenant'] = $tenant;
}
return ApiResponse::response('result', $data);
}
@@ -168,12 +167,14 @@ public static function switchMyTenant(int $tenantId)
$apiUser = app('api_user');
// 1) 현재 유저의 기본 테넌트를 모두 해제
UserTenant::where('user_id', $apiUser)
UserTenant::withoutGlobalScopes()
->where('user_id', $apiUser)
->where('is_default', 1)
->update(['is_default' => 0]);
// 2) 지정한 tenant_id를 기본 테넌트로 설정
$updated = UserTenant::where('user_id', $apiUser)
$updated = UserTenant::withoutGlobalScopes()
->where('user_id', $apiUser)
->where('tenant_id', $tenantId)
->update(['is_default' => 1]);

View File

@@ -257,6 +257,16 @@ public static function storeTenants(array $params = [])
$tenant = Tenant::create($payload);
// 성성된 테넌트를 나의 테넌트로 셋팅
$apiUser = app('api_user');
UserTenant::create([
'user_id' => $apiUser,
'tenant_id' => $tenant->id,
'is_active' => 0,
'is_default' => 1,
'joined_at' => now(),
]);
// 생성된 리소스를 그대로 반환 (목록 카드용 요약 원하면 컬럼 제한)
return ApiResponse::response('result', $tenant);
}