-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathindex.js
More file actions
260 lines (252 loc) · 11.1 KB
/
index.js
File metadata and controls
260 lines (252 loc) · 11.1 KB
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
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 { 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 {Teams} Instance of Teams.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.organization('organization_uid').teams('teamsUid').fetch()
* .then((team) => console.log(team))
*
*/
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 {Boolean} 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
* @async
* @param {Object=} param - Query parameters.
* @prop {Number=} param.limit - The limit parameter will return a specific number of stacks in the output.
* @prop {Number=} param.skip - The skip parameter will skip a specific number of stacks in the output.
* @prop {String=} param.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.
* @prop {String=} param.desc - When fetching stacks, you can sort them in descending order with respect to the value of a specific field in the response body.
* @prop {Boolean=} param.include_count - To retrieve the count of stack.
* @prop {String=} param.typeahead - The typeahead contains value to be included in search.
* @returns {Promise<ContentstackCollection>} Promise for ContentstackCollection instance.
* @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
* @async
* @param {String} email The email address of the user to whom you wish to transfer the ownership of the organization.
* @returns {Promise<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
* @async
* @returns {Promise<ContentstackCollection>} Promise for ContentstackCollection instance.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.organization('organization_uid').addUser({ users: { 'abc@test.com': ['org_uid1', 'org_uid2' ]}, stacks: { 'abc@test.com': { '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
* @async
* @returns {Promise<ContentstackCollection>} Promise for ContentstackCollection instance.
* @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 resendInvitation
* @async
* @returns {Promise<Object>} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.organization('organization_uid').resendInvitation('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
* @async
* @param {Object=} param - Query parameters.
* @prop {Number=} param.limit - The limit parameter will return a specific number of roles in the output.
* @prop {Number=} param.skip - The skip parameter will skip a specific number of roles in the output.
* @prop {String=} param.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.
* @prop {String=} param.desc - When fetching roles, you can sort them in descending order with respect to the value of a specific field in the response body.
* @prop {Boolean=} param.include_count - To retrieve the count of roles.
* @prop {Boolean=} param.include_stack_roles - The Include stack roles will return stack details in roles.
* @returns {Promise<ContentstackCollection>} Promise for ContentstackCollection 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)
}
}
} 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 {Number} limit - The 'limit' parameter will return a specific number of organizations in the output.
* @param {Number} 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 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 {Promise<ContentstackCollection>} Promise for ContentstackCollection instance.
* @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 })
})
}