109 lines
3.4 KiB
TypeScript
109 lines
3.4 KiB
TypeScript
import { getCookie } from "@/shared/lib";
|
|
import { ENDPOINT } from "../api/attachment/attachment-service";
|
|
|
|
const AUTH_USER_ID = process.env.NEXT_PUBLIC_AUTH_USER_ID || "token-id";
|
|
const CLAIM_NAME = process.env.NEXT_PUBLIC_CLAIM_NAME || "Authorization";
|
|
|
|
type DeleteOptions = {
|
|
handleDeleteStart?: () => void;
|
|
handleDeleteSuccess?: (response: Response) => void;
|
|
handleDeleteError?: (error: Error) => void;
|
|
};
|
|
|
|
export const handleDelete = async (
|
|
atflCd: string,
|
|
options?: DeleteOptions
|
|
): Promise<void> => {
|
|
if (!atflCd) {
|
|
const error = new Error("첨부파일 코드가 필요합니다.");
|
|
options?.handleDeleteError?.(error);
|
|
throw error;
|
|
}
|
|
|
|
try {
|
|
options?.handleDeleteStart?.();
|
|
|
|
const response = await fetch(
|
|
`${ENDPOINT.ATTACH_SERVICE.DELETE_FILE_BY_ATFLCD}/${atflCd}`,
|
|
{
|
|
method: "DELETE",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
[CLAIM_NAME]: getCookie(CLAIM_NAME) ?? "",
|
|
[AUTH_USER_ID]: getCookie(AUTH_USER_ID) ?? "",
|
|
},
|
|
}
|
|
);
|
|
|
|
if (!response.ok) {
|
|
throw new Error(`파일 삭제 실패: ${response.status} ${response.statusText}`);
|
|
}
|
|
|
|
options?.handleDeleteSuccess?.(response);
|
|
console.log("파일 삭제 완료:", atflCd);
|
|
|
|
} catch (error) {
|
|
const deleteError = error instanceof Error
|
|
? error
|
|
: new Error("파일 삭제 중 알 수 없는 오류가 발생했습니다.");
|
|
|
|
console.error("파일 삭제 오류:", deleteError);
|
|
options?.handleDeleteError?.(deleteError);
|
|
throw deleteError;
|
|
}
|
|
};
|
|
|
|
// 여러 파일을 삭제하는 함수
|
|
// export const handleMultipleDelete = async (
|
|
// atflCds: string[],
|
|
// options?: DeleteOptions & {
|
|
// handleProgress?: (completed: number, total: number) => void;
|
|
// }
|
|
// ): Promise<void> => {
|
|
// if (!atflCds || atflCds.length === 0) {
|
|
// const error = new Error("삭제할 파일이 없습니다.");
|
|
// options?.handleDeleteError?.(error);
|
|
// throw error;
|
|
// }
|
|
|
|
// try {
|
|
// options?.handleDeleteStart?.();
|
|
|
|
// const deletePromises = atflCds.map(async (atflCd, index) => {
|
|
// try {
|
|
// await handleDelete(atflCd);
|
|
// options?.handleProgress?.(index + 1, atflCds.length);
|
|
// return { success: true, atflCd };
|
|
// } catch (error) {
|
|
// return { success: false, atflCd, error };
|
|
// }
|
|
// });
|
|
|
|
// const results = await Promise.allSettled(deletePromises);
|
|
// const failedDeletes = results
|
|
// .filter((result): result is PromiseFulfilledResult<{ success: false; atflCd: string; error: any }> =>
|
|
// result.status === 'fulfilled' && !result.value.success
|
|
// )
|
|
// .map(result => result.value);
|
|
|
|
// if (failedDeletes.length > 0) {
|
|
// const error = new Error(
|
|
// `일부 파일 삭제 실패: ${failedDeletes.map(f => f.atflCd).join(', ')}`
|
|
// );
|
|
// options?.handleDeleteError?.(error);
|
|
// throw error;
|
|
// }
|
|
|
|
// options?.handleDeleteSuccess?.(new Response());
|
|
// console.log("모든 파일 삭제 완료:", atflCds);
|
|
|
|
// } catch (error) {
|
|
// const deleteError = error instanceof Error
|
|
// ? error
|
|
// : new Error("파일 삭제 중 알 수 없는 오류가 발생했습니다.");
|
|
|
|
// console.error("파일 삭제 오류:", deleteError);
|
|
// options?.handleDeleteError?.(deleteError);
|
|
// throw deleteError;
|
|
// }
|
|
// };
|