Skip to content

Commit aed059a

Browse files
Automations. Add activateFlowById and activateFlowTriggerById (#247)
* Automations. Add activateFlowById and activateFlowTriggerById
1 parent 1745d50 commit aed059a

File tree

4 files changed

+170
-4
lines changed

4 files changed

+170
-4
lines changed

backendless.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,9 @@ declare module Backendless {
610610
*/
611611

612612
function activateFlow(flowName: string, initialData?: object): Promise<void>
613+
function activateFlowById(flowId: string, initialData?: object): Promise<void>
613614
function activateFlowTrigger(flowName: string, triggerName: string, data?: object): Promise<void>
615+
function activateFlowTriggerById(flowId: string, triggerId: string, data?: object): Promise<void>
614616
}
615617

616618
/**

src/automations/index.js

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default class Automations {
1111
}
1212

1313
if (initialData !== undefined && !Utils.isObject(initialData)) {
14-
throw new Error('The "initialData" argument must be an object with an arbitrary structure.')
14+
throw new Error('The "initialData" argument must be an object.')
1515
}
1616

1717
return this.app.request.post({
@@ -23,6 +23,21 @@ export default class Automations {
2323
})
2424
}
2525

26+
async activateFlowById(flowId, initialData) {
27+
if (!flowId || typeof flowId !== 'string') {
28+
throw new Error('The "flowId" argument must be provided and must be a string.')
29+
}
30+
31+
if (initialData !== undefined && !Utils.isObject(initialData)) {
32+
throw new Error('The "initialData" argument must be an object.')
33+
}
34+
35+
return this.app.request.post({
36+
url : `${this.app.urls.automationFlow()}/${flowId}/activate`,
37+
data: initialData || {}
38+
})
39+
}
40+
2641
async activateFlowTrigger(flowName, triggerName, data) {
2742
if (!flowName || typeof flowName !== 'string') {
2843
throw new Error('The "flowName" argument must be provided and must be a string.')
@@ -33,7 +48,7 @@ export default class Automations {
3348
}
3449

3550
if (data !== undefined && !Utils.isObject(data)) {
36-
throw new Error('The "data" argument must be an object with an arbitrary structure.')
51+
throw new Error('The "data" argument must be an object.')
3752
}
3853

3954
return this.app.request.post({
@@ -42,4 +57,23 @@ export default class Automations {
4257
data : data || {},
4358
})
4459
}
60+
61+
async activateFlowTriggerById(flowId, triggerId, data) {
62+
if (!flowId || typeof flowId !== 'string') {
63+
throw new Error('The "flowId" argument must be provided and must be a string.')
64+
}
65+
66+
if (!triggerId || typeof triggerId !== 'string') {
67+
throw new Error('The "triggerId" argument must be provided and must be a string.')
68+
}
69+
70+
if (data !== undefined && !Utils.isObject(data)) {
71+
throw new Error('The "data" argument must be an object.')
72+
}
73+
74+
return this.app.request.post({
75+
url : `${this.app.urls.automationFlow()}/${flowId}/trigger/${triggerId}/activate`,
76+
data: data || {},
77+
})
78+
}
4579
}

test/tsd.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1526,11 +1526,15 @@ function testAutomations() {
15261526
type TestObjType = { [x: string]: number }
15271527
const obj: TestObjType = {x: 1, y: 2};
15281528
const flowName: string = 'str';
1529+
const flowId: string = 'id';
15291530
const triggerName: string = 'str';
1531+
const triggerId: string = 'id';
15301532
let promiseObject: Promise<void>;
15311533

15321534
promiseObject = Backendless.Automations.activateFlow(flowName, obj);
1535+
promiseObject = Backendless.Automations.activateFlowById(flowId, obj);
15331536
promiseObject = Backendless.Automations.activateFlowTrigger(flowName, triggerName, obj);
1537+
promiseObject = Backendless.Automations.activateFlowTriggerById(flowId, triggerId, obj);
15341538
}
15351539

15361540
function testMessaging() {

test/unit/specs/automations/basic.js

Lines changed: 128 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ describe('<Automations> Basic', function() {
77
forTest(this)
88

99
const FLOW_NAME = 'FlowName'
10+
const FLOW_ID = 'FlowID'
1011
const TRIGGER_NAME = 'TriggerName'
12+
const TRIGGER_ID = 'TriggerID'
1113

1214
describe('activate flow by name', function() {
1315
it('success', async () => {
@@ -68,6 +70,60 @@ describe('<Automations> Basic', function() {
6870
})
6971
})
7072

73+
describe('activate flow by id', function() {
74+
it('success', async () => {
75+
const req1 = prepareMockRequest()
76+
const req2 = prepareMockRequest()
77+
await Backendless.Automations.activateFlowById(FLOW_ID)
78+
await Backendless.Automations.activateFlowById(FLOW_ID, { name: 'Nick' })
79+
80+
expect(req1).to.deep.include({
81+
method: 'POST',
82+
path : `${APP_PATH}/automation/flow/${FLOW_ID}/activate`,
83+
body : {}
84+
})
85+
86+
expect(req2).to.deep.include({
87+
method: 'POST',
88+
path : `${APP_PATH}/automation/flow/${FLOW_ID}/activate`,
89+
body : {
90+
name: 'Nick',
91+
}
92+
})
93+
94+
})
95+
96+
it('fails when flow id is invalid', async () => {
97+
const errorMsg = 'The "flowId" argument must be provided and must be a string.'
98+
99+
await expect(Backendless.Automations.activateFlowById()).to.eventually.be.rejectedWith(errorMsg)
100+
await expect(Backendless.Automations.activateFlowById(undefined)).to.eventually.be.rejectedWith(errorMsg)
101+
await expect(Backendless.Automations.activateFlowById(null)).to.eventually.be.rejectedWith(errorMsg)
102+
await expect(Backendless.Automations.activateFlowById(true)).to.eventually.be.rejectedWith(errorMsg)
103+
await expect(Backendless.Automations.activateFlowById(false)).to.eventually.be.rejectedWith(errorMsg)
104+
await expect(Backendless.Automations.activateFlowById(0)).to.eventually.be.rejectedWith(errorMsg)
105+
await expect(Backendless.Automations.activateFlowById(123)).to.eventually.be.rejectedWith(errorMsg)
106+
await expect(Backendless.Automations.activateFlowById('')).to.eventually.be.rejectedWith(errorMsg)
107+
await expect(Backendless.Automations.activateFlowById({})).to.eventually.be.rejectedWith(errorMsg)
108+
await expect(Backendless.Automations.activateFlowById([])).to.eventually.be.rejectedWith(errorMsg)
109+
await expect(Backendless.Automations.activateFlowById(() => ({}))).to.eventually.be.rejectedWith(errorMsg)
110+
})
111+
112+
it('fails when initial data is invalid', async () => {
113+
const errorMsg = 'The "initialData" argument must be an object with an arbitrary structure.'
114+
115+
await expect(Backendless.Automations.activateFlowById(FLOW_ID, null)).to.eventually.be.rejectedWith(errorMsg)
116+
await expect(Backendless.Automations.activateFlowById(FLOW_ID, true)).to.eventually.be.rejectedWith(errorMsg)
117+
await expect(Backendless.Automations.activateFlowById(FLOW_ID, false)).to.eventually.be.rejectedWith(errorMsg)
118+
await expect(Backendless.Automations.activateFlowById(FLOW_ID, 0)).to.eventually.be.rejectedWith(errorMsg)
119+
await expect(Backendless.Automations.activateFlowById(FLOW_ID, 123)).to.eventually.be.rejectedWith(errorMsg)
120+
await expect(Backendless.Automations.activateFlowById(FLOW_ID, 'asd')).to.eventually.be.rejectedWith(errorMsg)
121+
await expect(Backendless.Automations.activateFlowById(FLOW_ID, '')).to.eventually.be.rejectedWith(errorMsg)
122+
await expect(Backendless.Automations.activateFlowById(FLOW_ID, [])).to.eventually.be.rejectedWith(errorMsg)
123+
await expect(Backendless.Automations.activateFlowById(FLOW_ID, () => ({}))).to.eventually.be.rejectedWith(errorMsg)
124+
})
125+
})
126+
71127
describe('activate flow trigger', function() {
72128
it('success', async () => {
73129
const req1 = prepareMockRequest()
@@ -110,9 +166,9 @@ describe('<Automations> Basic', function() {
110166
it('fails when trigger name is invalid', async () => {
111167
const errorMsg = 'The "triggerName" argument must be provided and must be a string.'
112168

113-
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, )).to.eventually.be.rejectedWith(errorMsg)
169+
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME,)).to.eventually.be.rejectedWith(errorMsg)
114170
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, undefined)).to.eventually.be.rejectedWith(errorMsg)
115-
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME,null)).to.eventually.be.rejectedWith(errorMsg)
171+
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, null)).to.eventually.be.rejectedWith(errorMsg)
116172
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, true)).to.eventually.be.rejectedWith(errorMsg)
117173
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, false)).to.eventually.be.rejectedWith(errorMsg)
118174
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, 0)).to.eventually.be.rejectedWith(errorMsg)
@@ -138,4 +194,74 @@ describe('<Automations> Basic', function() {
138194
})
139195
})
140196

197+
describe('activate flow trigger by id', function() {
198+
it('success', async () => {
199+
const req1 = prepareMockRequest()
200+
const req2 = prepareMockRequest()
201+
await Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID)
202+
await Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, { name: 'Nick' })
203+
204+
expect(req1).to.deep.include({
205+
method: 'POST',
206+
path : `${APP_PATH}/automation/flow/${ FLOW_ID }/trigger/${ TRIGGER_ID }/activate`,
207+
body : {},
208+
})
209+
210+
expect(req2).to.deep.include({
211+
method: 'POST',
212+
path : `${APP_PATH}/automation/flow/${ FLOW_ID }/trigger/${ TRIGGER_ID }/activate`,
213+
body : {
214+
name: 'Nick',
215+
}
216+
})
217+
218+
})
219+
220+
it('fails when flow id is invalid', async () => {
221+
const errorMsg = 'The "flowId" argument must be provided and must be a string.'
222+
223+
await expect(Backendless.Automations.activateFlowTriggerById()).to.eventually.be.rejectedWith(errorMsg)
224+
await expect(Backendless.Automations.activateFlowTriggerById(undefined)).to.eventually.be.rejectedWith(errorMsg)
225+
await expect(Backendless.Automations.activateFlowTriggerById(null)).to.eventually.be.rejectedWith(errorMsg)
226+
await expect(Backendless.Automations.activateFlowTriggerById(true)).to.eventually.be.rejectedWith(errorMsg)
227+
await expect(Backendless.Automations.activateFlowTriggerById(false)).to.eventually.be.rejectedWith(errorMsg)
228+
await expect(Backendless.Automations.activateFlowTriggerById(0)).to.eventually.be.rejectedWith(errorMsg)
229+
await expect(Backendless.Automations.activateFlowTriggerById(123)).to.eventually.be.rejectedWith(errorMsg)
230+
await expect(Backendless.Automations.activateFlowTriggerById('')).to.eventually.be.rejectedWith(errorMsg)
231+
await expect(Backendless.Automations.activateFlowTriggerById({})).to.eventually.be.rejectedWith(errorMsg)
232+
await expect(Backendless.Automations.activateFlowTriggerById([])).to.eventually.be.rejectedWith(errorMsg)
233+
await expect(Backendless.Automations.activateFlowTriggerById(() => ({}))).to.eventually.be.rejectedWith(errorMsg)
234+
})
235+
236+
it('fails when trigger id is invalid', async () => {
237+
const errorMsg = 'The "triggerId" argument must be provided and must be a string.'
238+
239+
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID,)).to.eventually.be.rejectedWith(errorMsg)
240+
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, undefined)).to.eventually.be.rejectedWith(errorMsg)
241+
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, null)).to.eventually.be.rejectedWith(errorMsg)
242+
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, true)).to.eventually.be.rejectedWith(errorMsg)
243+
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, false)).to.eventually.be.rejectedWith(errorMsg)
244+
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, 0)).to.eventually.be.rejectedWith(errorMsg)
245+
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, 123)).to.eventually.be.rejectedWith(errorMsg)
246+
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, '')).to.eventually.be.rejectedWith(errorMsg)
247+
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, {})).to.eventually.be.rejectedWith(errorMsg)
248+
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, [])).to.eventually.be.rejectedWith(errorMsg)
249+
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, () => ({}))).to.eventually.be.rejectedWith(errorMsg)
250+
})
251+
252+
it('fails when data is invalid', async () => {
253+
const errorMsg = 'The "data" argument must be an object with an arbitrary structure.'
254+
255+
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, null)).to.eventually.be.rejectedWith(errorMsg)
256+
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, true)).to.eventually.be.rejectedWith(errorMsg)
257+
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, false)).to.eventually.be.rejectedWith(errorMsg)
258+
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, 0)).to.eventually.be.rejectedWith(errorMsg)
259+
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, 123)).to.eventually.be.rejectedWith(errorMsg)
260+
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, 'asd')).to.eventually.be.rejectedWith(errorMsg)
261+
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, '')).to.eventually.be.rejectedWith(errorMsg)
262+
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, [])).to.eventually.be.rejectedWith(errorMsg)
263+
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, () => ({}))).to.eventually.be.rejectedWith(errorMsg)
264+
})
265+
})
266+
141267
})

0 commit comments

Comments
 (0)