/
add-field-to-every-pipe.js
176 lines (162 loc) · 4.33 KB
/
add-field-to-every-pipe.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
const {Command, flags} = require('@oclif/command')
const {GraphQLClient, gql} = require('graphql-request')
const asyncForEach = require('../utils/async-foreach')
class AddFieldToEveryPipeCommand extends Command {
async run() {
const {flags, args} = this.parse(AddFieldToEveryPipeCommand)
// setup GraphQL Client
let headers = {
Authorization: 'Bearer ' + args.token,
}
const normalClient = new GraphQLClient('https://api.pipefy.com/graphql', {
headers: headers,
})
await asyncForEach(args.organizationId, async orgId => {
let pipesPhases = await this.loadPipesWithPhases(normalClient, orgId)
this.log(`Got ${pipesPhases.pipes.length} pipes`)
await asyncForEach(pipesPhases.pipes, async pipe => {
this.log(`Handling pipe "${pipe.name}"`)
await asyncForEach(pipe.phases, async phase => {
if (phase.name.trim() === args.phaseName.trim()) {
let results = await this.processPhase(
normalClient,
phase.id,
flags,
)
this.log(
`Created field with id ${results.phase_field.id} and internal id ${results.phase_field.internal_id} in phase ${phase.name}`,
)
} else {
// this.log(`${phase.name} != ${args.phaseName}`)
}
})
})
})
}
async processPhase(client, phaseId, flags) {
let query = gql`mutation {
createPhaseField(input: {
label: "${flags.label}",
phase_id: "${phaseId}",
type: "${flags.type}",
description: "${flags.description}",
help: "${flags.help}",
minimal_view: ${flags.minimal},
editable: ${flags.editable}
}) {clientMutationId phase_field { id internal_id } }
}`
let results = await client.request(query)
return results.createPhaseField
}
async loadPhases(client, pipeId) {
let query = gql`query {
pipe(id: "${pipeId}") {
phases {
id, name
}
}
}
`
let results = await client.request(query)
return results.pipe.phases
}
async loadPipesWithPhases(client, organizationId) {
let query = gql`query {
organization(id: "${organizationId}") {
pipes{
id, name
phases {
id, name
}
}
}
}
`
let results = await client.request(query)
return results.organization
}
}
AddFieldToEveryPipeCommand.description = `Add a field to every phase with the same name in all pipes
...
This command loops all your Pipefy pipes adds the field as specified to every phase with the specified name.
`
AddFieldToEveryPipeCommand.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: '',
}),
type: flags.string({
required: true,
description: 'The type of the field.',
char: 't',
options: [
'assignee_select',
'attachment',
'checklist_horizontal',
'checklist_vertical',
'cnpj',
'connector',
'cpf',
'currency',
'date',
'datetime',
'due_date',
'email',
'id',
'label_select',
'long_text',
'number',
'phone',
'radio_horizontal',
'radio_vertical',
'select',
'short_text',
'statement',
'time',
],
}),
minimal: flags.boolean({
required: false,
default: true,
description: 'Whether to use the minimal view',
}),
editable: flags.boolean({
required: false,
default: true,
description: 'Whether the field can be edited in other phases',
}),
}
AddFieldToEveryPipeCommand.args = [
{
name: 'token',
required: true,
description: 'The API-Token for the Pipefy GraphQL API',
hidden: false,
},
{
name: 'organizationId',
required: true,
description: 'The id of the organization to load the pipes for.',
hidden: false,
parse: input => input.split(','),
},
{
name: 'phaseName',
required: true,
description: 'The name of the phase in all pipes to add the fields to.',
hidden: false,
// parse: input => input.split(','),
},
]
module.exports = AddFieldToEveryPipeCommand