Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#8633) accept array on schedule.start_from #8639

Merged
merged 7 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions shared-libs/transitions/src/lib/schedules.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ module.exports = {
if (typeof startFrom === 'undefined') {
startFrom = 'reported_date';
}

const docStart = objectPath.get(doc, startFrom);
1yuv marked this conversation as resolved.
Show resolved Hide resolved
const docStart = Array.isArray(startFrom)? objectPath.coalesce(doc, startFrom): objectPath.get(doc, startFrom);
dianabarsan marked this conversation as resolved.
Show resolved Hide resolved

// if the document does not have the `start_from` property (or its
// falsey) do nothing; this will be rerun on next document change
Expand Down
126 changes: 126 additions & 0 deletions shared-libs/transitions/test/integration/schedules.js
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,132 @@ describe('functional schedules', () => {
});
});

it('schedule can be configured from single element in start_from array.', () => {
config.get.returns([{
form: 'PATR',
events: [
{
name: 'on_create',
trigger: 'assign_schedule',
params: 'group1',
bool_expr: ''
}
],
validations: [],
messages: [
{
message: [{
content: 'thanks {{contact.name}}',
locale: 'en'
}],
recipient: 'reporting_unit'
}
]
}]);
sinon.stub(utils, 'getRegistrations').resolves([]);
sinon.stub(schedules, 'getScheduleConfig').returns({
name: 'group1',
start_from: ['reported_date'],
registration_response: '',
messages: [
{
message: [{
content: 'Mustaches. Overrated or underrated?',
locale: 'en'
}],
group: 1,
offset: '12 weeks',
send_time: '',
recipient: 'reporting_unit'
}
]
});

const doc = {
reported_date: moment().toISOString(),
form: 'PATR',
from: contact.phone,
contact: contact
};

return transition.onMatch({ doc: doc }).then(complete => {
assert.equal(complete, true);
assert(doc.tasks);
assert.equal(doc.tasks && doc.tasks.length, 1);
assert(doc.scheduled_tasks);
assert.equal(doc.scheduled_tasks && doc.scheduled_tasks.length, 1);
1yuv marked this conversation as resolved.
Show resolved Hide resolved

testMessage(
getMessage(doc, 0),
'+1234',
'thanks Julie'
);
});
});

it('schedule is configured from second field, if first is not available.', () => {
config.get.returns([{
form: 'PATR',
events: [
{
name: 'on_create',
trigger: 'assign_schedule',
params: 'group1',
bool_expr: ''
}
],
validations: [],
messages: [
{
message: [{
content: 'thanks {{contact.name}}',
locale: 'en'
}],
recipient: 'reporting_unit'
}
]
}]);
sinon.stub(utils, 'getRegistrations').resolves([]);
sinon.stub(schedules, 'getScheduleConfig').returns({
name: 'group1',
start_from: ['reported_date', 'dob'],
registration_response: '',
messages: [
{
message: [{
content: 'Mustaches. Overrated or underrated?',
locale: 'en'
}],
group: 1,
offset: '12 weeks',
send_time: '',
recipient: 'reporting_unit'
}
]
});

const doc = {
dob: moment().toISOString(),
form: 'PATR',
from: contact.phone,
contact: contact
};

return transition.onMatch({ doc: doc }).then(complete => {
assert.equal(complete, true);
assert(doc.tasks);
assert.equal(doc.tasks && doc.tasks.length, 1);
assert(doc.scheduled_tasks);
assert.equal(doc.scheduled_tasks && doc.scheduled_tasks.length, 1);

testMessage(
getMessage(doc, 0),
'+1234',
'thanks Julie'
);
});
});

const testMessage = (message, expectedTo, expectedContent) => {
assert(/^[a-z0-9-]*$/.test(message.uuid));
assert.equal(message.to, expectedTo);
Expand Down