Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/dx 1594 update move items #235

Merged
merged 3 commits into from
Dec 21, 2024
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
18 changes: 18 additions & 0 deletions lib/stack/bulkOperation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,24 @@ export function BulkOperation (http, data = {}) {
}
};

this.updateItems = async ({ data, bulk_version = "" }) => {
this.urlPath = `/bulk/release/update_items`;
const headers = {
headers: {
...cloneDeep(this.stackHeaders),
},
};
if (bulk_version) headers.headers.bulk_version = bulk_version;
try {
const response = await http.put(this.urlPath, data, headers);
if (response.data) {
return response.data;
}
} catch (error) {
console.error(error);
}
};

this.jobStatus = async ({ job_id, bulk_version = "" }) => {
this.urlPath = `/bulk/jobs/${job_id}`;
const headers = {
Expand Down
24 changes: 23 additions & 1 deletion lib/stack/release/items/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ export function ReleaseItem (http, data = {}) {
...cloneDeep(param)
}
} || {}

if (param.release_version) {
headers.headers['release_version'] = param.release_version;
delete headers.params.release_version;
}
const response = await http.get(this.urlPath, headers)
if (response.data) {
return new ContentstackCollection(response, http, this.stackHeaders, ReleaseItemCollection)
Expand All @@ -177,6 +180,25 @@ export function ReleaseItem (http, data = {}) {
error(err)
}
}

this.move = async ({ param, release_version = '' }) => {
const headers = {
headers: {
...cloneDeep(this.stackHeaders)
}
} || {}
if (release_version) {
headers.headers['release_version'] = release_version;
}
try {
const response = await http.put(`${this.urlPath}/move`, param, headers)
if (response.data) {
return response.data
}
} catch (err) {
throw error(err)
}
}
}
return this
}
Expand Down
71 changes: 67 additions & 4 deletions test/sanity-check/api/release-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, setup } from 'mocha'
import { jsonReader } from '../utility/fileOperations/readwrite.js'
import { releaseCreate } from '../mock/release.js'
import { releaseCreate, releaseCreate2 } from '../mock/release.js'
import { expect } from 'chai'
import { cloneDeep } from 'lodash'
import { contentstackClient } from '../utility/ContentstackClient.js'
Expand All @@ -11,6 +11,7 @@ dotenv.config()
let client = {}
let releaseUID = ''
let releaseUID2 = ''
let releaseUID3 = ''
let entries = {}
const itemToDelete = {}
const jobId = ''
Expand All @@ -35,6 +36,19 @@ describe('Relases api Test', () => {
.catch(done)
})

it('should create a Release 2', done => {
makeRelease()
.create(releaseCreate2)
.then((release) => {
releaseUID2 = release.uid
expect(release.name).to.be.equal(releaseCreate2.release.name)
expect(release.description).to.be.equal(releaseCreate2.release.description)
expect(release.uid).to.be.not.equal(null)
done()
})
.catch(done)
})

