Skip to content

Commit

Permalink
Events as a function
Browse files Browse the repository at this point in the history
  • Loading branch information
kennsippell committed Oct 12, 2023
1 parent f54486e commit 00de5c5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
13 changes: 8 additions & 5 deletions src/lib/validate-declarative-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,14 @@ const TaskSchema = joi.array().items(
'should define property "resolvedIf" as: function(contact, report) { ... }.'
)
),
events: joi.alternatives().conditional('events', {
is: joi.array().length(1),
then: joi.array().items(EventSchema('optional')).min(1).required(),
otherwise: joi.array().items(EventSchema('required')).unique('id').required(),
}),
events: joi.alternatives().try(
joi.function(),
joi.alternatives().conditional('events', {
is: joi.array().length(1),
then: joi.array().items(EventSchema('optional')).min(1),
otherwise: joi.array().items(EventSchema('required')).unique('id'),
})
).required(),
priority: joi.alternatives().try(
joi.object({
level: joi.string().valid('high', 'medium').optional(),
Expand Down
15 changes: 13 additions & 2 deletions src/nools/task-emitter.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,19 @@ function emitTasks(taskDefinition, Utils, Task, emit, c, r) {

function emitForEvents(scheduledTaskIdx) {
var i, dueDate = null, event, priority, task;
for (i = 0; i < taskDefinition.events.length; i++) {
event = taskDefinition.events[i];

var events;
if (typeof taskDefinition.events === 'function') {
events = taskDefinition.events(c, r);
if (!Array.isArray(events)) {
throw Error('events did not return an array');
}
} else {
events = taskDefinition.events;
}

for (i = 0; i < events.length; i++) {
event = events[i];

if (event.dueDate) {
dueDate = event.dueDate(event, c, r, scheduledTaskIdx);
Expand Down
7 changes: 7 additions & 0 deletions test/lib/validate-declarative-schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,13 @@ describe('validate-declarative-schema', () => {
]);
});

it('events can be function', () => {
const task = buildTaskWithAction('contact');
task.events = () => [];
const actual = validate([task], TaskSchema);
expect(actual).to.be.empty;
});

it('array.unique internal', () => {
const schema = joi.array().items(joi.object({
event: joi.array().items(joi.object()).unique('id'),
Expand Down

0 comments on commit 00de5c5

Please sign in to comment.