Skip to content

Commit

Permalink
refactor:#345 patch 요청 공통함수로 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
dalzzy committed Feb 12, 2025
1 parent 4750dfa commit e911856
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 43 deletions.
1 change: 1 addition & 0 deletions src/api/admin/member/deleteUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const PATH = '/api/v1/admin/users';

const BASE_URL = import.meta.env.VITE_API_URL;

// 유저 추방
const deleteUserApi = async (userId: number | number[]) => {
const accessToken = localStorage.getItem('accessToken');
const userIds = Array.isArray(userId) ? userId : [userId];
Expand Down
62 changes: 19 additions & 43 deletions src/api/admin/member/patchUserManagement.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import axios from 'axios';

const PATH = '/api/v1/admin/users';

const BASE_URL = import.meta.env.VITE_API_URL;

// 비밀번호 초기화
const resetPwdApi = async (userId: number | number[]) => {
// PATCH 공통함수
const sendPatchRequest = async (
endpoint: string,
params: string,
errorMessage: string,
) => {
const accessToken = localStorage.getItem('accessToken');
const userIds = Array.isArray(userId) ? userId : [userId];

try {
const queryParam = userIds.map((id) => `userId=${id}`).join('&');
const url = `${BASE_URL}${PATH}/reset?${queryParam}`;
const url = `${BASE_URL}${PATH}${endpoint}?${params}`;
const response = await axios.patch(
url,
{},
Expand All @@ -23,52 +23,28 @@ const resetPwdApi = async (userId: number | number[]) => {
);
return response.data;
} catch (error: any) {
throw new Error(error.response?.data?.message || '비밀번호 초기화 실패');
throw new Error(error.response?.data?.message || errorMessage);
}
};

// 비밀번호 초기화
const resetPwdApi = async (userId: number | number[]) => {
const userIds = Array.isArray(userId) ? userId : [userId];
const queryParam = userIds.map((id) => `userId=${id}`).join('&');
return sendPatchRequest('/reset', queryParam, '비밀번호 초기화 실패');
};

// 가입 신청 승인
const approveSignupApi = async (userId: number | number[]) => {
const accessToken = localStorage.getItem('accessToken');
const userIds = Array.isArray(userId) ? userId : [userId];

try {
const queryParam = userIds.map((id) => `userId=${id}`).join('&');
const url = `${BASE_URL}${PATH}?${queryParam}`;
const response = await axios.patch(
url,
{},
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
);
return response.data;
} catch (error: any) {
throw new Error(error.response?.data?.message || '가입 승인 실패');
}
const queryParam = userIds.map((id) => `userId=${id}`).join('&');
return sendPatchRequest('', queryParam, '가입 승인 실패');
};

// 관리자로 승격/강등
const changeUserRoleApi = async (userId: number, role: 'ADMIN' | 'USER') => {
const accessToken = localStorage.getItem('accessToken');

try {
const url = `${BASE_URL}${PATH}/role?userId=${userId}&role=${role}`;
const response = await axios.patch(
url,
{},
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
);
return response.data;
} catch (error: any) {
throw new Error(error.response?.data?.message || '역할 변경 실패');
}
const queryParam = `userId=${userId}&role=${role}`;
return sendPatchRequest('/role', queryParam, '역할 변경 실패');
};

export { resetPwdApi, approveSignupApi, changeUserRoleApi };

0 comments on commit e911856

Please sign in to comment.