From 2e2d560d5b9b5bbf7f8e425e22dd107974bab931 Mon Sep 17 00:00:00 2001 From: rizkix Date: Tue, 11 Dec 2018 10:23:06 +0700 Subject: [PATCH 1/9] hotfix: move nluId and nluVisibility to parent object --- components/projects/project.ts | 2 +- lib/components/projects/project.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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/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) { From ffa5851a1b74e918a198917a63fd4801dc13bf90 Mon Sep 17 00:00:00 2001 From: rizkix Date: Tue, 11 Dec 2018 10:27:55 +0700 Subject: [PATCH 2/9] hotfix: create team error namespace --- components/users/user.ts | 3 --- lib/components/users/user.js | 2 -- 2 files changed, 5 deletions(-) diff --git a/components/users/user.ts b/components/users/user.ts index 7fed980..262cbda 100644 --- a/components/users/user.ts +++ b/components/users/user.ts @@ -187,9 +187,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/users/user.js b/lib/components/users/user.js index 4609f53..a77d30b 100644 --- a/lib/components/users/user.js +++ b/lib/components/users/user.js @@ -190,8 +190,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 { From 4888161dbfd1690c5b8e542b90e7f99fe8ee99a2 Mon Sep 17 00:00:00 2001 From: rizkix Date: Tue, 11 Dec 2018 11:17:39 +0700 Subject: [PATCH 3/9] hotfix: switch team error --- components/users/user.ts | 5 +++-- lib/components/users/user.js | 5 +++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/components/users/user.ts b/components/users/user.ts index 262cbda..eb21c55 100644 --- a/components/users/user.ts +++ b/components/users/user.ts @@ -119,14 +119,15 @@ 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"); if (currentType === type) { throw new Error(`Unable to switch : already on ${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); + 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; diff --git a/lib/components/users/user.js b/lib/components/users/user.js index a77d30b..46f6fc3 100644 --- a/lib/components/users/user.js +++ b/lib/components/users/user.js @@ -124,12 +124,13 @@ class User extends merapi_1.Component { try { const firstLogin = this.helper.getProp("first_login"); const currentType = this.helper.getProp("current_user_type"); + const currentLogin = this.helper.getProp("current_login"); if (currentType === type) { throw new Error(`Unable to switch : already on ${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); + 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; From 42621e1902d42585d387a50799dd1a1723022c27 Mon Sep 17 00:00:00 2001 From: rizkix Date: Tue, 11 Dec 2018 14:54:22 +0700 Subject: [PATCH 4/9] hotfix: add/remove member to team --- components/users/team.ts | 43 ++++++++++++++++++++++++++---------- components/users/user.ts | 1 - lib/components/users/team.js | 39 ++++++++++++++++++++++++-------- lib/components/users/user.js | 1 - 4 files changed, 61 insertions(+), 23 deletions(-) diff --git a/components/users/team.ts b/components/users/team.ts index 29753ef..33923f9 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,48 @@ 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); + 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 eb21c55..699de79 100644 --- a/components/users/user.ts +++ b/components/users/user.ts @@ -119,7 +119,6 @@ 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"); if (currentType === type) { throw new Error(`Unable to switch : already on ${type}`); 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 46f6fc3..3338bc5 100644 --- a/lib/components/users/user.js +++ b/lib/components/users/user.js @@ -124,7 +124,6 @@ class User extends merapi_1.Component { try { const firstLogin = this.helper.getProp("first_login"); const currentType = this.helper.getProp("current_user_type"); - const currentLogin = this.helper.getProp("current_login"); if (currentType === type) { throw new Error(`Unable to switch : already on ${type}`); } From 4671a261fe9df337c5a56a24be603e460e9b610b Mon Sep 17 00:00:00 2001 From: rizkix Date: Tue, 11 Dec 2018 14:56:42 +0700 Subject: [PATCH 5/9] cleaning up --- components/users/team.ts | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/components/users/team.ts b/components/users/team.ts index 33923f9..c53bcf0 100644 --- a/components/users/team.ts +++ b/components/users/team.ts @@ -71,7 +71,9 @@ export default class Team extends Component { 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); + 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; @@ -79,7 +81,9 @@ export default class Team extends Component { throw new Error("Cannot add user to team"); } - const requestUserData = await this.helper.toPromise(this.api.userApi, this.api.userApi.usersUserIdGet, username); + 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; @@ -87,7 +91,9 @@ export default class Team extends Component { throw new Error("Cannot add user to team"); } - const requestTeamMember = await this.helper.toPromise(this.api.teamApi, this.api.teamApi.teamsTeamIdUsersGet, teamInfo.id); + 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; From 0c1fa0c96ad5d4dcaf756c30b80c37a05101da63 Mon Sep 17 00:00:00 2001 From: rizkix Date: Tue, 11 Dec 2018 15:06:13 +0700 Subject: [PATCH 6/9] cleaning up 2 --- components/users/user.ts | 5 +++++ lib/components/users/user.js | 3 +++ 2 files changed, 8 insertions(+) diff --git a/components/users/user.ts b/components/users/user.ts index 699de79..b30095e 100644 --- a/components/users/user.ts +++ b/components/users/user.ts @@ -126,6 +126,11 @@ export default class User extends Component { if (type === "team") { 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 }); diff --git a/lib/components/users/user.js b/lib/components/users/user.js index 3338bc5..406160f 100644 --- a/lib/components/users/user.js +++ b/lib/components/users/user.js @@ -129,6 +129,9 @@ class User extends merapi_1.Component { } if (type === "team") { 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 }); From 3ac5f43796b8f5ac4a0bb87912f40c7f0aff9540 Mon Sep 17 00:00:00 2001 From: rizkix Date: Tue, 11 Dec 2018 16:05:29 +0700 Subject: [PATCH 7/9] fix from review --- components/users/team.ts | 4 ++-- components/users/user.ts | 2 +- lib/components/users/user.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/components/users/team.ts b/components/users/team.ts index c53bcf0..7a1ac57 100644 --- a/components/users/team.ts +++ b/components/users/team.ts @@ -94,7 +94,7 @@ export default class Team extends Component { const requestTeamMember = await this.helper.toPromise(this.api.teamApi, this.api.teamApi.teamsTeamIdUsersGet, teamInfo.id); - let teamMember: any; + let teamMember: any[]; if (requestTeamMember && requestTeamMember.response) { teamMember = requestTeamMember.response.body; } else { @@ -109,7 +109,7 @@ export default class Team extends Component { }; } - private checkUser(userId : string, member : any) : boolean { + 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 b30095e..ff380db 100644 --- a/components/users/user.ts +++ b/components/users/user.ts @@ -128,7 +128,7 @@ export default class User extends Component { const { response } = await this.helper.toPromise(this.api.userApi, this.api.userApi.usersUserIdGet, firstLogin.id); if (!response) { - throw new Error(`Unable to switch team`); + throw new Error("Unable to switch team"); } const teams = response && response.body ? response.body.teams.filter((team: any) => team.username === name) : []; diff --git a/lib/components/users/user.js b/lib/components/users/user.js index 406160f..86cad37 100644 --- a/lib/components/users/user.js +++ b/lib/components/users/user.js @@ -130,7 +130,7 @@ class User extends merapi_1.Component { if (type === "team") { const { response } = yield this.helper.toPromise(this.api.userApi, this.api.userApi.usersUserIdGet, firstLogin.id); if (!response) { - throw new Error(`Unable to switch team`); + throw new Error("Unable to switch team"); } const teams = response && response.body ? response.body.teams.filter((team) => team.username === name) : []; if (teams.length > 0) { From dac175b927718a805326f15fa03184f079e73d90 Mon Sep 17 00:00:00 2001 From: rizkix Date: Tue, 11 Dec 2018 18:06:08 +0700 Subject: [PATCH 8/9] fix: cannot switch from team to team --- components/users/user.ts | 8 +++++++- lib/components/users/user.js | 6 +++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/components/users/user.ts b/components/users/user.ts index ff380db..5f058e2 100644 --- a/components/users/user.ts +++ b/components/users/user.ts @@ -119,11 +119,17 @@ 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"); - if (currentType === type) { + if (type === "user" && currentType === type) { throw new Error(`Unable to switch : already on ${type}`); } + if (type === "team" && currentType === type && name === currentLogin) { + throw new Error(`Unable to switch : already on ${currentLogin} team`); + } + + if (type === "team") { const { response } = await this.helper.toPromise(this.api.userApi, this.api.userApi.usersUserIdGet, firstLogin.id); diff --git a/lib/components/users/user.js b/lib/components/users/user.js index 86cad37..e5d3430 100644 --- a/lib/components/users/user.js +++ b/lib/components/users/user.js @@ -124,9 +124,13 @@ 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) { + const currentLogin = this.helper.getProp("current_login"); + if (type === "user" && currentType === type) { throw new Error(`Unable to switch : already on ${type}`); } + if (type === "team" && currentType === type && name === currentLogin) { + throw new Error(`Unable to switch : already on ${currentLogin} team`); + } if (type === "team") { const { response } = yield this.helper.toPromise(this.api.userApi, this.api.userApi.usersUserIdGet, firstLogin.id); if (!response) { From 6e0e647d00c012cc0f2e8baf0ac4b69714ba0db0 Mon Sep 17 00:00:00 2001 From: rizkix Date: Tue, 11 Dec 2018 18:10:03 +0700 Subject: [PATCH 9/9] cleanup --- components/users/user.ts | 9 +++------ lib/components/users/user.js | 8 +++----- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/components/users/user.ts b/components/users/user.ts index 5f058e2..838d056 100644 --- a/components/users/user.ts +++ b/components/users/user.ts @@ -120,13 +120,10 @@ export default class User extends Component { 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 (type === "user" && currentType === type) { - throw new Error(`Unable to switch : already on ${type}`); - } - - if (type === "team" && currentType === type && name === currentLogin) { - throw new Error(`Unable to switch : already on ${currentLogin} team`); + if (currentType === type && username === currentLogin) { + throw new Error(`Unable to switch : already on ${currentLogin} ${type}`); } diff --git a/lib/components/users/user.js b/lib/components/users/user.js index e5d3430..0b4256b 100644 --- a/lib/components/users/user.js +++ b/lib/components/users/user.js @@ -125,11 +125,9 @@ class User extends merapi_1.Component { const firstLogin = this.helper.getProp("first_login"); const currentType = this.helper.getProp("current_user_type"); const currentLogin = this.helper.getProp("current_login"); - if (type === "user" && currentType === type) { - throw new Error(`Unable to switch : already on ${type}`); - } - if (type === "team" && currentType === type && name === currentLogin) { - throw new Error(`Unable to switch : already on ${currentLogin} team`); + const username = name ? name : currentLogin; + if (currentType === type && username === currentLogin) { + throw new Error(`Unable to switch : already on ${currentLogin} ${type}`); } if (type === "team") { const { response } = yield this.helper.toPromise(this.api.userApi, this.api.userApi.usersUserIdGet, firstLogin.id);