Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/CS-41983 - Exporting the stack role mappings for either all the teams in the org or for specific team #1121

Merged
merged 10 commits into from
Oct 26, 2023
92 changes: 79 additions & 13 deletions packages/contentstack-export-to-csv/src/util/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -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);
}
})
Expand All @@ -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;
Expand Down Expand Up @@ -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}`);
Expand All @@ -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;
Expand All @@ -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 Stack = await getStackData(managementAPIClient, stackRoleMapping.stackApiKey);
const stackRole = {};
const roles = await Stack.role().fetchAll();
roles?.items.forEach((role) => {
if (!stackRole.hasOwnProperty(role?.uid)) {
stackRole[role?.uid] = role?.name;
}
});
const stackRoleMapOfTeam = stackRoleMapping?.roles.map((role) => {
return {
'Team Name': teamName,
'Team Uid': teamUid,
'Stack Name': Stack?.name,
'Stack Uid': Stack?.uid,
'Role Name': stackRole[role],
'Role Uid': role,
};
});

return stackRoleMapOfTeam;
}

async function getStackData(managementAPIClient, stackApiKey) {
return await managementAPIClient.stack({ api_key: stackApiKey }).fetch();
}

async function getTeamsUserDetails(teamsObject) {
const allTeamUsers = [];
teamsObject.forEach((team) => {
Expand Down