Skip to content

Commit

Permalink
use subscriber flag throughout workflow commands (#164)
Browse files Browse the repository at this point in the history
  • Loading branch information
steveims committed Mar 19, 2024
1 parent 768e3ad commit 30d3e5d
Show file tree
Hide file tree
Showing 17 changed files with 38 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/commands/broadcast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export default class Broadcast extends Command {
})

if (await prompt.run()) {
await this.relay.saveWorkflow(workflow)
await this.relay.saveWorkflow(subscriber_id, workflow)
this.log(`Broadcast saved!`)
} else {
this.log(`Broadcast cancelled!`)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/workflow/args/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class SetArgsCommand extends Command {
...workflow.config.trigger.start.workflow.args,
...args,
}
await this.relay.saveWorkflow(workflow)
await this.relay.saveWorkflow(subscriberId, workflow)
CliUx.ux.action.stop(`success`)
CliUx.ux.styledHeader(`New Workflow arguments`)
CliUx.ux.styledJSON(workflow.config.trigger.start.workflow.args)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/workflow/args/unset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class UnsetArgsCommand extends Command {
workflow.config.trigger.start.workflow.args,
argv,
)
await this.relay.saveWorkflow(workflow)
await this.relay.saveWorkflow(subscriberId, workflow)
CliUx.ux.action.stop(`success`)
CliUx.ux.styledHeader(`New Workflow arguments`)
CliUx.ux.styledJSON(workflow.config.trigger.start.workflow.args)
Expand Down
5 changes: 3 additions & 2 deletions src/commands/workflow/create/battery.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright © 2022 Relay Inc.

import { CreateCommand } from '../../../lib/command'
import { enum as enumFlag, integer, workflowFlags } from '../../../lib/flags'
import { enum as enumFlag, integer, subscriber, workflowFlags } from '../../../lib/flags'

// eslint-disable-next-line quotes
import debugFn = require('debug')
Expand All @@ -22,6 +22,7 @@ export class BatteryWorkflowCommand extends CreateCommand {
static strict = false

static flags = {
...subscriber,
...workflowFlags,
trigger: enumFlag({
required: true,
Expand Down Expand Up @@ -53,7 +54,7 @@ export class BatteryWorkflowCommand extends CreateCommand {
workflow.config.trigger[mapTap(flags.trigger)] = flags.threshold
}

await this.saveWorkflow(workflow, flags[`dry-run`])
await this.saveWorkflow(flags[`subscriber-id`], workflow, flags[`dry-run`])

} catch (err) {
debug(err)
Expand Down
5 changes: 3 additions & 2 deletions src/commands/workflow/create/button.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright © 2022 Relay Inc.

import { CreateCommand } from '../../../lib/command'
import { enum as enumFlag, workflowFlags } from '../../../lib/flags'
import { enum as enumFlag, subscriber, workflowFlags } from '../../../lib/flags'

// eslint-disable-next-line quotes
import debugFn = require('debug')
Expand All @@ -22,6 +22,7 @@ export class ButtonWorkflowCommand extends CreateCommand {
static strict = false

static flags = {
...subscriber,
...workflowFlags,
trigger: enumFlag({
required: true,
Expand All @@ -45,7 +46,7 @@ export class ButtonWorkflowCommand extends CreateCommand {
throw new Error(`Trigger type button requires specifying number of taps. For instance '--trigger single'`)
}

await this.saveWorkflow(workflow, flags[`dry-run`])
await this.saveWorkflow(flags[`subscriber-id`], workflow, flags[`dry-run`])

} catch (err) {
debug(err)
Expand Down
5 changes: 3 additions & 2 deletions src/commands/workflow/create/call.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright © 2022 Relay Inc.

import { CreateCommand } from '../../../lib/command'
import { enum as enumFlag, workflowFlags } from '../../../lib/flags'
import { enum as enumFlag, subscriber, workflowFlags } from '../../../lib/flags'

// eslint-disable-next-line quotes
import debugFn = require('debug')
Expand All @@ -27,6 +27,7 @@ export class CallWorkflowCommand extends CreateCommand {
static strict = false

static flags = {
...subscriber,
...workflowFlags,
trigger: enumFlag({
required: true,
Expand All @@ -50,7 +51,7 @@ export class CallWorkflowCommand extends CreateCommand {
workflow.config.trigger[mapType(flags.trigger as Direction)] = `.*`
}

await this.saveWorkflow(workflow, flags[`dry-run`])
await this.saveWorkflow(flags[`subscriber-id`], workflow, flags[`dry-run`])

} catch (err) {
debug(err)
Expand Down
5 changes: 3 additions & 2 deletions src/commands/workflow/create/event.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright © 2022 Relay Inc.

import { CreateCommand } from '../../../lib/command'
import { enum as enumFlag, workflowFlags } from '../../../lib/flags'
import { enum as enumFlag, subscriber, workflowFlags } from '../../../lib/flags'

// eslint-disable-next-line quotes
import debugFn = require('debug')
Expand All @@ -22,6 +22,7 @@ export class EventWorkflowCommand extends CreateCommand {
static strict = false

static flags = {
...subscriber,
...workflowFlags,
trigger: enumFlag({
required: true,
Expand All @@ -45,7 +46,7 @@ export class EventWorkflowCommand extends CreateCommand {
throw new Error(`Trigger type event requires specifying a device event. For instance '--trigger emergency'`)
}

await this.saveWorkflow(workflow, flags[`dry-run`])
await this.saveWorkflow(flags[`subscriber-id`], workflow, flags[`dry-run`])

} catch (err) {
debug(err)
Expand Down
5 changes: 3 additions & 2 deletions src/commands/workflow/create/geofence.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright © 2022 Relay Inc.

import { CreateCommand } from '../../../lib/command'
import { string, workflowFlags } from '../../../lib/flags'
import { string, subscriber, workflowFlags } from '../../../lib/flags'

// eslint-disable-next-line quotes
import debugFn = require('debug')
Expand All @@ -25,6 +25,7 @@ export class GeofenceWorkflowCommand extends CreateCommand {
static strict = false

static flags = {
...subscriber,
...workflowFlags,
trigger: string({
required: true,
Expand Down Expand Up @@ -56,7 +57,7 @@ export class GeofenceWorkflowCommand extends CreateCommand {
throw new Error(`Trigger type geofence requires specifying a trigger and geofence ID.`)
}

await this.saveWorkflow(workflow, flags[`dry-run`])
await this.saveWorkflow(flags[`subscriber-id`], workflow, flags[`dry-run`])

} catch (err) {
debug(err)
Expand Down
5 changes: 3 additions & 2 deletions src/commands/workflow/create/http.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright © 2022 Relay Inc.

import { CreateCommand } from '../../../lib/command'
import { enum as enumFlag, workflowFlags } from '../../../lib/flags'
import { enum as enumFlag, subscriber, workflowFlags } from '../../../lib/flags'

// eslint-disable-next-line quotes
import debugFn = require('debug')
Expand All @@ -22,6 +22,7 @@ export class HttpWorkflowCommand extends CreateCommand {
static strict = false

static flags = {
...subscriber,
...workflowFlags,
trigger: enumFlag({
required: true,
Expand All @@ -45,7 +46,7 @@ export class HttpWorkflowCommand extends CreateCommand {
throw new Error(`Trigger type http requires specifying an HTTP method. For instance '--trigger POST'`)
}

await this.saveWorkflow(workflow, flags[`dry-run`])
await this.saveWorkflow(flags[`subscriber-id`], workflow, flags[`dry-run`])

} catch (err) {
debug(err)
Expand Down
3 changes: 2 additions & 1 deletion src/commands/workflow/create/nfc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class NfcWorkflowCommand extends CreateCommand {
static strict = false

static flags = {
...Flags.subscriber,
...Flags.workflowFlags,
matcher: Flags.string({
char: `m`,
Expand Down Expand Up @@ -98,7 +99,7 @@ export class NfcWorkflowCommand extends CreateCommand {

debug(`nfc workflow => ${JSON.stringify(workflow, null, 2)}`)

await this.saveWorkflow(workflow, flags[`dry-run`])
await this.saveWorkflow(flags[`subscriber-id`], workflow, flags[`dry-run`])

} catch (err) {
debug(err)
Expand Down
5 changes: 3 additions & 2 deletions src/commands/workflow/create/phrase.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright © 2022 Relay Inc.

import { CreateCommand } from '../../../lib/command'
import { string, workflowFlags } from '../../../lib/flags'
import { string, subscriber, workflowFlags } from '../../../lib/flags'

// eslint-disable-next-line quotes
import debugFn = require('debug')
Expand All @@ -20,6 +20,7 @@ export class PhraseWorkflowCommand extends CreateCommand {
static strict = false

static flags = {
...subscriber,
...workflowFlags,
trigger: string({
required: true,
Expand All @@ -42,7 +43,7 @@ export class PhraseWorkflowCommand extends CreateCommand {
throw new Error(`Trigger type phrase requires specifying a phrase. For instance '--phrase hello'`)
}

await this.saveWorkflow(workflow, flags[`dry-run`])
await this.saveWorkflow(flags[`subscriber-id`], workflow, flags[`dry-run`])

} catch (err) {
debug(err)
Expand Down
5 changes: 3 additions & 2 deletions src/commands/workflow/create/position.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright © 2022 Relay Inc.

import { CreateCommand } from '../../../lib/command'
import { string, workflowFlags } from '../../../lib/flags'
import { string, subscriber, workflowFlags } from '../../../lib/flags'

// eslint-disable-next-line quotes
import debugFn = require('debug')
Expand All @@ -25,6 +25,7 @@ export class PositionWorkflowCommand extends CreateCommand {
static strict = false

static flags = {
...subscriber,
...workflowFlags,
trigger: string({
required: true,
Expand Down Expand Up @@ -63,7 +64,7 @@ export class PositionWorkflowCommand extends CreateCommand {
throw new Error(`Trigger type position requires specifying a transition trigger and position ID.`)
}

await this.saveWorkflow(workflow, flags[`dry-run`])
await this.saveWorkflow(flags[`subscriber-id`], workflow, flags[`dry-run`])

} catch (err) {
debug(err)
Expand Down
5 changes: 3 additions & 2 deletions src/commands/workflow/create/timer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright © 2022 Relay Inc.

import { CreateCommand } from '../../../lib/command'
import { workflowFlags, timerFlags } from '../../../lib/flags'
import { subscriber, workflowFlags, timerFlags } from '../../../lib/flags'

import { createTimerWorkflow } from '../../../lib/workflow'

Expand All @@ -17,6 +17,7 @@ export class TimerWorkflowCommand extends CreateCommand {
static strict = false

static flags = {
...subscriber,
...workflowFlags,
...timerFlags,
}
Expand All @@ -28,7 +29,7 @@ export class TimerWorkflowCommand extends CreateCommand {

const workflow = await createTimerWorkflow(flags, raw)

await this.saveWorkflow(workflow, flags[`dry-run`])
await this.saveWorkflow(flags[`subscriber_id`], workflow, flags[`dry-run`])

} catch (err) {
debug(err)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/workflow/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class InstallWorkflowCommand extends CreateCommand {
workflow.install_rule = flags[`install-group`]
}

await this.saveWorkflow(workflow, dryRun)
await this.saveWorkflow(subscriberId, workflow, dryRun)

} else {
this.log(`Workflow ID does not exist: ${workflowId}`)
Expand Down
2 changes: 1 addition & 1 deletion src/commands/workflow/uninstall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class UninstallWorkflowCommand extends CreateCommand {
workflow.install_rule = undefined
}

await this.saveWorkflow(workflow, dryRun)
await this.saveWorkflow(subscriberId, workflow, dryRun)

} else {
this.log(`Workflow ID does not exist: ${workflowId}`)
Expand Down
6 changes: 2 additions & 4 deletions src/lib/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Login } from './login'
import { vars } from './vars'

import debugFn = require('debug') // eslint-disable-line quotes
import { clearConfig, clearSubscribers, getDefaultSubscriber, getDefaultSubscriberId, getSession, getToken, Session, Subscriber, TokenAccount, SubscriberPagedResults, SubscriberQuery } from './session'
import { clearConfig, clearSubscribers, getDefaultSubscriber, getSession, getToken, Session, Subscriber, TokenAccount, SubscriberPagedResults, SubscriberQuery } from './session'
import { Capabilities, CustomAudio, CustomAudioUpload, DeviceId, DeviceIds, Geofence, GeofenceResults, Group, HistoricalWorkflowInstance, HttpMethod, NewWorkflow, Tag, TagForCreate, TagResults, SubscriberInfo, Workflow, WorkflowEventQuery, WorkflowEventResults, WorkflowEvents, WorkflowInstance, Workflows, Venues, VenueResults, Positions, PositionResults, AuditEventType, ProfileAuditEventResults, RawAuditEventResults, ProfileAuditEvent, PagingParams, TaskResults, WorkflowLogQuery, NewTask, NewScheduledTask, Task, TaskType, TaskTypeResults, MajorResults, MinorResults, NewMajor, Minor, Major, ResourceResults, NewMinor, TaskGroup, TaskGroupResults, NewTaskGroup } from './api'

import { normalize } from './utils'
Expand Down Expand Up @@ -409,9 +409,7 @@ export class APIClient {
const workflows = await this.workflows(subscriberId)
return find(workflows, ({ workflow_id }) => includes(workflow_id, id))
}
async saveWorkflow(workflow: NewWorkflow): Promise<Workflow[]> {
// TODO use parameterized subscriberId
const subscriberId = getDefaultSubscriberId()
async saveWorkflow(subscriberId: string, workflow: NewWorkflow): Promise<Workflow[]> {
const { body } = await this.post<Workflow[]>(`/ibot/workflow?subscriber_id=${subscriberId}`, {
body: workflow,
})
Expand Down
4 changes: 2 additions & 2 deletions src/lib/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ export abstract class Command extends Base {

export abstract class CreateCommand extends Command {

async saveWorkflow(workflow: NewWorkflow, dryRun: boolean): Promise<void> {
async saveWorkflow(subscriberId: string, workflow: NewWorkflow, dryRun: boolean): Promise<void> {
if (!dryRun) {
debug(workflow)
const workflows = await this.relay.saveWorkflow(workflow)
const workflows = await this.relay.saveWorkflow(subscriberId, workflow)
printWorkflows(workflows, { extended: true })
} else {
this.log(`Workflow dry-run:\n${JSON.stringify(workflow, null, 2)}`)
Expand Down

0 comments on commit 30d3e5d

Please sign in to comment.