Skip to content

Commit

Permalink
BKNDLSS-34294 - JS-SDK. Add method to run a specific automation flow
Browse files Browse the repository at this point in the history
  • Loading branch information
viktor-liablin committed Dec 6, 2023
1 parent 21f67c5 commit a2034fa
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 65 deletions.
2 changes: 1 addition & 1 deletion backendless.d.ts
Expand Up @@ -609,7 +609,7 @@ declare module Backendless {
* @type: Function
*/

function activateFlowByName(flowName: string, initialData?: object): Promise<void>
function activateFlow(flowName: string, initialData?: object): Promise<void>
}

/**
Expand Down
15 changes: 8 additions & 7 deletions src/automations/index.js
Expand Up @@ -5,7 +5,7 @@ export default class Automations {
this.app = app
}

async activateFlowByName(flowName, initialData) {
async activateFlow(flowName, initialData) {
if (!flowName || typeof flowName !== 'string') {
throw new Error('The "flowName" argument must be provided and must be a string.')
}
Expand All @@ -23,21 +23,22 @@ export default class Automations {
})
}

async activateFlowTrigger(flowId, triggerId, data) {
if (!flowId || typeof flowId !== 'string') {
throw new Error('The "flowId" argument must be provided and must be a string.')
async activateFlowTrigger(flowName, triggerName, data) {
if (!flowName || typeof flowName !== 'string') {
throw new Error('The "flowName" argument must be provided and must be a string.')
}

if (!triggerId || typeof triggerId !== 'string') {
throw new Error('The "triggerId" argument must be provided and must be a string.')
if (!triggerName || typeof triggerName !== 'string') {
throw new Error('The "triggerName" argument must be provided and must be a string.')
}

if (data !== undefined && !Utils.isObject(data)) {
throw new Error('The "data" argument must be an object with an arbitrary structure.')
}

return this.app.request.post({
url : `${this.app.urls.automationFlowTrigger(flowId, triggerId)}/activate`,
url : `${this.app.urls.automationFlowTrigger()}/activate-by-name`,
query: { flowName, triggerName },
data,
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/urls.js
Expand Up @@ -17,8 +17,8 @@ export default class Urls {
return `${this.automation()}/flow`
}

automationFlowTrigger(flowId, triggerId) {
return `${this.automationFlow()}/${flowId}/trigger/${triggerId}`
automationFlowTrigger() {
return `${this.automationFlow()}/trigger`
}

//bl
Expand Down
4 changes: 3 additions & 1 deletion test/tsd.ts
Expand Up @@ -1526,9 +1526,11 @@ function testAutomations() {
type TestObjType = { [x: string]: number }
const obj: TestObjType = {x: 1, y: 2};
const flowName: string = 'str';
const triggerName: string = 'str';
let promiseObject: Promise<void>;

promiseObject = Backendless.Automations.activateFlowByName(flowName, obj);
promiseObject = Backendless.Automations.activateFlow(flowName, obj);
promiseObject = Backendless.Automations.activateFlowTrigger(flowName, triggerName, obj);
}

function testMessaging() {
Expand Down
107 changes: 53 additions & 54 deletions test/unit/specs/automations/basic.js
Expand Up @@ -7,15 +7,14 @@ describe('<Automations> Basic', function() {
forTest(this)

const FLOW_NAME = 'FlowName'
const FLOW_ID = 'FlowID'
const TRIGGER_ID = 'FlowID'
const TRIGGER_NAME = 'TriggerName'

describe('activate flow by name', function() {
it('success', async () => {
const req1 = prepareMockRequest()
const req2 = prepareMockRequest()
await Backendless.Automations.activateFlowByName(FLOW_NAME)
await Backendless.Automations.activateFlowByName(FLOW_NAME, { name: 'Nick' })
await Backendless.Automations.activateFlow(FLOW_NAME)
await Backendless.Automations.activateFlow(FLOW_NAME, { name: 'Nick' })

expect(req1).to.deep.include({
method: 'POST',
Expand All @@ -41,58 +40,58 @@ describe('<Automations> Basic', function() {
it('fails when flow name is invalid', async () => {
const errorMsg = 'The "flowName" argument must be provided and must be a string.'

await expect(Backendless.Automations.activateFlowByName()).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowByName(undefined)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowByName(null)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowByName(true)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowByName(false)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowByName(0)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowByName(123)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowByName('')).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowByName({})).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowByName([])).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowByName(() => ({}))).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlow()).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlow(undefined)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlow(null)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlow(true)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlow(false)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlow(0)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlow(123)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlow('')).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlow({})).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlow([])).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlow(() => ({}))).to.eventually.be.rejectedWith(errorMsg)
})

it('fails when initial data is invalid', async () => {
const errorMsg = 'The "initialData" argument must be an object with an arbitrary structure.'

await expect(Backendless.Automations.activateFlowByName(FLOW_NAME, null)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowByName(FLOW_NAME, true)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowByName(FLOW_NAME, false)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowByName(FLOW_NAME, 0)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowByName(FLOW_NAME, 123)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowByName(FLOW_NAME, 'asd')).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowByName(FLOW_NAME, '')).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowByName(FLOW_NAME, [])).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowByName(FLOW_NAME, () => ({}))).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlow(FLOW_NAME, null)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlow(FLOW_NAME, true)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlow(FLOW_NAME, false)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlow(FLOW_NAME, 0)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlow(FLOW_NAME, 123)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlow(FLOW_NAME, 'asd')).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlow(FLOW_NAME, '')).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlow(FLOW_NAME, [])).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlow(FLOW_NAME, () => ({}))).to.eventually.be.rejectedWith(errorMsg)
})
})

describe('activate flow trigger', function() {
it('success', async () => {
const req1 = prepareMockRequest()
const req2 = prepareMockRequest()
await Backendless.Automations.activateFlowTrigger(FLOW_ID, TRIGGER_ID)
await Backendless.Automations.activateFlowTrigger(FLOW_ID, TRIGGER_ID, { name: 'Nick' })
await Backendless.Automations.activateFlowTrigger(FLOW_NAME, TRIGGER_NAME)
await Backendless.Automations.activateFlowTrigger(FLOW_NAME, TRIGGER_NAME, { name: 'Nick' })

expect(req1).to.deep.include({
method: 'POST',
path : `${APP_PATH}/automation/flow/${FLOW_ID}/trigger/${TRIGGER_ID}/activate`,
path : `${APP_PATH}/automation/flow/trigger/activate-by-name?flowName=${FLOW_NAME}&triggerName=${TRIGGER_NAME}`,
})

expect(req2).to.deep.include({
method: 'POST',
path : `${APP_PATH}/automation/flow/${FLOW_ID}/trigger/${TRIGGER_ID}/activate`,
path : `${APP_PATH}/automation/flow/trigger/activate-by-name?flowName=${FLOW_NAME}&triggerName=${TRIGGER_NAME}`,
body : {
name: 'Nick',
}
})

})

it('fails when flow ID is invalid', async () => {
const errorMsg = 'The "flowId" argument must be provided and must be a string.'
it('fails when flow name is invalid', async () => {
const errorMsg = 'The "flowName" argument must be provided and must be a string.'

await expect(Backendless.Automations.activateFlowTrigger()).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(undefined)).to.eventually.be.rejectedWith(errorMsg)
Expand All @@ -107,34 +106,34 @@ describe('<Automations> Basic', function() {
await expect(Backendless.Automations.activateFlowTrigger(() => ({}))).to.eventually.be.rejectedWith(errorMsg)
})

it('fails when trigger ID is invalid', async () => {
const errorMsg = 'The "triggerId" argument must be provided and must be a string.'

await expect(Backendless.Automations.activateFlowTrigger(FLOW_ID, )).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_ID, undefined)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_ID,null)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_ID, true)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_ID, false)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_ID, 0)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_ID, 123)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_ID, '')).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_ID, {})).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_ID, [])).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_ID, () => ({}))).to.eventually.be.rejectedWith(errorMsg)
it('fails when trigger name is invalid', async () => {
const errorMsg = 'The "triggerName" argument must be provided and must be a string.'

await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, )).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, undefined)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME,null)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, true)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, false)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, 0)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, 123)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, '')).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, {})).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, [])).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, () => ({}))).to.eventually.be.rejectedWith(errorMsg)
})

it('fails when initial data is invalid', async () => {
it('fails when data is invalid', async () => {
const errorMsg = 'The "data" argument must be an object with an arbitrary structure.'

await expect(Backendless.Automations.activateFlowTrigger(FLOW_ID, TRIGGER_ID, null)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_ID, TRIGGER_ID, true)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_ID, TRIGGER_ID, false)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_ID, TRIGGER_ID, 0)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_ID, TRIGGER_ID, 123)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_ID, TRIGGER_ID, 'asd')).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_ID, TRIGGER_ID, '')).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_ID, TRIGGER_ID, [])).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_ID, TRIGGER_ID, () => ({}))).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, TRIGGER_NAME, null)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, TRIGGER_NAME, true)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, TRIGGER_NAME, false)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, TRIGGER_NAME, 0)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, TRIGGER_NAME, 123)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, TRIGGER_NAME, 'asd')).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, TRIGGER_NAME, '')).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, TRIGGER_NAME, [])).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTrigger(FLOW_NAME, TRIGGER_NAME, () => ({}))).to.eventually.be.rejectedWith(errorMsg)
})
})

Expand Down

0 comments on commit a2034fa

Please sign in to comment.