-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-warning-to-every-phase.js
141 lines (126 loc) · 3.6 KB
/
add-warning-to-every-phase.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
const {Command, flags} = require('@oclif/command')
const {GraphQLClient, gql} = require('graphql-request')
const asyncForEach = require('../utils/async-foreach')
class AddWarningToEveryPhaseCommand extends Command {
async run() {
const {flags, args} = this.parse(AddWarningToEveryPhaseCommand)
// setup GraphQL Client
let headers = {
Authorization: 'Bearer ' + args.token,
}
const normalClient = new GraphQLClient('https://api.pipefy.com/graphql', {
headers: headers,
})
let phases = await this.loadPhases(normalClient, args.pipeId)
await asyncForEach(phases, async phase => {
this.log('Processing phase ' + phase.name)
await this.processPhase(normalClient, phase.id, flags)
})
}
async addConditional(client, actionFieldId, conditionFieldId, phaseId) {
let query = gql`mutation {
createFieldCondition(input: {
name: "PEAG: Warning for field with id ${actionFieldId} by ${conditionFieldId} on phase ${phaseId}",
phaseId: "${phaseId}",
actions: [
{
phaseFieldId: "${actionFieldId}",
actionId: "show",
whenEvaluator: true
},
{
phaseFieldId: "${actionFieldId}",
actionId: "hide",
whenEvaluator: false
}
],
condition: {
expressions: [{
field_address: "${conditionFieldId}",
operation: "blank",
value: ""
}]
}
}) { clientMutationId }
}`
return client.request(query)
}
async processPhase(client, phaseId, flags) {
let query = gql`mutation {
createPhaseField(input: {
label: "${flags.label}",
phase_id: "${phaseId}",
type: "statement",
description: "${flags.description}",
help: "${flags.help}",
minimal_view: ${flags.minimal},
editable: false
}) { clientMutationId phase_field { id internal_id } }
}`
const results = await client.request(query)
console.log(results)
await this.addConditional(client, results.createPhaseField.phase_field.id, flags.fieldId, phaseId)
}
async loadPhases(client, pipeId) {
let query = gql`query {
pipe(id: "${pipeId}") {
phases {
id, name
}
}
}
`
let results = await client.request(query)
return results.pipe.phases
}
}
AddWarningToEveryPhaseCommand.description = `Add a conditional statement field to every phase in a pipe
...
This command loops all your Pipefy phases of the pipe specified and adds the warning as specified.
The warning is shown if the field you specify by id is emtpy.
`
AddWarningToEveryPhaseCommand.flags = {
label: flags.string({
required: true,
description:
'The label of the field.',
char: 'l',
}),
description: flags.string({
required: false,
default: '',
description:
'The description of the field.',
char: 'd',
}),
help: flags.string({
required: false,
default: '',
}),
minimal: flags.boolean({
required: false,
default: false,
description:
'Whether to use the minimal view',
}),
fieldId: flags.string({
required: true,
description: 'Id of the field to trigger the warning',
}),
}
AddWarningToEveryPhaseCommand.args = [
{
name: 'token',
required: true,
description: 'The API-Token for the Pipefy GraphQL API',
hidden: false,
},
{
name: 'pipeId',
required: true,
description: 'The id of the pipe to add the fields to.',
hidden: false,
parse: input => input.split(','),
},
]
module.exports = AddWarningToEveryPhaseCommand