-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
287 lines (277 loc) · 12 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
import cloneDeep from 'lodash/cloneDeep'
import error from '../core/contentstackError'
import { fetch, fetchAll } from '../entity'
import ContentstackCollection from '../contentstackCollection'
import { RoleCollection } from '../stack/roles'
import { StackCollection } from '../stack'
import { UserCollection } from '../user'
import { App } from '../app'
import { AppRequest } from '../app/request'
import { Teams } from './teams'
/**
* Organization is the top-level entity in the hierarchy of Contentstack, consisting of stacks and stack resources, and users. Organization allows easy management of projects as well as users within the Organization. Read more about <a href='https://www.contentstack.com/docs/guide/organization'>Organizations.</a>.
* @namespace Organization
*/
export function Organization (http, data) {
this.urlPath = '/organizations'
if (data && data.organization) {
Object.assign(this, cloneDeep(data.organization))
this.urlPath = `/organizations/${this.uid}`
/**
* @description The teams call fetches teams details.
* @memberof Organization
* @func teams
* @returns {Promise<Organization.Organization>} Promise for Organization instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.organization('organization_uid').teams('teamsUid').fetch()
* .then((organization) => console.log(organization))
*
*/
this.teams = (teamUid = null) => {
data.organizationUid = this.uid
if (teamUid) {
data.uid = teamUid
}
return new Teams(http, data)
}
/**
* @description The fetch Organization call fetches Organization details.
* @memberof Organization
* @func fetch
* @param {Int} include_plan The include_plan parameter includes the details of the plan that the organization has subscribed to.
* @returns {Promise<Organization.Organization>} Promise for Organization instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.organization('organization_uid').fetch()
* .then((organization) => console.log(organization))
*
*/
this.fetch = fetch(http, 'organization')
/**
* @description The Get all stacks in an organization call fetches the list of all stacks in an Organization.
* @memberof Organization
* @func stacks
* @param {Int} limit The limit parameter will return a specific number of stacks in the output.
* @param {Int} skip The skip parameter will skip a specific number of stacks in the output.
* @param {String} asc When fetching stacks, you can sort them in the ascending order with respect to the value of a specific field in the response body.
* @param {String} desc When fetching stacks, you can sort them in the decending order with respect to the value of a specific field in the response body.
* @param {Boolean} include_count To retrieve the count of stack.
* @param {String} typeahead The type head contains value to be included in search.
* @returns {ContentstackCollection} Instance of ContentstackCollection.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.organization('organization_uid').stacks({ include_count: true })
* .then((collection) => console.log(collection))
*
*/
this.stacks = async (param) => {
try {
const response = await http.get(`${this.urlPath}/stacks`, { params: param })
if (response.data) {
return new ContentstackCollection(response, http, null, StackCollection)
} else {
return error(response)
}
} catch (err) {
return error(err)
}
}
/**
* @description The Transfer organization ownership call transfers the ownership of an Organization to another user.
* @memberof Organization
* @func transferOwnership
* @param {String} email The email address of the user to whom you wish to transfer the ownership of the organization.
* @returns {Object} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.organization('organization_uid').transferOwnership('emailId')
* .then((response) => console.log(response.notice))
*
*/
this.transferOwnership = async (email) => {
try {
const response = await http.post(`${this.urlPath}/transfer-ownership`, { transfer_to: email })
if (response.data) {
return response.data
} else {
return error(response)
}
} catch (err) {
return error(err)
}
}
/**
* @description The Add users to organization call allows you to send invitations to add users to your organization. Only the owner or the admin of the organization can add users.
* @memberof Organization
* @func addUser
* @returns {ContentstackCollection} ContentstackCollection of instance.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.organization('organization_uid').addUser({ users: { '[email protected]': ['org_uid1', 'org_uid2' ]}, stacks: { '[email protected]': { 'api_key1': [ 'stack_role_id' ] } } })
* .then((response) => console.log(response))
*
*/
this.addUser = async (data) => {
try {
const response = await http.post(`${this.urlPath}/share`, { share: { ...data } })
if (response.data) {
return new ContentstackCollection(response, http, null, UserCollection)
} else {
return error(response)
}
} catch (err) {
return error(err)
}
}
/**
* @description The Get all organization invitations call gives you a list of all the Organization invitations.
* @memberof Organization
* @func getInvitations
* @returns {String} Success message of invitation send.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.organization('organization_uid').getInvitations()
* .then((response) => console.log(response.notice))
*
*/
this.getInvitations = async (param) => {
try {
const response = await http.get(`${this.urlPath}/share`, { params: param })
if (response.data) {
return new ContentstackCollection(response, http, null, UserCollection)
} else {
return error(response)
}
} catch (err) {
return error(err)
}
}
/**
* @description The Resend pending organization invitation call allows you to resend Organization invitations to users who have not yet accepted the earlier invitation.
* @memberof Organization
* @func resendInvitition
* @returns {Object} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.organization('organization_uid').resendInvitition('invitation_uid')
* .then((response) => console.log(response.notice))
*
*/
this.resendInvitation = async (invitationUid) => {
try {
const response = await http.get(`${this.urlPath}/${invitationUid}/resend_invitation`)
if (response.data) {
return response.data
} else {
return error(response)
}
} catch (err) {
return error(err)
}
}
/**
* @description A role is a collection of permissions that will be applicable to all the users who are assigned this role.
* @memberof Organization
* @func roles
* @param {Int} limit The limit parameter will return a specific number of roles in the output.
* @param {Int} skip The skip parameter will skip a specific number of roles in the output.
* @param {String} asc When fetching roles, you can sort them in the ascending order with respect to the value of a specific field in the response body.
* @param {String} desc When fetching roles, you can sort them in the decending order with respect to the value of a specific field in the response body.
* @param {Boolean}include_count To retrieve the count of roles.
* @param {Boolean} include_stack_roles The Include stack roles will return stack details in roles.
* @returns {Array<Role>} Array of Role instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.organization('organization_uid').roles()
* .then((roles) => console.log(roles))
*
*/
this.roles = async (param) => {
try {
const response = await http.get(`${this.urlPath}/roles`, { params: param })
if (response.data) {
return new ContentstackCollection(response, http, null, RoleCollection)
} else {
return error(response)
}
} catch (err) {
return error(err)
}
}
/**
* @description Market place application information
* @memberof Organization
* @func app
* @param {String} uid: The ID of the app that you want to fetch details of.
* @returns {App} Instance of App
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client({ authtoken: 'TOKEN'})
*
* client.organization('organization_uid').app('app_uid').fetch()
* .then((app) => console.log(app))
*/
this.app = (uid = null) => {
return new App(http, uid !== null ? { data: { uid, organization_uid: this.uid } } : { organization_uid: this.uid })
}
/**
* @description The Create request call is used to create a app request for an app.
* @returns Request
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client({ authtoken: 'TOKEN'})
*
* client.organization('organization_uid').appRequest()
*
*/
this.appRequest = () => {
return new AppRequest(http, { organization_uid: this.organization_uid }, this.params)
}
} else {
/**
* @description The Get all organizations call lists all organizations related to the system user in the order that they were created.
* @memberof Organization
* @func fetchAll
* @param {Int} limit TThe ‘limit’ parameter will return a specific number of organizations in the output.
* @param {Int} skip The ‘skip’ parameter will skip a specific number of organizations in the output.
* @param {String} asc The ‘asc’ parameter allows you to sort the list of organizations in the ascending order with respect to the value of a specific field.
* @param {String} desc The ‘desc’ parameter allows you to sort the list of Organizations in the descending order with respect to the value of a specific field.
* @param {Boolean}include_count The ‘include_count’ parameter returns the total number of organizations related to the user.
* @param {String} typeahead The typeahead parameter is a type of filter that allows you to perform a name-based search on all organizations based on the value provided.
* @returns {ContentstackCollection} Result collection of content of specified module.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.organization().fetchAll()
* .then((collection) => console.log(collection))
*
*/
this.fetchAll = fetchAll(http, OrganizationCollection)
}
}
export function OrganizationCollection (http, data) {
const obj = cloneDeep(data.organizations || [])
return obj.map((userdata) => {
return new Organization(http, { organization: userdata })
})
}