Skip to content

Commit

Permalink
feat: aggregate & getManyComplex (#12)
Browse files Browse the repository at this point in the history
* feat: aggregate & getManyComplex

* feat: aggregate & getManyComplex

Added some testing

* feat: aggregate & getManyComplex

Updating the readme for the new endpoints

* feat: aggregate & getManyComplex

ReadMe Correction
  • Loading branch information
walter-manger authored Mar 14, 2017
1 parent e186eb9 commit 621f48e
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,62 @@ api.locations.getOne('PRODUCT', '1').then((response) => {
})
```

##### api.content.getManyComplex(type, filter): [{content}, ...]
Returns an array of content objects matching the `type` and `filter` option(s).

###### args:
* `type: {string}`
The type of content to get.
* `filter: {Object}`
A json object that is used to filter results from the api.

```js
// return all items that have values.showProducts = true paged with 10 per page
api.content.getManyComplex('PRODUCT', { pagination: { perPage: 10, page: 0 }, valuesFields: { showProducts: { equals: true } }}).then((response) => {
console.log('headers:', response.header) // contains paging information

// Available Pagination Headers
// Powerchord-Pagination-Desc:false
// Powerchord-Pagination-Page:0
// Powerchord-Pagination-Pagecount:4
// Powerchord-Pagination-Perpage:10
// Powerchord-Pagination-Resultcount:40
// Powerchord-Pagination-Sortby:title

console.log('content items:', response.data)
})
```

##### api.content.aggregate(type, listFilter, aggregate): [{content}, ...]
Returns an object of results objects matching the `type` and `filter` option(s).

###### args:
* `type: {string}`
The type of content to get.
* `listFilter: {Object}`
A json object that is used to filter results from the api.
* `aggregate: {Object}`
A json object that is used to specify the value fields to perform an aggregate on.

```js
// return aggregate values for the description value in the values.specifications collection
api.content.aggregate('PRODUCT',
{
valuesFields: {
showProduct: {
equals:true
}
}
},
{
parent:"specifications",
category:"description",
key:"value"
}).then((response) => {
console.log('aggregate items:', response.data)
})
```

#### Analytics
##### api.analytics.track(events)

Expand Down
29 changes: 29 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ function forteApi(credentials, scope, options) {
}
},
content: {
aggregate: function aggregate(type, list, aggregate) {
validateArgs('content_aggregate', arguments);
return client.post(_util.ApiPaths.content.aggregate(scope, type), { data: { list, aggregate } } );
},
getManyComplex: function getManyComplex(type, filter) {
validateArgs('content_getManyComplex', arguments);
return client.post(_util.ApiPaths.content.getMany(scope, type), {
data: filter,
});
},
getMany(type, filter) {
validateArgs('content_getMany', arguments)
return client.get(ApiPaths.content.getMany(scope, type), { params: filter })
Expand Down Expand Up @@ -306,6 +316,25 @@ const validators = {
throw new InvalidArgumentError('hostname');
}
},
content_aggregate(type, list, aggregate) {
if(isInvalidString(type)) {
throw new InvalidArgumentError('type')
}
if(isEmptyObject(list)) {
throw new InvalidArgumentError('list')
}
if(isEmptyObject(aggregate)) {
throw new InvalidArgumentError('aggregate')
}
},
content_getManyComplex(type, filter) {
if(isInvalidString(type)) {
throw new InvalidArgumentError('type')
}
if(isEmptyObject(filter)) {
throw new InvalidArgumentError('filter')
}
},
content_getMany(type, filter) {
if(isInvalidString(type)) {
throw new InvalidArgumentError('type')
Expand Down
3 changes: 3 additions & 0 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export const ApiPaths = {
}
},
content: {
aggregate: function aggregate(scope, type) {
return '/forte/organizations/' + scope.trunk + '/' + scope.branch + '/content/documents/' + type + '/aggregate/';
},
getMany: function getMany(scope, type) {
return '/forte/' + scope.trunk + '/' + scope.branch + '/content/' + type + '/';
},
Expand Down
41 changes: 41 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,47 @@ describe('forteApi', () => {
})
})
})

describe('.getManyComplex(type, filter)', () => {
const invalidTypes = [null, undefined, {}, '']
invalidTypes.forEach((type) => {
it(`should throw for type '${JSON.stringify(type)}'`, () => {
assert.throws(() => { api.content.getManyComplex(type, { status: 'active' }) }, InvalidArgumentError)
})
})

const invalidFilters = [null, undefined, {}, '']
invalidFilters.forEach((filter) => {
it(`should throw for filter '${JSON.stringify(filter)}'`, () => {
assert.throws(() => { api.content.getManyComplex('products', filter) }, InvalidArgumentError)
})
})
})

describe('.aggregate(type, list, aggregate)', () => {
const invalidTypes = [null, undefined, {}, '']
invalidTypes.forEach((type) => {
it(`should throw for type '${JSON.stringify(type)}'`, () => {
assert.throws(() => { api.content.aggregate(type, {}, {}) }, InvalidArgumentError)
})
})

const invalidLists = [null, undefined, '', {}]
invalidLists.forEach((list) => {
it(`should throw for list '${JSON.stringify(list)}'`, () => {
assert.throws(() => { api.content.aggregate('product', list, { test: ''}) }, InvalidArgumentError)
})
})

const invalidAggregates = [null, undefined, '', {}]
invalidAggregates.forEach((aggregate) => {
it(`should throw for aggregate '${JSON.stringify(aggregate)}'`, () => {
assert.throws(() => { api.content.aggregate('product', { test: ''}, aggregate ) }, InvalidArgumentError)
})
})

})

})

describe('api.composite', () => {
Expand Down

0 comments on commit 621f48e

Please sign in to comment.