fase(19): scheduling module refactor
This commit is contained in:
96
dist/modules/scheduling/domain/entities/Schedule.js
vendored
Normal file
96
dist/modules/scheduling/domain/entities/Schedule.js
vendored
Normal 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;
|
||||
Reference in New Issue
Block a user