2025-07-17 10:05:47 +09:00
< ? php
namespace App\Services ;
use App\Helpers\ApiResponse ;
use Illuminate\Support\Facades\DB ;
2025-08-14 00:55:08 +09:00
use Illuminate\Support\Facades\Log ;
use Illuminate\Support\Facades\Hash ;
use App\Models\Members\User ;
use App\Models\Members\UserTenant ;
2025-07-17 10:05:47 +09:00
class MemberService
{
/**
* 회원 조회 ( 리스트 )
*/
2025-07-26 14:34:48 +09:00
public static function getMembers ( $request )
2025-07-17 10:05:47 +09:00
{
2025-07-26 14:34:48 +09:00
$pageNo = $request -> page ? ? 1 ;
$pageSize = $request -> size ? ? 10 ;
$query = User :: whereHas ( 'userTenants' , function ( $q ) {
$q -> active ();
}) -> debug ();
$query = $query -> paginate ( $pageSize , [ '*' ], 'page' , $pageNo );
return ApiResponse :: response ( 'result' , $query );
2025-07-17 10:05:47 +09:00
}
/**
* 단일 회원 조회
*/
2025-07-26 16:02:29 +09:00
public static function getMember ( int $userNo )
2025-07-17 10:05:47 +09:00
{
2025-07-26 14:23:13 +09:00
$query = User :: whereHas ( 'userTenants' , function ( $q ) {
$q -> active ();
}) -> where ( 'id' , $userNo );
return ApiResponse :: response ( 'first' , $query );
2025-07-17 10:05:47 +09:00
}
2025-07-22 16:45:11 +09:00
/**
* 내정보 확인
*/
2025-07-26 16:02:29 +09:00
public static function getMyInfo ()
2025-07-22 16:45:11 +09:00
{
2025-07-28 10:28:56 +09:00
$debug = ( app () -> environment ( 'local' )) ? true : false ;
2025-07-28 01:16:59 +09:00
if ( $debug ) DB :: enableQueryLog (); // 쿼리 추적
2025-07-22 16:45:11 +09:00
2025-07-26 14:23:13 +09:00
$apiUser = app ( 'api_user' );
2025-07-26 16:02:29 +09:00
$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 );
2025-07-22 16:45:11 +09:00
$data = [
2025-07-26 16:02:29 +09:00
'user' => $user -> userTenant -> user ,
'tenant' => $user -> userTenant -> tenant ,
2025-07-22 16:45:11 +09:00
];
2025-07-26 16:02:29 +09:00
return ApiResponse :: response ( 'result' , $data );
2025-07-22 16:45:11 +09:00
}
2025-07-17 10:05:47 +09:00
/**
2025-08-14 00:55:08 +09:00
* 내정보 수정
2025-07-17 10:05:47 +09:00
*/
2025-08-14 00:55:08 +09:00
public static function getMyUpdate ( $request )
2025-07-17 10:05:47 +09:00
{
2025-08-14 00:55:08 +09:00
$debug = app () -> environment ( 'local' );
if ( $debug ) DB :: enableQueryLog ();
2025-07-17 10:05:47 +09:00
2025-08-14 00:55:08 +09:00
$apiUser = app ( 'api_user' );
// 요청으로 받은 수정 데이터 유효성 검사
$validatedData = $request -> validate ([
'name' => 'sometimes|string|max:255' ,
'phone' => 'sometimes|string|max:20' ,
'email' => 'sometimes|email|max:100' ,
'options' => 'nullable|json' ,
'profile_photo_path' => 'nullable|string|max:255' ,
]);
$user = User :: find ( $apiUser );
if ( ! $user ) {
return ApiResponse :: error ( 'User not found.' , 404 );
2025-07-17 10:05:47 +09:00
}
2025-08-14 00:55:08 +09:00
// 사용자 정보 업데이트
$user -> update ( $validatedData );
// 수정 성공 시 success 반환
return ApiResponse :: response ( 'success' );
}
/**
* 내 비밀번호 수정
*/
public static function setMyPassword ( $request )
{
$debug = app () -> environment ( 'local' );
if ( $debug ) DB :: enableQueryLog ();
$apiUserId = app ( 'api_user' ); // 현재 로그인한 사용자 PK
// 유효성 검사 (확인 비밀번호는 선택)
$validated = $request -> validate ([
'current_password' => 'required|string' ,
'new_password' => 'required|string|min:8|max:64' ,
]);
// 선택적으로 확인 비밀번호가 온 경우 체크
if ( $request -> filled ( 'new_password_confirmation' ) &&
$request -> input ( 'new_password_confirmation' ) !== $validated [ 'new_password' ]) {
return ApiResponse :: error ( '비밀번호 확인이 일치하지 않습니다.' , 400 );
2025-07-17 10:05:47 +09:00
}
2025-08-14 00:55:08 +09:00
// 유저 조회
$user = User :: find ( $apiUserId );
if ( ! $user ) {
return ApiResponse :: error ( '유저를 찾을 수 없음' , 404 );
}
2025-07-17 10:05:47 +09:00
2025-08-14 00:55:08 +09:00
// 현재 비밀번호 확인
if ( ! Hash :: check ( $validated [ 'current_password' ], $user -> password )) {
return ApiResponse :: error ( '현재 비밀번호가 일치하지 않습니다.' , 400 );
}
2025-07-17 10:05:47 +09:00
2025-08-14 00:55:08 +09:00
// 기존 비밀번호와 동일한지 방지
if ( Hash :: check ( $validated [ 'new_password' ], $user -> password )) {
return ApiResponse :: error ( '새 비밀번호가 기존 비밀번호와 동일합니다.' , 400 );
2025-07-17 10:05:47 +09:00
}
2025-08-14 00:55:08 +09:00
// 비밀번호 변경 (guarded 우회: 직접 대입 + save)
$user -> password = Hash :: make ( $validated [ 'new_password' ]);
$saved = $user -> save ();
// (선택) 모든 기존 토큰 무효화하려면 아래 주석 해제
// $user->tokens()->delete();
2025-07-17 10:05:47 +09:00
return ApiResponse :: response ( 'success' );
}
/**
2025-08-14 00:55:08 +09:00
* 나의 테넌트 목록
2025-07-17 10:05:47 +09:00
*/
2025-08-14 00:55:08 +09:00
public static function getMyTenants ()
2025-07-17 10:05:47 +09:00
{
2025-08-14 00:55:08 +09:00
$debug = app () -> environment ( 'local' );
if ( $debug ) DB :: enableQueryLog ();
2025-07-17 10:05:47 +09:00
2025-08-14 00:55:08 +09:00
$apiUser = app ( 'api_user' );
$data = UserTenant :: join ( 'tenants' , 'user_tenants.tenant_id' , '=' , 'tenants.id' )
-> where ( 'user_tenants.user_id' , $apiUser )
-> get ([
'tenants.id' ,
'tenants.company_name' ,
'user_tenants.is_active' ,
'user_tenants.is_default'
]);
return ApiResponse :: response ( 'result' , $data );
2025-07-17 10:05:47 +09:00
}
2025-08-14 00:55:08 +09:00
2025-07-17 10:05:47 +09:00
/**
2025-08-14 00:55:08 +09:00
* 나의 테넌트 전환
2025-07-17 10:05:47 +09:00
*/
2025-08-14 00:55:08 +09:00
public static function switchMyTenant ( int $tenantId )
2025-07-17 10:05:47 +09:00
{
2025-08-14 00:55:08 +09:00
$debug = app () -> environment ( 'local' );
if ( $debug ) DB :: enableQueryLog ();
2025-07-17 10:05:47 +09:00
2025-08-14 00:55:08 +09:00
$apiUser = app ( 'api_user' );
// 1) 현재 유저의 기본 테넌트를 모두 해제
UserTenant :: where ( 'user_id' , $apiUser )
-> where ( 'is_default' , 1 )
-> update ([ 'is_default' => 0 ]);
// 2) 지정한 tenant_id를 기본 테넌트로 설정
$updated = UserTenant :: where ( 'user_id' , $apiUser )
-> where ( 'tenant_id' , $tenantId )
-> update ([ 'is_default' => 1 ]);
if ( ! $updated ) {
return ApiResponse :: error ( '해당 테넌트를 찾을 수 없습니다.' , 404 );
}
2025-07-17 10:05:47 +09:00
return ApiResponse :: response ( 'success' );
}
}