Skip to content

Commit

Permalink
Merge pull request #80 from contentstack/feat/cs-41208-taxonomy-support
Browse files Browse the repository at this point in the history
feat/cs 41208 Taxonomy support
  • Loading branch information
harshithad0703 authored Sep 14, 2023
2 parents 56ff7e4 + 655c991 commit 03ed99d
Show file tree
Hide file tree
Showing 7 changed files with 370 additions and 7 deletions.
25 changes: 24 additions & 1 deletion lib/stack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import { Label } from './label'
import { Branch } from './branch'
import { BranchAlias } from './branchAlias'
import { AuditLog } from './auditlog'
// import { format } from 'util'
import { Taxonomy } from './taxonomy'

/**
* A stack is a space that stores the content of a project (a web or mobile property). Within a stack, you can create content structures, content entries, users, etc. related to the project. Read more about <a href='https://www.contentstack.com/docs/guide/stack'>Stacks</a>.
* @namespace Stack
Expand Down Expand Up @@ -684,6 +685,28 @@ export function Stack (http, data) {
}
return new Role(http, data)
}

/**
* @description Taxonomies allow you to group a collection of content within a stack. Using taxonomies you can group content types that need to work together
* @param {String} uid The UID of the Taxonomy you want to get details.
* @returns {Taxonomy} Instance of Taxonomy.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).taxonomy().create()
* .then((taxonomy) => console.log(taxonomy))
*
* client.stack({ api_key: 'api_key'}).taxonomy('taxonomy_uid').fetch()
* .then((taxonomy) => console.log(taxonomy))
*/
this.taxonomy = (taxonomyUid = '') => {
const data = { stackHeaders: this.stackHeaders }
if (taxonomyUid) {
data.taxonomy = { uid: taxonomyUid }
}
return new Taxonomy(http, data)
}
} else {
/**
* @description The Create stack call creates a new stack in your Contentstack account.
Expand Down
110 changes: 110 additions & 0 deletions lib/stack/taxonomy/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import cloneDeep from 'lodash/cloneDeep'
import {
create,
fetch,
query,
update,
deleteEntity
} from '../../entity'

export function Taxonomy (http, data) {
this.stackHeaders = data.stackHeaders
this.urlPath = `/taxonomies`

if (data.taxonomy) {
Object.assign(this, cloneDeep(data.taxonomy))
this.urlPath = `/taxonomies/${this.uid}`

/**
* @description The Update taxonomy call is used to update an existing taxonomy.
* @memberof Taxonomy
* @func update
* @returns {Promise<Taxonomy.Taxonomy>} Promise for Taxonomy instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).taxonomy('taxonomy_uid').fetch()
* .then((taxonomy) => {
* taxonomy.name = 'taxonomy name'
* return taxonomy.update()
* })
* .then((taxonomy) => console.log(taxonomy))
*
*/
this.update = update(http, 'taxonomy')

/**
* @description The Delete taxonomy call is used to delete an existing taxonomy.
* @memberof Taxonomy
* @func delete
* @returns {Promise<Taxonomy.Taxonomy>} Response Object.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).taxonomy('taxonomy_uid').delete()
* .then((response) => console.log(response.notice))
*
*/
this.delete = deleteEntity(http)

/**
* @description The Fetch taxonomy call is used to fetch an existing taxonomy.
* @memberof Taxonomy
* @func fetch
* @returns {Promise<Taxonomy.Taxonomy>} Promise for Taxonomy instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).taxonomy('taxonomy_uid').fetch()
* .then((taxonomy) => console.log(taxonomy))
*
*/
this.fetch = fetch(http, 'taxonomy')
} else {
/**
* @description The Create taxonomy call is used to create a taxonomy.
* @memberof Taxonomy
* @func create
* @returns {Promise<Taxonomy.Taxonomy>} Promise for Taxonomy instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* const taxonomy = {
* uid: 'taxonomy_testing1',
* name: 'taxonomy testing',
* description: 'Description for Taxonomy testing'
* }
* client.stack({ api_key: 'api_key'}).taxonomy().create({taxonomy})
* .then(taxonomy) => console.log(taxonomy)
*
*/
this.create = create({ http })

/**
* @description The Query on Taxonomy will allow to fetch details of all Taxonomies.
* @memberof Taxonomy
* @param {Object} params - URI parameters
* @prop {Object} params.query - Queries that you can use to fetch filtered results.
* @func query
* @returns {Array<Taxonomy>} Array of Taxonomy.
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack().taxonomy().query().find()
* .then((taxonomies) => console.log(taxonomies)
*/
this.query = query({ http: http, wrapperCollection: TaxonomyCollection })
}
}
export function TaxonomyCollection (http, data) {
const obj = cloneDeep(data.taxonomy) || []
const taxonomyCollection = obj.map((userdata) => {
return new Taxonomy(http, { taxonomy: userdata, stackHeaders: data.stackHeaders })
})
return taxonomyCollection
}
86 changes: 86 additions & 0 deletions test/api/taxonomy-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { expect } from 'chai'
import { describe, it, setup } from 'mocha'
import { jsonReader } from '../utility/fileOperations/readwrite'
import { contentstackClient } from '../utility/ContentstackClient.js'

var client = {}
var stack = {}

