fase(19): scheduling module refactor

This commit is contained in:
debian
2026-03-08 05:49:00 -04:00
parent 1cf597fee1
commit 49e76c92b1
39 changed files with 1546 additions and 24 deletions

View File

@@ -0,0 +1,96 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Schedule = exports.CreateScheduleSchema = void 0;
const AggregateRoot_1 = require("../../../../shared/domain/AggregateRoot");
const UniqueId_1 = require("../../../../shared/domain/UniqueId");
const Result_1 = require("../../../../shared/domain/Result");
const CronExpression_1 = require("../value-objects/CronExpression");
const ScheduleCreated_1 = require("../events/ScheduleCreated");
const ScheduleToggled_1 = require("../events/ScheduleToggled");
const zod_1 = require("zod");
exports.CreateScheduleSchema = zod_1.z.object({
name: zod_1.z.string().min(1).max(100),
url: zod_1.z.string().url(),
cronExpression: zod_1.z.string().min(1),
config: zod_1.z.record(zod_1.z.string(), zod_1.z.unknown()).optional().default({}),
enabled: zod_1.z.boolean().optional().default(true),
});
class Schedule extends AggregateRoot_1.AggregateRoot {
get name() { return this.props.name; }
get url() { return this.props.url; }
get cronExpression() { return this.props.cronExpression; }
get config() { return this.props.config; }
get enabled() { return this.props.enabled; }
get lastRunAt() { return this.props.lastRunAt; }
get nextRunAt() { return this.props.nextRunAt; }
get createdAt() { return this.props.createdAt; }
static create(input) {
const parsed = exports.CreateScheduleSchema.safeParse(input);
if (!parsed.success) {
return (0, Result_1.Err)(parsed.error.issues.map((e) => e.message).join(', '));
}
const cronResult = CronExpression_1.CronExpression.create(parsed.data.cronExpression);
if (!cronResult.ok) {
return (0, Result_1.Err)(cronResult.error);
}
const id = UniqueId_1.UniqueId.create();
const now = Date.now();
const schedule = new Schedule({
name: parsed.data.name,
url: parsed.data.url,
cronExpression: cronResult.value,
config: parsed.data.config,
enabled: parsed.data.enabled,
lastRunAt: null,
nextRunAt: now + 60000, // approximate next run
createdAt: now,
}, id);
schedule.addDomainEvent(new ScheduleCreated_1.ScheduleCreated(id.toString(), {
name: parsed.data.name,
url: parsed.data.url,
cronExpression: parsed.data.cronExpression,
}));
return (0, Result_1.Ok)(schedule);
}
static reconstitute(id, props) {
const cronResult = CronExpression_1.CronExpression.create(props.cronExpression);
// If stored cron is invalid, store raw value — shouldn't happen in practice
const cronExpr = (0, Result_1.isOk)(cronResult)
? cronResult.value
: { props: { value: props.cronExpression }, value: props.cronExpression };
return new Schedule({
name: props.name,
url: props.url,
cronExpression: cronExpr,
config: props.config,
enabled: props.enabled,
lastRunAt: props.lastRunAt,
nextRunAt: props.nextRunAt,
createdAt: props.createdAt,
}, UniqueId_1.UniqueId.from(id));
}
toggle(enabled) {
this.props.enabled = enabled;
this.addDomainEvent(new ScheduleToggled_1.ScheduleToggled(this.id.toString(), { enabled }));
}
markFired(now) {
this.props.lastRunAt = now;
this.props.nextRunAt = now + 60000; // approximate
}
update(fields) {
if (fields.cronExpression !== undefined) {
const cronResult = CronExpression_1.CronExpression.create(fields.cronExpression);
if (!cronResult.ok)
return (0, Result_1.Err)(cronResult.error);
this.props.cronExpression = cronResult.value;
}
if (fields.name !== undefined)
this.props.name = fields.name;
if (fields.url !== undefined)
this.props.url = fields.url;
if (fields.config !== undefined)
this.props.config = fields.config;
return (0, Result_1.Ok)(undefined);
}
}
exports.Schedule = Schedule;

View File

@@ -0,0 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScheduleCreated = void 0;
const crypto_1 = require("crypto");
class ScheduleCreated {
constructor(aggregateId, payload) {
this.aggregateId = aggregateId;
this.payload = payload;
this.eventId = (0, crypto_1.randomUUID)();
this.eventName = 'scheduling.schedule_created';
this.occurredOn = new Date();
}
}
exports.ScheduleCreated = ScheduleCreated;

View File

@@ -0,0 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScheduleFired = void 0;
const crypto_1 = require("crypto");
class ScheduleFired {
constructor(aggregateId, payload) {
this.aggregateId = aggregateId;
this.payload = payload;
this.eventId = (0, crypto_1.randomUUID)();
this.eventName = 'scheduling.schedule_fired';
this.occurredOn = new Date();
}
}
exports.ScheduleFired = ScheduleFired;

View File

@@ -0,0 +1,14 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.ScheduleToggled = void 0;
const crypto_1 = require("crypto");
class ScheduleToggled {
constructor(aggregateId, payload) {
this.aggregateId = aggregateId;
this.payload = payload;
this.eventId = (0, crypto_1.randomUUID)();
this.eventName = 'scheduling.schedule_toggled';
this.occurredOn = new Date();
}
}
exports.ScheduleToggled = ScheduleToggled;

View File

@@ -0,0 +1,2 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

View File

@@ -0,0 +1,54 @@
"use strict";
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.CronExpression = void 0;
const ValueObject_1 = require("../../../../shared/domain/ValueObject");
const Result_1 = require("../../../../shared/domain/Result");
const cron = __importStar(require("node-cron"));
class CronExpression extends ValueObject_1.ValueObject {
get value() {
return this.props.value;
}
static create(expression) {
if (!expression || expression.trim().length === 0) {
return (0, Result_1.Err)('Cron expression cannot be empty');
}
if (!cron.validate(expression)) {
return (0, Result_1.Err)(`Invalid cron expression: "${expression}"`);
}
return (0, Result_1.Ok)(new CronExpression({ value: expression }));
}
}
exports.CronExpression = CronExpression;