diff --git a/lib/organization/index.js b/lib/organization/index.js index 5d35409f..3d51755f 100644 --- a/lib/organization/index.js +++ b/lib/organization/index.js @@ -7,6 +7,7 @@ import { StackCollection } from '../stack' import { UserCollection } from '../user' import { App } from '../app' import { AppRequest } from '../app/request' +import { Teams, TeamsCollection } from './team' /** * Organization is the top-level entity in the hierarchy of Contentstack, consisting of stacks and stack resources, and users. Organization allows easy management of projects as well as users within the Organization. Read more about Organizations.. * @namespace Organization @@ -18,6 +19,14 @@ export function Organization (http, data) { Object.assign(this, cloneDeep(data.organization)) this.urlPath = `/organizations/${this.uid}` + this.teams = (team_uid = null) => { + delete data.organization + data.organization_uid = this.uid + if (team_uid) { + data.team = { uid: team_uid } + } + return new Teams(http, data) + } /** * @description The fetch Organization call fetches Organization details. * @memberof Organization diff --git a/lib/organization/team/index.js b/lib/organization/team/index.js new file mode 100644 index 00000000..891415a3 --- /dev/null +++ b/lib/organization/team/index.js @@ -0,0 +1,40 @@ +import cloneDeep from 'lodash/cloneDeep' +import { + create, + fetch, + update, + query, + deleteEntity, + parseData, + fetchAll +} from '../../entity' + +export function Teams (http, data) { + this.organization_uid = data.organization_uid + + this.urlPath = `/organizations/${this.organization_uid}/teams` + + if (data && data.team) { + Object.assign(this, cloneDeep(data.team)) + console.log('org uid 2', this.organization_uid) + + this.urlPath = `/organizations/${this.organization_uid}/teams/${this.uid}` + + this.update = update(http, 'team') + + this.delete = deleteEntity(http) + + this.fetch = fetch(http, 'team') + } else { + this.create = create({ http }) + this.query = fetchAll(http, TeamsCollection) + // this.query = query({ http: http, wrapperCollection: TeamsCollection }) + } +} +export function TeamsCollection (http, teamsData) { + const obj = cloneDeep(teamsData.teams) || [] + const teamsCollection = obj.map((team) => { + return new Teams(http, { team: team }) + }) + return teamsCollection +} diff --git a/test/api/team-test.js b/test/api/team-test.js new file mode 100644 index 00000000..16d2dae5 --- /dev/null +++ b/test/api/team-test.js @@ -0,0 +1,78 @@ +import { describe, it, beforeEach } from 'mocha' +import { expect } from 'chai' +import { jsonReader } from '../utility/fileOperations/readwrite' +// import * as contentstack from '../../lib/contentstack.js' +import { contentstackClient } from '../utility/ContentstackClient.js' + +var client = {} + +var org_uid = 'blt242b133b4e9bd736' +const team_uid = '6539eed4b502f0a73415188e' +const team = { + name: 'team_name', + users: [], + stackRoleMapping: [], + organizationRole: 'organization_role_uid' +} + +describe('Teams API Test', () => { + beforeEach(() => { + const user = jsonReader('loggedinuser.json') + client = contentstackClient(user.authtoken) + }) + it('Query and get all teams', async () => { + try { + const response = await client.organization(org_uid).teams().query() + console.log('res', response) + } catch (err) { + console.log(err) + } + }) + it('fetch test', async () => { + try { + const response = await client.organization(org_uid).teams(team_uid).fetch() + console.log('res2', await response) + } catch (err) { + console.log(err) + } + }) + // it('create test', async () => { + // try { + // const response = await makeTeams(org_uid).create({ + // name: 't2', + // users: [], + // stackRoleMapping: [], + // organizationRole: 'blt09e5dfced326aaea' }) + // console.log('res2', response) + // } catch (err) { + // console.log(err) + // } + // }) + // it('delete test', async () => { + // try { + // const response = await makeTeams(org_uid, '').delete() + // console.log('res2', response) + // } catch (err) { + // console.log(err) + // } + // }) + // it('update test', async () => { + // try { + // var response = await makeTeams(org_uid, team_uid).fetch() + // .then((team) => { + // team.name = 'updated' + // console.log('tttt', team) + // return response.update() + // }) + // // const res = response.update() + // console.log('update', response) + // // console.log('update12', res) + // } catch (err) { + // console.log(err) + // } + // }) +}) + +function makeTeams (org_uid, team_uid = null) { + return client.organization(org_uid).teams(team_uid) +} diff --git a/test/test.js b/test/test.js index d7e30522..5cb5e213 100644 --- a/test/test.js +++ b/test/test.js @@ -27,3 +27,4 @@ require('./api/contentType-delete-test') require('./api/delete-test') require('./api/taxonomy-test') require('./api/terms-test') +require('./api/team-test') diff --git a/test/unit/mock/objects.js b/test/unit/mock/objects.js index 1c82cc1a..a30a2b51 100644 --- a/test/unit/mock/objects.js +++ b/test/unit/mock/objects.js @@ -774,7 +774,11 @@ const termsMock = { referenced_entries_count: 4 }] } - +const teamsMock = { + organization_uid: 'organization_uid', + uid: 'UID', + name: 'name' +} function mockCollection (mockData, type) { const mock = { ...cloneDeep(noticeMock), @@ -845,6 +849,7 @@ export { auditLogItemMock, taxonomyMock, termsMock, + teamsMock, mockCollection, entryMockCollection, checkSystemFields diff --git a/test/unit/taxonomy-test.js b/test/unit/taxonomy-test.js index b5f09386..06f4d300 100644 --- a/test/unit/taxonomy-test.js +++ b/test/unit/taxonomy-test.js @@ -3,7 +3,7 @@ import { expect } from 'chai' import { describe, it } from 'mocha' import MockAdapter from 'axios-mock-adapter' import { Taxonomy } from '../../lib/stack/taxonomy' -import { systemUidMock, stackHeadersMock, taxonomyMock, noticeMock } from './mock/objects' +import { systemUidMock, stackHeadersMock, taxonomyMock, noticeMock, termsMock } from './mock/objects' describe('Contentstack Taxonomy test', () => { it('taxonomy create test', done => { @@ -121,6 +121,27 @@ describe('Contentstack Taxonomy test', () => { expect(taxonomy.query).to.be.equal(undefined) done() }) + + it('term create test', done => { + var mock = new MockAdapter(Axios) + mock.onPost(`/taxonomies/taxonomy_uid/terms`).reply(200, { + term: { + ...termsMock + } + }) + makeTaxonomy({ + taxonomy: { + uid: 'taxonomy_uid' + }, + stackHeaders: stackHeadersMock + }).terms() + .create() + .then((term) => { + console.log(term) + done() + }) + .catch(done) + }) }) function makeTaxonomy (data = {}) { diff --git a/test/unit/team-test.js b/test/unit/team-test.js new file mode 100644 index 00000000..6660a35b --- /dev/null +++ b/test/unit/team-test.js @@ -0,0 +1,104 @@ +import Axios from 'axios' +import { expect } from 'chai' +import { describe, it } from 'mocha' +import MockAdapter from 'axios-mock-adapter' +import { Teams } from '../../lib/organization/team' +import { systemUidMock, teamsMock, noticeMock } from './mock/objects' + +describe('Contentstack Team test', () => { + it('team create test', done => { + var mock = new MockAdapter(Axios) + mock.onPost(`/organizations/organization_uid/teams`).reply(200, { + team: { + ...teamsMock + } + }) + makeTeams() + .create() + .then((team) => { + checkTeams(team) + done() + }) + .catch(done) + }) + it('Team fetch test', done => { + var mock = new MockAdapter(Axios) + mock.onGet(`/organizations/organization_uid/teams/UID`).reply(200, { + team: { + ...teamsMock + } + }) + makeTeams({ + team: { + ...systemUidMock + } + }) + .fetch() + .then((team) => { + console.log('fetch', team) + checkTeams(team) + done() + }) + .catch(done) + }) + it('Teams query test', done => { + var mock = new MockAdapter(Axios) + mock.onGet(`/organizations/organization_uid/teams`).reply(200, { + teams: [ + teamsMock + ] + }) + makeTeams() + .query() + .then((teams) => { + console.log(teams) + checkTeams(teams.items[0]) + done() + }) + .catch(done) + }) + it('Team update test', done => { + var mock = new MockAdapter(Axios) + mock.onPut(`/organizations/organization_uid/teams/UID`).reply(200, { + team: { + ...teamsMock + } + }) + makeTeams({ + team: { + ...systemUidMock + } + }) + .update() + .then((team) => { + checkTeams(team) + done() + }) + .catch(done) + }) + it('team delete test', done => { + var mock = new MockAdapter(Axios) + mock.onDelete(`/organizations/organization_uid/teams/UID`).reply(200, { + ...noticeMock + }) + makeTeams({ + team: { + ...systemUidMock + } + }) + .delete() + .then((team) => { + expect(team.notice).to.be.equal(noticeMock.notice) + done() + }) + .catch(done) + }) +}) + +function makeTeams (data = {}) { + return new Teams(Axios, { organization_uid: 'organization_uid', ...data }) +} + +function checkTeams (teams) { + expect(teams.name).to.be.equal('name') +}