Initial
This commit is contained in:
55
server/controllers/jobOutputController.js
Normal file
55
server/controllers/jobOutputController.js
Normal file
@@ -0,0 +1,55 @@
|
||||
import fs from 'node:fs';
|
||||
import {
|
||||
getJobOutputFile,
|
||||
listJobOutputs,
|
||||
listJobOutputsForJob
|
||||
} from '../models/jobOutputModel.js';
|
||||
import { getJob } from '../models/jobModel.js';
|
||||
import { generateJobOutputs } from '../services/jobOutputService.js';
|
||||
|
||||
function isAdmin(req) {
|
||||
return req.user.role === 'admin';
|
||||
}
|
||||
|
||||
export function index(req, res) {
|
||||
res.json(listJobOutputs(req.user.id, isAdmin(req)));
|
||||
}
|
||||
|
||||
export function byJob(req, res) {
|
||||
res.json(listJobOutputsForJob(req.params.jobId, req.user.id, isAdmin(req)));
|
||||
}
|
||||
|
||||
export async function generate(req, res) {
|
||||
const job = getJob(req.params.jobId, req.user.id, isAdmin(req));
|
||||
if (!job) return res.status(404).json({ error: 'Job not found' });
|
||||
await generateJobOutputs(req.params.jobId);
|
||||
const outputs = listJobOutputsForJob(req.params.jobId, req.user.id, isAdmin(req));
|
||||
if (!outputs.length) return res.status(409).json({ error: 'Job is not ready for output generation' });
|
||||
res.json(outputs);
|
||||
}
|
||||
|
||||
export function content(req, res) {
|
||||
const result = getJobOutputFile(req.params.id, req.user.id, isAdmin(req));
|
||||
if (!result) return res.status(404).json({ error: 'Job output not found' });
|
||||
if (result.output.mimeType === 'application/json') {
|
||||
return res.json({ output: result.output, table: JSON.parse(fs.readFileSync(result.filePath, 'utf8')) });
|
||||
}
|
||||
if (result.output.mimeType.startsWith('text/')) {
|
||||
return res.json({ output: result.output, text: fs.readFileSync(result.filePath, 'utf8') });
|
||||
}
|
||||
return res.json({ output: result.output });
|
||||
}
|
||||
|
||||
export function view(req, res) {
|
||||
const result = getJobOutputFile(req.params.id, req.user.id, isAdmin(req));
|
||||
if (!result) return res.status(404).json({ error: 'Job output not found' });
|
||||
res.type(result.output.mimeType || 'application/octet-stream');
|
||||
res.setHeader('Content-Disposition', `inline; filename="${encodeURIComponent(result.output.name)}"`);
|
||||
res.sendFile(result.filePath);
|
||||
}
|
||||
|
||||
export function download(req, res) {
|
||||
const result = getJobOutputFile(req.params.id, req.user.id, isAdmin(req));
|
||||
if (!result) return res.status(404).json({ error: 'Job output not found' });
|
||||
res.download(result.filePath, result.output.name);
|
||||
}
|
||||
80
server/controllers/runPlanScheduleController.js
Normal file
80
server/controllers/runPlanScheduleController.js
Normal file
@@ -0,0 +1,80 @@
|
||||
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 });
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user