diff --git a/packages/contentstack-export-to-csv/src/commands/cm/export-to-csv.js b/packages/contentstack-export-to-csv/src/commands/cm/export-to-csv.js index 0f4f0aafd3..34669b3adb 100644 --- a/packages/contentstack-export-to-csv/src/commands/cm/export-to-csv.js +++ b/packages/contentstack-export-to-csv/src/commands/cm/export-to-csv.js @@ -257,17 +257,14 @@ class ExportToCsvCommand extends Command { case config.exportTeams: case 'teams': { try{ - let organization; - if (org) { organization = { uid: org, name: orgName || org }; } else { organization = await util.chooseOrganization(managementAPIClient, action); // prompt for organization } - + await util.exportTeams(managementAPIClient,organization,teamUid); - } catch (error) { if (error.message || error.errorMessage) { cliux.error(util.formatError(error)); diff --git a/packages/contentstack-export-to-csv/src/util/index.js b/packages/contentstack-export-to-csv/src/util/index.js index 740a923e2e..cec5a1b49e 100644 --- a/packages/contentstack-export-to-csv/src/util/index.js +++ b/packages/contentstack-export-to-csv/src/util/index.js @@ -464,7 +464,7 @@ function startupQuestions() { type: 'list', name: 'action', message: 'Choose Action', - choices: [config.exportEntries, config.exportUsers, 'Exit'], + choices: [config.exportEntries, config.exportUsers, config.exportTeams, 'Exit'], }, ]; inquirer @@ -684,7 +684,7 @@ function wait(time) { } function handleErrorMsg(err) { - cliux.print(`Error: ${err?.errorMessage ? err?.message: messageHandler.parse('CLI_EXPORT_CSV_API_FAILED')}`, { color: 'red' }); + cliux.print(`Error: ${(err?.errorMessage || err?.message) ? err?.errorMessage || err?.message : messageHandler.parse('CLI_EXPORT_CSV_API_FAILED')}`, { color: 'red' }) process.exit(1); } @@ -699,13 +699,13 @@ async function apiRequestHandler(org, queryParam = {}) { return await new HttpClient() .headers(headers) .queryParams(queryParam) - .get(`${configHandler.get('region')?.cma}/organizations/${org.uid}/teams`) + .get(`${configHandler.get('region')?.cma}/organizations/${org?.uid}/teams`) .then((res) => { const { status, data } = res; - if(status===200) { + if (status === 200) { return data; } else { - cliux.print(`${data.error_message || data.message || data.errorMessage}`, { color: 'red' }); + cliux.print(`${data?.error_message || data?.message || data?.errorMessage}`, { color: 'red' }); process.exit(1); } }) @@ -717,7 +717,7 @@ async function apiRequestHandler(org, queryParam = {}) { async function exportOrgTeams(managementAPIClient, org) { let teamsObjectArray = []; let skip = 0; - let limit = config.limit || 100; + let limit = config?.limit || 100; do { const data = await apiRequestHandler(org, { skip: skip, limit: limit, includeUserDetails: true }); skip += limit; @@ -783,6 +783,14 @@ async function cleanTeamsData(data, managementAPIClient, org) { } async function exportTeams(managementAPIClient, organization, teamUid) { + cliux.print( + `info: Exporting the ${ + teamUid && organization?.name + ? `team with uid ${teamUid} in Organisation ${organization?.name} ` + : `teams of Organisation ` + organization?.name + }`, + { color: 'blue' }, + ); const allTeamsData = await exportOrgTeams(managementAPIClient, organization); if (!allTeamsData?.length) { cliux.print(`info: There are not teams in the organization named ${organization?.name}`); @@ -795,24 +803,33 @@ async function exportTeams(managementAPIClient, organization, teamUid) { const fileName = `${kebabize(organization.name.replace(config.organizationNameRegex, ''))}_teams_export.csv`; write(this, modifiedTeam, fileName, ' organization Team details'); // exporting teams user data or a single team user data - await getTeamsDetail(allTeamsData,organization,teamUid); + cliux.print( + `info: Exporting the teams user data for ${teamUid ? `Team ` + teamUid : `Organisation ` + organization?.name}`, + { color: 'blue' }, + ); + await getTeamsDetail(allTeamsData, organization, teamUid); + cliux.print( + `info: Exporting the Stack Role Details for ${ + teamUid ? `Team ` + teamUid : `Organisation ` + organization?.name + }`, + { color: 'blue' }, + ); + // Exporting the stack Role data for all the teams or exporting stack role data for a single team + await exportRoleMappings(managementAPIClient, allTeamsData, teamUid); } } -async function getTeamsDetail(allTeamsData, organization ,teamUid) { +async function getTeamsDetail(allTeamsData, organization, teamUid) { if (!teamUid) { - const userData = await getTeamsUserDetails(allTeamsData); const fileName = `${kebabize( organization.name.replace(config.organizationNameRegex, ''), )}_team_User_Details_export.csv`; write(this, userData, fileName, 'Team User details'); - } else { - const team = allTeamsData.filter((team) => team.uid === teamUid)[0]; - + team.users.forEach((user) => { user['team-name'] = team.name; user['team-uid'] = team.uid; @@ -823,12 +840,61 @@ async function getTeamsDetail(allTeamsData, organization ,teamUid) { const fileName = `${kebabize( organization.name.replace(config.organizationNameRegex, ''), )}_team_${teamUid}_User_Details_export.csv`; - + write(this, team.users, fileName, 'Team User details'); - } } +async function exportRoleMappings(managementAPIClient, allTeamsData, teamUid) { + let stackRoleWithTeamData = []; + if (teamUid) { + const team = find(allTeamsData,function(teamObject) { return teamObject?.uid===teamUid }); + for (const stack of team?.stackRoleMapping) { + const roleData = await mapRoleWithTeams(managementAPIClient, stack, team?.name, team?.uid); + stackRoleWithTeamData.push(...roleData); + } + } else { + for (const team of allTeamsData ?? []) { + for (const stack of team?.stackRoleMapping ?? []) { + const roleData = await mapRoleWithTeams(managementAPIClient, stack, team?.name, team?.uid); + stackRoleWithTeamData.push(...roleData); + } + } + } + const fileName = `${kebabize('Stack_Role_Mapping'.replace(config.organizationNameRegex, ''))}${ + teamUid ? teamUid : '' + }.csv`; + + write(this, stackRoleWithTeamData, fileName, 'Team Stack Role details'); +} + +async function mapRoleWithTeams(managementAPIClient, stackRoleMapping, teamName, teamUid) { + const roles = await getRoleData(managementAPIClient, stackRoleMapping.stackApiKey); + const stackRole = {}; + roles?.items.forEach((role) => { + if (!stackRole.hasOwnProperty(role?.uid)) { + stackRole[role?.uid] = role?.name; + stackRole[role?.stack?.api_key] = {name: role?.stack?.name, uid: role?.stack?.uid } + } + }); + const stackRoleMapOfTeam = stackRoleMapping?.roles.map((role) => { + return { + 'Team Name': teamName, + 'Team Uid': teamUid, + 'Stack Name': stackRole[stackRoleMapping?.stackApiKey]?.name, + 'Stack Uid': stackRole[stackRoleMapping?.stackApiKey]?.uid, + 'Role Name': stackRole[role], + 'Role Uid': role, + }; + }); + + return stackRoleMapOfTeam; +} + +async function getRoleData(managementAPIClient, stackApiKey) { + return await managementAPIClient.stack({ api_key: stackApiKey }).role().fetchAll(); +} + async function getTeamsUserDetails(teamsObject) { const allTeamUsers = []; teamsObject.forEach((team) => {