-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
307 lines (295 loc) · 10.3 KB
/
index.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import cloneDeep from 'lodash/cloneDeep'
import {
create,
update,
deleteEntity,
fetch,
fetchAll
} from '../../entity'
import error from '../../core/contentstackError'
import ContentstackCollection from '../../contentstackCollection'
import { PublishRules, PublishRulesCollection } from './publishRules'
/**
* Workflow is a tool that allows you to streamline the process of content creation and publishing, and lets you manage the content lifecycle of your project smoothly. Read more about <a href='https://www.contentstack.com/docs/developers/set-up-workflows-and-publish-rules'>Workflows and Publish Rules</a>.
* @namespace Workflow
*/
export function Workflow (http, data = {}) {
this.stackHeaders = data.stackHeaders
this.urlPath = `/workflows`
if (data.workflow) {
Object.assign(this, cloneDeep(data.workflow))
this.urlPath = `/workflows/${this.uid}`
/**
* @description The Update Workflow request allows you to add a workflow stage or update the details of the existing stages of a workflow.
* @memberof Workflow
* @func update
* @returns {Promise<Workflow.Workflow>} Promise for Workflow instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).workflow('workflow_uid').fetch()
* .then((workflow) => {
* workflow.name = 'My New Workflow'
* workflow.description = 'Workflow description'
* return workflow.update()
* })
* .then((workflow) => console.log(workflow))
*
*/
this.update = update(http, 'workflow')
/**
* @description The Disable Workflow request allows you to disable a workflow.
* @memberof Workflow
* @func disable
* @returns {Promise<Workflow.Workflow>} Promise for Workflow instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).workflow('workflow_uid').disable()
* .then((workflow) => console.log(workflow))
*
*/
this.disable = async function () {
try {
const response = await http.get(`/workflows/${this.uid}/disable`, {
headers: {
...cloneDeep(this.stackHeaders)
}
})
if (response.data) {
return response.data
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}
/**
* @description The Enable Workflow request allows you to enable a workflow.
* @memberof Workflow
* @func enable
* @returns {Promise<Workflow.Workflow>} Promise for Workflow instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).workflow('workflow_uid').enable()
* .then((workflow) => console.log(workflow))
*
*/
this.enable = async function () {
try {
const response = await http.get(`/workflows/${this.uid}/enable`, {
headers: {
...cloneDeep(this.stackHeaders)
}
})
if (response.data) {
return response.data
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}
/**
* @description The Delete Workflow call is used to delete an existing Workflow permanently from your Stack.
* @memberof Workflow
* @func delete
* @returns {Object} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).workflow('workflow_uid').delete()
* .then((response) => console.log(response.notice))
*/
this.delete = deleteEntity(http)
/**
* @description The fetch workflow retrieves the comprehensive details of a specific Workflow of a stack.
* @memberof Workflow
* @func fetch
* @returns {Promise<Workflow.Workflow>} Promise for Workflow instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).workflow('workflow_uid').fetch()
* .then((workflow) => console.log(workflow))
*
*/
this.fetch = fetch(http, 'workflow')
} else {
this.contentType = (contentTypeUID) => {
if (contentTypeUID) {
/**
* @description The Delete Workflow call is used to delete an existing Workflow permanently from your Stack.
* @memberof Workflow
* @func getPublishRules
* @returns {Object} Returns Object.
* @param {string} action Enter the action that has been set in the Publishing Rule. Example:publish/unpublish
* @param {string} locale Enter the code of the locale where your Publishing Rule will be applicable.
* @param {string} environment Enter the UID of the environment where your Publishing Rule will be applicable.
* @returns {ContentstackCollection} Result collection of content of PublishRules.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).workflow('workflow_uid').contentType('contentType_uid').getPublishRules()
* .then((collection) => console.log(collection))
*/
const getPublishRules = async function (params) {
const headers = {}
if (this.stackHeaders) {
headers.headers = this.stackHeaders
}
if (params) {
headers.params = {
...cloneDeep(params)
}
}
try {
const response = await http.get(`/workflows/content_type/${contentTypeUID}`, headers)
if (response.data) {
return new ContentstackCollection(response, http, this.stackHeaders, PublishRulesCollection)
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}
return {
getPublishRules,
stackHeaders: { ...this.stackHeaders }
}
}
}
/**
* @description The Create a Workflow request allows you to create a Workflow.
* @memberof Workflow
* @func create
* @returns {Promise<Workflow.Workflow>} Promise for Workflow instance
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* const workflow = {
*"workflow_stages": [
* {
* "color": "#2196f3",
* "SYS_ACL": {
* "roles": {
* "uids": []
* },
* "users": {
* "uids": [
* "$all"
* ]
* },
* "others": {}
* },
* "next_available_stages": [
* "$all"
* ],
* "allStages": true,
* "allUsers": true,
* "specificStages": false,
* "specificUsers": false,
* "entry_lock": "$none", //assign any one of the assign any one of the ($none/$others/$all)
* "name": "Review"
* },
* {
* "color": "#74ba76",
* "SYS_ACL": {
* "roles": {
* "uids": []
* },
* "users": {
* "uids": [
* "$all"
* ]
* },
* "others": {}
* },
* "allStages": true,
* "allUsers": true,
* "specificStages": false,
* "specificUsers": false,
* "next_available_stages": [
* "$all"
* ],
* "entry_lock": "$none",
* "name": "Complete"
* }
* ],
* "admin_users": {
* "users": []
* },
* "name": "Workflow Name",
* "enabled": true,
* "content_types": [
* "$all"
* ]
* }
* client.stack().workflow().create({ workflow })
* .then((workflow) => console.log(workflow))
*/
this.create = create({ http: http })
/**
* @description The Get all Workflows request retrieves the details of all the Workflows of a stack.
* @memberof Workflow
* @func fetchAll
* @param {Int} limit The limit parameter will return a specific number of Workflows in the output.
* @param {Int} skip The skip parameter will skip a specific number of Workflows in the output.
* @param {Boolean}include_count To retrieve the count of Workflows.
* @returns {ContentstackCollection} Result collection of content of specified module.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).workflow().fetchAll()
* .then((collection) => console.log(collection))
*
*/
this.fetchAll = fetchAll(http, WorkflowCollection)
/**
* @description The Publish rule allow you to create, fetch, delete, update the publish rules.
* @memberof Workflow
* @func publishRule
* @param {Int} ruleUid The UID of the Publish rules you want to get details.
* @returns {PublishRules} Instace of PublishRules.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).workflow().publishRule().fetchAll()
* .then((collection) => console.log(collection))
*
* client.stack({ api_key: 'api_key'}).workflow().publishRule('rule_uid').fetch()
* .then((publishrule) => console.log(publishrule))
*
*/
this.publishRule = (ruleUid = null) => {
const publishInfo = { stackHeaders: this.stackHeaders }
if (ruleUid) {
publishInfo.publishing_rule = { uid: ruleUid }
}
return new PublishRules(http, publishInfo)
}
}
}
export function WorkflowCollection (http, data) {
let obj = cloneDeep(data.workflows) || []
if (!Array.isArray(obj)) {
obj = [obj]
}
return obj.map((userData) => {
return new Workflow(http, { workflow: userData, stackHeaders: data.stackHeaders })
})
}