เพิ่ม function Schedule

This commit is contained in:
Net 2024-07-19 14:33:29 +07:00
parent 84e8af2e5b
commit a9ecba3a17

View file

@ -7,8 +7,10 @@ import { useCounterMixin } from "@/stores/mixin";
import type {
DataBackup,
BackUpRunning,
Schedule,
} from "@/modules/04_system/interface/response/Main";
import type { ScheduleCreate } from "@/modules/04_system/interface/request/Main";
import { ref } from "vue";
const mixin = useCounterMixin();
@ -23,6 +25,7 @@ const {
export const useDataStore = defineStore("systemStore", () => {
const dataBackUp = ref<DataBackup[]>([]);
const dataSchedule = ref<Schedule[]>([]);
const prevBackupRunTotal = ref<number>(-1);
const backupRunTotal = ref<number>(0);
@ -178,11 +181,130 @@ export const useDataStore = defineStore("systemStore", () => {
return sizeNumber.toFixed(2) + " " + units[i];
}
function convertCronToForm(schedule: string, typeReturn: string): string {
let allValues = schedule.split(" ");
if (typeReturn === "typeOfSchedule") {
if (
allValues[3].includes("*") &&
allValues[4].includes("*") &&
allValues[5].includes("*")
) {
return `daily`;
}
if (
allValues[3].includes("*") &&
allValues[4].includes("*") &&
!allValues[5].includes("*")
) {
return `weekly`;
}
if (
!allValues[3].includes("*") &&
allValues[4].includes("*") &&
allValues[5].includes("*")
) {
return `monthly`;
}
}
console.log(allValues);
if (typeReturn === "time") {
if (allValues[2].includes("/")) {
console.log(allValues[2]);
return "";
} else {
return `${allValues[2]}:${allValues[1]}`;
}
}
if (typeReturn === "timeStartEvery") {
return allValues[2].split("/")[1];
}
if (typeReturn === "date") {
return `${allValues[5].split(",")}`;
}
return "";
}
async function getSchedule() {
showLoader();
await http
.get<Schedule[]>(config.API.backup + "/schedule")
.then((res) => {
dataSchedule.value = res.data.map((item) => {
return {
id: item.id,
name: item.name,
schedule: item.schedule,
type: convertCronToForm(item.schedule, "typeOfSchedule") || "",
time: convertCronToForm(item.schedule, "time") || "",
date: convertCronToForm(item.schedule, "date") || "",
timeStartEvery:
convertCronToForm(item.schedule, "timeStartEvery") || "",
startAt: item.startAt,
};
});
console.log(dataSchedule.value);
})
.finally(() => {
hideLoader();
});
}
async function createSchedule(data: ScheduleCreate) {
showLoader();
await http
.post(config.API.backup + "/schedule", {
name: data.name,
schedule: data.schedule,
startAt: data.startAt !== undefined ? data.startAt : undefined,
})
.then(async (res) => {
return res.data;
})
.finally(() => {
hideLoader();
});
}
async function editSchedule(data: ScheduleCreate) {
showLoader();
await http
.put(config.API.backup + "/schedule", {
data,
})
.then(async (res) => {
return res.data;
})
.finally(() => {
hideLoader();
});
}
async function deleteSchedule(id: string) {
showLoader();
await http
.delete(config.API.backup + "/schedule/" + id)
.then(async (res) => {
await fetchListBackup();
})
.finally(() => {
hideLoader();
});
}
return {
dataBackUp,
backupRunTotal,
restoreRunTotal,
dataSchedule,
fetchListBackup,
createBackUp,
restore,
@ -192,5 +314,10 @@ export const useDataStore = defineStore("systemStore", () => {
restoreRunningList,
getSize,
getSchedule,
createSchedule,
editSchedule,
deleteSchedule,
};
});