feat: toggle able schedule

This commit is contained in:
Methapon2001 2024-07-18 09:29:00 +07:00
parent ca12d80aa8
commit 00efbdc6a2

View file

@ -34,6 +34,14 @@ const BACKUP_MINIO_ACCESS_KEY = getEnvVar("BACKUP_MINIO_ACCESS_KEY");
const BACKUP_MINIO_SECRET_KEY = getEnvVar("BACKUP_MINIO_SECRET_KEY");
const BACKUP_MINIO_BUCKET = getEnvVar("BACKUP_MINIO_BUCKET");
function jsonParseOrPlainText(str: string) {
try {
return JSON.parse(str);
} catch (_) {
return str;
}
}
@Route("/api/v1/backup")
@Security("keycloak")
export class BackupController extends Controller {
@ -100,14 +108,7 @@ export class BackupController extends Controller {
{
headers: { Authorization: `Bearer ${WINDMILL_API_KEY}` },
},
).then(async (r) => {
const data = await r.json();
if (typeof data === "object" && "error" in data) {
console.error(data);
throw new Error("Cannot get status.");
}
return data;
});
).then(async (r) => jsonParseOrPlainText(await r.text()));
}
@Post("create")
@ -156,14 +157,7 @@ export class BackupController extends Controller {
},
}),
},
).then(async (r) => {
const data = await r.text();
if (data.includes("error")) {
console.error(data);
throw new Error("Backup Error");
}
return data;
});
).then(async (r) => jsonParseOrPlainText(await r.text()));
}
@Post("restore")
@ -205,14 +199,7 @@ export class BackupController extends Controller {
},
}),
},
).then(async (r) => {
const data = await r.text();
if (data.includes("error")) {
console.error(data);
throw new Error("Backup Error");
}
return data;
});
).then(async (r) => jsonParseOrPlainText(await r.text()));
}
@Delete("delete")
@ -234,12 +221,7 @@ export class BackupController extends Controller {
}),
},
).then(async (r) => {
const data = await r.text();
if (data.includes("error")) {
console.error(data);
throw new Error("Error delete backup.");
}
return data;
return jsonParseOrPlainText(await r.text());
});
}
@ -265,6 +247,7 @@ export class BackupController extends Controller {
id: v.path.replace("f/backup_schedule/", ""),
name: v.summary,
schedule: v.schedule,
enabled: v.enabled,
}));
}
@ -313,21 +296,13 @@ export class BackupController extends Controller {
},
},
}),
}).then(async (r) => {
const data = await r.text();
if (data.includes("error")) {
console.error(data);
throw new Error("Error create schedule.");
}
console.log(data);
return data;
});
}).then(async (r) => jsonParseOrPlainText(await r.text()));
}
@Put("schedule/{id}")
async updateSchedule(
@Path() id: string,
@Body() body: { name: string; schedule: string; timezone?: string },
@Body() body: { name: string; schedule: string; enabled?: boolean; timezone?: string },
) {
if (!/^[a-zA-Z0-9\-]+$/.test(body.name)) {
throw new HttpError(HttpStatus.BAD_REQUEST, "Invalid name.");
@ -372,17 +347,53 @@ export class BackupController extends Controller {
}),
},
).then(async (r) => {
if (r.status >= 400) {
console.log(await r.json());
throw new Error("Error trying to run script/flow.");
}
const data = await r.text();
const body = jsonParseOrPlainText(await r.text());
if (data.includes("error")) {
console.error(data);
throw new Error("Error create schedule.");
if (r.status === 200) {
await fetch(
`${WINDMILL_URL}/api/w/${WINDMILL_WORKSPACE}/schedules/setenabled/f/backup_schedule/${id}`,
{
method: "POST",
headers: {
Authorization: `Bearer ${WINDMILL_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ enabled: body.enabled }),
},
);
}
return body;
});
}
@Post("schedule/{id}/toggle")
async toggleSchedule(@Path() id: string) {
return await fetch(
`${WINDMILL_URL}/api/w/${WINDMILL_WORKSPACE}/schedules/get/f/backup_schedule/${id}`,
{
headers: {
Authorization: `Bearer ${WINDMILL_API_KEY}`,
"Content-Type": "application/json",
},
},
).then(async (r) => {
const body = jsonParseOrPlainText(await r.text());
if (typeof body === "object") {
const result = await fetch(
`${WINDMILL_URL}/api/w/${WINDMILL_WORKSPACE}/schedules/setenabled/f/backup_schedule/${id}`,
{
method: "POST",
headers: {
Authorization: `Bearer ${WINDMILL_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({ enabled: !body.enabled }),
},
);
return jsonParseOrPlainText(await result.text());
}
return data;
});
}
@ -397,16 +408,6 @@ export class BackupController extends Controller {
"Content-Type": "application/json",
},
},
).then(async (r) => {
if (r.status === 404) {
throw new HttpError(HttpStatus.NOT_FOUND, "Schedule not found.");
}
const data = await r.text();
if (data.includes("error")) {
console.error(data);
throw new Error("Error create schedule.");
}
});
).then(async (r) => jsonParseOrPlainText(await r.text()));
}
}