@contentstack/cli-cm-export-to-csv 1.0.4 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2022 Contentstack
3
+ Copyright (c) 2023 Contentstack
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
@@ -1 +1,86 @@
1
- {"version":"1.0.4","commands":{"cm:export-to-csv":{"id":"cm:export-to-csv","description":"Export entries or organization users to csv using this command","pluginName":"@contentstack/cli-cm-export-to-csv","pluginType":"core","aliases":[],"examples":["csdx cm:export-to-csv","","Exporting entries to csv","csdx cm:export-to-csv --action <entries> --locale <locale> --alias <management-token-alias> --content-type <content-type>","","Exporting entries to csv with stack name provided","csdx cm:export-to-csv --action <entries> --locale <locale> --alias <management-token-alias> --content-type <content-type> --stack-name <stack-name>","","Exporting organization users to csv","csdx cm:export-to-csv --action <users> --org <org-uid>","","Exporting organization users to csv with organization name provided","csdx cm:export-to-csv --action <users> --org <org-uid> --org-name <org-name>"],"flags":{"action":{"name":"action","type":"option","description":"Option to export data (entries, users)","required":false,"options":["entries","users"]},"alias":{"name":"alias","type":"option","char":"a","description":"Alias of the management token"},"org":{"name":"org","type":"option","description":"Provide organization UID to clone org users","required":false},"stack-name":{"name":"stack-name","type":"option","char":"n","description":"Name of the stack that needs to be created as csv filename.","required":false},"org-name":{"name":"org-name","type":"option","description":"Name of the organization that needs to be created as csv filename.","required":false},"locale":{"name":"locale","type":"option","description":"Locale for which entries need to be exported","required":false},"content-type":{"name":"content-type","type":"option","description":"Content type for which entries needs to be exported","required":false}},"args":[]}}}
1
+ {
2
+ "version": "1.2.0",
3
+ "commands": {
4
+ "cm:export-to-csv": {
5
+ "id": "cm:export-to-csv",
6
+ "description": "Export entries or organization users to csv using this command",
7
+ "strict": true,
8
+ "pluginName": "@contentstack/cli-cm-export-to-csv",
9
+ "pluginAlias": "@contentstack/cli-cm-export-to-csv",
10
+ "pluginType": "core",
11
+ "aliases": [],
12
+ "examples": [
13
+ "csdx cm:export-to-csv",
14
+ "",
15
+ "Exporting entries to csv",
16
+ "csdx cm:export-to-csv --action <entries> --locale <locale> --alias <management-token-alias> --content-type <content-type>",
17
+ "",
18
+ "Exporting entries to csv with stack name provided",
19
+ "csdx cm:export-to-csv --action <entries> --locale <locale> --alias <management-token-alias> --content-type <content-type> --stack-name <stack-name>",
20
+ "",
21
+ "Exporting organization users to csv",
22
+ "csdx cm:export-to-csv --action <users> --org <org-uid>",
23
+ "",
24
+ "Exporting organization users to csv with organization name provided",
25
+ "csdx cm:export-to-csv --action <users> --org <org-uid> --org-name <org-name>"
26
+ ],
27
+ "flags": {
28
+ "action": {
29
+ "name": "action",
30
+ "type": "option",
31
+ "description": "Option to export data (entries, users)",
32
+ "required": false,
33
+ "multiple": false,
34
+ "options": [
35
+ "entries",
36
+ "users"
37
+ ]
38
+ },
39
+ "alias": {
40
+ "name": "alias",
41
+ "type": "option",
42
+ "char": "a",
43
+ "description": "Alias of the management token",
44
+ "multiple": false
45
+ },
46
+ "org": {
47
+ "name": "org",
48
+ "type": "option",
49
+ "description": "Provide organization UID to clone org users",
50
+ "required": false,
51
+ "multiple": false
52
+ },
53
+ "stack-name": {
54
+ "name": "stack-name",
55
+ "type": "option",
56
+ "char": "n",
57
+ "description": "Name of the stack that needs to be created as csv filename.",
58
+ "required": false,
59
+ "multiple": false
60
+ },
61
+ "org-name": {
62
+ "name": "org-name",
63
+ "type": "option",
64
+ "description": "Name of the organization that needs to be created as csv filename.",
65
+ "required": false,
66
+ "multiple": false
67
+ },
68
+ "locale": {
69
+ "name": "locale",
70
+ "type": "option",
71
+ "description": "Locale for which entries need to be exported",
72
+ "required": false,
73
+ "multiple": false
74
+ },
75
+ "content-type": {
76
+ "name": "content-type",
77
+ "type": "option",
78
+ "description": "Content type for which entries needs to be exported",
79
+ "required": false,
80
+ "multiple": false
81
+ }
82
+ },
83
+ "args": {}
84
+ }
85
+ }
86
+ }
package/package.json CHANGED
@@ -1,15 +1,12 @@
1
1
  {
2
2
  "name": "@contentstack/cli-cm-export-to-csv",
3
3
  "description": "Export entities to csv",
4
- "version": "1.0.4",
4
+ "version": "1.2.0",
5
5
  "author": "Abhinav Gupta @abhinav-from-contentstack",
6
6
  "bugs": "https://github.com/contentstack/cli/issues",
7
7
  "dependencies": {
8
- "@contentstack/cli-command": "^1.0.4",
9
- "@contentstack/cli-utilities": "^1.0.5",
10
- "@contentstack/management": "^1.6.0",
11
- "@oclif/command": "^1.8.16",
12
- "@oclif/config": "^1.18.3",
8
+ "@contentstack/cli-command": "^1.2.0",
9
+ "@contentstack/cli-utilities": "^1.2.0",
13
10
  "chalk": "^4.1.0",
14
11
  "fast-csv": "^4.3.6",
15
12
  "inquirer": "8.2.4",
@@ -17,18 +14,18 @@
17
14
  "mkdirp": "^1.0.4"
18
15
  },
19
16
  "devDependencies": {
20
- "@oclif/dev-cli": "^1.26.0",
21
- "@oclif/test": "^1.2.8",
17
+ "@oclif/test": "^2.2.10",
22
18
  "chai": "^4.2.0",
23
19
  "debug": "^4.3.1",
24
20
  "eslint": "^8.18.0",
25
21
  "eslint-config-oclif": "^4.0.0",
26
22
  "globby": "^10.0.2",
27
23
  "mocha": "^10.0.0",
28
- "nyc": "^15.1.0"
24
+ "nyc": "^15.1.0",
25
+ "oclif": "^3.1.2"
29
26
  },
30
27
  "engines": {
31
- "node": ">=8.0.0"
28
+ "node": ">=14.0.0"
32
29
  },
33
30
  "files": [
34
31
  "/npm-shrinkwrap.json",
@@ -45,9 +42,10 @@
45
42
  "scripts": {
46
43
  "pack": "npm pack && mv *.tgz ../../build",
47
44
  "postpack": "rm -f oclif.manifest.json",
48
- "prepack": "oclif-dev manifest && oclif-dev readme",
45
+ "prepack": "oclif manifest && oclif readme",
49
46
  "test": "nyc mocha --forbid-only \"test/**/*.test.js\"",
50
- "version": "oclif-dev readme && git add README.md"
47
+ "version": "oclif readme && git add README.md",
48
+ "clean": "rm -rf ./node_modules tsconfig.build.tsbuildinfo"
51
49
  },
52
50
  "csdxConfig": {
53
51
  "expiredCommands": {}
@@ -1,6 +1,5 @@
1
- const { Command, flags } = require('@contentstack/cli-command');
2
- const { configHandler } = require('@contentstack/cli-utilities');
3
- const ContentstackManagementSDK = require('@contentstack/management');
1
+ const { Command } = require('@contentstack/cli-command');
2
+ const { configHandler, managementSDKClient, flags } = require('@contentstack/cli-utilities');
4
3
  const util = require('../../util');
5
4
  const config = require('../../util/config');
6
5
 
@@ -44,22 +43,9 @@ class ExportToCsvCommand extends Command {
44
43
  }),
45
44
  };
46
45
 
47
- get getAuthToken() {
48
- try {
49
- return this.authToken;
50
- } catch (error) {
51
- return undefined;
52
- }
53
- }
54
-
55
- get managementAPIClient() {
56
- this._managementAPIClient = ContentstackManagementSDK.client({ host: this.cmaHost, authtoken: this.getAuthToken });
57
- return this._managementAPIClient;
58
- }
59
-
60
46
  async run() {
61
47
  try {
62
- let action;
48
+ let action, managementAPIClient;
63
49
  const {
64
50
  flags: {
65
51
  org,
@@ -70,7 +56,11 @@ class ExportToCsvCommand extends Command {
70
56
  'content-type': contentTypesFlag,
71
57
  alias: managementTokenAlias,
72
58
  },
73
- } = this.parse(ExportToCsvCommand);
59
+ } = await this.parse(ExportToCsvCommand);
60
+
61
+ if (!managementTokenAlias) {
62
+ managementAPIClient = await managementSDKClient({ host: this.cmaHost });
63
+ }
74
64
 
75
65
  if (actionFlag) {
76
66
  action = actionFlag;
@@ -83,12 +73,16 @@ class ExportToCsvCommand extends Command {
83
73
  case 'entries': {
84
74
  try {
85
75
  let stack;
86
- let stackClient;
76
+ let stackAPIClient;
87
77
  let language;
88
78
  let contentTypes = [];
89
79
  const listOfTokens = configHandler.get('tokens');
90
80
 
91
81
  if (managementTokenAlias && listOfTokens[managementTokenAlias]) {
82
+ managementAPIClient = await managementSDKClient({
83
+ host: this.cmaHost,
84
+ management_token: listOfTokens[managementTokenAlias].token,
85
+ });
92
86
  stack = {
93
87
  name: stackName || managementTokenAlias,
94
88
  apiKey: listOfTokens[managementTokenAlias].apiKey,
@@ -99,7 +93,7 @@ class ExportToCsvCommand extends Command {
99
93
  } else {
100
94
  let organization;
101
95
 
102
- if (!this.getAuthToken) {
96
+ if (!this.isAuthenticated()) {
103
97
  this.error(config.CLI_EXPORT_CSV_ENTRIES_ERROR, {
104
98
  exit: 2,
105
99
  suggestions: ['https://www.contentstack.com/docs/developers/cli/authentication/'],
@@ -109,21 +103,21 @@ class ExportToCsvCommand extends Command {
109
103
  if (org) {
110
104
  organization = { uid: org };
111
105
  } else {
112
- organization = await util.chooseOrganization(this.managementAPIClient); // prompt for organization
106
+ organization = await util.chooseOrganization(managementAPIClient); // prompt for organization
113
107
  }
114
108
 
115
- stack = await util.chooseStack(this.managementAPIClient, organization.uid); // prompt for stack
109
+ stack = await util.chooseStack(managementAPIClient, organization.uid); // prompt for stack
116
110
  }
117
111
 
118
- stackClient = this.getStackClient(stack);
119
- const contentTypeCount = await util.getContentTypeCount(stackClient);
120
- const environments = await util.getEnvironments(stackClient); // fetch environments, because in publish details only env uid are available and we need env names
112
+ stackAPIClient = this.getStackClient(managementAPIClient, stack);
113
+ const contentTypeCount = await util.getContentTypeCount(stackAPIClient);
114
+ const environments = await util.getEnvironments(stackAPIClient); // fetch environments, because in publish details only env uid are available and we need env names
121
115
 
122
116
  if (contentTypesFlag) {
123
117
  contentTypes = contentTypesFlag.split(',').map(this.snakeCase);
124
118
  } else {
125
119
  for (let index = 0; index <= contentTypeCount / 100; index++) {
126
- const contentTypesMap = await util.getContentTypes(stackClient, index);
120
+ const contentTypesMap = await util.getContentTypes(stackAPIClient, index);
127
121
  contentTypes = contentTypes.concat(Object.values(contentTypesMap)); // prompt for content Type
128
122
  }
129
123
  }
@@ -140,16 +134,16 @@ class ExportToCsvCommand extends Command {
140
134
  if (locale) {
141
135
  language = { code: locale };
142
136
  } else {
143
- language = await util.chooseLanguage(stackClient); // prompt for language
137
+ language = await util.chooseLanguage(stackAPIClient); // prompt for language
144
138
  }
145
139
 
146
140
  while (contentTypes.length > 0) {
147
141
  let contentType = contentTypes.pop();
148
142
 
149
- const entriesCount = await util.getEntriesCount(stackClient, contentType, language.code);
143
+ const entriesCount = await util.getEntriesCount(stackAPIClient, contentType, language.code);
150
144
  let flatEntries = [];
151
145
  for (let index = 0; index < entriesCount / 100; index++) {
152
- const entriesResult = await util.getEntries(stackClient, contentType, language.code, index);
146
+ const entriesResult = await util.getEntries(stackAPIClient, contentType, language.code, index);
153
147
  const flatEntriesResult = util.cleanEntries(
154
148
  entriesResult.items,
155
149
  language.code,
@@ -170,7 +164,7 @@ class ExportToCsvCommand extends Command {
170
164
  case config.exportUsers:
171
165
  case 'users': {
172
166
  try {
173
- if (!this.getAuthToken) {
167
+ if (!this.isAuthenticated()) {
174
168
  this.error(config.CLI_EXPORT_CSV_LOGIN_FAILED, {
175
169
  exit: 2,
176
170
  suggestions: ['https://www.contentstack.com/docs/developers/cli/authentication/'],
@@ -181,11 +175,11 @@ class ExportToCsvCommand extends Command {
181
175
  if (org) {
182
176
  organization = { uid: org, name: orgName || org };
183
177
  } else {
184
- organization = await util.chooseOrganization(this.managementAPIClient, action); // prompt for organization
178
+ organization = await util.chooseOrganization(managementAPIClient, action); // prompt for organization
185
179
  }
186
180
 
187
- const orgUsers = await util.getOrgUsers(this.managementAPIClient, organization.uid, this);
188
- const orgRoles = await util.getOrgRoles(this.managementAPIClient, organization.uid, this);
181
+ const orgUsers = await util.getOrgUsers(managementAPIClient, organization.uid, this);
182
+ const orgRoles = await util.getOrgRoles(managementAPIClient, organization.uid, this);
189
183
  const mappedUsers = util.getMappedUsers(orgUsers);
190
184
  const mappedRoles = util.getMappedRoles(orgRoles);
191
185
  const listOfUsers = util.cleanOrgUsers(orgUsers, mappedUsers, mappedRoles);
@@ -213,14 +207,14 @@ class ExportToCsvCommand extends Command {
213
207
  return (string || '').split(' ').join('_').toLowerCase();
214
208
  }
215
209
 
216
- getStackClient(stack) {
210
+ getStackClient(managementAPIClient, stack) {
217
211
  if (stack.token) {
218
- return ContentstackManagementSDK.client({ host: this.cmaHost }).stack({
212
+ return managementAPIClient.stack({
219
213
  api_key: stack.apiKey,
220
214
  management_token: stack.token,
221
215
  });
222
216
  }
223
- return this.managementAPIClient.stack({ api_key: stack.apiKey });
217
+ return managementAPIClient.stack({ api_key: stack.apiKey });
224
218
  }
225
219
  }
226
220
 
package/src/util/index.js CHANGED
@@ -1,5 +1,4 @@
1
1
  const inquirer = require('inquirer');
2
- const { HttpClient } = require('@contentstack/cli-utilities');
3
2
  const os = require('os');
4
3
  const checkboxPlus = require('inquirer-checkbox-plus-prompt');
5
4
  const config = require('./config.js');
@@ -33,10 +32,13 @@ function chooseOrganization(managementAPIClient, action) {
33
32
  loop: false,
34
33
  },
35
34
  ];
36
- inquirer.prompt(_chooseOrganization).then(({ chosenOrg }) => {
37
- if (chosenOrg === config.cancelString) exitProgram();
38
- resolve({ name: chosenOrg, uid: organizations[chosenOrg] });
39
- });
35
+ inquirer
36
+ .prompt(_chooseOrganization)
37
+ .then(({ chosenOrg }) => {
38
+ if (chosenOrg === config.cancelString) exitProgram();
39
+ resolve({ name: chosenOrg, uid: organizations[chosenOrg] });
40
+ })
41
+ .catch(reject);
40
42
  } catch (error) {
41
43
  reject(error);
42
44
  }
@@ -98,10 +100,13 @@ function chooseStack(managementAPIClient, orgUid) {
98
100
  },
99
101
  ];
100
102
 
101
- inquirer.prompt(_chooseStack).then(({ chosenStack }) => {
102
- if (chosenStack === config.cancelString) exitProgram();
103
- resolve({ name: chosenStack, apiKey: stacks[chosenStack] });
104
- });
103
+ inquirer
104
+ .prompt(_chooseStack)
105
+ .then(({ chosenStack }) => {
106
+ if (chosenStack === config.cancelString) exitProgram();
107
+ resolve({ name: chosenStack, apiKey: stacks[chosenStack] });
108
+ })
109
+ .catch(reject);
105
110
  } catch (error) {
106
111
  reject(error);
107
112
  }
@@ -109,9 +114,6 @@ function chooseStack(managementAPIClient, orgUid) {
109
114
  }
110
115
 
111
116
  function getStacks(managementAPIClient, orgUid) {
112
- // Adding a query object in query, because it throws an error
113
- // the error is coming from query function lib/entity.js, @contentstack/management pacakge
114
- // where params.query is being set
115
117
  return new Promise((resolve, reject) => {
116
118
  let result = {};
117
119
  managementAPIClient
@@ -130,12 +132,10 @@ function getStacks(managementAPIClient, orgUid) {
130
132
  });
131
133
  }
132
134
 
133
- function chooseContentType(stack, skip) {
134
- return new Promise(async (resolve) => {
135
- let contentTypes = await getContentTypes(stack, skip);
135
+ function chooseContentType(stackAPIClient, skip) {
136
+ return new Promise(async (resolve, reject) => {
137
+ let contentTypes = await getContentTypes(stackAPIClient, skip);
136
138
  let contentTypesList = Object.values(contentTypes);
137
- // contentTypesList.push(config.cancelString)
138
-
139
139
  let _chooseContentType = [
140
140
  {
141
141
  type: 'checkbox',
@@ -146,9 +146,10 @@ function chooseContentType(stack, skip) {
146
146
  },
147
147
  ];
148
148
 
149
- inquirer.prompt(_chooseContentType).then(({ chosenContentTypes }) => {
150
- resolve(chosenContentTypes);
151
- });
149
+ inquirer
150
+ .prompt(_chooseContentType)
151
+ .then(({ chosenContentTypes }) => resolve(chosenContentTypes))
152
+ .catch(reject);
152
153
  });
153
154
  }
154
155
 
@@ -182,19 +183,22 @@ function chooseInMemContentTypes(contentTypesList) {
182
183
  },
183
184
  },
184
185
  ];
185
- inquirer.prompt(_chooseContentType).then(({ chosenContentTypes }) => {
186
- if (chosenContentTypes.length === 0) {
187
- reject('Please select atleast one content type.');
188
- }
189
- resolve(chosenContentTypes);
190
- });
186
+ inquirer
187
+ .prompt(_chooseContentType)
188
+ .then(({ chosenContentTypes }) => {
189
+ if (chosenContentTypes.length === 0) {
190
+ reject('Please select atleast one content type.');
191
+ }
192
+ resolve(chosenContentTypes);
193
+ })
194
+ .catch(reject);
191
195
  });
192
196
  }
193
197
 
194
- function getContentTypes(stack, skip) {
198
+ function getContentTypes(stackAPIClient, skip) {
195
199
  return new Promise((resolve, reject) => {
196
200
  let result = {};
197
- stack
201
+ stackAPIClient
198
202
  .contentType()
199
203
  .query({ skip: skip * 100 })
200
204
  .find()
@@ -208,9 +212,9 @@ function getContentTypes(stack, skip) {
208
212
  });
209
213
  }
210
214
 
211
- function chooseLanguage(stack) {
212
- return new Promise(async (resolve) => {
213
- let languages = await getLanguages(stack);
215
+ function chooseLanguage(stackAPIClient) {
216
+ return new Promise(async (resolve, reject) => {
217
+ let languages = await getLanguages(stackAPIClient);
214
218
  let languagesList = Object.keys(languages);
215
219
  languagesList.push(config.cancelString);
216
220
 
@@ -223,17 +227,20 @@ function chooseLanguage(stack) {
223
227
  },
224
228
  ];
225
229
 
226
- inquirer.prompt(_chooseLanguage).then(({ chosenLanguage }) => {
227
- if (chosenLanguage === config.cancelString) exitProgram();
228
- resolve({ name: chosenLanguage, code: languages[chosenLanguage] });
229
- });
230
+ inquirer
231
+ .prompt(_chooseLanguage)
232
+ .then(({ chosenLanguage }) => {
233
+ if (chosenLanguage === config.cancelString) exitProgram();
234
+ resolve({ name: chosenLanguage, code: languages[chosenLanguage] });
235
+ })
236
+ .catch(reject);
230
237
  });
231
238
  }
232
239
 
233
- function getLanguages(stack) {
240
+ function getLanguages(stackAPIClient) {
234
241
  return new Promise((resolve, reject) => {
235
242
  let result = {};
236
- stack
243
+ stackAPIClient
237
244
  .locale()
238
245
  .query()
239
246
  .find()
@@ -247,9 +254,9 @@ function getLanguages(stack) {
247
254
  });
248
255
  }
249
256
 
250
- function getEntries(stack, contentType, language, skip) {
257
+ function getEntries(stackAPIClient, contentType, language, skip) {
251
258
  return new Promise((resolve, reject) => {
252
- stack
259
+ stackAPIClient
253
260
  .contentType(contentType)
254
261
  .entry()
255
262
  .query({ include_publish_details: true, locale: language, skip: skip * 100 })
@@ -259,9 +266,9 @@ function getEntries(stack, contentType, language, skip) {
259
266
  });
260
267
  }
261
268
 
262
- function getEntriesCount(stack, contentType, language) {
269
+ function getEntriesCount(stackAPIClient, contentType, language) {
263
270
  return new Promise((resolve, reject) => {
264
- stack
271
+ stackAPIClient
265
272
  .contentType(contentType)
266
273
  .entry()
267
274
  .query({ include_publish_details: true, locale: language })
@@ -271,9 +278,9 @@ function getEntriesCount(stack, contentType, language) {
271
278
  });
272
279
  }
273
280
 
274
- function getEnvironments(stack) {
281
+ function getEnvironments(stackAPIClient) {
275
282
  let result = {};
276
- return stack
283
+ return stackAPIClient
277
284
  .environment()
278
285
  .query()
279
286
  .find()
@@ -285,15 +292,13 @@ function getEnvironments(stack) {
285
292
  });
286
293
  }
287
294
 
288
- function getContentTypeCount(stack) {
295
+ function getContentTypeCount(stackAPIClient) {
289
296
  return new Promise((resolve, reject) => {
290
- stack
297
+ stackAPIClient
291
298
  .contentType()
292
299
  .query()
293
300
  .count()
294
- .then((contentTypes) => {
295
- resolve(contentTypes.content_types);
296
- })
301
+ .then((contentTypes) => resolve(contentTypes.content_types))
297
302
  .catch((error) => reject(error));
298
303
  });
299
304
  }
@@ -363,7 +368,7 @@ function write(command, entries, fileName, message) {
363
368
  }
364
369
 
365
370
  function startupQuestions() {
366
- return new Promise((resolve) => {
371
+ return new Promise((resolve, reject) => {
367
372
  let actions = [
368
373
  {
369
374
  type: 'list',
@@ -372,10 +377,13 @@ function startupQuestions() {
372
377
  choices: [config.exportEntries, config.exportUsers, 'Exit'],
373
378
  },
374
379
  ];
375
- inquirer.prompt(actions).then((answers) => {
376
- if (answers.action === 'Exit') exitProgram();
377
- resolve(answers.action);
378
- });
380
+ inquirer
381
+ .prompt(actions)
382
+ .then((answers) => {
383
+ if (answers.action === 'Exit') exitProgram();
384
+ resolve(answers.action);
385
+ })
386
+ .catch(reject);
379
387
  });
380
388
  }
381
389
 
@@ -386,12 +394,17 @@ function getOrgUsers(managementAPIClient, orgUid, ecsv) {
386
394
  .then(async (response) => {
387
395
  let organization = response.organizations.filter((org) => org.uid === orgUid).pop();
388
396
  if (organization.is_owner === true) {
389
- let cma = ecsv.region.cma;
390
- let authtoken = ecsv.authToken;
391
- return HttpClient.create()
392
- .headers({ authtoken: authtoken })
393
- .get(`${cma}/v3/organizations/${organization.uid}/share`)
394
- .then((_response) => resolve({ items: _response.data.shares }));
397
+ return managementAPIClient
398
+ .organization(organization.uid)
399
+ .fetch()
400
+ .then((_response) => {
401
+ _response
402
+ .getInvitations()
403
+ .then((_data) => {
404
+ resolve({ items: _data.items });
405
+ })
406
+ .catch(reject);
407
+ });
395
408
  }
396
409
  if (!organization.getInvitations) {
397
410
  return reject(new Error(config.adminError));
@@ -420,6 +433,7 @@ async function getUsers(organization, params, result = []) {
420
433
  return getUsers(organization, params, result);
421
434
  }
422
435
  } catch (error) {
436
+ console.error(error);
423
437
  throw error;
424
438
  }
425
439
  }
@@ -448,16 +462,25 @@ function getOrgRoles(managementAPIClient, orgUid, ecsv) {
448
462
  .then((response) => {
449
463
  let organization = response.organizations.filter((org) => org.uid === orgUid).pop();
450
464
  if (organization.is_owner === true) {
451
- let cma = ecsv.region.cma;
452
- let authtoken = ecsv.authToken;
453
- return axios
454
- .get(`${cma}/v3/organizations/${organization.uid}/roles`, { headers: { authtoken: authtoken } })
455
- .then((_response) => resolve({ items: _response.data.roles }));
465
+ return managementAPIClient
466
+ .organization(organization.uid)
467
+ .fetch()
468
+ .then((_response) => {
469
+ _response
470
+ .roles()
471
+ .then((_data) => {
472
+ resolve({ items: _data.items });
473
+ })
474
+ .catch(reject);
475
+ });
456
476
  }
457
477
  if (!organization.roles) {
458
478
  return reject(new Error(config.adminError));
459
479
  }
460
- organization.roles().then((roles) => resolve(roles));
480
+ organization
481
+ .roles()
482
+ .then((roles) => resolve(roles))
483
+ .catch(reject);
461
484
  })
462
485
  .catch((error) => reject(error));
463
486
  });
@@ -540,10 +563,20 @@ function formatError(error) {
540
563
  if (error.errors && Object.keys(error.errors).length > 0) {
541
564
  Object.keys(error.errors).forEach((e) => {
542
565
  let entity = e;
543
- if (e === 'authorization') entity = 'Management Token';
544
- if (e === 'api_key') entity = 'Stack API key';
545
- if (e === 'uid') entity = 'Content Type';
546
- if (e === 'access_token') entity = 'Delivery Token';
566
+ switch (e) {
567
+ case 'authorization':
568
+ entity = 'Management Token';
569
+ break;
570
+ case 'api_key':
571
+ entity = 'Stack API key';
572
+ break;
573
+ case 'uid':
574
+ entity = 'Content Type';
575
+ break;
576
+ case 'access_token':
577
+ entity = 'Delivery Token';
578
+ break;
579
+ }
547
580
  message += ' ' + [entity, error.errors[e]].join(' ');
548
581
  });
549
582
  }
@@ -551,7 +584,7 @@ function formatError(error) {
551
584
  }
552
585
 
553
586
  function wait(time) {
554
- return new Promise(res => {
587
+ return new Promise((res) => {
555
588
  setTimeout(res, time);
556
589
  });
557
590
  }