diff --git a/lib/stack/bulkOperation/index.js b/lib/stack/bulkOperation/index.js index 05621003..945f8dd9 100644 --- a/lib/stack/bulkOperation/index.js +++ b/lib/stack/bulkOperation/index.js @@ -9,6 +9,24 @@ export function BulkOperation (http, data = {}) { this.stackHeaders = data.stackHeaders this.urlPath = `/bulk` + this.addItems = async ({ data, bulk_version = "" }) => { + this.urlPath = `/bulk/release/items`; + const headers = { + headers: { + ...cloneDeep(this.stackHeaders), + }, + }; + if (bulk_version) headers.headers.bulk_version = bulk_version; + try { + const response = await http.post(this.urlPath, data, headers); + if (response.data) { + return response.data; + } + } catch (error) { + console.error(error); + } + }; + /** * The Publish entries and assets in bulk request allows you to publish multiple entries and assets at the same time. * @memberof BulkOperation diff --git a/test/sanity-check/api/release-test.js b/test/sanity-check/api/release-test.js index 20c9c822..8b4a1a7c 100644 --- a/test/sanity-check/api/release-test.js +++ b/test/sanity-check/api/release-test.js @@ -213,6 +213,38 @@ describe('Relases api Test', () => { .catch(done) }) + it('Bulk Operation: should add items to a release', done => { + const items = { + release: releaseUID, + action: 'publish', + locale: ['en-us'], + reference: true, + items: [ + { + version: entries[1]._version, + uid: entries[1].uid, + content_type_uid: multiPageCT.content_type.uid, + locale: 'en-us', + title: entries[1].title + }, + { + version: entries[2]._version, + uid: entries[2].uid, + content_type_uid: multiPageCT.content_type.uid, + locale: 'en-us', + title: entries[2].title + }, + ], + } + doBulkOperation().addItems({ data: items, bulk_version: '2.0' }) + .then((response) => { + expect(response.notice).to.equal('Your add to release request is in progress.') + expect(response.job_id).to.not.equal(undefined) + done() + }) + .catch(done) + }) + it('should delete specific Releases with Uid ', done => { makeRelease(releaseUID) .delete() @@ -237,3 +269,7 @@ describe('Relases api Test', () => { function makeRelease (uid = null) { return client.stack({ api_key: process.env.API_KEY }).release(uid) } + +function doBulkOperation(uid = null) { + return client.stack({ api_key: process.env.API_KEY }).bulkOperation() +} \ No newline at end of file diff --git a/test/unit/bulkOperation-test.js b/test/unit/bulkOperation-test.js new file mode 100644 index 00000000..0f21f6fd --- /dev/null +++ b/test/unit/bulkOperation-test.js @@ -0,0 +1,186 @@ +import Axios from 'axios'; +import { expect } from 'chai'; +import MockAdapter from 'axios-mock-adapter'; +import { describe, it } from 'mocha'; +import { BulkOperation } from '../../lib/stack/bulkOperation'; +import { stackHeadersMock } from './mock/objects'; + +describe('Contentstack BulkOperation test', () => { + it('BulkOperation test without uid', done => { + const bulkOperation = makeBulkOperation(); + expect(bulkOperation.urlPath).to.be.equal('/bulk'); + expect(bulkOperation.stackHeaders).to.be.equal(undefined); + expect(bulkOperation.addItems).to.not.equal(undefined); + expect(bulkOperation.publish).to.not.equal(undefined); + expect(bulkOperation.unpublish).to.not.equal(undefined); + expect(bulkOperation.delete).to.not.equal(undefined); + done(); + }); + + it('BulkOperation test with stackHeaders', done => { + const bulkOperation = makeBulkOperation({ stackHeaders: { ...stackHeadersMock } }); + expect(bulkOperation.urlPath).to.be.equal('/bulk'); + expect(bulkOperation.stackHeaders).to.not.equal(undefined); + expect(bulkOperation.stackHeaders.api_key).to.be.equal(stackHeadersMock.api_key); + expect(bulkOperation.addItems).to.not.equal(undefined); + expect(bulkOperation.publish).to.not.equal(undefined); + expect(bulkOperation.unpublish).to.not.equal(undefined); + expect(bulkOperation.delete).to.not.equal(undefined); + done(); + }); + + it('should add items to a release', async () => { + const items = { + release: 'blt05e951e5f3a1d342', + action: 'publish', + locale: ['en-us'], + reference: true, + items: [ + { + content_type_uid: 'ct_1', + uid: 'bltf6e197a18a11ec5f', + version: 2, + locale: 'en-us', + title: 'validation test', + }, + ], + }; + + var mock = new MockAdapter(Axios); + mock.onPost('/bulk/release/items').reply(200, { + notice: 'Your add to release request is in progress.', + job_id: 'job_id', + }); + + const response = await makeBulkOperation().addItems({ data: items, bulk_version: '2.0' }); + expect(response.notice).to.equal('Your add to release request is in progress.'); + expect(response.job_id).to.not.equal(undefined); + }); + + it('should publish items in bulk', async () => { + const publishDetails = { + entries: [ + { + uid: 'entry_uid', + content_type: 'content_type_uid', + version: 'version', + locale: 'entry_locale', + }, + ], + assets: [{ uid: 'uid' }], + locales: ['en'], + environments: ['env_uid'], + }; + + var mock = new MockAdapter(Axios); + mock.onPost('/bulk/publish').reply(200, { + notice: 'Your publish request is in progress.', + job_id: 'job_id', + }); + + const response = await makeBulkOperation().publish({ details: publishDetails }); + expect(response.notice).to.equal('Your publish request is in progress.'); + expect(response.job_id).to.not.equal(undefined); + }); + + it('should unpublish items in bulk', async () => { + const unpublishDetails = { + entries: [ + { + uid: 'entry_uid', + content_type: 'content_type_uid', + version: 'version', + locale: 'entry_locale', + }, + ], + assets: [{ uid: 'uid' }], + locales: ['en'], + environments: ['env_uid'], + }; + + var mock = new MockAdapter(Axios); + mock.onPost('/bulk/unpublish').reply(200, { + notice: 'Your unpublish request is in progress.', + job_id: 'job_id', + }); + + const response = await makeBulkOperation().unpublish({ details: unpublishDetails }); + expect(response.notice).to.equal('Your unpublish request is in progress.'); + expect(response.job_id).to.not.equal(undefined); + }); + + it('should delete items in bulk', async () => { + const deleteDetails = { + entries: [ + { + uid: 'entry_uid', + content_type: 'content_type_uid', + locale: 'entry_locale', + }, + ], + assets: [{ uid: 'uid' }], + }; + + var mock = new MockAdapter(Axios); + mock.onPost('/bulk/delete').reply(200, { + notice: 'Your delete request is in progress.', + job_id: 'job_id', + }); + + const response = await makeBulkOperation().delete({ details: deleteDetails }); + expect(response.notice).to.equal('Your delete request is in progress.'); + expect(response.job_id).to.not.equal(undefined); + }); + + it('should update items in bulk', async () => { + 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', + }, + ], + }, + }, + }; + + var mock = new MockAdapter(Axios); + mock.onPost('/bulk/workflow').reply(200, { + notice: 'Your update request is in progress.', + job_id: 'job_id', + }); + + const response = await makeBulkOperation().update(updateBody); + expect(response.notice).to.equal('Your update request is in progress.'); + expect(response.job_id).to.not.equal(undefined); + }); +}); + +function makeBulkOperation(data) { + return new BulkOperation(Axios, data); +} \ No newline at end of file diff --git a/test/unit/index.js b/test/unit/index.js index cde49f51..a49ffca1 100644 --- a/test/unit/index.js +++ b/test/unit/index.js @@ -14,6 +14,7 @@ require('./extension-test') require('./branch-test') require('./branchAlias-test') require('./release-test') +require('./bulkOperation-test') require('./asset-test') require('./webhook-test') require('./workflow-test') diff --git a/types/stack/bulkOperation/index.d.ts b/types/stack/bulkOperation/index.d.ts index 867345da..feb2160d 100644 --- a/types/stack/bulkOperation/index.d.ts +++ b/types/stack/bulkOperation/index.d.ts @@ -6,14 +6,15 @@ export interface BulkOperation extends SystemFields { publish(config: BulkOperationConfig): Promise unpublish(config: BulkOperationConfig): Promise delete(config: BulkDeleteConfig): Promise + addItems(config: AddItemsConfig): Promise } - export interface BulkOperationConfig { details: PublishItems skip_workflow_stage?: boolean approvals?: boolean is_nested?: boolean api_version?: string + bulk_version?: string } export interface PublishItems extends PublishDetails { @@ -44,4 +45,9 @@ export interface BulkDeleteAsset { export interface BranchData extends AnyProperty { name: string source: string +} + +export interface BulkAddItemsConfig { + data: AnyProperty; + bulk_version?: string; } \ No newline at end of file