Skip to content

Commit

Permalink
Merge pull request #553 from contentstack/fix/users_pagination
Browse files Browse the repository at this point in the history
Fix/users pagination
  • Loading branch information
netrajpatel authored Nov 29, 2022
2 parents 43c2f12 + fe1f920 commit 2db3f00
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions packages/contentstack-export-to-csv/src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ function getOrgUsers(managementAPIClient, orgUid, ecsv) {
return new Promise((resolve, reject) => {
managementAPIClient
.getUser({ include_orgs_roles: true })
.then((response) => {
.then(async (response) => {
let organization = response.organizations.filter((org) => org.uid === orgUid).pop();
if (organization.is_owner === true) {
let cma = ecsv.region.cma;
Expand All @@ -396,12 +396,34 @@ function getOrgUsers(managementAPIClient, orgUid, ecsv) {
if (!organization.getInvitations) {
return reject(new Error(config.adminError));
}
organization.getInvitations().then((users) => resolve(users));
try {
const users = await getUsers(organization, { skip: 0, page: 1, limit: 100 });
return resolve({ items: users });
} catch (error) {
return reject(error);
}
})
.catch((error) => reject(error));
});
}

async function getUsers(organization, params, result = []) {
try {
const users = await organization.getInvitations(params);
if (!users.items || (users.items && !users.items.length)) {
return result;
} else {
result = result.concat(users.items);
params.skip = params.page * params.limit;
params.page++;
await wait(200);
return getUsers(organization, params, result);
}
} catch (error) {
throw error;
}
}

function getMappedUsers(users) {
let mappedUsers = {};
users.items.forEach((user) => {
Expand Down Expand Up @@ -528,6 +550,12 @@ function formatError(error) {
return message;
}

function wait(time) {
return new Promise(res => {
setTimeout(res, time);
});
}

module.exports = {
chooseOrganization: chooseOrganization,
chooseStack: chooseStack,
Expand Down

0 comments on commit 2db3f00

Please sign in to comment.