Skip to content

Commit 623f5b7

Browse files
committed
feat: ✨ adds update fn, auditlog fn, users type correction
1 parent bcefaf9 commit 623f5b7

File tree

10 files changed

+379
-41
lines changed

10 files changed

+379
-41
lines changed

lib/stack/auditlog/index.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import cloneDeep from 'lodash/cloneDeep'
2+
import error from '../../core/contentstackError'
3+
import { fetchAll, parseData } from '../../entity'
4+
5+
/**
6+
*
7+
* @namespace AuditLog
8+
*/
9+
export function AuditLog (http, data = {}) {
10+
this.stackHeaders = data.stackHeaders
11+
this.urlPath = `/audit-logs`
12+
if (data.logs) {
13+
Object.assign(this, cloneDeep(data.logs))
14+
this.urlPath = `/audit-logs/${this.uid}`
15+
16+
/**
17+
* @description The fetch AuditLog call fetches AuditLog details.
18+
* @memberof AuditLog
19+
* @func fetch
20+
* @returns {Promise<Branch.Branch>} Promise for Branch instance
21+
* @example
22+
* import * as contentstack from '@contentstack/management'
23+
* const client = contentstack.client()
24+
*
25+
* client.stack({ api_key: 'api_key'}).auditLog('audit_log_item_uid').fetch()
26+
* .then((log) => console.log(log))
27+
*
28+
*/
29+
this.fetch = async function (param = {}) {
30+
try {
31+
const headers = {
32+
headers: { ...cloneDeep(this.stackHeaders) },
33+
params: {
34+
...cloneDeep(param)
35+
}
36+
} || {}
37+
const response = await http.get(this.urlPath, headers)
38+
if (response.data) {
39+
return new AuditLog(http, parseData(response, this.stackHeaders))
40+
} else {
41+
throw error(response)
42+
}
43+
} catch (err) {
44+
throw error(err)
45+
}
46+
}
47+
} else {
48+
/**
49+
* @description The Get all AuditLog request retrieves the details of all the Branch of a stack.
50+
* @memberof AuditLog
51+
* @func fetchAll
52+
* @param {Int} limit The limit parameter will return a specific number of Branch in the output.
53+
* @param {Int} skip The skip parameter will skip a specific number of Branch in the output.
54+
* @param {Boolean}include_count To retrieve the count of Branch.
55+
* @returns {ContentstackCollection} Result collection of content of specified module.
56+
* @example
57+
* import * as contentstack from '@contentstack/management'
58+
* const client = contentstack.client()
59+
*
60+
* client.stack({ api_key: 'api_key'}).auditLog().fetchAll()
61+
* .then((logs) => console.log(logs))
62+
*
63+
*/
64+
this.fetchAll = fetchAll(http, LogCollection)
65+
}
66+
return this
67+
}
68+
69+
export function LogCollection (http, data) {
70+
const obj = cloneDeep(data.logs) || []
71+
const logCollection = obj.map((userdata) => {
72+
return new AuditLog(http, { logs: userdata, stackHeaders: data.stackHeaders })
73+
})
74+
return logCollection
75+
}

lib/stack/bulkOperation/index.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,4 +220,55 @@ export function BulkOperation (http, data = {}) {
220220
}
221221
return publishUnpublish(http, '/bulk/delete', httpBody, headers)
222222
}
223+
224+
/**
225+
* The Delete entries and assets in bulk request allows you to delete multiple entries and assets at the same time.
226+
* @memberof BulkOperation
227+
* @func update
228+
* @returns {Promise<String>} Success message
229+
* @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.
230+
* @example
231+
* import * as contentstack from '@contentstack/management'
232+
* const client = contentstack.client()
233+
*
234+
* const updateBody = {
235+
* "entries": [{
236+
* "content_type": "content_type_uid1",
237+
* "uid": "entry_uid",
238+
* "locale": "en-us"
239+
* }, {
240+
* "content_type": "content_type_uid2",
241+
* "uid": "entry_uid",
242+
* "locale": "en-us"
243+
* }],
244+
* "workflow": {
245+
* "workflow_stage": {
246+
* "comment": "Workflow-related Comments",
247+
* "due_date": "Thu Dec 01 2018",
248+
* "notify": false,
249+
* "uid": "workflow_stage_uid",
250+
* "assigned_to": [{
251+
* "uid": "user_uid",
252+
* "name": "user_name",
253+
* "email": "user_email_id"
254+
* }],
255+
* "assigned_by_roles": [{
256+
* "uid": "role_uid",
257+
* "name": "role_name"
258+
* }]
259+
* }
260+
* }
261+
* }
262+
* client.stack({ api_key: 'api_key'}).bulkOperation().update(updateBody)
263+
* .then((response) => { console.log(response.notice) })
264+
*
265+
*/
266+
this.update = async (updateBody = {}) => {
267+
const headers = {
268+
headers: {
269+
...cloneDeep(this.stackHeaders)
270+
}
271+
}
272+
return publishUnpublish(http, '/bulk/workflow', updateBody, headers)
273+
}
223274
}