it('should fetch a Release from Uid', done => {
makeRelease(releaseUID)
.fetch()
Expand Down Expand Up @@ -101,7 +115,7 @@ describe('Relases api Test', () => {
it('should fetch a Release items from Uid', done => {
makeRelease(releaseUID)
.item()
.findAll()
.findAll({release_version: '2.0'})
.then((collection) => {
const itemdelete = collection.items[0]
itemToDelete['version'] = itemdelete.version
Expand All @@ -115,6 +129,26 @@ describe('Relases api Test', () => {
.catch(done)
})

it('should move release items from release1 to release2', done => {
const data = {
release_uid: releaseUID2,
items: [
{
uid: entries[1].uid,
locale: 'en-us'
}
]
}
makeRelease(releaseUID)
.item()
.move({ param: data, release_version: '2.0' })
.then((release) => {
expect(release.notice).to.be.equal('Release items moved successfully!')
done()
})
.catch(done)
})

it('should delete specific item', done => {
makeRelease(releaseUID)
.item()
Expand Down Expand Up @@ -205,7 +239,7 @@ describe('Relases api Test', () => {
makeRelease(releaseUID)
.clone({ name: 'New Clone Name', description: 'New Desc' })
.then((release) => {
releaseUID2 = release.uid
releaseUID3 = release.uid
expect(release.name).to.be.equal('New Clone Name')
expect(release.description).to.be.equal('New Desc')
expect(release.uid).to.be.not.equal(null)
Expand Down Expand Up @@ -257,6 +291,25 @@ describe('Relases api Test', () => {
.catch(done)
})

it('Bulk Operation: should update items to a release', done => {
const items = {
release: releaseUID,
action: 'publish',
locale: ['en-us'],
reference: true,
items: [
'$all'
],
}
doBulkOperation().updateItems({ data: items, bulk_version: '2.0' })
.then((response) => {
expect(response.notice).to.equal('Your update release items to latest version 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()
Expand All @@ -267,7 +320,7 @@ describe('Relases api Test', () => {
.catch(done)
})

it('should delete cloned Release with Uid', done => {
it('should delete specific Releases with Uid 2', done => {
makeRelease(releaseUID2)
.delete()
.then((data) => {
Expand All @@ -276,6 +329,16 @@ describe('Relases api Test', () => {
})
.catch(done)
})

it('should delete cloned Release with Uid', done => {
makeRelease(releaseUID3)
.delete()
.then((data) => {
expect(data.notice).to.be.equal('Release deleted successfully.')
done()
})
.catch(done)
})
})

function makeRelease (uid = null) {
Expand Down
11 changes: 10 additions & 1 deletion test/sanity-check/mock/release.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,13 @@ const releaseCreate = {
}
}

export { releaseCreate }
const releaseCreate2 = {
release: {
name: 'Second release',
description: 'Adding release date 2020-21-07',
locked: false,
archived: false
}
}

export { releaseCreate, releaseCreate2 }
26 changes: 24 additions & 2 deletions test/unit/bulkOperation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ describe('Contentstack BulkOperation test', () => {

it('should add items to a release', async () => {
const items = {
release: 'blt05e951e5f3a1d342',
release: 'release_uid',
action: 'publish',
locale: ['en-us'],
reference: true,
items: [
{
content_type_uid: 'ct_1',
uid: 'bltf6e197a18a11ec5f',
uid: 'entry_uid',
version: 2,
locale: 'en-us',
title: 'validation test',
Expand All @@ -57,6 +57,28 @@ describe('Contentstack BulkOperation test', () => {
expect(response.job_id).to.not.equal(undefined);
});

it('should update items to a release', async () => {
const items = {
release: 'release_uid',
action: 'publish',
locale: ['en-us'],
reference: true,
items: [
'$all'
],
};

var mock = new MockAdapter(Axios);
mock.onPut('/bulk/release/update_items').reply(200, {
notice: 'Your update release items to latest version request is in progress.',
job_id: 'job_id',
});

const response = await makeBulkOperation().updateItems({ data: items, bulk_version: '2.0' });
expect(response.notice).to.equal('Your update release items to latest version request is in progress.');
expect(response.job_id).to.not.equal(undefined);
});

it('should publish items in bulk', async () => {
const publishDetails = {
entries: [
Expand Down
20 changes: 20 additions & 0 deletions test/unit/release-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,26 @@ describe('Contentstack Release test', () => {
})
.catch(done)
})

it('Release move test', done => {
var mock = new MockAdapter(Axios)
mock.onPut('/releases/UID/items/move').reply(200, {
...noticeMock
})
makeRelease({
release: {
...systemUidMock
},
stackHeaders: stackHeadersMock
})
.item()
.move({ data: { release_uid: 'UID2', items: ['$all'] }, release_version: '2.0' })
.then((response) => {
expect(response.notice).to.be.equal(noticeMock.notice)
done()
})
.catch(done)
})
})

function makeRelease (data) {
Expand Down
2 changes: 1 addition & 1 deletion types/stack/bulkOperation/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface BulkOperation extends SystemFields {
unpublish(config: BulkOperationConfig): Promise<Response>
delete(config: BulkDeleteConfig): Promise<Response>
addItems(config: AddItemsConfig): Promise<Response>

updateItems(config: AddItemsConfig): Promise<Response>
jobStatus(config: BulkJobStatus): Promise<Response>
}
export interface BulkOperationConfig {
Expand Down
12 changes: 12 additions & 0 deletions types/stack/release/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface ReleaseDeploy extends AnyProperty {
export interface ReleaseItem extends SystemFields, Creatable<Release, { item?: ReleaseItemData; items?: Array<ReleaseItemData> }> {
delete(param?: { items: Array<ReleaseItemData> }): Promise<void>; // Changed return type to Promise<void>
findAll(param?: AnyProperty): Promise<ContentstackCollection<ReleaseItemData>>;
move(param: { param: MoveReleaseItems, release_version?: string }): Promise<any>;
}

// Data structure for Release item properties
Expand All @@ -42,3 +43,14 @@ export interface ReleaseItemData extends AnyProperty {
content_type_uid: string;
action: 'publish' | 'unpublish';
}

export interface MoveReleaseItems extends AnyProperty {
release_uid: string;
items: Array<ReleaseItemData>;
}

export interface MoveItemData extends AnyProperty {
uid: string;
locale: string;
variant_id?: string;
}
Loading