77 lines
2.8 KiB
JavaScript
77 lines
2.8 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.createSchedulingRouter = createSchedulingRouter;
|
|
const express_1 = require("express");
|
|
const UniqueId_1 = require("../../../../shared/domain/UniqueId");
|
|
function createSchedulingRouter(deps) {
|
|
const router = (0, express_1.Router)();
|
|
const { createSchedule, toggleSchedule, deleteSchedule, listSchedules, schedulingService, scheduleRepo } = deps;
|
|
// GET /api/schedules
|
|
router.get('/', async (_req, res) => {
|
|
const result = await listSchedules.execute({});
|
|
if (!result.ok) {
|
|
res.status(500).json({ error: result.error });
|
|
return;
|
|
}
|
|
res.json(result.value);
|
|
});
|
|
// POST /api/schedules
|
|
router.post('/', async (req, res) => {
|
|
const body = req.body;
|
|
const result = await createSchedule.execute({
|
|
name: body.name ?? '',
|
|
url: body.url ?? '',
|
|
cronExpression: body.cronExpression ?? '',
|
|
config: body.config ?? {},
|
|
enabled: body.enabled !== false,
|
|
});
|
|
if (!result.ok) {
|
|
res.status(400).json({ error: result.error });
|
|
return;
|
|
}
|
|
// Register cron after creation
|
|
const schedule = await scheduleRepo.findById(UniqueId_1.UniqueId.from(result.value.id));
|
|
if (schedule) {
|
|
schedulingService.registerCron(schedule);
|
|
}
|
|
res.status(201).json(result.value);
|
|
});
|
|
// PATCH /api/schedules/:id/toggle
|
|
router.patch('/:id/toggle', async (req, res) => {
|
|
const id = String(req.params['id']);
|
|
const { enabled } = req.body;
|
|
if (enabled === undefined) {
|
|
res.status(400).json({ error: 'enabled is required' });
|
|
return;
|
|
}
|
|
const result = await toggleSchedule.execute({ id, enabled });
|
|
if (!result.ok) {
|
|
res.status(result.error === 'Schedule not found' ? 404 : 400).json({ error: result.error });
|
|
return;
|
|
}
|
|
// Update cron registration
|
|
const schedule = await scheduleRepo.findById(UniqueId_1.UniqueId.from(id));
|
|
if (schedule) {
|
|
if (enabled) {
|
|
schedulingService.registerCron(schedule);
|
|
}
|
|
else {
|
|
schedulingService.unregisterCron(id);
|
|
}
|
|
}
|
|
res.json(result.value);
|
|
});
|
|
// DELETE /api/schedules/:id
|
|
router.delete('/:id', async (req, res) => {
|
|
const id = String(req.params['id']);
|
|
schedulingService.unregisterCron(id);
|
|
const result = await deleteSchedule.execute({ id });
|
|
if (!result.ok) {
|
|
res.status(result.error === 'Schedule not found' ? 404 : 400).json({ error: result.error });
|
|
return;
|
|
}
|
|
res.status(204).send();
|
|
});
|
|
return router;
|
|
}
|