Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions lib/stack/auditlog/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import cloneDeep from 'lodash/cloneDeep'
import error from '../../core/contentstackError'
import { fetchAll, parseData } from '../../entity'

/**
*
* @namespace AuditLog
*/
export function AuditLog (http, data = {}) {
this.stackHeaders = data.stackHeaders
this.urlPath = `/audit-logs`
if (data.logs) {
Object.assign(this, cloneDeep(data.logs))
this.urlPath = `/audit-logs/${this.uid}`

/**
* @description The fetch AuditLog call fetches AuditLog details.
* @memberof AuditLog
* @func fetch
* @returns {Promise<Branch.Branch>} Promise for Branch instance
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).auditLog('audit_log_item_uid').fetch()
* .then((log) => console.log(log))
*
*/
this.fetch = async function (param = {}) {
try {
const headers = {
headers: { ...cloneDeep(this.stackHeaders) },
params: {
...cloneDeep(param)
}
} || {}
const response = await http.get(this.urlPath, headers)
if (response.data) {
return new AuditLog(http, parseData(response, this.stackHeaders))
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}
} else {
/**
* @description The Get all AuditLog request retrieves the details of all the Branch of a stack.
* @memberof AuditLog
* @func fetchAll
* @param {Int} limit The limit parameter will return a specific number of Branch in the output.
* @param {Int} skip The skip parameter will skip a specific number of Branch in the output.
* @param {Boolean}include_count To retrieve the count of Branch.
* @returns {ContentstackCollection} Result collection of content of specified module.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).auditLog().fetchAll()
* .then((logs) => console.log(logs))
*
*/
this.fetchAll = fetchAll(http, LogCollection)
}
return this
}

export function LogCollection (http, data) {
const obj = cloneDeep(data.logs) || []
const logCollection = obj.map((userdata) => {
return new AuditLog(http, { logs: userdata, stackHeaders: data.stackHeaders })
})
return logCollection
}
51 changes: 51 additions & 0 deletions lib/stack/bulkOperation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,55 @@ export function BulkOperation (http, data = {}) {
}
return publishUnpublish(http, '/bulk/delete', httpBody, headers)
}

/**
* The Delete entries and assets in bulk request allows you to delete multiple entries and assets at the same time.
* @memberof BulkOperation
* @func update
* @returns {Promise<String>} Success message
* @param {Boolean} updateBody - Set this with details specifing the content type UIDs, entry UIDs or asset UIDs, and locales of which the entries or assets you want to update.
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* const updateBody = {
* "entries": [{
* "content_type": "content_type_uid1",
* "uid": "entry_uid",
* "locale": "en-us"
* }, {
* "content_type": "content_type_uid2",
* "uid": "entry_uid",
* "locale": "en-us"
* }],
* "workflow": {
* "workflow_stage": {
* "comment": "Workflow-related Comments",
* "due_date": "Thu Dec 01 2018",
* "notify": false,
* "uid": "workflow_stage_uid",
* "assigned_to": [{
* "uid": "user_uid",
* "name": "user_name",
* "email": "user_email_id"
* }],
* "assigned_by_roles": [{
* "uid": "role_uid",
* "name": "role_name"
* }]
* }
* }
* }
* client.stack({ api_key: 'api_key'}).bulkOperation().update(updateBody)
* .then((response) => { console.log(response.notice) })
*
*/
this.update = async (updateBody = {}) => {
const headers = {
headers: {
...cloneDeep(this.stackHeaders)
}
}
return publishUnpublish(http, '/bulk/workflow', updateBody, headers)
}
}
107 changes: 67 additions & 40 deletions lib/stack/contentType/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,33 @@ export function ContentType (http, data = {}) {
}
return new Entry(http, data)
}

/**
* @description References call will fetch all the content types in which a specified content type is referenced.
* @returns {Promise<ContentType.references>} Promise for ContenttypeReferences
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').references()
* .then((contentType) => console.log(contentType))
*/
this.references = async () => {
try {
const headers = {
headers: { ...cloneDeep(this.stackHeaders) }
}

const response = await http.get(`/content_types/${this.uid}/references`, headers)
if (response.data) {
return response.data
} else {
throw error(response)
}
} catch (err) {
throw error(err)
}
}
} else {
/**
* @description The Create a content type call creates a new content type in a particular stack of your Contentstack account.
Expand Down Expand Up @@ -123,52 +150,52 @@ export function ContentType (http, data = {}) {
}

/**
* @description The Create a content type call creates a new content type in a particular stack of your Contentstack account.
* @memberof ContentType
* @func create
* @returns {Promise<ContentType.ContentType>} Promise for ContentType instance
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* const content_type = {name: 'My New contentType'}
* client.stack().contentType().create({ content_type })
* .then((contentType) => console.log(contentType))
*/
* @description The Create a content type call creates a new content type in a particular stack of your Contentstack account.
* @memberof ContentType
* @func create
* @returns {Promise<ContentType.ContentType>} Promise for ContentType instance
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
* const content_type = {name: 'My New contentType'}
* client.stack().contentType().create({ content_type })
* .then((contentType) => console.log(contentType))
*/
this.create = create({ http: http })

/**
* @description The Query on Content Type will allow to fetch details of all or specific Content Type
* @memberof ContentType
* @func query
* @param {Boolean} include_count Set this to 'true' to include in response the total count of content types available in your stack.
* @returns {Array<ContentType>} Array of ContentTyoe.
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).contentType().query({ query: { name: 'Content Type Name' } }).find()
* .then((contentTypes) => console.log(contentTypes))
*/
* @description The Query on Content Type will allow to fetch details of all or specific Content Type
* @memberof ContentType
* @func query
* @param {Boolean} include_count Set this to 'true' to include in response the total count of content types available in your stack.
* @returns {Array<ContentType>} Array of ContentTyoe.
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).contentType().query({ query: { name: 'Content Type Name' } }).find()
* .then((contentTypes) => console.log(contentTypes))
*/
this.query = query({ http: http, wrapperCollection: ContentTypeCollection })

