Skip to content

Fix/workflow and ct gf overwrite fix #78

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

Merged
merged 4 commits into from
Aug 22, 2023
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
5 changes: 3 additions & 2 deletions lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ export const publishUnpublish = async (http, url, httpBody, headers, locale = nu
export const upload = async ({ http, urlPath, stackHeaders, formData, params, method = 'POST' }) => {
const headers = {
headers: {
...params,
...cloneDeep(stackHeaders)
},
params: {
...cloneDeep(params)
}
} || {}

if (method === 'POST') {
return http.post(urlPath, formData, headers)
} else {
Expand Down
7 changes: 4 additions & 3 deletions lib/stack/contentType/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,17 +192,18 @@ export function ContentType (http, data = {}) {
* const data = {
* content_type: 'path/to/file.json',
* }
* client.stack({ api_key: 'api_key'}).contentType().import(data)
* client.stack({ api_key: 'api_key'}).contentType().import(data, { overwrite: true })
* .then((contentType) => console.log(contentType))
*
*/
this.import = async function (data) {
this.import = async function (data, params = {}) {
try {
const response = await upload({
http: http,
urlPath: `${this.urlPath}/import`,
stackHeaders: this.stackHeaders,
formData: createFormData(data)
formData: createFormData(data),
params: params
})
if (response.data) {
return new this.constructor(http, parseData(response, this.stackHeaders))
Expand Down
7 changes: 4 additions & 3 deletions lib/stack/globalField/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,18 @@ export function GlobalField (http, data = {}) {
* const data = {
* global_field: 'path/to/file.json',
* }
* client.stack({ api_key: 'api_key'}).globalField().import(data)
* client.stack({ api_key: 'api_key'}).globalField().import(data, { overwrite: true })
* .then((globalField) => console.log(globalField))
*
*/
this.import = async function (data) {
this.import = async function (data, params = {}) {
try {
const response = await upload({
http: http,
urlPath: `${this.urlPath}/import`,
stackHeaders: this.stackHeaders,
formData: createFormData(data)
formData: createFormData(data),
params: params
})
if (response.data) {
return new this.constructor(http, parseData(response, this.stackHeaders))
Expand Down
6 changes: 5 additions & 1 deletion lib/stack/workflow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,11 @@ export function Workflow (http, data = {}) {
}

export function WorkflowCollection (http, data) {
const obj = cloneDeep(data.workflows) || []
let obj = cloneDeep(data.workflows) || []
if (!Array.isArray(obj)) {
obj = [obj]
}

return obj.map((userData) => {
return new Workflow(http, { workflow: userData, stackHeaders: data.stackHeaders })
})
Expand Down
1 change: 0 additions & 1 deletion test/unit/auditLog-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ describe('Contentstack AuditLog test', () => {
makeAuditLog()
.fetchAll()
.then((response) => {
console.log(response);
expect(response.items[0].created_at).to.be.equal('created_at_date')
expect(response.items[0].uid).to.be.equal('UID')
done()
Expand Down
25 changes: 25 additions & 0 deletions test/unit/contentType-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,31 @@ describe('Contentstack ContentType test', () => {
})
.catch(done)
})

it('ContentType import test with overwrite flag', done => {
var mock = new MockAdapter(Axios)
mock.onPost('/content_types/import').reply(200, {
content_type: {
...contentTypeMock
}
})
const contentTypeUpload = { content_type: path.join(__dirname, '../api/mock/contentType.json') }
const form = createFormData(contentTypeUpload)()
var boundary = form.getBoundary()

expect(boundary).to.be.equal(form.getBoundary())
expect(boundary.length).to.be.equal(50)
makeContentType()
.import(contentTypeUpload, { overwrite: true })
.then((contentType) => {
checkContentType(contentType)
done()
})
.catch((err) => {
console.log('><><><><><><><', err)
done()
})
})
})

function makeContentType (data) {
Expand Down
22 changes: 22 additions & 0 deletions test/unit/globalField-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,28 @@ describe('Contentstack GlobalField test', () => {
})
.catch(done)
})

it('Global Field import test with overwrite flag', done => {
var mock = new MockAdapter(Axios)
mock.onPost('/global_fields/import').reply(200, {
global_field: {
...globalFieldMock
}
})
const gfUpload = { global_field: path.join(__dirname, '../api/mock/globalfield.json') }
const form = createFormData(gfUpload)()
var boundary = form.getBoundary()

expect(boundary).to.be.equal(form.getBoundary())
expect(boundary.length).to.be.equal(50)
makeGlobalField()
.import(gfUpload, { overwrite: true })
.then((webhook) => {
checkGlobalField(webhook)
done()
})
.catch(done)
})
})

function makeGlobalField (data) {
Expand Down
40 changes: 29 additions & 11 deletions test/unit/workflow-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ describe('Contentstack Workflow test', () => {

it('Workflow Collection test with blank data', done => {
const workflow = new WorkflowCollection(Axios, {})
expect(workflow.length).to.be.equal(0)
expect(workflow[0]).to.be.equal(undefined)
done()
})

Expand All @@ -93,7 +93,15 @@ describe('Contentstack Workflow test', () => {
workflowMock
]
})
expect(workflow.length).to.be.equal(1)
expect(typeof workflow[0]).to.be.equal('object')
checkWorkflow(workflow[0])
done()
})

it('Workflow Collection test with data without array', done => {
const workflow = new WorkflowCollection(Axios, {
workflows: workflowMock
})
checkWorkflow(workflow[0])
done()
})
Expand Down Expand Up @@ -130,6 +138,20 @@ describe('Contentstack Workflow test', () => {
.catch(done)
})

it('Workflow Fetch all without Stack Headers test with response in object format instead of array of workflows', done => {
var mock = new MockAdapter(Axios)
mock.onGet('/workflows').reply(200, {
workflows: workflowMock
})
makeWorkflow()
.fetchAll()
.then((workflows) => {
checkWorkflow(workflows.items[0])
done()
})
.catch(done)
})

it('Workflow Fetch all with params test', done => {
var mock = new MockAdapter(Axios)
mock.onGet('/workflows').reply(200, {
Expand Down Expand Up @@ -269,7 +291,6 @@ describe('Contentstack Workflow test', () => {
.catch(done)
})


it('Workflow content type get publish rules', done => {
var mock = new MockAdapter(Axios)
mock.onGet('/workflows/content_type/ct_UID').reply(200, {
Expand All @@ -288,7 +309,6 @@ describe('Contentstack Workflow test', () => {
.catch(done)
})


it('Workflow content type get publish rules', done => {
var mock = new MockAdapter(Axios)
mock.onGet('/workflows/content_type/ct_UID').reply(200, {
Expand All @@ -297,7 +317,7 @@ describe('Contentstack Workflow test', () => {
]
})
makeWorkflow().contentType('ct_UID')
.getPublishRules({ action: "publish", locale: "en-us" })
.getPublishRules({ action: 'publish', locale: 'en-us' })
.then((response) => {
checkPublishRules(response.items[0])
done()
Expand All @@ -306,10 +326,9 @@ describe('Contentstack Workflow test', () => {
})
})


function makeWorkflow (data) {
return new Workflow(Axios, data)
}
return new Workflow(Axios, data)
}

function checkWorkflow (workflow) {
checkSystemFields(workflow)
Expand All @@ -325,7 +344,6 @@ function checkPublishRules (publishRules) {
checkSystemFields(publishRules)
expect(publishRules.locale).to.be.equal('en-us')
expect(publishRules.action).to.be.equal('publish')
expect(publishRules.environment).to.be.equal("env")
expect(publishRules.workflow_stage).to.be.equal("stage")
expect(publishRules.environment).to.be.equal('env')
expect(publishRules.workflow_stage).to.be.equal('stage')
}

2 changes: 1 addition & 1 deletion types/stack/contentType/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface ContentType extends SystemFields, SystemFunction<ContentType> {
}

export interface ContentTypes extends Queryable<ContentType, {content_type: ContentTypeData}> {
import(data: {content_type: string}): Promise<ContentType>
import(data: {content_type: string}, params?: any): Promise<ContentType>
generateUid(name: string): string
}

Expand Down
2 changes: 1 addition & 1 deletion types/stack/globalField/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface GlobalField extends SystemFields, SystemFunction<GlobalField> {
}

export interface GlobalFields extends Queryable<GlobalField, {global_field: GlobalFieldData}> {
import(data: {global_field: string}): Promise<GlobalField>
import(data: {global_field: string}, params?: any): Promise<GlobalField>
}

export interface GlobalFieldData extends AnyProperty {
Expand Down