const taxonomy = {
uid: 'taxonomy_testing1',
name: 'taxonomy testing',
description: 'Description for Taxonomy testing'
}

var taxonomyUID = ''
var taxonomyDelUID = 'taxonomy_testing'

describe('taxonomy api Test', () => {
setup(() => {
const user = jsonReader('loggedinuser.json')
stack = jsonReader('stack.json')
client = contentstackClient(user.authtoken)
})

it('Create taxonomy', done => {
makeTaxonomy()
.create([{ taxonomy }])
.then((taxonomyResponse) => {
expect(taxonomyResponse.name).to.be.equal(taxonomy.name)
done()
})
.catch(done)
})

it('Fetch taxonomy from uid', done => {
makeTaxonomy(taxonomyUID)
.fetch()
.then((taxonomyResponse) => {
expect(taxonomyResponse.uid).to.be.equal(taxonomyUID)
expect(taxonomyResponse.name).to.be.not.equal(null)
done()
})
.catch(done)
})

it('Update taxonomy from uid', done => {
makeTaxonomy(taxonomyUID)
.fetch()
.then((taxonomyResponse) => {
taxonomyResponse.name = 'Updated Name'
return taxonomyResponse.update()
})
.then((taxonomyResponse) => {
expect(taxonomyResponse.uid).to.be.equal(taxonomyUID)
expect(taxonomyResponse.name).to.be.equal('Updated Name')
done()
})
.catch(done)
})

it('Delete taxonomy from uid', done => {
makeTaxonomy(taxonomyDelUID)
.delete()
.then((taxonomyResponse) => {
expect(taxonomyResponse.notice).to.be.equal('Taxonomy deleted successfully.')
done()
})
.catch(done)
})

it('Query to get all taxonomies', async () => {
makeTaxonomy()
.query()
.find()
.then((response) => {
response.items.forEach((taxonomyResponse) => {
expect(taxonomyResponse.uid).to.be.not.equal(null)
expect(taxonomyResponse.name).to.be.not.equal(null)
})
})
})
})

function makeTaxonomy (uid = null) {
return client.stack({ api_key: stack.api_key }).taxonomy(uid)
}
1 change: 1 addition & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ require('./api/release-test')
require('./api/label-test')
require('./api/contentType-delete-test')
require('./api/delete-test')
require('./api/taxonomy-test')
1 change: 1 addition & 0 deletions test/unit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ require('./deployment-test')
require('./app-request-test')
require('./authorization-test')
require('./auditLog-test')
require('./taxonomy-test')
22 changes: 16 additions & 6 deletions test/unit/mock/objects.js
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ const appInstallMock = {
status: 'installed',
installation_uid: 'installationUID',
redirect_to: 'config',
redirect_uri: 'redirect_uri',
redirect_uri: 'redirect_uri'
}

const installationMock = {
Expand Down Expand Up @@ -613,16 +613,16 @@ const branchCompareAllMock = {
compare_branch: 'dev'
},
diff: [...globalFieldDiff, ...contentTypeDiff],
next_url:'https://api.contentstack.io/v3/stacks/branches/compare?base_branch=main&compare_branch=dev&skip=0&limit=100'
next_url: 'https://api.contentstack.io/v3/stacks/branches/compare?base_branch=main&compare_branch=dev&skip=0&limit=100'
}

const branchCompareContentTypeMock = {
branches: {
base_branch: 'UID',
compare_branch: 'dev'
},
diff: [ ...contentTypeDiff ],
next_url:'https://api.contentstack.io/v3/stacks/branches/compare?base_branch=main&compare_branch=dev&skip=0&limit=100'
diff: [...contentTypeDiff],
next_url: 'https://api.contentstack.io/v3/stacks/branches/compare?base_branch=main&compare_branch=dev&skip=0&limit=100'
}

const branchCompareGlobalFieldMock = {
Expand All @@ -631,7 +631,7 @@ const branchCompareGlobalFieldMock = {
compare_branch: 'dev'
},
diff: [...globalFieldDiff],
next_url:'https://api.contentstack.io/v3/stacks/branches/compare?base_branch=main&compare_branch=dev&skip=0&limit=100'
next_url: 'https://api.contentstack.io/v3/stacks/branches/compare?base_branch=main&compare_branch=dev&skip=0&limit=100'
}

const branchMergeAllMock = {
Expand All @@ -647,7 +647,7 @@ const branchMergeAllMock = {
status: 'in_progress'
},
merged_at: null,
errors: [
errors: [
{
code: 'error_code',
message: 'Error message'
Expand Down Expand Up @@ -725,6 +725,15 @@ const auditLogsMock = {
]
}

const taxonomyMock = {
uid: 'UID',
name: 'name',
description: 'Description for Taxonomy',
terms_count: 4,
referenced_terms_count: 3,
referenced_entries_count: 6
}

function mockCollection (mockData, type) {
const mock = {
...cloneDeep(noticeMock),
Expand Down Expand Up @@ -793,6 +802,7 @@ export {
branchMergeQueueFetchMock,
auditLogsMock,
auditLogItemMock,
taxonomyMock,
mockCollection,
entryMockCollection,
checkSystemFields
Expand Down
Loading

0 comments on commit 03ed99d

Please sign in to comment.