/**
* @description The Import a content type call imports a content type into a stack.
* @memberof ContentType
* @func import
* @param {String} data.content_type path to file
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* const data = {
* content_type: 'path/to/file.json',
* }
* client.stack({ api_key: 'api_key'}).contentType().import(data)
* .then((contentType) => console.log(contentType))
*
*/
* @description The Import a content type call imports a content type into a stack.
* @memberof ContentType
* @func import
* @param {String} data.content_type path to file
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* const data = {
* content_type: 'path/to/file.json',
* }
* client.stack({ api_key: 'api_key'}).contentType().import(data)
* .then((contentType) => console.log(contentType))
*
*/
this.import = async function (data) {
try {
const response = await upload({
Expand Down
25 changes: 25 additions & 0 deletions lib/stack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { BulkOperation } from './bulkOperation'
import { Label } from './label'
import { Branch } from './branch'
import { BranchAlias } from './branchAlias'
import { AuditLog } from './auditlog'
// import { format } from 'util'
/**
* 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>.
Expand Down Expand Up @@ -718,6 +719,30 @@ export function Stack (http, data) {
* .then((stack) => console.log(stack))
*/
this.query = query({ http: http, wrapperCollection: StackCollection })

/**
* @description Audit log displays a record of all the activities performed in a stack and helps you keep a track of all published items, updates, deletes, and current status of the existing content.
* @param {String}
* @returns {AuditLog}
*
* @example
* import * as contentstack from '@contentstack/management'
* const client = contentstack.client()
*
* client.stack({ api_key: 'api_key'}).auditLog().fetchAll()
* .then((logs) => console.log(logs))
*
* client.stack({ api_key: 'api_key' }).auditLog('log_item_uid').fetch()
* .then((log) => console.log(log))
*
*/
this.auditLog = (logItemUid = null) => {
const data = { stackHeaders: this.stackHeaders }
if (logItemUid) {
data.logs = { uid: logItemUid }
}
return new AuditLog(http, data)
}
}
return this
}
Expand Down
71 changes: 71 additions & 0 deletions test/unit/auditLog-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import Axios from 'axios'
import { expect } from 'chai'
import { describe, it } from 'mocha'
import MockAdapter from 'axios-mock-adapter'
import { noticeMock, stackHeadersMock, systemUidMock, auditLogsMock, auditLogItemMock } from './mock/objects'
import { AuditLog } from '../../lib/stack/auditlog'

describe('Contentstack AuditLog test', () => {
it('AuditLog test without uid', done => {
const branch = makeAuditLog()
expect(branch).to.not.equal(undefined)
expect(branch.uid).to.be.equal(undefined)
expect(branch.urlPath).to.be.equal('/audit-logs')
expect(branch.fetch).to.equal(undefined)
expect(branch.fetchAll).to.not.equal(undefined)
done()
})

it('AuditLog test with uid', done => {
const branch = makeAuditLog({ logs: { uid: 'logUid' } })
expect(branch).to.not.equal(undefined)
expect(branch.uid).to.be.equal('logUid')
expect(branch.urlPath).to.be.equal('/audit-logs/logUid')
expect(branch.fetch).to.not.equal(undefined)
expect(branch.fetchAll).to.equal(undefined)
done()
})

it('AuditLog Fetch all without Stack Headers test', done => {
var mock = new MockAdapter(Axios)
mock.onGet('/audit-logs').reply(200, auditLogsMock)
makeAuditLog()
.fetchAll()
.then((response) => {
expect(response.items[0].created_at).to.be.equal('created_at_date')
expect(response.items[0].uid).to.be.equal('UID')
done()
})
.catch(done)
})

it('AuditLog Fetch all with params test', done => {
var mock = new MockAdapter(Axios)
mock.onGet('/audit-logs').reply(200, auditLogsMock)
makeAuditLog({ stackHeaders: stackHeadersMock })
.fetchAll({})
.then((response) => {
expect(response.items[0].created_at).to.be.equal('created_at_date')
expect(response.items[0].uid).to.be.equal('UID')
done()
})
.catch(done)
})

it('AuditLog fetch test', done => {
var mock = new MockAdapter(Axios)
mock.onGet('/audit-logs/UID').reply(200, auditLogItemMock)
makeAuditLog({ stackHeaders: stackHeadersMock, logs: { uid: 'UID' } })
.fetch()
.then((response) => {
expect(response.created_at).to.be.equal('created_at_date')
expect(response.uid).to.be.equal('UID')
done()
})
.catch(done)
})
})

function makeAuditLog (data) {
return new AuditLog(Axios, data)
}
Loading