lib/stack/contentType/index.js

Lines changed: 67 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,33 @@ export function ContentType (http, data = {}) {
9595
}
9696
return new Entry(http, data)
9797
}
98+
99+
/**
100+
* @description References call will fetch all the content types in which a specified content type is referenced.
101+
* @returns {Promise<ContentType.references>} Promise for ContenttypeReferences
102+
* @example
103+
* import * as contentstack from '@contentstack/management'
104+
* const client = contentstack.client()
105+
*
106+
* client.stack({ api_key: 'api_key'}).contentType('content_type_uid').references()
107+
* .then((contentType) => console.log(contentType))
108+
*/
109+
this.references = async () => {
110+
try {
111+
const headers = {
112+
headers: { ...cloneDeep(this.stackHeaders) }
113+
}
114+
115+
const response = await http.get(`/content_types/${this.uid}/references`, headers)
116+
if (response.data) {
117+
return response.data
118+
} else {
119+
throw error(response)
120+
}
121+
} catch (err) {
122+
throw error(err)
123+
}
124+
}
98125
} else {
99126
/**
100127
* @description The Create a content type call creates a new content type in a particular stack of your Contentstack account.
@@ -123,52 +150,52 @@ export function ContentType (http, data = {}) {
123150
}
124151

125152
/**
126-
* @description The Create a content type call creates a new content type in a particular stack of your Contentstack account.
127-
* @memberof ContentType
128-
* @func create
129-
* @returns {Promise<ContentType.ContentType>} Promise for ContentType instance
130-
*
131-
* @example
132-
* import * as contentstack from '@contentstack/management'
133-
* const client = contentstack.client()
134-
* const content_type = {name: 'My New contentType'}
135-
* client.stack().contentType().create({ content_type })
136-
* .then((contentType) => console.log(contentType))
137-
*/
153+
* @description The Create a content type call creates a new content type in a particular stack of your Contentstack account.
154+
* @memberof ContentType
155+
* @func create
156+
* @returns {Promise<ContentType.ContentType>} Promise for ContentType instance
157+
*
158+
* @example
159+
* import * as contentstack from '@contentstack/management'
160+
* const client = contentstack.client()
161+
* const content_type = {name: 'My New contentType'}
162+
* client.stack().contentType().create({ content_type })
163+
* .then((contentType) => console.log(contentType))
164+
*/
138165
this.create = create({ http: http })
139166

140167
/**
141-
* @description The Query on Content Type will allow to fetch details of all or specific Content Type
142-
* @memberof ContentType
143-
* @func query
144-
* @param {Boolean} include_count Set this to 'true' to include in response the total count of content types available in your stack.
145-
* @returns {Array<ContentType>} Array of ContentTyoe.
146-
*
147-
* @example
148-
* import * as contentstack from '@contentstack/management'
149-
* const client = contentstack.client()
150-
*
151-
* client.stack({ api_key: 'api_key'}).contentType().query({ query: { name: 'Content Type Name' } }).find()
152-
* .then((contentTypes) => console.log(contentTypes))
153-
*/
168+
* @description The Query on Content Type will allow to fetch details of all or specific Content Type
169+
* @memberof ContentType
170+
* @func query
171+
* @param {Boolean} include_count Set this to 'true' to include in response the total count of content types available in your stack.
172+
* @returns {Array<ContentType>} Array of ContentTyoe.
173+
*
174+
* @example
175+
* import * as contentstack from '@contentstack/management'
176+
* const client = contentstack.client()
177+
*
178+
* client.stack({ api_key: 'api_key'}).contentType().query({ query: { name: 'Content Type Name' } }).find()
179+
* .then((contentTypes) => console.log(contentTypes))
180+
*/
154181
this.query = query({ http: http, wrapperCollection: ContentTypeCollection })
155182

156183
/**
157-
* @description The Import a content type call imports a content type into a stack.
158-
* @memberof ContentType
159-
* @func import
160-
* @param {String} data.content_type path to file
161-
* @example
162-
* import * as contentstack from '@contentstack/management'
163-
* const client = contentstack.client()
164-
*
165-
* const data = {
166-
* content_type: 'path/to/file.json',
167-
* }
168-
* client.stack({ api_key: 'api_key'}).contentType().import(data)
169-
* .then((contentType) => console.log(contentType))
170-
*
171-
*/
184+
* @description The Import a content type call imports a content type into a stack.
185+
* @memberof ContentType
186+
* @func import
187+
* @param {String} data.content_type path to file
188+
* @example
189+
* import * as contentstack from '@contentstack/management'
190+
* const client = contentstack.client()
191+
*
192+
* const data = {
193+
* content_type: 'path/to/file.json',
194+
* }
195+
* client.stack({ api_key: 'api_key'}).contentType().import(data)
196+
* .then((contentType) => console.log(contentType))
197+
*
198+
*/
172199
this.import = async function (data) {
173200
try {
174201
const response = await upload({

lib/stack/index.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { BulkOperation } from './bulkOperation'
1717
import { Label } from './label'
1818
import { Branch } from './branch'
1919
import { BranchAlias } from './branchAlias'
20+
import { AuditLog } from './auditlog'
2021
// import { format } from 'util'
2122
/**
2223
* 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>.
@@ -718,6 +719,30 @@ export function Stack (http, data) {
718719
* .then((stack) => console.log(stack))
719720
*/
720721
this.query = query({ http: http, wrapperCollection: StackCollection })
722+
723+
/**
724+
* @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.
725+
* @param {String}
726+
* @returns {AuditLog}
727+
*
728+
* @example
729+
* import * as contentstack from '@contentstack/management'
730+
* const client = contentstack.client()
731+
*
732+
* client.stack({ api_key: 'api_key'}).auditLog().fetchAll()
733+
* .then((logs) => console.log(logs))
734+
*
735+
* client.stack({ api_key: 'api_key' }).auditLog('log_item_uid').fetch()
736+
* .then((log) => console.log(log))
737+
*
738+
*/
739+
this.auditLog = (logItemUid = null) => {
740+
const data = { stackHeaders: this.stackHeaders }
741+
if (logItemUid) {
742+
data.logs = { uid: logItemUid }
743+
}
744+
return new AuditLog(http, data)
745+
}
721746
}
722747
return this
723748
}

test/unit/auditLog-test.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import Axios from 'axios'
2+
import { expect } from 'chai'
3+
import { describe, it } from 'mocha'
4+
import MockAdapter from 'axios-mock-adapter'
5+
import { noticeMock, stackHeadersMock, systemUidMock, auditLogsMock, auditLogItemMock } from './mock/objects'
6+
import { AuditLog } from '../../lib/stack/auditlog'
7+
8+
describe('Contentstack AuditLog test', () => {
9+
it('AuditLog test without uid', done => {
10+
const branch = makeAuditLog()
11+
expect(branch).to.not.equal(undefined)
12+
expect(branch.uid).to.be.equal(undefined)
13+
expect(branch.urlPath).to.be.equal('/audit-logs')
14+
expect(branch.fetch).to.equal(undefined)
15+
expect(branch.fetchAll).to.not.equal(undefined)
16+
done()
17+
})
18+
19+
it('AuditLog test with uid', done => {
20+
const branch = makeAuditLog({ logs: { uid: 'logUid' } })
21+
expect(branch).to.not.equal(undefined)
22+
expect(branch.uid).to.be.equal('logUid')
23+
expect(branch.urlPath).to.be.equal('/audit-logs/logUid')
24+
expect(branch.fetch).to.not.equal(undefined)
25+
expect(branch.fetchAll).to.equal(undefined)
26+
done()
27+
})
28+
29+
it('AuditLog Fetch all without Stack Headers test', done => {
30+
var mock = new MockAdapter(Axios)
31+
mock.onGet('/audit-logs').reply(200, auditLogsMock)
32+
makeAuditLog()
33+
.fetchAll()
34+
.then((response) => {
35+
expect(response.items[0].created_at).to.be.equal('created_at_date')
36+
expect(response.items[0].uid).to.be.equal('UID')
37+
done()
38+
})
39+
.catch(done)
40+
})
41+
42+
it('AuditLog Fetch all with params test', done => {
43+
var mock = new MockAdapter(Axios)
44+
mock.onGet('/audit-logs').reply(200, auditLogsMock)
45+
makeAuditLog({ stackHeaders: stackHeadersMock })
46+
.fetchAll({})
47+
.then((response) => {
48+
expect(response.items[0].created_at).to.be.equal('created_at_date')
49+
expect(response.items[0].uid).to.be.equal('UID')
50+
done()
51+
})
52+
.catch(done)
53+
})
54+
55+
it('AuditLog fetch test', done => {
56+
var mock = new MockAdapter(Axios)
57+
mock.onGet('/audit-logs/UID').reply(200, auditLogItemMock)
58+
makeAuditLog({ stackHeaders: stackHeadersMock, logs: { uid: 'UID' } })
59+
.fetch()
60+
.then((response) => {
61+
expect(response.created_at).to.be.equal('created_at_date')
62+
expect(response.uid).to.be.equal('UID')
63+
done()
64+
})
65+
.catch(done)
66+
})
67+
})
68+
69+
function makeAuditLog (data) {
70+
return new AuditLog(Axios, data)
71+
}

0 commit comments

Comments
 (0)