Skip to content

Commit

Permalink
Add executions modes to activateFlowTriggerById (#252)
Browse files Browse the repository at this point in the history
* Add executions modes to activateFlowTriggerById
  • Loading branch information
viktor-liablin authored Jun 24, 2024
1 parent 8fca513 commit e107975
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
4 changes: 3 additions & 1 deletion backendless.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -612,10 +612,12 @@ declare module Backendless {
* @type: Function
*/

type Execution = 'activateAny' | 'activateAll' | string

function activateFlow(flowName: string, initialData?: object): Promise<void>
function activateFlowById(flowId: string, initialData?: object): Promise<void>
function activateFlowTrigger(flowName: string, triggerName: string, data?: object): Promise<void>
function activateFlowTriggerById(flowId: string, triggerId: string, data?: object, executionId?: string): Promise<void>
function activateFlowTriggerById(flowId: string, triggerId: string, data?: object, execution?: Execution): Promise<void>
}

/**
Expand Down
20 changes: 15 additions & 5 deletions src/automations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export default class Automations {
})
}

async activateFlowTriggerById(flowId, triggerId, data, executionId) {
async activateFlowTriggerById(flowId, triggerId, data, execution) {
if (!flowId || typeof flowId !== 'string') {
throw new Error('The "flowId" argument must be provided and must be a string.')
}
Expand All @@ -71,14 +71,24 @@ export default class Automations {
throw new Error('The "data" argument must be an object.')
}

if (executionId !== undefined && (typeof executionId !== 'string' || !executionId)) {
throw new Error('The "executionId" argument must be a non-empty string.')
if (execution !== undefined && (typeof execution !== 'string' || !execution)) {
throw new Error(
// eslint-disable-next-line
'The "execution" argument must be a non-empty string and must be one of this values: "activateAny", "activateAll" or Execution ID.'
)
}

const query = {}

if (executionId) {
query.executionId = executionId
switch (execution) {
case 'activateAny':
query.activateAny = true
break
case 'activateAll':
query.activateAll = true
break
default:
query.executionId = execution
}

return this.app.request.post({
Expand Down
4 changes: 2 additions & 2 deletions test/tsd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1529,13 +1529,13 @@ function testAutomations() {
const flowId: string = 'id';
const triggerName: string = 'str';
const triggerId: string = 'id';
const executionId: string = 'id';
const execution: string = 'id';
let promiseObject: Promise<void>;

promiseObject = Backendless.Automations.activateFlow(flowName, obj);
promiseObject = Backendless.Automations.activateFlowById(flowId, obj);
promiseObject = Backendless.Automations.activateFlowTrigger(flowName, triggerName, obj);
promiseObject = Backendless.Automations.activateFlowTriggerById(flowId, triggerId, obj, executionId);
promiseObject = Backendless.Automations.activateFlowTriggerById(flowId, triggerId, obj, execution);
}

function testMessaging() {
Expand Down
25 changes: 24 additions & 1 deletion test/unit/specs/automations/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ describe('<Automations> Basic', function() {
const FLOW_NAME = 'FlowName'
const FLOW_ID = 'FlowID'
const EXECUTION_ID = 'ExecutionID'
const EXECUTION_ANY = 'activateAny'
const EXECUTION_ALL = 'activateAll'
const TRIGGER_NAME = 'TriggerName'
const TRIGGER_ID = 'TriggerID'

Expand Down Expand Up @@ -200,10 +202,14 @@ describe('<Automations> Basic', function() {
const req1 = prepareMockRequest()
const req2 = prepareMockRequest()
const req3 = prepareMockRequest()
const req4 = prepareMockRequest()
const req5 = prepareMockRequest()

await Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID)
await Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, { name: 'Nick' })
await Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, { name: 'Nick' }, EXECUTION_ID)
await Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, { name: 'Nick' }, EXECUTION_ANY)
await Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, { name: 'Nick' }, EXECUTION_ALL)

expect(req1).to.deep.include({
method: 'POST',
Expand All @@ -227,6 +233,22 @@ describe('<Automations> Basic', function() {
}
})

expect(req4).to.deep.include({
method: 'POST',
path : `${APP_PATH}/automation/flow/${ FLOW_ID }/trigger/${ TRIGGER_ID }/activate?activateAny=true`,
body : {
name: 'Nick',
}
})

expect(req5).to.deep.include({
method: 'POST',
path : `${APP_PATH}/automation/flow/${ FLOW_ID }/trigger/${ TRIGGER_ID }/activate?activateAll=true`,
body : {
name: 'Nick',
}
})

})

it('fails when flow id is invalid', async () => {
Expand Down Expand Up @@ -276,7 +298,8 @@ describe('<Automations> Basic', function() {
})

it('fails when execution id is invalid', async () => {
const errorMsg = 'The "executionId" argument must be a non-empty string.'
// eslint-disable-next-line
const errorMsg = 'The "execution" argument must be a non-empty string and must be one of this values: "activateAny", "activateAll" or Execution ID.'

await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, {}, null)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, {}, true)).to.eventually.be.rejectedWith(errorMsg)
Expand Down

0 comments on commit e107975

Please sign in to comment.