diff --git a/src/views/vacation/VacationManagement.vue b/src/views/vacation/VacationManagement.vue index a8021e5..c207266 100644 --- a/src/views/vacation/VacationManagement.vue +++ b/src/views/vacation/VacationManagement.vue @@ -325,51 +325,66 @@ const filteredReceivedVacations = computed(() => { // 휴가 변경사항 저장 async function saveVacationChanges() { if (!hasChanges.value) return; + const selectedDatesArray = Array.from(selectedDates.value); - const vacationChangesByYear = selectedDatesArray.reduce((acc, [date, type]) => { - const year = date.split("-")[0]; // YYYY-MM-DD에서 YYYY 추출 - if (!acc[year]) acc[year] = { add: [], delete: [] }; + + // 전체 연도를 고려하여 삭제 및 추가할 데이터를 분리 + const vacationChanges = selectedDatesArray.reduce((acc, [date, type]) => { if (type !== "delete") { - acc[year].add.push({ date, type }); + acc.add.push({ date, type }); } else { - acc[year].delete.push(date); + acc.delete.push(date); } return acc; - }, {}); + }, { add: [], delete: [] }); + try { - for (const year of Object.keys(vacationChangesByYear)) { - const vacationsToAdd = vacationChangesByYear[year].add; - // 즉시 삭제 반영을 위해 삭제 대상 id 가져오기 - const vacationsToDeleteForYear = myVacations.value - .filter(vac => { - if (!vac.date) return false; - const vacDate = vac.date.split("T")[0]; - return vacationChangesByYear[year].delete.includes(vacDate); - }); - const vacationIdsToDelete = vacationsToDeleteForYear.map(vac => vac.id); - if (vacationsToAdd.length > 0 || vacationIdsToDelete.length > 0) { - const response = await axios.post("vacation/batchUpdate", { - add: vacationsToAdd, - delete: vacationIdsToDelete, - }); - if (response.data && response.data.status === "OK") { - toastStore.onToast(`휴가 변경 사항이 저장되었습니다.`, 's'); - // 즉시 삭제 반영: myVacations에서 해당 ID 삭제 - myVacations.value = myVacations.value.filter(vac => !vacationIdsToDelete.includes(vac.id)); - // 서버에서 최신 데이터 가져와 반영 + // 모든 연도의 데이터를 고려하여 삭제할 ID 찾기 + const allYears = new Set(vacationChanges.delete.map(date => date.split("-")[0])); + let vacationIdsToDelete = []; + for (const year of allYears) { + await fetchVacationHistory(year); // 각 연도의 최신 데이터를 가져오기 + const vacationsToDelete = myVacations.value.filter(vac => { + if (!vac.date) return false; + const vacDate = vac.date.split("T")[0]; + return vacationChanges.delete.includes(vacDate); + }); + vacationIdsToDelete.push(...vacationsToDelete.map(vac => vac.id)); + } + + if (vacationChanges.add.length > 0 || vacationIdsToDelete.length > 0) { + const response = await axios.post("vacation/batchUpdate", { + add: vacationChanges.add, + delete: vacationIdsToDelete, + }); + if (response.data && response.data.status === "OK") { + toastStore.onToast(`휴가 변경 사항이 저장되었습니다.`, 's'); + + // 삭제된 ID를 반영하여 모든 연도의 `myVacations.value`를 업데이트 + myVacations.value = myVacations.value.filter(vac => !vacationIdsToDelete.includes(vac.id)); + + // 삭제 후 최신 데이터 불러오기 (기존 데이터를 유지하면서 추가) + const yearsToUpdate = new Set( + [...vacationChanges.add.map(v => v.date.split("-")[0]), + ...vacationChanges.delete.map(v => v.split("-")[0])] + ); + + for (const year of yearsToUpdate) { const updatedVacations = await fetchVacationHistory(year); if (updatedVacations) { - myVacations.value = updatedVacations; // 최신 데이터 적용 + myVacations.value = [...myVacations.value, ...updatedVacations.filter(newVac => + !myVacations.value.some(oldVac => oldVac.id === newVac.id) + )]; } - } else { - toastStore.onToast(`휴가 변경 중 오류가 발생했습니다.`, 'e'); } + } else { + toastStore.onToast(`휴가 변경 중 오류가 발생했습니다.`, 'e'); } } await fetchRemainingVacation(); selectedDates.value.clear(); updateCalendarEvents(); - // 현재 보고 있는 연도의 데이터 새로고침 + // 캘린더 새로고침 const currentDate = fullCalendarRef.value.getApi().getDate(); await loadCalendarData(currentDate.getFullYear(), currentDate.getMonth() + 1); } catch (error) { @@ -377,7 +392,6 @@ async function saveVacationChanges() { toastStore.onToast('휴가 저장 요청에 실패했습니다.', 'e'); } } - /* 휴가 조회 */ // 로그인 사용자의 연차 사용 내역 async function fetchVacationHistory(year) {