diff --git a/components/projects/project.ts b/components/projects/project.ts index 53ff759..2579316 100644 --- a/components/projects/project.ts +++ b/components/projects/project.ts @@ -55,7 +55,7 @@ export default class Project { delete nluOptions.privateNlu; } - const requestBody = { ...projectData, options: { ...options, nluOptions } }; + const requestBody = { ...projectData, options: { ...options, ...nluOptions } }; try { const { response } = await this.helper.toPromise( diff --git a/components/users/team.ts b/components/users/team.ts index 29753ef..7a1ac57 100644 --- a/components/users/team.ts +++ b/components/users/team.ts @@ -16,8 +16,7 @@ export default class Team extends Component { if (this.checkUser(userInfo.id, teamMember)) { throw new Error(`User ${username} already on this team`); } - - const { response } = await this.helper.toPromise(this.api.teamApi, this.api.teamApi.teamsTeamIdUsersUserIdPost, teamInfo.data.id, userInfo.id, { roleId: role } ); + const { response } = await this.helper.toPromise(this.api.teamApi, this.api.teamApi.teamsTeamIdUsersUserIdPost, teamInfo.id, userInfo.id, { roleId: role } ); if (!response.body) { throw new Error("Error adding user to team: invalid roleId"); } @@ -31,7 +30,7 @@ export default class Team extends Component { } } - public async removeMember(username : string) { + public async removeMember(username: string) { const answer = await this.helper.inquirerPrompt([ { type: "confirm", @@ -52,7 +51,7 @@ export default class Team extends Component { throw new Error(`User ${username} not a member of this team`); } - const { response } = await this.helper.toPromise(this.api.teamApi, this.api.teamApi.teamsTeamIdUsersUserIdDelete, teamInfo.data.id, userInfo.id); + const { response } = await this.helper.toPromise(this.api.teamApi, this.api.teamApi.teamsTeamIdUsersUserIdDelete, teamInfo.id, userInfo.id); console.log(`Success remove ${username} from ${currentLogin}`); } else { @@ -64,28 +63,54 @@ export default class Team extends Component { } } - private async getInfo(username : string) { + private async getInfo(username: string) { const currentLogin = this.helper.getProp("current_login") as string; const currentUserType = this.helper.getProp("current_user_type") as string; if (currentUserType !== "team") { throw new Error("Must be on team to do this operation"); } + + const requestTeamData = + await this.helper.toPromise(this.api.userApi, this.api.userApi.usersUserIdGet, currentLogin); + + let teamInfo: any; + if (requestTeamData.response && requestTeamData.response.body) { + teamInfo = requestTeamData.response.body; + } else { + throw new Error("Cannot add user to team"); + } - const team = await this.helper.toPromise(this.api.userApi, this.api.userApi.usersUserIdGet, currentLogin); - const { data } = await this.helper.toPromise(this.api.userApi, this.api.userApi.usersUserIdGet, username); - const member = await this.helper.toPromise(this.api.teamApi, this.api.teamApi.teamsTeamIdUsersGet, team.data.id); + const requestUserData = + await this.helper.toPromise(this.api.userApi, this.api.userApi.usersUserIdGet, username); + + let userInfo: any; + if (requestUserData && requestUserData.response) { + userInfo = requestUserData.response.body; + } else { + throw new Error("Cannot add user to team"); + } + + const requestTeamMember = + await this.helper.toPromise(this.api.teamApi, this.api.teamApi.teamsTeamIdUsersGet, teamInfo.id); + + let teamMember: any[]; + if (requestTeamMember && requestTeamMember.response) { + teamMember = requestTeamMember.response.body; + } else { + throw new Error("Cannot add user to team"); + } return { - teamInfo: team, - userInfo: data, - teamMember: member, + teamInfo, + userInfo, + teamMember, currentLogin }; } - private checkUser(userId : string, member : any) : boolean { - const teamMember = member.response.body.map((x : JsonObject) => x.userId); + private checkUser(userId: string, member: any[]): boolean { + const teamMember = member.map((x: JsonObject) => x.userId); if (teamMember.indexOf(userId) > -1) { return true; } diff --git a/components/users/user.ts b/components/users/user.ts index 7fed980..838d056 100644 --- a/components/users/user.ts +++ b/components/users/user.ts @@ -119,14 +119,22 @@ export default class User extends Component { try { const firstLogin = this.helper.getProp("first_login") as JsonObject; const currentType = this.helper.getProp("current_user_type"); + const currentLogin = this.helper.getProp("current_login"); + const username = name ? name : currentLogin; - if (currentType === type) { - throw new Error(`Unable to switch : already on ${type}`); + if (currentType === type && username === currentLogin) { + throw new Error(`Unable to switch : already on ${currentLogin} ${type}`); } + if (type === "team") { - const info = await this.helper.toPromise(this.api.userApi, this.api.userApi.usersUserIdGet, firstLogin.id); - const teams = info ? info.data.teams.filter((team: any) => team.username === name) : []; + const { response } = await this.helper.toPromise(this.api.userApi, this.api.userApi.usersUserIdGet, firstLogin.id); + + if (!response) { + throw new Error("Unable to switch team"); + } + + const teams = response && response.body ? response.body.teams.filter((team: any) => team.username === name) : []; if (teams.length > 0) { const result = await this.helper.toPromise(this.api.authApi, this.api.authApi.tokensPost, { type: "team", teamId: teams[0].teamId }); const token = result.data.id; @@ -187,9 +195,6 @@ export default class User extends Component { const { response } = await this.helper.toPromise(this.api.teamApi, this.api.teamApi.teamsPost, { username: name, password: "", roleId: "teamAdmin" }); if (response && response.body.id) { - const { data } = await this.helper.toPromise(this.api.userApi, this.api.userApi.usersUserIdGet, currentLogin); - const result = await this.helper.toPromise(this.api.teamApi, this.api.teamApi.teamsTeamIdUsersUserIdPost, response.body.id, data.id, { roleId: "teamAdmin" }); - console.log(`Team ${name} created !`); } else { console.log(`Team ${name} exist !`); diff --git a/lib/components/projects/project.js b/lib/components/projects/project.js index a844a66..05b0e5a 100644 --- a/lib/components/projects/project.js +++ b/lib/components/projects/project.js @@ -58,7 +58,7 @@ class Project { nluOptions.nluVisibility = nluOptions.privateNlu ? "private" : "public"; delete nluOptions.privateNlu; } - const requestBody = Object.assign({}, projectData, { options: Object.assign({}, options, { nluOptions }) }); + const requestBody = Object.assign({}, projectData, { options: Object.assign({}, options, nluOptions) }); try { const { response } = yield this.helper.toPromise(this.api.projectApi, this.api.projectApi.projectsPost, requestBody); if (response && response.body && response.body.id) { diff --git a/lib/components/users/team.js b/lib/components/users/team.js index 4341710..64da88f 100644 --- a/lib/components/users/team.js +++ b/lib/components/users/team.js @@ -24,7 +24,7 @@ class Team extends merapi_1.Component { if (this.checkUser(userInfo.id, teamMember)) { throw new Error(`User ${username} already on this team`); } - const { response } = yield this.helper.toPromise(this.api.teamApi, this.api.teamApi.teamsTeamIdUsersUserIdPost, teamInfo.data.id, userInfo.id, { roleId: role }); + const { response } = yield this.helper.toPromise(this.api.teamApi, this.api.teamApi.teamsTeamIdUsersUserIdPost, teamInfo.id, userInfo.id, { roleId: role }); if (!response.body) { throw new Error("Error adding user to team: invalid roleId"); } @@ -58,7 +58,7 @@ class Team extends merapi_1.Component { if (!this.checkUser(userInfo.id, teamMember)) { throw new Error(`User ${username} not a member of this team`); } - const { response } = yield this.helper.toPromise(this.api.teamApi, this.api.teamApi.teamsTeamIdUsersUserIdDelete, teamInfo.data.id, userInfo.id); + const { response } = yield this.helper.toPromise(this.api.teamApi, this.api.teamApi.teamsTeamIdUsersUserIdDelete, teamInfo.id, userInfo.id); console.log(`Success remove ${username} from ${currentLogin}`); } else { @@ -77,19 +77,40 @@ class Team extends merapi_1.Component { if (currentUserType !== "team") { throw new Error("Must be on team to do this operation"); } - const team = yield this.helper.toPromise(this.api.userApi, this.api.userApi.usersUserIdGet, currentLogin); - const { data } = yield this.helper.toPromise(this.api.userApi, this.api.userApi.usersUserIdGet, username); - const member = yield this.helper.toPromise(this.api.teamApi, this.api.teamApi.teamsTeamIdUsersGet, team.data.id); + const requestTeamData = yield this.helper.toPromise(this.api.userApi, this.api.userApi.usersUserIdGet, currentLogin); + let teamInfo; + if (requestTeamData.response && requestTeamData.response.body) { + teamInfo = requestTeamData.response.body; + } + else { + throw new Error("Cannot add user to team"); + } + const requestUserData = yield this.helper.toPromise(this.api.userApi, this.api.userApi.usersUserIdGet, username); + let userInfo; + if (requestUserData && requestUserData.response) { + userInfo = requestUserData.response.body; + } + else { + throw new Error("Cannot add user to team"); + } + const requestTeamMember = yield this.helper.toPromise(this.api.teamApi, this.api.teamApi.teamsTeamIdUsersGet, teamInfo.id); + let teamMember; + if (requestTeamMember && requestTeamMember.response) { + teamMember = requestTeamMember.response.body; + } + else { + throw new Error("Cannot add user to team"); + } return { - teamInfo: team, - userInfo: data, - teamMember: member, + teamInfo, + userInfo, + teamMember, currentLogin }; }); } checkUser(userId, member) { - const teamMember = member.response.body.map((x) => x.userId); + const teamMember = member.map((x) => x.userId); if (teamMember.indexOf(userId) > -1) { return true; } diff --git a/lib/components/users/user.js b/lib/components/users/user.js index 4609f53..0b4256b 100644 --- a/lib/components/users/user.js +++ b/lib/components/users/user.js @@ -124,12 +124,17 @@ class User extends merapi_1.Component { try { const firstLogin = this.helper.getProp("first_login"); const currentType = this.helper.getProp("current_user_type"); - if (currentType === type) { - throw new Error(`Unable to switch : already on ${type}`); + const currentLogin = this.helper.getProp("current_login"); + const username = name ? name : currentLogin; + if (currentType === type && username === currentLogin) { + throw new Error(`Unable to switch : already on ${currentLogin} ${type}`); } if (type === "team") { - const info = yield this.helper.toPromise(this.api.userApi, this.api.userApi.usersUserIdGet, firstLogin.id); - const teams = info ? info.data.teams.filter((team) => team.username === name) : []; + const { response } = yield this.helper.toPromise(this.api.userApi, this.api.userApi.usersUserIdGet, firstLogin.id); + if (!response) { + throw new Error("Unable to switch team"); + } + const teams = response && response.body ? response.body.teams.filter((team) => team.username === name) : []; if (teams.length > 0) { const result = yield this.helper.toPromise(this.api.authApi, this.api.authApi.tokensPost, { type: "team", teamId: teams[0].teamId }); const token = result.data.id; @@ -190,8 +195,6 @@ class User extends merapi_1.Component { } const { response } = yield this.helper.toPromise(this.api.teamApi, this.api.teamApi.teamsPost, { username: name, password: "", roleId: "teamAdmin" }); if (response && response.body.id) { - const { data } = yield this.helper.toPromise(this.api.userApi, this.api.userApi.usersUserIdGet, currentLogin); - const result = yield this.helper.toPromise(this.api.teamApi, this.api.teamApi.teamsTeamIdUsersUserIdPost, response.body.id, data.id, { roleId: "teamAdmin" }); console.log(`Team ${name} created !`); } else {