Skip to content

Commit 83bfa98

Browse files
Added api_version support in get job status method.
1 parent 2a3fb9d commit 83bfa98

File tree

5 files changed

+177
-4
lines changed

5 files changed

+177
-4
lines changed

lib/stack/bulkOperation/index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,13 @@ export function BulkOperation (http, data = {}) {
9494
* @returns {Promise<Object>} Response Object.
9595
* @param {String} params.job_id - The ID of the job.
9696
* @param {String} [params.bulk_version] - The bulk version.
97+
* @param {String} [params.api_version] - The API version.
9798
* @example
9899
* client.stack({ api_key: 'api_key'}).bulkOperation().jobStatus({ job_id: 'job_id' })
99100
* .then((response) => { console.log(response) })
100101
*/
101102
// eslint-disable-next-line camelcase
102-
this.jobStatus = async ({ job_id, bulk_version = '' }) => {
103+
this.jobStatus = async ({ job_id, bulk_version = '', api_version = '' }) => {
103104
// eslint-disable-next-line camelcase
104105
this.urlPath = `/bulk/jobs/${job_id}`
105106
const headers = {
@@ -109,12 +110,18 @@ export function BulkOperation (http, data = {}) {
109110
}
110111
// eslint-disable-next-line camelcase
111112
if (bulk_version) headers.headers.bulk_version = bulk_version
113+
// eslint-disable-next-line camelcase
114+
if (api_version) headers.headers.api_version = api_version
112115
try {
113116
const response = await http.get(this.urlPath, headers)
114117
if (response.data) {
118+
// eslint-disable-next-line camelcase
119+
if (api_version) delete headers.headers.api_version
115120
return response.data
116121
}
117122
} catch (error) {
123+
// eslint-disable-next-line camelcase
124+
if (api_version) delete headers.headers.api_version
118125
console.error(error)
119126
}
120127
}

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@contentstack/management",
3-
"version": "1.23.1",
3+
"version": "1.23.2",
44
"description": "The Content Management API is used to manage the content of your Contentstack account",
55
"main": "./dist/node/contentstack-management.js",
66
"browser": "./dist/web/contentstack-management.js",

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

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@ import { describe, it, setup } from 'mocha'
33
import { jsonReader } from '../../sanity-check/utility/fileOperations/readwrite'
44
import { contentstackClient } from '../../sanity-check/utility/ContentstackClient'
55
import { singlepageCT, multiPageCT } from '../mock/content-type.js'
6+
import { createManagementToken } from '../mock/managementToken.js'
67
import dotenv from 'dotenv'
78
dotenv.config()
89

910
let client = {}
11+
let clientWithManagementToken = {}
1012
let entryUid1 = ''
1113
let assetUid1 = ''
1214
let entryUid2 = ''
1315
let assetUid2 = ''
16+
let jobId1 = ''
17+
let jobId2 = ''
18+
let jobId3 = ''
19+
let tokenUidDev = ''
20+
let tokenUid = ''
1421

1522
describe('BulkOperation api test', () => {
1623
setup(() => {
@@ -24,6 +31,22 @@ describe('BulkOperation api test', () => {
2431
entryUid2 = entryRead2.uid
2532
assetUid2 = assetRead2.uid
2633
client = contentstackClient(user.authtoken)
34+
clientWithManagementToken = contentstackClient()
35+
})
36+
37+
it('should create a Management Token for get job status', done => {
38+
makeManagementToken()
39+
.create(createManagementToken)
40+
.then((token) => {
41+
tokenUidDev = token.token
42+
tokenUid = token.uid
43+
expect(token.name).to.be.equal(createManagementToken.token.name)
44+
expect(token.description).to.be.equal(createManagementToken.token.description)
45+
expect(token.scope[0].module).to.be.equal(createManagementToken.token.scope[0].module)
46+
expect(token.uid).to.be.not.equal(null)
47+
done()
48+
})
49+
.catch(done)
2750
})
2851

2952
it('should publish one entry when publishDetails of an entry is passed', done => {
@@ -47,6 +70,7 @@ describe('BulkOperation api test', () => {
4770
.then((response) => {
4871
expect(response.notice).to.not.equal(undefined)
4972
expect(response.job_id).to.not.equal(undefined)
73+
jobId1 = response.job_id
5074
done()
5175
})
5276
.catch(done)
@@ -71,6 +95,7 @@ describe('BulkOperation api test', () => {
7195
.then((response) => {
7296
expect(response.notice).to.not.equal(undefined)
7397
expect(response.job_id).to.not.equal(undefined)
98+
jobId2 = response.job_id
7499
done()
75100
})
76101
.catch(done)
@@ -110,6 +135,138 @@ describe('BulkOperation api test', () => {
110135
.then((response) => {
111136
expect(response.notice).to.not.equal(undefined)
112137
expect(response.job_id).to.not.equal(undefined)
138+
jobId3 = response.job_id
139+
done()
140+
})
141+
.catch(done)
142+
})
143+
144+
it('should get job status for the first publish job', done => {
145+
doBulkOperationWithManagementToken(tokenUidDev)
146+
.jobStatus({ job_id: jobId1, api_version: '3.2' })
147+
.then((response) => {
148+
console.dir(response)
149+
expect(response).to.not.equal(undefined)
150+
expect(response.uid).to.not.equal(undefined)
151+
expect(response.status).to.not.equal(undefined)
152+
expect(response.action).to.not.equal(undefined)
153+
expect(response.summary).to.not.equal(undefined)
154+
expect(response.body).to.not.equal(undefined)
155+
done()
156+
})
157+
.catch((error) => {
158+
console.error('Job status error:', error)
159+
done(error)
160+
})
161+
})
162+
163+
it('should validate detailed job status response structure', done => {
164+
doBulkOperationWithManagementToken(tokenUidDev)
165+
.jobStatus({ job_id: jobId1, api_version: '3.2' })
166+
.then((response) => {
167+
console.dir(response)
168+
expect(response).to.not.equal(undefined)
169+
// Validate main job properties
170+
expect(response.uid).to.not.equal(undefined)
171+
expect(response.created_by).to.not.equal(undefined)
172+
expect(response.updated_by).to.not.equal(undefined)
173+
expect(response.created_at).to.not.equal(undefined)
174+
expect(response.updated_at).to.not.equal(undefined)
175+
expect(response.action).to.not.equal(undefined)
176+
expect(response.api_key).to.not.equal(undefined)
177+
expect(response.status).to.not.equal(undefined)
178+
179+
// Validate body structure
180+
expect(response.body).to.not.equal(undefined)
181+
expect(response.body.branch).to.not.equal(undefined)
182+
expect(response.body.locales).to.be.an('array')
183+
expect(response.body.environments).to.be.an('array')
184+
expect(response.body.published_at).to.not.equal(undefined)
185+
186+
// Validate summary structure
187+
expect(response.summary).to.not.equal(undefined)
188+
expect(response.summary.approvals).to.be.a('number')
189+
expect(response.summary.skip).to.be.a('number')
190+
expect(response.summary.state).to.not.equal(undefined)
191+
expect(response.summary.success).to.be.a('number')
192+
expect(response.summary.total_processed).to.be.a('number')
193+
expect(response.summary.unsuccess).to.be.a('number')
194+
195+
done()
196+
})
197+
.catch((error) => {
198+
console.error('Detailed job status error:', error)
199+
done(error)
200+
})
201+
})
202+
203+
it('should get job status for the second publish job', done => {
204+
doBulkOperationWithManagementToken(tokenUidDev)
205+
.jobStatus({ job_id: jobId2, api_version: '3.2' })
206+
.then((response) => {
207+
expect(response).to.not.equal(undefined)
208+
expect(response.uid).to.not.equal(undefined)
209+
expect(response.status).to.not.equal(undefined)
210+
expect(response.action).to.not.equal(undefined)
211+
expect(response.summary).to.not.equal(undefined)
212+
expect(response.body).to.not.equal(undefined)
213+
done()
214+
})
215+
.catch(done)
216+
})
217+
218+
it('should get job status for the third publish job', done => {
219+
doBulkOperationWithManagementToken(tokenUidDev)
220+
.jobStatus({ job_id: jobId3, api_version: '3.2' })
221+
.then((response) => {
222+
expect(response).to.not.equal(undefined)
223+
expect(response.uid).to.not.equal(undefined)
224+
expect(response.status).to.not.equal(undefined)
225+
expect(response.action).to.not.equal(undefined)
226+
expect(response.summary).to.not.equal(undefined)
227+
expect(response.body).to.not.equal(undefined)
228+
done()
229+
})
230+
.catch(done)
231+
})
232+
233+
it('should get job status with bulk_version parameter', done => {
234+
doBulkOperationWithManagementToken(tokenUidDev)
235+
.jobStatus({ job_id: jobId1, bulk_version: 'v3', api_version: '3.2' })
236+
.then((response) => {
237+
expect(response).to.not.equal(undefined)
238+
expect(response.uid).to.not.equal(undefined)
239+
expect(response.status).to.not.equal(undefined)
240+
expect(response.action).to.not.equal(undefined)
241+
expect(response.summary).to.not.equal(undefined)
242+
expect(response.body).to.not.equal(undefined)
243+
done()
244+
})
245+
.catch(done)
246+
})
247+
248+
it('should test job status endpoint accessibility', done => {
249+
doBulkOperationWithManagementToken(tokenUidDev)
250+
.jobStatus({ job_id: 'test-job-id', api_version: '3.2' })
251+
.then((response) => {
252+
done()
253+
})
254+
.catch((error) => {
255+
console.log('Job status endpoint error (expected for invalid job ID):', error.message)
256+
// This is expected to fail with invalid job ID, but should not be an auth error
257+
if (error.message && error.message.includes('authentication') || error.message.includes('401')) {
258+
done(error)
259+
} else {
260+
done() // Expected error for invalid job ID
261+
}
262+
})
263+
})
264+
265+
it('should delete a Management Token', done => {
266+
makeManagementToken(tokenUid)
267+
.delete()
268+
.then((data) => {
269+
expect(data.notice).to.be.equal('Management Token deleted successfully.')
113270
done()
114271
})
115272
.catch(done)
@@ -119,3 +276,11 @@ describe('BulkOperation api test', () => {
119276
function doBulkOperation (uid = null) {
120277
return client.stack({ api_key: process.env.API_KEY }).bulkOperation()
121278
}
279+
280+
function doBulkOperationWithManagementToken (tokenUidDev) {
281+
return clientWithManagementToken.stack({ api_key: process.env.API_KEY, management_token: tokenUidDev }).bulkOperation()
282+
}
283+
284+
function makeManagementToken (uid = null) {
285+
return client.stack({ api_key: process.env.API_KEY }).managementToken(uid)
286+
}

types/stack/bulkOperation/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,5 @@ export interface BulkAddItemsConfig {
5858
export interface BulkJobStatus {
5959
job_id: AnyProperty;
6060
bulk_version?: string;
61+
api_version?: string;
6162
}

0 commit comments

Comments
 (0)