81 lines
2.4 KiB
JavaScript
81 lines
2.4 KiB
JavaScript
import { camelJob } from '../models/mappers.js';
|
|
import {
|
|
createRunPlanSchedule,
|
|
deleteRunPlanSchedule,
|
|
getRunPlanSchedule,
|
|
listRunPlanScheduleRuns,
|
|
listRunPlanSchedules,
|
|
previewRunPlanSchedule,
|
|
recordRunPlanScheduleDispatch,
|
|
setRunPlanScheduleEnabled,
|
|
updateRunPlanSchedule
|
|
} from '../models/runPlanScheduleModel.js';
|
|
import { executeRunPlan } from '../services/powershellRunner.js';
|
|
|
|
function isAdmin(req) {
|
|
return req.user?.role === 'admin';
|
|
}
|
|
|
|
export function index(req, res) {
|
|
res.json({
|
|
schedules: listRunPlanSchedules(req.user.id, isAdmin(req)),
|
|
history: listRunPlanScheduleRuns(req.user.id, isAdmin(req))
|
|
});
|
|
}
|
|
|
|
export function create(req, res) {
|
|
try {
|
|
res.status(201).json(createRunPlanSchedule(req.body, req.user.id));
|
|
} catch (error) {
|
|
res.status(400).json({ error: error.message });
|
|
}
|
|
}
|
|
|
|
export function update(req, res) {
|
|
try {
|
|
const schedule = updateRunPlanSchedule(req.params.id, req.body, req.user.id, isAdmin(req));
|
|
if (!schedule) return res.status(404).json({ error: 'Schedule not found' });
|
|
res.json(schedule);
|
|
} catch (error) {
|
|
res.status(400).json({ error: error.message });
|
|
}
|
|
}
|
|
|
|
export function destroy(req, res) {
|
|
if (!deleteRunPlanSchedule(req.params.id, req.user.id, isAdmin(req))) {
|
|
return res.status(404).json({ error: 'Schedule not found' });
|
|
}
|
|
res.status(204).end();
|
|
}
|
|
|
|
export function toggle(req, res) {
|
|
const schedule = setRunPlanScheduleEnabled(req.params.id, Boolean(req.body?.enabled), req.user.id, isAdmin(req));
|
|
if (!schedule) return res.status(404).json({ error: 'Schedule not found' });
|
|
res.json(schedule);
|
|
}
|
|
|
|
export function preview(req, res) {
|
|
try {
|
|
res.json({ occurrences: previewRunPlanSchedule(req.body) });
|
|
} catch (error) {
|
|
res.status(400).json({ error: error.message });
|
|
}
|
|
}
|
|
|
|
export function runNow(req, res) {
|
|
const schedule = getRunPlanSchedule(req.params.id, req.user.id, isAdmin(req));
|
|
if (!schedule) return res.status(404).json({ error: 'Schedule not found' });
|
|
try {
|
|
const job = executeRunPlan(schedule.runplanId, req.user.id);
|
|
recordRunPlanScheduleDispatch(schedule, {
|
|
jobId: job.id,
|
|
plannedFor: new Date().toISOString(),
|
|
status: 'manual',
|
|
message: `Operator launched schedule ${schedule.name} manually.`
|
|
});
|
|
res.status(202).json(camelJob(job));
|
|
} catch (error) {
|
|
res.status(400).json({ error: error.message });
|
|
}
|
|
}
|