-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-default-values.js
181 lines (167 loc) · 4.83 KB
/
add-default-values.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
177
178
179
180
181
const {Command, flags} = require('@oclif/command')
const {GraphQLClient, gql} = require('graphql-request')
const asyncForEach = require('../utils/async-foreach')
class AddDefaultValuesCommand extends Command {
async run() {
const {flags, args} = this.parse(AddDefaultValuesCommand)
// setup GraphQL Client
let headers = {
Authorization: 'Bearer ' + args.token,
}
const normalClient = new GraphQLClient('https://api.pipefy.com/graphql', {
headers: headers,
})
await this.processPhase(normalClient, flags)
// await asyncForEach(databaseId, async (pipeId) => {
// this.log("Processing pipe " + pipeId);
// await this.processDatabase(normalClient, databaseId, flags);
// });
}
/**
* Add the default values to one phase as specified
*
* @param {object} client The GraphQL Cleint
* @param {object} flags The console command's flags
*/
async processPhase(client, flags) {
let phase = await this.getPhaseEntries(client, flags.phaseId, false)
phase = phase.phase
this.log(
`Phase "${phase.name}" has currently ${phase.cards_count} entries (found ${phase.cards.edges.length})`,
)
let nrUpdated = 0
await asyncForEach(phase.cards.edges, async card => {
card = card.node
let needsUpdating = true
card.fields.forEach(field => {
if (
field.field.internal_id === flags.fieldId ||
field.field.id === flags.fieldId
) {
if (field.value) {
needsUpdating = false
}
}
})
if (needsUpdating) {
if (!flags.dry) {
await this.setFieldValue(client, card.id, flags.fieldId, flags.value)
}
nrUpdated++
}
})
this.log(`Updated ${nrUpdated} cards`)
}
/**
* Use the GraphQL API to set the value of a field
*
* @param {object} client The graphQL client
* @param {int} cardId The id of the card to set the field on
* @param {string} fieldId The id of the field to set the value of
* @param {string|int|boolean} value The value to set the field to
*/
async setFieldValue(client, cardId, fieldId, value) {
let query = gql`mutation {
updateCardField(input: {
card_id: "${cardId}",
field_id: "${fieldId}"
new_value: "${value}"
}) {
success, clientMutationId
}
}`
let results = await client.request(query)
if (!results.updateCardField.success) {
this.warn('Update of field not successfull for card with id ' + cardId)
this.log(results)
}
}
/**
* Fetch the Entries (Cards) in a phase
*
* @param {GraphQLClient} client the client to fetch data with
* @param {string} phaseId the ID of the phase to load the entries for
* @param {string} cursor The cursor where to start
* @returns {object} the table ojbect
*/
async getPhaseEntries(client, phaseId, cursor = false) {
let recordArgs = 'first: 50'
if (cursor !== false) {
recordArgs = 'after: "' + cursor + '"'
}
let query = gql`query {
phase(id: "${phaseId}") {
cards(${recordArgs}) {
edges {
node {
title
id
fields {
field {
internal_id
id
}
value
}
}
}
pageInfo {
endCursor
hasNextPage
}
}
cards_count
name
}
}
`
let results = await client.request(query)
const pageInfo = results.phase.cards.pageInfo
if (pageInfo.hasNextPage) {
this.log(`Loading additional cards for cursor "${pageInfo.endCursor}"`)
const nextData = await this.getPhaseEntries(
client,
phaseId,
pageInfo.endCursor,
)
results.phase.cards.edges = results.phase.cards.edges.concat(
nextData.phase.cards.edges,
)
}
return results
}
}
AddDefaultValuesCommand.description = `Set a value to a field in all cards of a phase
...
This command loops all your Cards of the specified pipe and phase
and sets the specified value on the specified field
`
AddDefaultValuesCommand.flags = {
phaseId: flags.string({
required: true,
description: 'The phase to change the cards in',
}),
fieldId: flags.string({
required: true,
description: 'The field id to change its values',
}),
value: flags.string({
required: true,
description: 'The value to change the field to',
}),
dry: flags.boolean({
required: false,
default: false,
description:
'Whether to do a dry run: just output how many entries would be changed etc.',
}),
}
AddDefaultValuesCommand.args = [
{
name: 'token',
required: true,
description: 'The API-Token for the Pipefy GraphQL API',
hidden: false,
},
]
module.exports = AddDefaultValuesCommand