fase(19): scheduling module refactor
This commit is contained in:
76
dist/modules/scheduling/infrastructure/http/SchedulingController.js
vendored
Normal file
76
dist/modules/scheduling/infrastructure/http/SchedulingController.js
vendored
Normal file
@@ -0,0 +1,76 @@
|
||||
"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;
|
||||
}
|
||||
74
dist/modules/scheduling/infrastructure/repositories/KyselyScheduleRepository.js
vendored
Normal file
74
dist/modules/scheduling/infrastructure/repositories/KyselyScheduleRepository.js
vendored
Normal file
@@ -0,0 +1,74 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.KyselyScheduleRepository = void 0;
|
||||
const Schedule_1 = require("../../domain/entities/Schedule");
|
||||
class KyselyScheduleRepository {
|
||||
constructor(db) {
|
||||
this.db = db;
|
||||
}
|
||||
async save(schedule) {
|
||||
await this.db
|
||||
.insertInto('schedules')
|
||||
.values({
|
||||
id: schedule.id.toString(),
|
||||
name: schedule.name,
|
||||
url: schedule.url,
|
||||
config_json: JSON.stringify(schedule.config),
|
||||
cron_expression: schedule.cronExpression.value,
|
||||
enabled: schedule.enabled ? 1 : 0,
|
||||
last_run_at: schedule.lastRunAt,
|
||||
next_run_at: schedule.nextRunAt,
|
||||
created_at: schedule.createdAt,
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
async findById(id) {
|
||||
const row = await this.db
|
||||
.selectFrom('schedules')
|
||||
.selectAll()
|
||||
.where('id', '=', id.toString())
|
||||
.executeTakeFirst();
|
||||
if (!row)
|
||||
return null;
|
||||
return this.toEntity(row);
|
||||
}
|
||||
async findAll(enabledOnly = false) {
|
||||
let query = this.db.selectFrom('schedules').selectAll().orderBy('created_at', 'desc');
|
||||
if (enabledOnly) {
|
||||
query = query.where('enabled', '=', 1);
|
||||
}
|
||||
const rows = await query.execute();
|
||||
return rows.map((r) => this.toEntity(r));
|
||||
}
|
||||
async update(schedule) {
|
||||
await this.db
|
||||
.updateTable('schedules')
|
||||
.set({
|
||||
name: schedule.name,
|
||||
url: schedule.url,
|
||||
config_json: JSON.stringify(schedule.config),
|
||||
cron_expression: schedule.cronExpression.value,
|
||||
enabled: schedule.enabled ? 1 : 0,
|
||||
last_run_at: schedule.lastRunAt,
|
||||
next_run_at: schedule.nextRunAt,
|
||||
})
|
||||
.where('id', '=', schedule.id.toString())
|
||||
.execute();
|
||||
}
|
||||
async delete(id) {
|
||||
await this.db.deleteFrom('schedules').where('id', '=', id.toString()).execute();
|
||||
}
|
||||
toEntity(row) {
|
||||
return Schedule_1.Schedule.reconstitute(row.id, {
|
||||
name: row.name,
|
||||
url: row.url,
|
||||
cronExpression: row.cron_expression,
|
||||
config: JSON.parse(row.config_json),
|
||||
enabled: row.enabled === 1,
|
||||
lastRunAt: row.last_run_at,
|
||||
nextRunAt: row.next_run_at,
|
||||
createdAt: row.created_at,
|
||||
});
|
||||
}
|
||||
}
|
||||
exports.KyselyScheduleRepository = KyselyScheduleRepository;
|
||||
Reference in New Issue
Block a user