Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@ndustrial/contxt-sdk

Package Overview
Dependencies
Maintainers
9
Versions
123
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ndustrial/contxt-sdk - npm Package Compare versions

Comparing version 0.0.19 to 0.0.20

src/utils/assets/formatAssetAttributeValueFiltersToServer.js

16

CHANGELOG.md

@@ -0,1 +1,17 @@

## [v0.0.20](http://github.com/ndustrialio/contxt-sdk-js/tree/v0.0.20) (2018-07-16)
**Added**
* Methods around the display and manipulation of Asset Attributes Values. They are namespaced under assets (i.e. `assets.attributes.methodName()`) and include:
* AssetAttributes#createValue to add an asset attribute value
* AssetAttributes#deleteValue to delete an asset attribute value
* AssetAttributes#getEffectiveValuesByAssetId to get the effective asset attribute values for a particular asset
* AssetAttributes#getValuesByAttributeId to get a paginated list of asset attribute values for a particular attribute of a particular asset
* AssetAttributes#updateValue to update an asset attribute value
**Changed**
* Now supporting the `data_type` field for `AssetAttributes`
## [v0.0.19](http://github.com/ndustrialio/contxt-sdk-js/tree/v0.0.19) (2018-07-09)

@@ -2,0 +18,0 @@

153

docs/AssetAttributes.md

@@ -16,7 +16,7 @@ <a name="AssetAttributes"></a>

