This commit is contained in:
parent
a3ea106265
commit
69fb0669be
@ -325,51 +325,66 @@ const filteredReceivedVacations = computed(() => {
|
|||||||
// 휴가 변경사항 저장
|
// 휴가 변경사항 저장
|
||||||
async function saveVacationChanges() {
|
async function saveVacationChanges() {
|
||||||
if (!hasChanges.value) return;
|
if (!hasChanges.value) return;
|
||||||
|
|
||||||
const selectedDatesArray = Array.from(selectedDates.value);
|
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") {
|
if (type !== "delete") {
|
||||||
acc[year].add.push({ date, type });
|
acc.add.push({ date, type });
|
||||||
} else {
|
} else {
|
||||||
acc[year].delete.push(date);
|
acc.delete.push(date);
|
||||||
}
|
}
|
||||||
return acc;
|
return acc;
|
||||||
}, {});
|
}, { add: [], delete: [] });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
for (const year of Object.keys(vacationChangesByYear)) {
|
// 모든 연도의 데이터를 고려하여 삭제할 ID 찾기
|
||||||
const vacationsToAdd = vacationChangesByYear[year].add;
|
const allYears = new Set(vacationChanges.delete.map(date => date.split("-")[0]));
|
||||||
// 즉시 삭제 반영을 위해 삭제 대상 id 가져오기
|
let vacationIdsToDelete = [];
|
||||||
const vacationsToDeleteForYear = myVacations.value
|
for (const year of allYears) {
|
||||||
.filter(vac => {
|
await fetchVacationHistory(year); // 각 연도의 최신 데이터를 가져오기
|
||||||
|
const vacationsToDelete = myVacations.value.filter(vac => {
|
||||||
if (!vac.date) return false;
|
if (!vac.date) return false;
|
||||||
const vacDate = vac.date.split("T")[0];
|
const vacDate = vac.date.split("T")[0];
|
||||||
return vacationChangesByYear[year].delete.includes(vacDate);
|
return vacationChanges.delete.includes(vacDate);
|
||||||
});
|
});
|
||||||
const vacationIdsToDelete = vacationsToDeleteForYear.map(vac => vac.id);
|
vacationIdsToDelete.push(...vacationsToDelete.map(vac => vac.id));
|
||||||
if (vacationsToAdd.length > 0 || vacationIdsToDelete.length > 0) {
|
}
|
||||||
|
|
||||||
|
if (vacationChanges.add.length > 0 || vacationIdsToDelete.length > 0) {
|
||||||
const response = await axios.post("vacation/batchUpdate", {
|
const response = await axios.post("vacation/batchUpdate", {
|
||||||
add: vacationsToAdd,
|
add: vacationChanges.add,
|
||||||
delete: vacationIdsToDelete,
|
delete: vacationIdsToDelete,
|
||||||
});
|
});
|
||||||
if (response.data && response.data.status === "OK") {
|
if (response.data && response.data.status === "OK") {
|
||||||
toastStore.onToast(`휴가 변경 사항이 저장되었습니다.`, 's');
|
toastStore.onToast(`휴가 변경 사항이 저장되었습니다.`, 's');
|
||||||
// 즉시 삭제 반영: myVacations에서 해당 ID 삭제
|
|
||||||
|
// 삭제된 ID를 반영하여 모든 연도의 `myVacations.value`를 업데이트
|
||||||
myVacations.value = myVacations.value.filter(vac => !vacationIdsToDelete.includes(vac.id));
|
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);
|
const updatedVacations = await fetchVacationHistory(year);
|
||||||
if (updatedVacations) {
|
if (updatedVacations) {
|
||||||
myVacations.value = updatedVacations; // 최신 데이터 적용
|
myVacations.value = [...myVacations.value, ...updatedVacations.filter(newVac =>
|
||||||
|
!myVacations.value.some(oldVac => oldVac.id === newVac.id)
|
||||||
|
)];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
toastStore.onToast(`휴가 변경 중 오류가 발생했습니다.`, 'e');
|
toastStore.onToast(`휴가 변경 중 오류가 발생했습니다.`, 'e');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
await fetchRemainingVacation();
|
await fetchRemainingVacation();
|
||||||
selectedDates.value.clear();
|
selectedDates.value.clear();
|
||||||
updateCalendarEvents();
|
updateCalendarEvents();
|
||||||
// 현재 보고 있는 연도의 데이터 새로고침
|
// 캘린더 새로고침
|
||||||
const currentDate = fullCalendarRef.value.getApi().getDate();
|
const currentDate = fullCalendarRef.value.getApi().getDate();
|
||||||
await loadCalendarData(currentDate.getFullYear(), currentDate.getMonth() + 1);
|
await loadCalendarData(currentDate.getFullYear(), currentDate.getMonth() + 1);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
@ -377,7 +392,6 @@ async function saveVacationChanges() {
|
|||||||
toastStore.onToast('휴가 저장 요청에 실패했습니다.', 'e');
|
toastStore.onToast('휴가 저장 요청에 실패했습니다.', 'e');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 휴가 조회 */
|
/* 휴가 조회 */
|
||||||
// 로그인 사용자의 연차 사용 내역
|
// 로그인 사용자의 연차 사용 내역
|
||||||
async function fetchVacationHistory(year) {
|
async function fetchVacationHistory(year) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user