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

Automations. Add activateFlowById and activateFlowTriggerById #247

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions backendless.d.ts
Expand Up @@ -610,7 +610,9 @@ declare module Backendless {
*/

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): Promise<void>
}

/**
Expand Down
38 changes: 36 additions & 2 deletions src/automations/index.js
Expand Up @@ -11,7 +11,7 @@ export default class Automations {
}

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

return this.app.request.post({
Expand All @@ -23,6 +23,21 @@ export default class Automations {
})
}

async activateFlowById(flowId, initialData) {
if (!flowId || typeof flowId !== 'string') {
throw new Error('The "flowId" argument must be provided and must be a string.')
}

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

return this.app.request.post({
url : `${this.app.urls.automationFlow()}/${flowId}/activate`,
data: initialData || {}
})
}

async activateFlowTrigger(flowName, triggerName, data) {
if (!flowName || typeof flowName !== 'string') {
throw new Error('The "flowName" argument must be provided and must be a string.')
Expand All @@ -33,7 +48,7 @@ export default class Automations {
}

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

return this.app.request.post({
Expand All @@ -42,4 +57,23 @@ export default class Automations {
data : data || {},
})
}

async activateFlowTriggerById(flowId, triggerId, data) {
if (!flowId || typeof flowId !== 'string') {
throw new Error('The "flowId" 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 (data !== undefined && !Utils.isObject(data)) {
throw new Error('The "data" argument must be an object.')
}

return this.app.request.post({
url : `${this.app.urls.automationFlow()}/${flowId}/trigger/${triggerId}/activate`,
data: data || {},
})
}
}
4 changes: 4 additions & 0 deletions test/tsd.ts
Expand Up @@ -1526,11 +1526,15 @@ function testAutomations() {
type TestObjType = { [x: string]: number }
const obj: TestObjType = {x: 1, y: 2};
const flowName: string = 'str';
const flowId: string = 'id';
const triggerName: string = 'str';
const triggerId: 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);
}

function testMessaging() {
Expand Down
130 changes: 128 additions & 2 deletions test/unit/specs/automations/basic.js
Expand Up @@ -7,7 +7,9 @@ describe('<Automations> Basic', function() {
forTest(this)

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

describe('activate flow by name', function() {
it('success', async () => {
Expand Down Expand Up @@ -68,6 +70,60 @@ describe('<Automations> Basic', function() {
})
})

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

expect(req1).to.deep.include({
method: 'POST',
path : `${APP_PATH}/automation/flow/${FLOW_ID}/activate`,
body : {}
})

expect(req2).to.deep.include({
method: 'POST',
path : `${APP_PATH}/automation/flow/${FLOW_ID}/activate`,
body : {
name: 'Nick',
}
})

})

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

await expect(Backendless.Automations.activateFlowById()).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowById(undefined)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowById(null)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowById(true)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowById(false)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowById(0)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowById(123)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowById('')).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowById({})).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowById([])).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowById(() => ({}))).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.activateFlowById(FLOW_ID, null)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowById(FLOW_ID, true)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowById(FLOW_ID, false)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowById(FLOW_ID, 0)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowById(FLOW_ID, 123)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowById(FLOW_ID, 'asd')).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowById(FLOW_ID, '')).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowById(FLOW_ID, [])).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowById(FLOW_ID, () => ({}))).to.eventually.be.rejectedWith(errorMsg)
})
})

describe('activate flow trigger', function() {
it('success', async () => {
const req1 = prepareMockRequest()
Expand Down Expand Up @@ -110,9 +166,9 @@ describe('<Automations> Basic', function() {
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,)).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, 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)
Expand All @@ -138,4 +194,74 @@ describe('<Automations> Basic', function() {
})
})

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

expect(req1).to.deep.include({
method: 'POST',
path : `${APP_PATH}/automation/flow/${ FLOW_ID }/trigger/${ TRIGGER_ID }/activate`,
body : {},
})

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

})

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

await expect(Backendless.Automations.activateFlowTriggerById()).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(undefined)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(null)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(true)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(false)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(0)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(123)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById('')).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById({})).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById([])).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(() => ({}))).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.activateFlowTriggerById(FLOW_ID,)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, undefined)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, null)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, true)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, false)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, 0)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, 123)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, '')).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, {})).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, [])).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, () => ({}))).to.eventually.be.rejectedWith(errorMsg)
})

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

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)
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, false)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, 0)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, 123)).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, 'asd')).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, '')).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, [])).to.eventually.be.rejectedWith(errorMsg)
await expect(Backendless.Automations.activateFlowTriggerById(FLOW_ID, TRIGGER_ID, () => ({}))).to.eventually.be.rejectedWith(errorMsg)
})
})

})