* [.update(assetAttributeId, update)](#AssetAttributes+update) ⇒ <code>Promise</code>
* [.createValue()](#AssetAttributes+createValue)
* [.deleteValue()](#AssetAttributes+deleteValue)
* [.getValue()](#AssetAttributes+getValue)
* [.getAllValues()](#AssetAttributes+getAllValues)
* [.updateValue()](#AssetAttributes+updateValue)
* [.createValue(assetId, assetAttributeValue)](#AssetAttributes+createValue) ⇒ <code>Promise</code>
* [.deleteValue(assetAttributeValueId)](#AssetAttributes+deleteValue) ⇒ <code>Promise</code>
* [.getEffectiveValuesByAssetId(assetId, [assetAttributeFilters])](#AssetAttributes+getEffectiveValuesByAssetId) ⇒ <code>Promise</code>
* [.getValuesByAttributeId(assetId, assetAttributeId, paginationOptions)](#AssetAttributes+getValuesByAttributeId) ⇒ <code>Promise</code>
* [.updateValue(assetAttributeId, update)](#AssetAttributes+updateValue) ⇒ <code>Promise</code>

@@ -49,2 +49,3 @@ <a name="new_AssetAttributes_new"></a>

| assetAttribute | <code>Object</code> | |
| assetAttribute.dataType | <code>string</code> | |
| assetAttribute.description | <code>string</code> | |

@@ -60,2 +61,3 @@ | [assetAttribute.isRequired] | <code>boolean</code> | |

.create('4f0e51c6-728b-4892-9863-6d002e61204d', {
dataType: 'boolean',
description: 'Square footage of a facility',

@@ -152,2 +154,3 @@ isRequired: true,

| update | <code>Object</code> | An object containing the updated data for the asset attribute |
| [update.dataType] | <code>string</code> | |
| [update.description] | <code>string</code> | |

@@ -162,2 +165,3 @@ | [update.isRequired] | <code>boolean</code> | |

.update('c7f927c3-11a7-4024-9269-e1231baeb765', {
dataType: 'boolean',
description: 'Temperature of a facility',

@@ -171,29 +175,150 @@ isRequired: false,

### contxtSdk.assets.attributes.createValue()
### contxtSdk.assets.attributes.createValue(assetId, assetAttributeValue) ⇒ <code>Promise</code>
Creates a new asset attribute value
API Endpoint: '/assets/:assetId/values'
Method: POST
**Kind**: instance method of [<code>AssetAttributes</code>](#AssetAttributes)
**Fulfill**: [<code>AssetAttributeValue</code>](./Typedefs.md#AssetAttributeValue)
**Reject**: <code>Error</code>
| Param | Type | Description |
| --- | --- | --- |
| assetId | <code>string</code> | The ID of the asset type (formatted as a UUID) |
| assetAttributeValue | <code>Object</code> | |
| assetAttributeValue.assetAttributeId | <code>string</code> | UUID corresponding to the asset attribute |
| assetAttributeValue.effectiveDate | <code>string</code> | ISO 8601 Extended Format date/time string |
| [assetAttributeValue.notes] | <code>string</code> | |
| assetAttributeValue.value | <code>string</code> | |
**Example**
```js
contxtSdk.assets.attributes
.createValue('2140cc2e-6d13-42ee-9941-487fe98f8e2d', {
assetAttributeId: 'cca11baa-cf7d-44c0-9d0a-6ad73d5f30cb',
effectiveDate: '2018-07-09T14:36:36.004Z',
notes: 'Iure delectus non sunt a voluptates pariatur fuga.',
value: '2206'
})
.then((assetAttributeValue) => console.log(assetAttributeValue))
.catch((err) => console.log(err));
```
<a name="AssetAttributes+deleteValue"></a>
### contxtSdk.assets.attributes.deleteValue()
### contxtSdk.assets.attributes.deleteValue(assetAttributeValueId) ⇒ <code>Promise</code>
Deletes an asset attribute value
API Endpoint: '/assets/attributes/values/:assetAttributeValueId'
Method: DELETE
**Kind**: instance method of [<code>AssetAttributes</code>](#AssetAttributes)
<a name="AssetAttributes+getValue"></a>
**Fulfill**: <code>undefined</code>
**Reject**: <code>Error</code>
### contxtSdk.assets.attributes.getValue()
Gets an asset attribute value
| Param | Type | Description |
| --- | --- | --- |
| assetAttributeValueId | <code>string</code> | The ID of the asset attribute value (formatted as a UUID) |
**Example**
```js
contxtSdk.assets.attributes.deleteValue(
'f4cd0d84-6c61-4d19-9322-7c1ab226dc83'
);
```
<a name="AssetAttributes+getEffectiveValuesByAssetId"></a>
### contxtSdk.assets.attributes.getEffectiveValuesByAssetId(assetId, [assetAttributeFilters]) ⇒ <code>Promise</code>
Gets the effective attribute values for a particular asset
API Endpoint: '/assets/:assetId/attributes/values'
Method: GET
**Kind**: instance method of [<code>AssetAttributes</code>](#AssetAttributes)
<a name="AssetAttributes+getAllValues"></a>
**Fulfill**: <code>AssetAttributeValue[]</code>
**Rejects**: <code>Error</code>
### contxtSdk.assets.attributes.getAllValues()
Gets a list of all asset attribute values
| Param | Type | Default | Description |
| --- | --- | --- | --- |
| assetId | <code>String</code> | | The ID of the asset for which you are looking up attribute values (formatted as a UUID) |
| [assetAttributeFilters] | <code>Object</code> | | Specific information that is used to filter the list of asset attribute values |
| [assetAttributeFilters.attributeLabel] | <code>String</code> | | Label of the parent asset attribute |
| [assetAttributeFilters.effectiveDate] | <code>String</code> | <code>(new Date()).toISOString()</code> | Effective date of the asset attribute values |
**Example**
```js
contxtSdk.assets.attributes
.getEffectiveValuesByAssetId('d7329ef3-ca63-4ad5-bb3e-632b702584f8', {
attributeLabel: 'Square Footage',
effectiveDate: '2018-07-11T19:14:49.715Z'
})
.then((assetAttributeValues) => {
console.log(assetAttributeValues);
})
.catch((err) => console.log(err));
```
<a name="AssetAttributes+getValuesByAttributeId"></a>
### contxtSdk.assets.attributes.getValuesByAttributeId(assetId, assetAttributeId, paginationOptions) ⇒ <code>Promise</code>
Gets a paginated list of asset attribute values for a particular attribute
of a particular asset
API Endpoint: '/assets/:assetId/attributes/:attributeId/values'
Method: GET
**Kind**: instance method of [<code>AssetAttributes</code>](#AssetAttributes)
**Fulfill**: [<code>AssetAttributeValueData</code>](./Typedefs.md#AssetAttributeValueData)
**Rejects**: <code>Error</code>
| Param | Type | Description |
| --- | --- | --- |
| assetId | <code>String</code> | The ID of the asset for which you are looking up attribute values (formatted as a UUID) |
| assetAttributeId | <code>String</code> | The ID of the asset attribute for which you are looking up attribute values (formatted as a UUID) |
| paginationOptions | [<code>PaginationOptions</code>](./Typedefs.md#PaginationOptions) | |
**Example**
```js
contxtSdk.assets.attributes
.getValuesByAttributeId(
'a4d80a97-cbf6-453b-bab5-0477e1ede04f',
'c2779610-44d7-4313-aea2-96cce58d6efd',
{
limit: 100,
offset: 0
}
)
.then((assetAttributeValuesData) => {
console.log(assetAttributeValuesData);
})
.catch((err) => console.log(err));
```
<a name="AssetAttributes+updateValue"></a>
### contxtSdk.assets.attributes.updateValue()
### contxtSdk.assets.attributes.updateValue(assetAttributeId, update) ⇒ <code>Promise</code>
Updates an asset attribute value
API Endpoint: '/assets/attributes/values/:assetAttributeValueId'
Method: PUT
**Kind**: instance method of [<code>AssetAttributes</code>](#AssetAttributes)
**Fulfill**: <code>undefined</code>
**Reject**: <code>Error</code>
| Param | Type | Description |
| --- | --- | --- |
| assetAttributeId | <code>string</code> | The ID of the asset attribute to update (formatted as a UUID) |
| update | <code>Object</code> | An object containing the updated data for the asset attribute value |
| [update.effectiveDate] | <code>string</code> | ISO 8601 Extended Format date/time string |
| [update.notes] | <code>string</code> | |
| [update.value] | <code>string</code> | |
**Example**
```js
contxtSdk.assets.attributes
.updateValue('2140cc2e-6d13-42ee-9941-487fe98f8e2d', {
effectiveDate: '2018-07-10T11:04:24.631Z',
notes: 'Dolores et sapiente sunt doloribus aut in.',
value: '61456'
})
.catch((err) => console.log(err));
```

@@ -75,2 +75,4 @@ ## Classes

<dd></dd>
<dt><a href="./Typedefs.md#AssetAttributeValueData">AssetAttributeValueData</a> : <code>Object</code></dt>
<dd></dd>
<dt><a href="./Typedefs.md#AssetType">AssetType</a> : <code>Object</code></dt>

@@ -122,2 +124,6 @@ <dd></dd>

<dd></dd>
<dt><a href="./Typedefs.md#PaginationMetadata">PaginationMetadata</a> : <code>Object</code></dt>
<dd></dd>
<dt><a href="./Typedefs.md#PaginationOptions">PaginationOptions</a> : <code>Object</code></dt>
<dd></dd>
<dt><a href="./Typedefs.md#SessionType">SessionType</a> : <code>Object</code></dt>

@@ -124,0 +130,0 @@ <dd><p>An adapter that allows the SDK to authenticate with different services and manage various tokens.

@@ -27,2 +27,3 @@ <a name="Asset"></a>

| createdAt | <code>string</code> | ISO 8601 Extended Format date/time string |
| dataType | <code>string</code> | Data Type of attribute with options "boolean", "date", "number", "string" |
| description | <code>string</code> | |

@@ -44,5 +45,3 @@ | id | <code>string</code> | UUID |

| --- | --- | --- |
| _metadata | <code>Object</code> | Metadata about the pagination settings |
| _metadata.offset | <code>number</code> | Offset of records in subsequent queries |
| _metadata.totalRecords | <code>number</code> | Total number of asset attributes found |
| _metadata | [<code>PaginationMetadata</code>](./Typedefs.md#PaginationMetadata) | Metadata about the pagination settings |
| records | [<code>Array.&lt;AssetAttribute&gt;</code>](#AssetAttribute) | |

@@ -67,2 +66,13 @@

<a name="AssetAttributeValueData"></a>
## AssetAttributeValueData : <code>Object</code>
**Kind**: global typedef
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| _metadata | [<code>PaginationMetadata</code>](./Typedefs.md#PaginationMetadata) | Metadata about the pagination settings |
| records | [<code>Array.&lt;AssetAttributeValue&gt;</code>](#AssetAttributeValue) | |
<a name="AssetType"></a>

@@ -339,2 +349,24 @@

<a name="PaginationMetadata"></a>
## PaginationMetadata : <code>Object</code>
**Kind**: global typedef
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| offset | <code>number</code> | Offset of records in subsequent queries |
| totalRecords | <code>number</code> | Total number of asset attributes found |
<a name="PaginationOptions"></a>
## PaginationOptions : <code>Object</code>
**Kind**: global typedef
**Properties**
| Name | Type | Description |
| --- | --- | --- |
| limit | <code>Number</code> | Maximum number of records to return per query |
| offset | <code>Number</code> | How many records from the first record to start the query |
<a name="SessionType"></a>

@@ -341,0 +373,0 @@

2

package.json
{
"name": "@ndustrial/contxt-sdk",
"version": "0.0.19",
"version": "0.0.20",
"description": "",

@@ -5,0 +5,0 @@ "main": "lib/index.js",

@@ -5,9 +5,28 @@ import isPlainObject from 'lodash.isplainobject';

formatAssetAttributeFromServer,
formatAssetAttributeToServer
formatAssetAttributeToServer,
formatAssetAttributeValueFiltersToServer,
formatAssetAttributeValueFromServer,
formatAssetAttributeValueToServer,
formatPaginatedDataFromServer,
formatPaginationOptionsToServer
} from '../utils/assets';
/**
* @typedef {Object} PaginationMetadata
* @property {number} offset Offset of records in subsequent queries
* @property {number} totalRecords Total number of asset attributes found
*/
/**
* @typedef {Object} PaginationOptions
* @property {Number} limit Maximum number of records to return per query
* @property {Number} offset How many records from the first record to start
* the query
*/
/**
* @typedef {Object} AssetAttribute
* @property {string} assetTypeId UUID corresponding with the asset type
* @property {string} createdAt ISO 8601 Extended Format date/time string
* @property {string} dataType Data Type of attribute with options "boolean", "date", "number", "string"
* @property {string} description

@@ -24,5 +43,3 @@ * @property {string} id UUID

* @typedef {Object} AssetAttributeData
* @property {Object} _metadata Metadata about the pagination settings
* @property {number} _metadata.offset Offset of records in subsequent queries
* @property {number} _metadata.totalRecords Total number of asset attributes found
* @property {PaginationMetadata} _metadata Metadata about the pagination settings
* @property {AssetAttribute[]} records

@@ -44,2 +61,8 @@ */

/**
* @typedef {Object} AssetAttributeValueData
* @property {PaginationMetadata} _metadata Metadata about the pagination settings
* @property {AssetAttributeValue[]} records
*/
/**
* Module that provides access to, and the manipulation of, information about

@@ -69,4 +92,4 @@ * different asset attributes and their values

* @param {string} assetTypeId The ID of the asset type (formatted as a UUID)
*
* @param {Object} assetAttribute
* @param {string} assetAttribute.dataType
* @param {string} assetAttribute.description

@@ -85,2 +108,3 @@ * @param {boolean} [assetAttribute.isRequired]

* .create('4f0e51c6-728b-4892-9863-6d002e61204d', {
* dataType: 'boolean',
* description: 'Square footage of a facility',

@@ -226,2 +250,3 @@ * isRequired: true,

* @param {Object} update An object containing the updated data for the asset attribute
* @param {string} [update.dataType]
* @param {string} [update.description]

@@ -239,2 +264,3 @@ * @param {boolean} [update.isRequired]

* .update('c7f927c3-11a7-4024-9269-e1231baeb765', {
* dataType: 'boolean',
* description: 'Temperature of a facility',

@@ -279,26 +305,268 @@ * isRequired: false,

* Creates a new asset attribute value
*
* API Endpoint: '/assets/:assetId/values'
* Method: POST
*
* @param {string} assetId The ID of the asset type (formatted as a UUID)
* @param {Object} assetAttributeValue
* @param {string} assetAttributeValue.assetAttributeId UUID corresponding to the asset attribute
* @param {string} assetAttributeValue.effectiveDate ISO 8601 Extended Format date/time string
* @param {string} [assetAttributeValue.notes]
* @param {string} assetAttributeValue.value
*
* @returns {Promise}
* @fulfill {AssetAttributeValue}
* @reject {Error}
*
* @example
* contxtSdk.assets.attributes
* .createValue('2140cc2e-6d13-42ee-9941-487fe98f8e2d', {
* assetAttributeId: 'cca11baa-cf7d-44c0-9d0a-6ad73d5f30cb',
* effectiveDate: '2018-07-09T14:36:36.004Z',
* notes: 'Iure delectus non sunt a voluptates pariatur fuga.',
* value: '2206'
* })
* .then((assetAttributeValue) => console.log(assetAttributeValue))
* .catch((err) => console.log(err));
*/
createValue() {}
createValue(assetId, assetAttributeValue = {}) {
const requiredFields = ['assetAttributeId', 'effectiveDate', 'value'];
if (!assetId) {
return Promise.reject(
new Error(
'An asset ID is required to create a new asset attribute value.'
)
);
}
for (let i = 0; i < requiredFields.length; i++) {
const field = requiredFields[i];
if (!assetAttributeValue[field]) {
return Promise.reject(
new Error(
`A ${field} is required to create a new asset attribute value.`
)
);
}
}
const data = formatAssetAttributeValueToServer(assetAttributeValue);
return this._request
.post(`${this._baseUrl}/assets/${assetId}/values`, data)
.then((assetAttributeValue) =>
formatAssetAttributeValueFromServer(assetAttributeValue)
);
}
/**
* Deletes an asset attribute value
*
* API Endpoint: '/assets/attributes/values/:assetAttributeValueId'
* Method: DELETE
*
* @param {string} assetAttributeValueId The ID of the asset attribute value (formatted as a UUID)
*
* @returns {Promise}
* @fulfill {undefined}
* @reject {Error}
*
* @example
* contxtSdk.assets.attributes.deleteValue(
* 'f4cd0d84-6c61-4d19-9322-7c1ab226dc83'
* );
*/
deleteValue() {}
deleteValue(assetAttributeValueId) {
if (!assetAttributeValueId) {
return Promise.reject(
new Error(
'An asset attribute value ID is required for deleting an asset attribute value.'
)
);
}
return this._request.delete(
`${this._baseUrl}/assets/attributes/values/${assetAttributeValueId}`
);
}
/**
* Gets an asset attribute value
* Gets the effective attribute values for a particular asset
*
* API Endpoint: '/assets/:assetId/attributes/values'
* Method: GET
*
* @param {String} assetId The ID of the asset for which you are looking up
* attribute values (formatted as a UUID)
* @param {Object} [assetAttributeFilters] Specific information that is used to
* filter the list of asset attribute values
* @param {String} [assetAttributeFilters.attributeLabel] Label of the parent
* asset attribute
* @param {String} [assetAttributeFilters.effectiveDate = (new Date()).toISOString()] Effective
* date of the asset attribute values
*
* @returns {Promise}
* @fulfill {AssetAttributeValue[]}
* @rejects {Error}
*
* @example
* contxtSdk.assets.attributes
* .getEffectiveValuesByAssetId('d7329ef3-ca63-4ad5-bb3e-632b702584f8', {
* attributeLabel: 'Square Footage',
* effectiveDate: '2018-07-11T19:14:49.715Z'
* })
* .then((assetAttributeValues) => {
* console.log(assetAttributeValues);
* })
* .catch((err) => console.log(err));
*/
getValue() {}
getEffectiveValuesByAssetId(assetId, assetAttributeFilters) {
if (!assetId) {
return Promise.reject(
new Error(
'An asset ID is required to get a list of asset attribute values.'
)
);
}
const formattedAssetAttributeFilters = formatAssetAttributeValueFiltersToServer(
assetAttributeFilters
);
return this._request
.get(`${this._baseUrl}/assets/${assetId}/attributes/values`, {
params: formattedAssetAttributeFilters
})
.then((assetAttributeValues) =>
assetAttributeValues.map(formatAssetAttributeValueFromServer)
);
}
/**
* Gets a list of all asset attribute values
* Gets a paginated list of asset attribute values for a particular attribute
* of a particular asset
*
* API Endpoint: '/assets/:assetId/attributes/:attributeId/values'
* Method: GET
*
* @param {String} assetId The ID of the asset for which you are looking up
* attribute values (formatted as a UUID)
* @param {String} assetAttributeId The ID of the asset attribute for which you are
* looking up attribute values (formatted as a UUID)
* @param {PaginationOptions}
*
* @returns {Promise}
* @fulfill {AssetAttributeValueData}
* @rejects {Error}
*
* @example
* contxtSdk.assets.attributes
* .getValuesByAttributeId(
* 'a4d80a97-cbf6-453b-bab5-0477e1ede04f',
* 'c2779610-44d7-4313-aea2-96cce58d6efd',
* {
* limit: 100,
* offset: 0
* }
* )
* .then((assetAttributeValuesData) => {
* console.log(assetAttributeValuesData);
* })
* .catch((err) => console.log(err));
*/
getAllValues() {}
getValuesByAttributeId(assetId, assetAttributeId, paginationOptions) {
if (!assetId) {
return Promise.reject(
new Error(
'An asset ID is required to get a list of asset attribute values.'
)
);
}
if (!assetAttributeId) {
return Promise.reject(
new Error(
'An asset attribute ID is required to get a list of asset attribute values.'
)
);
}
const formattedPaginationOptions = formatPaginationOptionsToServer(
paginationOptions
);
return this._request
.get(
`${
this._baseUrl
}/assets/${assetId}/attributes/${assetAttributeId}/values`,
{ params: formattedPaginationOptions }
)
.then((assetAttributeValueData) =>
formatPaginatedDataFromServer(
assetAttributeValueData,
formatAssetAttributeValueFromServer
)
);
}
/**
* Updates an asset attribute value
*
* API Endpoint: '/assets/attributes/values/:assetAttributeValueId'
* Method: PUT
*
* @param {string} assetAttributeId The ID of the asset attribute to update (formatted as a UUID)
* @param {Object} update An object containing the updated data for the asset attribute value
* @param {string} [update.effectiveDate] ISO 8601 Extended Format date/time string
* @param {string} [update.notes]
* @param {string} [update.value]
*
* @returns {Promise}
* @fulfill {undefined}
* @reject {Error}
*
* @example
* contxtSdk.assets.attributes
* .updateValue('2140cc2e-6d13-42ee-9941-487fe98f8e2d', {
* effectiveDate: '2018-07-10T11:04:24.631Z',
* notes: 'Dolores et sapiente sunt doloribus aut in.',
* value: '61456'
* })
* .catch((err) => console.log(err));
*/
updateValue() {}
updateValue(assetAttributeValueId, update) {
if (!assetAttributeValueId) {
return Promise.reject(
new Error(
'An asset attribute value ID is required to update an asset attribute value.'
)
);
}
if (!update) {
return Promise.reject(
new Error('An update is required to update an asset attribute value.')
);
}
if (!isPlainObject(update)) {
return Promise.reject(
new Error(
'The asset attribute value update must be a well-formed object with the data you wish to update.'
)
);
}
const formattedUpdate = formatAssetAttributeValueToServer(update);
return this._request.put(
`${this._baseUrl}/assets/attributes/values/${assetAttributeValueId}`,
formattedUpdate
);
}
}
export default AssetAttributes;

@@ -385,5 +385,4 @@ import omit from 'lodash.omit';

});
it('throws an error when the asset type ID is missing', function() {
expect(promise).to.be.rejectedWith(
return expect(promise).to.be.rejectedWith(
'An asset type ID is required to get a list of all asset attributes.'

@@ -491,11 +490,485 @@ );

describe('createValue', function() {});
describe('createValue', function() {
context('when all required information is supplied', function() {
let assetId;
let formatAssetAttributeValueFromServer;
let formatAssetAttributeValueToServer;
let promise;
let request;
let valueFromServerAfterFormat;
let valueFromServerBeforeFormat;
let valueToServerAfterFormat;
let valueToServerBeforeFormat;
describe('deleteValue', function() {});
beforeEach(function() {
valueToServerBeforeFormat = fixture.build('assetAttributeValue');
valueToServerAfterFormat = fixture.build(
'assetAttributeValue',
valueToServerBeforeFormat,
{
fromServer: true
}
);
valueFromServerAfterFormat = fixture.build('assetAttributeValue');
valueFromServerBeforeFormat = fixture.build(
'assetAttributeValue',
valueFromServerAfterFormat,
{ fromServer: true }
);
assetId = fixture.build('asset').id;
describe('getValue', function() {});
formatAssetAttributeValueFromServer = this.sandbox
.stub(assetsUtils, 'formatAssetAttributeValueFromServer')
.returns(valueFromServerAfterFormat);
formatAssetAttributeValueToServer = this.sandbox
.stub(assetsUtils, 'formatAssetAttributeValueToServer')
.returns(valueToServerAfterFormat);
request = {
...baseRequest,
post: this.sandbox.stub().resolves(valueFromServerBeforeFormat)
};
describe('getAllValues', function() {});
const assetAttributes = new AssetAttributes(
baseSdk,
request,
expectedHost
);
describe('updateValue', function() {});
promise = assetAttributes.createValue(
assetId,
valueToServerBeforeFormat
);
});
it('formats the submitted asset attribute value object to send to the server ', function() {
expect(formatAssetAttributeValueToServer).to.be.calledWith(
valueToServerBeforeFormat
);
});
it('creates a new asset attribute value', function() {
expect(request.post).to.be.calledWith(
`${expectedHost}/assets/${assetId}/values`,
valueToServerAfterFormat
);
});
it('formats the returned asset attribute object', function() {
return promise.then(() => {
expect(formatAssetAttributeValueFromServer).to.be.calledWith(
valueFromServerBeforeFormat
);
});
});
it('returns a fulfilled promise with the new asset attribute value information', function() {
return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
valueFromServerAfterFormat
);
});
});
context('when there is missing required information', function() {
let value;
let assetAttributes;
beforeEach(function() {
value = fixture.build('assetAttributeValue');
assetAttributes = new AssetAttributes(
baseSdk,
baseRequest,
expectedHost
);
});
it('throws an error if there is no asset type ID provided', function() {
const promise = assetAttributes.createValue(null, value);
return expect(promise).to.be.rejectedWith(
'An asset ID is required to create a new asset attribute value.'
);
});
['assetAttributeId', 'effectiveDate', 'value'].forEach(function(field) {
it(`throws an error when ${field} is missing`, function() {
const promise = assetAttributes.createValue(
value.id,
omit(value, [field])
);
return expect(promise).to.be.rejectedWith(
`A ${field} is required to create a new asset attribute value.`
);
});
});
});
});
describe('deleteValue', function() {
context('when all required information is supplied', function() {
let valueId;
let promise;
beforeEach(function() {
valueId = fixture.build('assetAttributeValue').id;
const assetAttributes = new AssetAttributes(
baseSdk,
baseRequest,
expectedHost
);
promise = assetAttributes.deleteValue(valueId);
});
it('requests to delete the asset attribute value', function() {
expect(baseRequest.delete).to.be.calledWith(
`${expectedHost}/assets/attributes/values/${valueId}`
);
});
it('returns a fulfilled promise', function() {
return expect(promise).to.be.fulfilled;
});
});
context('when there is missing required information', function() {
it('throws an error when the asset attribute value ID is missing', function() {
const assetAttributes = new AssetAttributes(
baseSdk,
baseRequest,
expectedHost
);
const promise = assetAttributes.deleteValue();
return expect(promise).to.be.rejectedWith(
'An asset attribute value ID is required for deleting an asset attribute value.'
);
});
});
});
describe('getEffectiveValuesByAssetId', function() {
context('when all required information is supplied', function() {
let assetId;
let formatAssetAttributeValueFiltersToServer;
let formatAssetAttributeValueFromServer;
let promise;
let request;
let valueFiltersToServerAfterFormat;
let valueFiltersToServerBeforeFormat;
let valuesFromServerAfterFormat;
let valuesFromServerBeforeFormat;
beforeEach(function() {
assetId = fixture.build('asset').id;
valueFiltersToServerAfterFormat = {
attribute_label: faker.hacker.phrase(),
effective_date: faker.date.recent().toISOString()
};
valueFiltersToServerBeforeFormat = {
attributeLabel: faker.hacker.phrase(),
effectiveDate: faker.date.recent().toISOString()
};
valuesFromServerAfterFormat = fixture.buildList(
'assetAttributeValue',
faker.random.number({ min: 1, max: 20 }),
{ assetId }
);
valuesFromServerBeforeFormat = valuesFromServerAfterFormat.map(
(values) =>
fixture.build('assetAttributeValue', values, { fromServer: true })
);
formatAssetAttributeValueFiltersToServer = this.sandbox
.stub(assetsUtils, 'formatAssetAttributeValueFiltersToServer')
.returns(valueFiltersToServerAfterFormat);
formatAssetAttributeValueFromServer = this.sandbox
.stub(assetsUtils, 'formatAssetAttributeValueFromServer')
.callsFake((value, index) => valuesFromServerAfterFormat[index]);
request = {
...baseRequest,
get: this.sandbox.stub().resolves(valuesFromServerBeforeFormat)
};
const assetAttributes = new AssetAttributes(
baseSdk,
request,
expectedHost
);
promise = assetAttributes.getEffectiveValuesByAssetId(
assetId,
valueFiltersToServerBeforeFormat
);
});
it('formats the filters sent to the server', function() {
expect(formatAssetAttributeValueFiltersToServer).to.be.calledWith(
valueFiltersToServerBeforeFormat
);
});
it('gets a list of asset attributes from the server', function() {
expect(request.get).to.be.calledWith(
`${expectedHost}/assets/${assetId}/attributes/values`,
{ params: valueFiltersToServerAfterFormat }
);
});
it('formats the asset attribute values', function() {
return promise.then(() => {
valuesFromServerBeforeFormat.forEach((value) => {
expect(formatAssetAttributeValueFromServer).to.be.calledWith(value);
});
});
});
it('resolves with a list of asset attribute values', function() {
return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
valuesFromServerAfterFormat
);
});
});
context('when there is missing required information', function() {
it('throws an error when the asset ID is missing', function() {
const assetAttributes = new AssetAttributes(
baseSdk,
baseRequest,
expectedHost
);
const promise = assetAttributes.getEffectiveValuesByAssetId(null);
return expect(promise).to.be.rejectedWith(
'An asset ID is required to get a list of asset attribute values.'
);
});
});
});
describe('getValuesByAttributeId', function() {
context('when all required information is supplied', function() {
let assetId;
let attributeId;
let formatPaginatedDataFromServer;
let formatPaginationOptionsToServer;
let paginationOptionsBeforeFormat;
let paginationOptionsAfterFormat;
let promise;
let request;
let valuesFromServerAfterFormat;
let valuesFromServerBeforeFormat;
beforeEach(function() {
assetId = fixture.build('asset').id;
attributeId = fixture.build('assetAttribute').id;
paginationOptionsBeforeFormat = {
limit: faker.random.number({ min: 10, max: 1000 }),
offset: faker.random.number({ max: 1000 })
};
paginationOptionsAfterFormat = {
limit: faker.random.number({ min: 10, max: 1000 }),
offset: faker.random.number({ max: 1000 })
};
valuesFromServerAfterFormat = {
_metadata: fixture.build('paginationMetadata'),
records: fixture.buildList(
'assetAttributeValue',
faker.random.number({ min: 1, max: 20 }),
{
assetId,
assetAttributeId: attributeId
}
)
};
valuesFromServerBeforeFormat = {
...valuesFromServerAfterFormat,
records: valuesFromServerAfterFormat.records.map((values) =>
fixture.build('assetAttributeValue', values, { fromServer: true })
)
};
formatPaginatedDataFromServer = this.sandbox
.stub(assetsUtils, 'formatPaginatedDataFromServer')
.returns(valuesFromServerAfterFormat);
formatPaginationOptionsToServer = this.sandbox
.stub(assetsUtils, 'formatPaginationOptionsToServer')
.returns(paginationOptionsAfterFormat);
request = {
...baseRequest,
get: this.sandbox.stub().resolves(valuesFromServerBeforeFormat)
};
const assetAttributes = new AssetAttributes(
baseSdk,
request,
expectedHost
);
promise = assetAttributes.getValuesByAttributeId(
assetId,
attributeId,
paginationOptionsBeforeFormat
);
});
it('formats the pagination options sent to the server', function() {
expect(formatPaginationOptionsToServer).to.be.calledWith(
paginationOptionsBeforeFormat
);
});
it('gets a list of asset attribute values from the server', function() {
expect(request.get).to.be.calledWith(
`${expectedHost}/assets/${assetId}/attributes/${attributeId}/values`,
{ params: paginationOptionsAfterFormat }
);
});
it('formats the asset attribute value data', function() {
return promise.then(() => {
expect(formatPaginatedDataFromServer).to.be.calledWith(
valuesFromServerBeforeFormat
);
});
});
it('resolves with a list of asset attribute values', function() {
return expect(promise).to.be.fulfilled.and.to.eventually.deep.equal(
valuesFromServerAfterFormat
);
});
});
context('when there is missing required information', function() {
it('throws an error when the asset ID is missing', function() {
const assetAttributes = new AssetAttributes(
baseSdk,
baseRequest,
expectedHost
);
const promise = assetAttributes.getValuesByAttributeId(
null,
fixture.build('assetAttribute').id
);
return expect(promise).to.be.rejectedWith(
'An asset ID is required to get a list of asset attribute values.'
);
});
it('throws an error when the asset attribute ID is missing', function() {
const assetAttributes = new AssetAttributes(
baseSdk,
baseRequest,
expectedHost
);
const promise = assetAttributes.getValuesByAttributeId(
fixture.build('asset').id,
null
);
return expect(promise).to.be.rejectedWith(
'An asset attribute ID is required to get a list of asset attribute values.'
);
});
});
});
describe('updateValue', function() {
context('when all required information is supplied', function() {
let valueToServerAfterFormat;
let valueToServerBeforeFormat;
let formatAssetAttributeValueToServer;
let promise;
beforeEach(function() {
valueToServerBeforeFormat = fixture.build('assetAttributeValue');
valueToServerAfterFormat = fixture.build(
'assetAttributeValue',
valueToServerBeforeFormat,
{ fromServer: true }
);
formatAssetAttributeValueToServer = this.sandbox
.stub(assetsUtils, 'formatAssetAttributeValueToServer')
.returns(valueToServerAfterFormat);
const assetAttributes = new AssetAttributes(
baseSdk,
baseRequest,
expectedHost
);
promise = assetAttributes.updateValue(
valueToServerBeforeFormat.id,
valueToServerBeforeFormat
);
});
it('formats the data into the right format', function() {
expect(formatAssetAttributeValueToServer).to.calledWith(
valueToServerBeforeFormat
);
});
it('updates the asset attribute value', function() {
expect(baseRequest.put).to.be.calledWith(
`${expectedHost}/assets/attributes/values/${
valueToServerAfterFormat.id
}`,
valueToServerAfterFormat
);
});
it('returns a fulfilled promise', function() {
return expect(promise).to.be.fulfilled;
});
});
context(
'when there is missing or malformed required information',
function() {
let assetAttributes;
beforeEach(function() {
assetAttributes = new AssetAttributes(
baseSdk,
baseRequest,
expectedHost
);
});
it('throws an error when there is not provided asset attribute value ID', function() {
const valueUpdate = fixture.build('assetAttributeValue');
const promise = assetAttributes.updateValue(null, valueUpdate);
return expect(promise).to.be.rejectedWith(
'An asset attribute value ID is required to update an asset attribute value.'
);
});
it('throws an error when there is no update provided', function() {
const valueUpdate = fixture.build('assetAttributeValue');
const promise = assetAttributes.updateValue(valueUpdate.id);
return expect(promise).to.be.rejectedWith(
'An update is required to update an asset attribute value.'
);
});
it('throws an error when the update is not a well-formed object', function() {
const valueUpdate = fixture.build('assetAttributeValue');
const promise = assetAttributes.updateValue(valueUpdate.id, [
valueUpdate
]);
return expect(promise).to.be.rejectedWith(
'The asset attribute value update must be a well-formed object with the data you wish to update.'
);
});
}
);
});
});

@@ -7,2 +7,3 @@ /**

* @param {string} input.created_at ISO 8601 Extended Format date/time string
* @param {string} input.data_type
* @param {string} input.description

@@ -16,3 +17,3 @@ * @param {string} input.id UUID

*
* @returns {Asset}
* @returns {AssetAttribute}
*

@@ -25,2 +26,3 @@ * @private

createdAt: input.created_at,
dataType: input.data_type,
description: input.description,

@@ -27,0 +29,0 @@ id: input.id,

@@ -18,2 +18,3 @@ import omit from 'lodash.omit';

createdAt: assetAttribute.created_at,
dataType: assetAttribute.data_type,
isRequired: assetAttribute.is_required,

@@ -26,2 +27,3 @@ organizationId: assetAttribute.organization_id,

'created_at',
'data_type',
'is_required',

@@ -28,0 +30,0 @@ 'organization_id',

@@ -7,2 +7,3 @@ /**

* @returns {Object} output
* @returns {string} output.data_type
* @returns {string} output.description

@@ -18,2 +19,3 @@ * @returns {boolean} [output.is_required]

return {
data_type: input.dataType,
description: input.description,

@@ -20,0 +22,0 @@ is_required: input.isRequired,

@@ -14,2 +14,3 @@ import omit from 'lodash.omit';

...assetAttribute,
data_type: assetAttribute.dataType,
is_required: assetAttribute.isRequired,

@@ -21,2 +22,3 @@ organization_id: assetAttribute.organizationId

'createdAt',
'dataType',
'id',

@@ -23,0 +25,0 @@ 'isRequired',

import formatAssetAttributeDataFromServer from './formatAssetAttributeDataFromServer';
import formatAssetAttributeFromServer from './formatAssetAttributeFromServer';
import formatAssetAttributeToServer from './formatAssetAttributeToServer';
import formatAssetAttributeValueFiltersToServer from './formatAssetAttributeValueFiltersToServer';
import formatAssetAttributeValueFromServer from './formatAssetAttributeValueFromServer';
import formatAssetAttributeValueToServer from './formatAssetAttributeValueToServer';
import formatAssetFromServer from './formatAssetFromServer';

@@ -11,2 +14,4 @@ import formatAssetOptionsToServer from './formatAssetOptionsToServer';

import formatAssetTypeToServer from './formatAssetTypeToServer';
import formatPaginatedDataFromServer from './formatPaginatedDataFromServer';
import formatPaginationOptionsToServer from './formatPaginationOptionsToServer';

@@ -17,2 +22,5 @@ export {

formatAssetAttributeToServer,
formatAssetAttributeValueFiltersToServer,
formatAssetAttributeValueFromServer,
formatAssetAttributeValueToServer,
formatAssetFromServer,

@@ -24,3 +32,5 @@ formatAssetOptionsToServer,

formatAssetTypesDataFromServer,
formatAssetTypeToServer
formatAssetTypeToServer,
formatPaginatedDataFromServer,
formatPaginationOptionsToServer
};

@@ -12,2 +12,4 @@ 'use strict';

createdAt: () => faker.date.past().toISOString(),
dataType: () =>
faker.random.arrayElement(['boolean', 'date', 'number', 'string']),
description: () => faker.lorem.sentence(),

@@ -31,2 +33,5 @@ id: () => faker.random.uuid(),

assetAttribute.data_type = assetAttribute.dataType;
delete assetAttribute.dataType;
assetAttribute.is_required = assetAttribute.isRequired;

@@ -33,0 +38,0 @@ delete assetAttribute.isRequired;

@@ -7,2 +7,3 @@ 'use strict';

require('./assetAttribute');
require('./assetAttributeValue');
require('./assetType');

@@ -21,4 +22,5 @@ require('./audience');

require('./organization');
require('./paginationMetadata');
require('./userProfile');
module.exports = factory;

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc