fix : 결과 전달시 두번 래핑되는 부분 수정
- 컨트롤러와 서비스에서 각각 래핑 후 결과 전달됨으로 이중 래핑되고 있음 -> 서비스에서 래핑하는 부분을 컨트롤러로 옮겨서 컨트롤러에서만 한번 래핑하는 걸로 수정
This commit is contained in:
@@ -4,14 +4,12 @@
|
||||
|
||||
use App\Helpers\ApiResponse;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Illuminate\Support\Str;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
use App\Models\Members\User;
|
||||
use App\Models\Members\UserTenant;
|
||||
use Spatie\Permission\Models\Role;
|
||||
|
||||
|
||||
class AdminService
|
||||
@@ -25,7 +23,7 @@ public static function getTenants(array $params = [])
|
||||
{
|
||||
$tenantId = app('tenant_id');
|
||||
if (!$tenantId) {
|
||||
return ApiResponse::error('활성 테넌트가 없습니다.', 400);
|
||||
return ['error' => '성 테넌트가 없습니다.', 'code' => 400];
|
||||
}
|
||||
|
||||
$page = isset($params['page']) ? (int)$params['page'] : 1;
|
||||
@@ -69,9 +67,7 @@ public static function getTenants(array $params = [])
|
||||
|
||||
$q->orderBy($sortBy, $sortDir);
|
||||
|
||||
$data = $q->paginate($size, ['*'], 'page', $page);
|
||||
|
||||
return ApiResponse::response('result', $data);
|
||||
return $data = $q->paginate($size, ['*'], 'page', $page);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,7 +79,7 @@ public static function store(array $params = [])
|
||||
{
|
||||
$tenantId = app('tenant_id');
|
||||
if (!$tenantId) {
|
||||
return ApiResponse::error('활성 테넌트가 없습니다.', 400);
|
||||
return ['error' => '활성 테넌트가 없습니다.', 'code' => 400];
|
||||
}
|
||||
|
||||
// 신규 회원 생성 + 역할 부여 지원
|
||||
@@ -98,7 +94,7 @@ public static function store(array $params = [])
|
||||
]);
|
||||
|
||||
if ($v->fails()) {
|
||||
return ApiResponse::error($v->errors()->first(), 422);
|
||||
return ['error' => $v->errors()->first(), 'code' => 422];
|
||||
}
|
||||
|
||||
$payload = $v->validated();
|
||||
@@ -136,9 +132,9 @@ public static function store(array $params = [])
|
||||
}
|
||||
}
|
||||
|
||||
return ApiResponse::response('result', [
|
||||
return [
|
||||
'user' => $user->only(['id','user_id','name','email','phone']),
|
||||
]);
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
@@ -150,20 +146,20 @@ public static function show(int $userNo)
|
||||
{
|
||||
$tenantId = app('tenant_id');
|
||||
if (!$tenantId) {
|
||||
return ApiResponse::error('활성 테넌트가 없습니다.', 400);
|
||||
return ['error' => '활성 테넌트가 없습니다.', 'code' => 400];
|
||||
}
|
||||
|
||||
if (!$userNo) {
|
||||
return ApiResponse::error('회원 정보가 없습니다.', 422);
|
||||
return ['error' => '회원 정보가 없습니다.', 'code' => 422];
|
||||
}
|
||||
|
||||
$user = User::whereHas('userTenants')->find($userNo);
|
||||
|
||||
if (!$user) {
|
||||
return ApiResponse::error('해당 사용자를 찾을 수 없습니다.', 404);
|
||||
return ['error' => '해당 사용자를 찾을 수 없습니다.', 'code' => 404];
|
||||
}
|
||||
|
||||
return ApiResponse::response('result', $user);
|
||||
return $user;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -175,22 +171,22 @@ public static function update(array $params = [], int $userNo)
|
||||
{
|
||||
$tenantId = app('tenant_id');
|
||||
if (!$tenantId) {
|
||||
return ApiResponse::error('활성 테넌트가 없습니다.', 400);
|
||||
return ['error' => '활성 테넌트가 없습니다.', 'code' => 400];
|
||||
}
|
||||
if (!$userNo) {
|
||||
return ApiResponse::error('회원 정보가 없습니다.', 422);
|
||||
return ['error' => '회원 정보가 없습니다.', 'code' => 422];
|
||||
}
|
||||
|
||||
// 1) 유저 존재/테넌트 소속 확인
|
||||
$user = User::find($userNo);
|
||||
if (!$user) {
|
||||
return ApiResponse::error('해당 회원을 찾을 수 없습니다.', 404);
|
||||
return ['error' => '해당 회원을 찾을 수 없습니다.', 'code' => 404];
|
||||
}
|
||||
$linked = UserTenant::where('tenant_id', $tenantId)
|
||||
->where('user_id', $userNo)
|
||||
->exists();
|
||||
if (!$linked) {
|
||||
return ApiResponse::error('이 테넌트에 소속된 회원이 아닙니다.', 403);
|
||||
return ['error' => '이 테넌트에 소속된 회원이 아닙니다.', 'code' => 403];
|
||||
}
|
||||
|
||||
// 2) 프로필 + roles만 수정
|
||||
@@ -205,7 +201,7 @@ public static function update(array $params = [], int $userNo)
|
||||
'roles.*' => 'string|max:100',
|
||||
]);
|
||||
if ($v->fails()) {
|
||||
return ApiResponse::error($v->errors()->first(), 422);
|
||||
return ['error' => $v->errors()->first(), 'code' => 422];
|
||||
}
|
||||
$payload = $v->validated();
|
||||
|
||||
@@ -214,7 +210,7 @@ public static function update(array $params = [], int $userNo)
|
||||
$hasProfileInput = (bool) array_intersect(array_keys($payload), $updatableKeys);
|
||||
$hasRolesInput = array_key_exists('roles', $payload);
|
||||
if (!$hasProfileInput && !$hasRolesInput) {
|
||||
return ApiResponse::error('수정할 항목이 없습니다.', 422);
|
||||
return ['error' => '수정할 항목이 없습니다.', 'code' => 422];
|
||||
}
|
||||
|
||||
return DB::transaction(function () use ($user, $payload, $tenantId, $updatableKeys) {
|
||||
@@ -252,10 +248,10 @@ public static function update(array $params = [], int $userNo)
|
||||
}
|
||||
}
|
||||
|
||||
return ApiResponse::response('result', [
|
||||
return [
|
||||
'user' => $user->only(['id','user_id','name','email','phone']),
|
||||
'roles' => method_exists($user, 'getRoleNames') ? $user->getRoleNames() : [],
|
||||
]);
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
@@ -267,10 +263,10 @@ public static function destroy(int $userNo)
|
||||
{
|
||||
$tenantId = app('tenant_id');
|
||||
if (!$tenantId) {
|
||||
return ApiResponse::error('활성 테넌트가 없습니다.', 400);
|
||||
return ['error' => '활성 테넌트가 없습니다.', 'code' => 400];
|
||||
}
|
||||
if (!$userNo) {
|
||||
return ApiResponse::error('회원 정보가 없습니다.', 422);
|
||||
return ['error' => '회원 정보가 없습니다.', 'code' => 422];
|
||||
}
|
||||
|
||||
$ut = UserTenant::where('user_id',$userNo)
|
||||
@@ -278,14 +274,14 @@ public static function destroy(int $userNo)
|
||||
->first();
|
||||
|
||||
if (!$ut) {
|
||||
return ApiResponse::error('해당 사용자를 찾을 수 없습니다.', 404);
|
||||
return ['error' => '해당 사용자를 찾을 수 없습니다.', 'code' => 404];
|
||||
}
|
||||
|
||||
$ut->left_at = now();
|
||||
$ut->save();
|
||||
$ut->delete(); // SoftDeletes 가정
|
||||
|
||||
return ApiResponse::response('success');
|
||||
return 'success';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -295,10 +291,10 @@ public static function restore(int $userNo)
|
||||
{
|
||||
$tenantId = app('tenant_id');
|
||||
if (!$tenantId) {
|
||||
return ApiResponse::error('활성 테넌트가 없습니다.', 400);
|
||||
return ['error' => '활성 테넌트가 없습니다.', 'code' => 400];
|
||||
}
|
||||
if (!$userNo) {
|
||||
return ApiResponse::error('회원 정보가 없습니다.', 422);
|
||||
return ['error' => '회원 정보가 없습니다.', 'code' => 422];
|
||||
}
|
||||
|
||||
$ut = UserTenant::withTrashed()
|
||||
@@ -307,7 +303,7 @@ public static function restore(int $userNo)
|
||||
->first();
|
||||
|
||||
if (!$ut) {
|
||||
return ApiResponse::error('해당 사용자를 찾을 수 없습니다.', 404);
|
||||
return ['error' => '해당 사용자를 찾을 수 없습니다.', 'code' => 404];
|
||||
}
|
||||
|
||||
if ($ut->trashed()) {
|
||||
@@ -316,7 +312,7 @@ public static function restore(int $userNo)
|
||||
$ut->save();
|
||||
}
|
||||
|
||||
return ApiResponse::response('success');
|
||||
return 'success';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -326,10 +322,10 @@ public static function toggle(int $userNo)
|
||||
{
|
||||
$tenantId = app('tenant_id');
|
||||
if (!$tenantId) {
|
||||
return ApiResponse::error('활성 테넌트가 없습니다.', 400);
|
||||
return ['error' => '활성 테넌트가 없습니다.', 'code' => 400];
|
||||
}
|
||||
if (!$userNo) {
|
||||
return ApiResponse::error('회원 정보가 없습니다.', 422);
|
||||
return ['error' => '회원 정보가 없습니다.', 'code' => 422];
|
||||
}
|
||||
|
||||
$ut = UserTenant::where('tenant_id', $tenantId)
|
||||
@@ -337,13 +333,13 @@ public static function toggle(int $userNo)
|
||||
->first();
|
||||
|
||||
if (!$ut) {
|
||||
return ApiResponse::error('해당 사용자를 찾을 수 없습니다.', 404);
|
||||
return ['error' => '해당 사용자를 찾을 수 없습니다.', 'code' => 404];
|
||||
}
|
||||
|
||||
$ut->is_active = $ut->is_active ? 0 : 1;
|
||||
$ut->save();
|
||||
|
||||
return ApiResponse::response('result',['is_active' => $ut->is_active]);
|
||||
return ['is_active' => $ut->is_active];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -354,7 +350,7 @@ public static function attach(array $params = [])
|
||||
{
|
||||
$tenantId = app('tenant_id');
|
||||
if (!$tenantId) {
|
||||
return ApiResponse::error('활성 테넌트가 없습니다.', 400);
|
||||
return ['error' => '활성 테넌트가 없습니다.', 'code' => 400];
|
||||
}
|
||||
|
||||
$v = Validator::make($params, [
|
||||
@@ -362,13 +358,13 @@ public static function attach(array $params = [])
|
||||
'role_name' => 'required|string|max:100',
|
||||
]);
|
||||
if ($v->fails()) {
|
||||
return ApiResponse::error($v->errors()->first(), 422);
|
||||
return ['error' => $v->errors()->first(), 'code' => 422];
|
||||
}
|
||||
|
||||
$user = User::find($params['user_id']);
|
||||
if (!method_exists($user, 'assignRole')) {
|
||||
// Spatie 미사용 환경 방어
|
||||
return ApiResponse::error('역할 시스템이 활성화되어 있지 않습니다.', 501);
|
||||
return ['error' => '역할 시스템이 활성화되어 있지 않습니다.', 'code' => 501];
|
||||
}
|
||||
|
||||
// teams(tenant) 스코프
|
||||
@@ -382,7 +378,7 @@ public static function attach(array $params = [])
|
||||
app()->instance('permission.team_id', $previousTeam);
|
||||
}
|
||||
|
||||
return ApiResponse::response('success');
|
||||
return 'success';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -393,7 +389,7 @@ public static function detach(array $params = [])
|
||||
{
|
||||
$tenantId = app('tenant_id');
|
||||
if (!$tenantId) {
|
||||
return ApiResponse::error('활성 테넌트가 없습니다.', 400);
|
||||
return ['error' => '활성 테넌트가 없습니다.', 'code' => 400];
|
||||
}
|
||||
|
||||
$v = Validator::make($params, [
|
||||
@@ -401,12 +397,12 @@ public static function detach(array $params = [])
|
||||
'role_name' => 'required|string|max:100',
|
||||
]);
|
||||
if ($v->fails()) {
|
||||
return ApiResponse::error($v->errors()->first(), 422);
|
||||
return ['error' => $v->errors()->first(), 'code' => 422];
|
||||
}
|
||||
|
||||
$user = User::find($params['user_id']);
|
||||
if (!method_exists($user, 'removeRole')) {
|
||||
return ApiResponse::error('역할 시스템이 활성화되어 있지 않습니다.', 501);
|
||||
return ['error' => '역할 시스템이 활성화되어 있지 않습니다.', 'code' => 501];
|
||||
}
|
||||
|
||||
$previousTeam = app()->has('permission.team_id') ? app('permission.team_id') : null;
|
||||
@@ -418,7 +414,7 @@ public static function detach(array $params = [])
|
||||
app()->instance('permission.team_id', $previousTeam);
|
||||
}
|
||||
|
||||
return ApiResponse::response('success');
|
||||
return 'success';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -431,10 +427,10 @@ public static function reset(array $params = [],int $userNo)
|
||||
{
|
||||
$tenantId = app('tenant_id');
|
||||
if (!$tenantId) {
|
||||
return ApiResponse::error('활성 테넌트가 없습니다.', 400);
|
||||
return ['error' => '활성 테넌트가 없습니다.', 'code' => 400];
|
||||
}
|
||||
if (!$userNo) {
|
||||
return ApiResponse::error('회원 정보가 없습니다.', 422);
|
||||
return ['error' => '회원 정보가 없습니다.', 'code' => 422];
|
||||
}
|
||||
|
||||
$v = Validator::make($params, [
|
||||
@@ -442,13 +438,13 @@ public static function reset(array $params = [],int $userNo)
|
||||
'return_password' => 'nullable|in:0,1', // 1이면 응답에 임시 비번 포함(개발용)
|
||||
]);
|
||||
if ($v->fails()) {
|
||||
return ApiResponse::error($v->errors()->first(), 422);
|
||||
return ['error' => $v->errors()->first(), 'code' => 422];
|
||||
}
|
||||
$payload = $v->validated();
|
||||
|
||||
$user = User::find($userNo);
|
||||
if (!$user) {
|
||||
return ApiResponse::error('유저를 찾을 수 없습니다.', 404);
|
||||
return ['error' => '유저를 찾을 수 없습니다.', 'code' => 404];
|
||||
}
|
||||
|
||||
$new = $payload['new_password'] ?? Str::random(12);
|
||||
@@ -464,6 +460,6 @@ public static function reset(array $params = [],int $userNo)
|
||||
$resp['temp_password'] = $new;
|
||||
}
|
||||
|
||||
return ApiResponse::response('result', $resp);
|
||||
return $resp;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user