Skip to content

Commit 3fba7cf

Browse files
add items to bulk operation and corresponding tests
1 parent 0e5105b commit 3fba7cf

File tree

5 files changed

+248
-1
lines changed

5 files changed

+248
-1
lines changed

lib/stack/bulkOperation/index.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,24 @@ export function BulkOperation (http, data = {}) {
99
this.stackHeaders = data.stackHeaders
1010
this.urlPath = `/bulk`
1111

12+
this.addItems = async ({ data, bulk_version = "" }) => {
13+
this.urlPath = `/bulk/release/items`;
14+
const headers = {
15+
headers: {
16+
...cloneDeep(this.stackHeaders),
17+
},
18+
};
19+
if (bulk_version) headers.headers.bulk_version = bulk_version;
20+
try {
21+
const response = await http.post(this.urlPath, data, headers);
22+
if (response.data) {
23+
return response.data;
24+
}
25+
} catch (error) {
26+
console.error(error);
27+
}
28+
};
29+
1230
/**
1331
* The Publish entries and assets in bulk request allows you to publish multiple entries and assets at the same time.
1432
* @memberof BulkOperation

test/sanity-check/api/release-test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,38 @@ describe('Relases api Test', () => {
213213
.catch(done)
214214
})
215215

216+
it('Bulk Operation: should add items to a release', done => {
217+
const items = {
218+
release: releaseUID,
219+
action: 'publish',
220+
locale: ['en-us'],
221+
reference: true,
222+
items: [
223+
{
224+
version: entries[1]._version,
225+
uid: entries[1].uid,
226+
content_type_uid: multiPageCT.content_type.uid,
227+
locale: 'en-us',
228+
title: entries[1].title
229+
},
230+
{
231+
version: entries[2]._version,
232+
uid: entries[2].uid,
233+
content_type_uid: multiPageCT.content_type.uid,
234+
locale: 'en-us',
235+
title: entries[2].title
236+
},
237+
],
238+
}
239+
doBulkOperation().addItems({ data: items, bulk_version: '2.0' })
240+
.then((response) => {
241+
expect(response.notice).to.equal('Your add to release request is in progress.')
242+
expect(response.job_id).to.not.equal(undefined)
243+
done()
244+
})
245+
.catch(done)
246+
})
247+
216248
it('should delete specific Releases with Uid ', done => {
217249
makeRelease(releaseUID)
218250
.delete()
@@ -237,3 +269,7 @@ describe('Relases api Test', () => {
237269
function makeRelease (uid = null) {
238270
return client.stack({ api_key: process.env.API_KEY }).release(uid)
239271
}
272+
273+
function doBulkOperation(uid = null) {
274+
return client.stack({ api_key: process.env.API_KEY }).bulkOperation()
275+
}

test/unit/bulkOperation-test.js

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
import Axios from 'axios';
2+
import { expect } from 'chai';
3+
import MockAdapter from 'axios-mock-adapter';
4+
import { describe, it } from 'mocha';
5+
import { BulkOperation } from '../../lib/stack/bulkOperation';
6+
import { stackHeadersMock } from './mock/objects';
7+
8+
describe('Contentstack BulkOperation test', () => {
9+
it('BulkOperation test without uid', done => {
10+
const bulkOperation = makeBulkOperation();
11+
expect(bulkOperation.urlPath).to.be.equal('/bulk');
12+
expect(bulkOperation.stackHeaders).to.be.equal(undefined);
13+
expect(bulkOperation.addItems).to.not.equal(undefined);
14+
expect(bulkOperation.publish).to.not.equal(undefined);
15+
expect(bulkOperation.unpublish).to.not.equal(undefined);
16+
expect(bulkOperation.delete).to.not.equal(undefined);
17+
done();
18+
});
19+
20+
it('BulkOperation test with stackHeaders', done => {
21+
const bulkOperation = makeBulkOperation({ stackHeaders: { ...stackHeadersMock } });
22+
expect(bulkOperation.urlPath).to.be.equal('/bulk');
23+
expect(bulkOperation.stackHeaders).to.not.equal(undefined);
24+
expect(bulkOperation.stackHeaders.api_key).to.be.equal(stackHeadersMock.api_key);
25+
expect(bulkOperation.addItems).to.not.equal(undefined);
26+
expect(bulkOperation.publish).to.not.equal(undefined);
27+
expect(bulkOperation.unpublish).to.not.equal(undefined);
28+
expect(bulkOperation.delete).to.not.equal(undefined);
29+
done();
30+
});
31+
32+
it('should add items to a release', async () => {
33+
const items = {
34+
release: 'blt05e951e5f3a1d342',
35+
action: 'publish',
36+
locale: ['en-us'],
37+
reference: true,
38+
items: [
39+
{
40+
content_type_uid: 'ct_1',
41+
uid: 'bltf6e197a18a11ec5f',
42+
version: 2,
43+
locale: 'en-us',
44+
title: 'validation test',
45+
},
46+
],
47+
};
48+
49+
var mock = new MockAdapter(Axios);
50+
mock.onPost('/bulk/release/items').reply(200, {
51+
notice: 'Your add to release request is in progress.',
52+
job_id: 'job_id',
53+
});
54+
55+
const response = await makeBulkOperation().addItems({ data: items, bulk_version: '2.0' });
56+
expect(response.notice).to.equal('Your add to release request is in progress.');
57+
expect(response.job_id).to.not.equal(undefined);
58+
});
59+
60+
it('should publish items in bulk', async () => {
61+
const publishDetails = {
62+
entries: [
63+
{
64+
uid: 'entry_uid',
65+
content_type: 'content_type_uid',
66+
version: 'version',
67+
locale: 'entry_locale',
68+
},
69+
],
70+
assets: [{ uid: 'uid' }],
71+
locales: ['en'],
72+
environments: ['env_uid'],
73+
};
74+
75+
var mock = new MockAdapter(Axios);
76+
mock.onPost('/bulk/publish').reply(200, {
77+
notice: 'Your publish request is in progress.',
78+
job_id: 'job_id',
79+
});
80+
81+
const response = await makeBulkOperation().publish({ details: publishDetails });
82+
expect(response.notice).to.equal('Your publish request is in progress.');
83+
expect(response.job_id).to.not.equal(undefined);
84+
});
85+
86+
it('should unpublish items in bulk', async () => {
87+
const unpublishDetails = {
88+
entries: [
89+
{
90+
uid: 'entry_uid',
91+
content_type: 'content_type_uid',
92+
version: 'version',
93+
locale: 'entry_locale',
94+
},
95+
],
96+
assets: [{ uid: 'uid' }],
97+
locales: ['en'],
98+
environments: ['env_uid'],
99+
};
100+
101+
var mock = new MockAdapter(Axios);
102+
mock.onPost('/bulk/unpublish').reply(200, {
103+
notice: 'Your unpublish request is in progress.',
104+
job_id: 'job_id',
105+
});
106+
107+
const response = await makeBulkOperation().unpublish({ details: unpublishDetails });
108+
expect(response.notice).to.equal('Your unpublish request is in progress.');
109+
expect(response.job_id).to.not.equal(undefined);
110+
});
111+
112+
it('should delete items in bulk', async () => {
113+
const deleteDetails = {
114+
entries: [
115+
{
116+
uid: 'entry_uid',
117+
content_type: 'content_type_uid',
118+
locale: 'entry_locale',
119+
},
120+
],
121+
assets: [{ uid: 'uid' }],
122+
};
123+
124+
var mock = new MockAdapter(Axios);
125+
mock.onPost('/bulk/delete').reply(200, {
126+
notice: 'Your delete request is in progress.',
127+
job_id: 'job_id',
128+
});
129+
130+
const response = await makeBulkOperation().delete({ details: deleteDetails });
131+
expect(response.notice).to.equal('Your delete request is in progress.');
132+
expect(response.job_id).to.not.equal(undefined);
133+
});
134+
135+
it('should update items in bulk', async () => {
136+
const updateBody = {
137+
entries: [
138+
{
139+
content_type: 'content_type_uid1',
140+
uid: 'entry_uid',
141+
locale: 'en-us',
142+
},
143+
{
144+
content_type: 'content_type_uid2',
145+
uid: 'entry_uid',
146+
locale: 'en-us',
147+
},
148+
],
149+
workflow: {
150+
workflow_stage: {
151+
comment: 'Workflow-related Comments',
152+
due_date: 'Thu Dec 01 2018',
153+
notify: false,
154+
uid: 'workflow_stage_uid',
155+
assigned_to: [
156+
{
157+
uid: 'user_uid',
158+
name: 'user_name',
159+
email: 'user_email_id',
160+
},
161+
],
162+
assigned_by_roles: [
163+
{
164+
uid: 'role_uid',
165+
name: 'role_name',
166+
},
167+
],
168+
},
169+
},
170+
};
171+
172+
var mock = new MockAdapter(Axios);
173+
mock.onPost('/bulk/workflow').reply(200, {
174+
notice: 'Your update request is in progress.',
175+
job_id: 'job_id',
176+
});
177+
178+
const response = await makeBulkOperation().update(updateBody);
179+
expect(response.notice).to.equal('Your update request is in progress.');
180+
expect(response.job_id).to.not.equal(undefined);
181+
});
182+
});
183+
184+
function makeBulkOperation(data) {
185+
return new BulkOperation(Axios, data);
186+
}

test/unit/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ require('./extension-test')
1414
require('./branch-test')
1515
require('./branchAlias-test')
1616
require('./release-test')
17+
require('./bulkOperation-test')
1718
require('./asset-test')
1819
require('./webhook-test')
1920
require('./workflow-test')

types/stack/bulkOperation/index.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ export interface BulkOperation extends SystemFields {
66
publish(config: BulkOperationConfig): Promise<Response>
77
unpublish(config: BulkOperationConfig): Promise<Response>
88
delete(config: BulkDeleteConfig): Promise<Response>
9+
addItems(config: AddItemsConfig): Promise<Response>
910
}
10-
1111
export interface BulkOperationConfig {
1212
details: PublishItems
1313
skip_workflow_stage?: boolean
1414
approvals?: boolean
1515
is_nested?: boolean
1616
api_version?: string
17+
bulk_version?: string
1718
}
1819

1920
export interface PublishItems extends PublishDetails {
@@ -44,4 +45,9 @@ export interface BulkDeleteAsset {
4445
export interface BranchData extends AnyProperty {
4546
name: string
4647
source: string
48+
}
49+
50+
export interface BulkAddItemsConfig {
51+
data: AnyProperty;
52+
bulk_version?: string;
4753
}

0 commit comments

Comments
 (0)