-
Notifications
You must be signed in to change notification settings - Fork 7
/
index.js
158 lines (149 loc) · 5.14 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
import cloneDeep from 'lodash/cloneDeep'
import { update, deleteEntity } from '../entity'
import error from '../core/contentstackError'
import { OrganizationCollection } from '../organization'
/**
* All accounts registered with Contentstack are known as Users. A stack can have many users with varying permissions and roles. Read Users to learn more.
* @namespace User
*/
export function User (http, data) {
Object.assign(this, cloneDeep(data.user))
this.urlPath = '/user'
if (this.authtoken) {
/**
* @description The Update User API Request updates the details of an existing user account.
* @memberof User
* @func update
* @returns {Promise<User.User>} Promise for User instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).getUser()
* .then((user) => {
* user.first_name = 'FirstName'
* user.last_name = 'LastName'
* user.company = 'Role description'
* return user.update()
* })
* .then((user) => console.log(user))
*/
this.update = update(http, 'user')
/**
* @description The Delete user call deletes the current authenticated user permanently from your Contentstack account.
* @memberof User
* @func delete
* @returns {Object} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).getUser()
* .then((user) => {
* return user.delete()
* })
* .then((response) => console.log(response.notice))
*/
this.delete = deleteEntity(http)
/**
* @description The Request for a password call sends a request for a temporary password to log in to an account in case a user has forgotten the login password.
* @memberof User
* @func requestPassword
* @returns {Object} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).getUser()
* .then((user) => {
* return user.requestPassword()
* })
* .then((response) => console.log(response.notice))
*
*/
this.requestPassword = function () {
return http.post('/user/forgot_password', { user: { email: this.email } })
.then((response) => {
return response.data
}, error)
}
/**
* @description The Reset password call sends a request for resetting the password of your Contentstack account.
* @memberof User
* @func resetPassword
* @returns {Object} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).getUser()
* .then((user) => {
* return user.resetPassword({ 'resetToken', 'new_password', 'new_password' })
* })
* .then((response) => console.log(response.notice))
*
*/
this.resetPassword = function ({ resetPasswordToken, password, passwordConfirm }) {
return http.post('/user/reset_password', { user:
{
reset_password_token: resetPasswordToken,
password: password,
password_confirmation: passwordConfirm
}
})
.then((response) => {
return response.data
}, error)
}
if (this.organizations) {
this.organizations = new OrganizationCollection(http, { organizations: this.organizations })
}
/**
* @description The Get all Tasks request retrieves a list of all tasks assigned to you.
* @memberof User
* @func getTasks
* @param {Object} query Enter the actual query that will be executed to retrieve the tasks. This query should be in JSON format.
* @param {Object} sort Enter the field UID on the basis of which you want to sort your tasks.
* @param {Int} limit Enter the maximum number of tasks that you want to retrieve in the response.
* @param {Int} skip Enter the number of tasks to be skipped.
* @returns {Object} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).getUser()
* .then((user) => {
* return user.getTasks()
* })
* .then((response) => console.log(response.assignments))
*
*/
this.getTasks = async (params) => {
const headers = {}
if (params) {
headers.params = {
...cloneDeep(params)
}
}
try {
const response = await http.get(`/user/assignments`, headers)
if (response.data) {
return response.data
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}
}
return this
}
export function UserCollection (http, data) {
const users = data.collaborators || data.shares || []
const obj = cloneDeep(users)
const userCollection = obj.map((userdata) => {
return new User(http, { user: userdata })
})
return userCollection
}