opengate-js 12.7.0 → 12.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/docs/dump.json CHANGED
@@ -42936,7 +42936,7 @@
42936
42936
  "__docId__": 1628,
42937
42937
  "kind": "file",
42938
42938
  "name": "src/provision/entities/DeviceBuilder.js",
42939
- "content": "'use strict';\r\n\r\nimport ComplexBuilder from './ComplexBuilder';\r\nimport {\r\n SubscriberID\r\n} from './SubscriberBuilder';\r\nimport {\r\n SubscriptionID\r\n} from './SubscriptionBuilder';\r\nimport HttpStatus from 'http-status-codes';\r\nimport q from 'q';\r\nimport jp from 'jsonpath';\r\n\r\nconst ID = 'provision.device.identifier';\r\n\r\nclass BoxBuilder {\r\n constructor(ogapi, obj, url, key, urlParameters, timeout) {\r\n let _this = this;\r\n let subscribers = {};\r\n let subscriptions = {};\r\n\r\n this._obj = obj;\r\n this._url = url;\r\n this._ogapi = ogapi;\r\n this._key = key;\r\n this._timeout = timeout || null;\r\n this._objClone = Object.assign({}, obj);\r\n\r\n this._deviceKeys = Object.keys(obj).filter(function(dsName) {\r\n return dsName.indexOf('subscriber') === -1 && dsName.indexOf('subscription') === -1;\r\n });\r\n this._subscriberKeys = Object.keys(obj).filter(function(dsName) {\r\n return dsName.indexOf('subscriber') !== -1;\r\n });\r\n this._subscriptionKeys = Object.keys(obj).filter(function(dsName) {\r\n return dsName.indexOf('subscription') !== -1;\r\n });\r\n this._administrationKeys = Object.keys(obj).filter(function(dsName) {\r\n return dsName.indexOf('provision.administration') !== -1;\r\n });\r\n this._wrappers = [];\r\n this._urlParameters = urlParameters;\r\n\r\n\r\n this._subscriberKeys.forEach((key) => {\r\n _this._obj[key].forEach((value) => {\r\n if (!subscribers[value._index.value]) {\r\n subscribers[value._index.value] = {};\r\n }\r\n subscribers[value._index.value][key] = {\r\n _value: value._value\r\n };\r\n });\r\n });\r\n\r\n this._subscriptionKeys.forEach((key) => {\r\n _this._obj[key].forEach((value) => {\r\n if (!subscriptions[value._index.value]) {\r\n subscriptions[value._index.value] = {};\r\n }\r\n subscriptions[value._index.value][key] = {\r\n _value: value._value\r\n };\r\n });\r\n });\r\n\r\n Object.keys(subscriptions).forEach((commsId) => {\r\n let obj = subscriptions[commsId];\r\n _this._administrationKeys.forEach((key) => {\r\n obj[key] = _this._obj[key];\r\n });\r\n _this._wrappers.push(new WrapperBuilder(_this._ogapi, obj, _this._url.replace('devices', 'subscriptions').replace('/' + _this._key._value._current.value, ''), obj[SubscriptionID]._value._current.value));\r\n });\r\n Object.keys(subscribers).forEach((commsId) => {\r\n let obj = subscribers[commsId];\r\n _this._administrationKeys.forEach((key) => {\r\n obj[key] = _this._obj[key];\r\n });\r\n _this._wrappers.push(new WrapperBuilder(_this._ogapi, obj, _this._url.replace('devices', 'subscribers').replace('/' + _this._key._value._current.value, ''), obj[SubscriberID]._value._current.value));\r\n });\r\n }\r\n\r\n _urlWithKey() {\r\n return this._url + '/' + this._key._value._current.value;\r\n }\r\n\r\n _getUrlParameters() {\r\n return this._urlParameters;\r\n }\r\n\r\n _setUrlParameters(parameters) {\r\n if (this._urlParameters) {\r\n var keys = Object.keys(parameters);\r\n for (var i = 0; i < keys.length; i++) {\r\n var key = keys[i];\r\n this._urlParameters[key] = parameters[key];\r\n }\r\n } else {\r\n this._urlParameters = parameters;\r\n }\r\n }\r\n\r\n\r\n create() {\r\n let defer = q.defer();\r\n let postObj = {};\r\n let putObj = this._objClone;\r\n let childEntityPromises = [];\r\n let _this = this;\r\n\r\n Object.keys(putObj).filter((key) => {\r\n return key.indexOf('subscriber') === -1 && key.indexOf('subscription') === -1;\r\n }).forEach((deviceKey) => {\r\n postObj[deviceKey] = putObj[deviceKey];\r\n }); \r\n\r\n this._wrappers.forEach((wrapper) => {\r\n childEntityPromises.push({\r\n wrapper: wrapper,\r\n promise: wrapper.execute(defer, 10)\r\n });\r\n });\r\n\r\n q.allSettled(\r\n childEntityPromises.reduce(function(previousValue, current) {\r\n previousValue.push(current.promise);\r\n return previousValue;\r\n }, [])).then(() => {\r\n defer.notify({\r\n message: 'OGAPI_201_ENTITIES_CREATED',\r\n type: 'success',\r\n percentage: 20\r\n });\r\n defer.notify({\r\n entity: _this._key._value._current.value,\r\n message: 'OGAPI_CREATING_DEVICE',\r\n type: 'success',\r\n percentage: 25\r\n });\r\n return _this._ogapi.Napi.post(_this._url, postObj, null, null, {\r\n flattened: true\r\n })\r\n .then((res) => {\r\n if (_this._wrappers.length > 0) {\r\n defer.notify({\r\n message: 'OGAPI_ADDING_RELATED_ENTITIES',\r\n type: 'success',\r\n percentage: 55\r\n });\r\n this._setUrlParameters({\r\n 'flattened': true\r\n });\r\n return _this._ogapi.Napi.put(_this._urlWithKey(), putObj, this._timeout, null, this._getUrlParameters())\r\n .then((res) => {\r\n if (res.statusCode === HttpStatus.OK) {\r\n if (typeof _this._onCreated === \"function\") {\r\n _this._onCreated(res.header.location);\r\n }\r\n defer.notify({\r\n entity: _this._key._value._current.value,\r\n message: 'OGAPI_DEVICE_CREATED ',\r\n type: 'success',\r\n percentage: 75\r\n });\r\n defer.resolve({\r\n location: res.header.location,\r\n statusCode: res.statusCode\r\n });\r\n } else {\r\n defer.reject({\r\n errors: res.errors,\r\n statusCode: res.statusCode\r\n });\r\n }\r\n });\r\n } else {\r\n if (res.statusCode === HttpStatus.CREATED) {\r\n if (typeof _this._onCreated === \"function\") {\r\n _this._onCreated(res.header.location);\r\n }\r\n defer.notify({\r\n entity: _this._key._value._current.value,\r\n message: 'OGAPI_DEVICE_CREATED',\r\n type: 'success',\r\n percentage: 75\r\n });\r\n defer.resolve({\r\n location: res.header.location,\r\n statusCode: res.statusCode\r\n });\r\n } else {\r\n defer.reject({\r\n errors: res.errors,\r\n statusCode: res.statusCode\r\n });\r\n }\r\n }\r\n\r\n })\r\n .catch((errores) => {\r\n \r\n if (errores.statusCode === HttpStatus.BAD_REQUEST) {\r\n let ms = jp.query(errores, '$..message')[0];\r\n\r\n if (ms.includes('Entity duplicated')) {\r\n defer.reject({\r\n errors: errores.data.errors,\r\n statusCode: errores.statusCode\r\n });\r\n } else {\r\n defer.reject({\r\n errors: errores.data.errors,\r\n statusCode: errores.statusCode\r\n });\r\n }\r\n\r\n } else {\r\n defer.reject({\r\n errors: errores.data,\r\n statusCode: errores.statusCode\r\n });\r\n }\r\n });\r\n }).catch((err) => {\r\n err.data.errors.forEach((err) => {\r\n var error = err.description;\r\n if (err.label)\r\n error += \":\" + err.label;\r\n defer.notify({\r\n message: 'Error: ' + error,\r\n type: 'error',\r\n percentage: 80\r\n });\r\n });\r\n let deletePromises = [_this.delete(defer, 90)];\r\n childEntityPromises.forEach((item) => {\r\n deletePromises.push(item.wrapper.delete(defer, 90));\r\n });\r\n q.allSettled(deletePromises).then(() => {\r\n defer.reject(err);\r\n }).catch(() => {\r\n defer.reject(err);\r\n });\r\n });\r\n return defer.promise;\r\n }\r\n \r\n update() {\r\n let defer = q.defer();\r\n let putObj = this._objClone;\r\n let childEntityPromises = [];\r\n let _this = this;\r\n\r\n this._wrappers.forEach((wrapper) => {\r\n childEntityPromises.push({\r\n wrapper: wrapper,\r\n promise: wrapper.execute(defer, 20)\r\n });\r\n });\r\n\r\n q.allSettled(\r\n childEntityPromises.reduce(function(previousValue, current) {\r\n previousValue.push(current.promise);\r\n return previousValue;\r\n }, [])).then(() => {\r\n defer.notify({\r\n message: 'OGAPI_201_ENTITIES_CREATED',\r\n type: 'success',\r\n percentage: 40\r\n });\r\n defer.notify({\r\n entity: _this._key._value._current.value,\r\n message: 'OGAPI_ADDING_RELATED_ENTITIES',\r\n type: 'success',\r\n percentage: 45\r\n });\r\n this._setUrlParameters({\r\n 'flattened': true\r\n });\r\n \r\n return _this._ogapi.Napi.put(_this._url, putObj, this._timeout, null, this._getUrlParameters())\r\n .then((res) => {\r\n if (res.statusCode === HttpStatus.OK) {\r\n if (typeof _this._onCreated === \"function\") {\r\n _this._onCreated(res.header.location);\r\n }\r\n defer.notify({\r\n entity: _this._key._value._current.value,\r\n message: 'OGAPI_DEVICE_UPDATED',\r\n type: 'success',\r\n percentage: 90\r\n });\r\n defer.resolve({\r\n location: res.header.location,\r\n statusCode: res.statusCode\r\n });\r\n } else {\r\n defer.reject({\r\n errors: res.errors,\r\n statusCode: res.statusCode\r\n });\r\n }\r\n });\r\n }).catch((err) => {\r\n defer.notify('OGAPI_SOMETHING_WRONG_UPDATING_DEVICE');\r\n defer.reject(err);\r\n });\r\n return defer.promise;\r\n }\r\n\r\n patch() {\r\n let defer = q.defer();\r\n let putObj = this._obj;\r\n let childEntityPromises = [];\r\n let _this = this;\r\n\r\n this._wrappers.forEach((wrapper) => {\r\n childEntityPromises.push({\r\n wrapper: wrapper,\r\n promise: wrapper.execute(defer, 20)\r\n });\r\n });\r\n\r\n q.allSettled(\r\n childEntityPromises.reduce(function(previousValue, current) {\r\n previousValue.push(current.promise);\r\n return previousValue;\r\n }, [])).then(() => {\r\n defer.notify({\r\n message: 'OGAPI_201_ENTITIES_CREATED',\r\n type: 'success',\r\n percentage: 40\r\n });\r\n defer.notify({\r\n entity: _this._key._value._current.value,\r\n message: 'OGAPI_ADDING_RELATED_ENTITIES',\r\n type: 'success',\r\n percentage: 45\r\n });\r\n this._setUrlParameters({\r\n 'flattened': true\r\n });\r\n \r\n return _this._ogapi.Napi.patch(_this._url, putObj, this._timeout, null, this._getUrlParameters())\r\n .then((res) => {\r\n if (res.statusCode === HttpStatus.OK) {\r\n if (typeof _this._onCreated === \"function\") {\r\n _this._onCreated(res.header.location);\r\n }\r\n defer.notify({\r\n entity: _this._key._value._current.value,\r\n message: 'OGAPI_DEVICE_UPDATED',\r\n type: 'success',\r\n percentage: 90\r\n });\r\n defer.resolve({\r\n location: res.header.location,\r\n statusCode: res.statusCode\r\n });\r\n } else {\r\n defer.reject({\r\n errors: res.errors,\r\n statusCode: res.statusCode\r\n });\r\n }\r\n });\r\n }).catch((err) => {\r\n defer.notify('OGAPI_SOMETHING_WRONG_UPDATING_DEVICE');\r\n defer.reject(err);\r\n });\r\n return defer.promise;\r\n }\r\n\r\n delete(defered, percentage) {\r\n let _this = this;\r\n return this._ogapi.Napi.delete(this._urlWithKey())\r\n .then((res) => {\r\n defered.notify({\r\n entity: _this._key,\r\n message: 'OGAPI_ENTITY_DELETED',\r\n type: 'warning',\r\n percentage: percentage\r\n });\r\n });\r\n }\r\n}\r\n\r\nclass WrapperBuilder {\r\n constructor(ogapi, obj, url, key) {\r\n this._obj = obj;\r\n this._url = url;\r\n this._ogapi = ogapi;\r\n this._key = key;\r\n this._created = false;\r\n }\r\n\r\n _urlWithKey() {\r\n return this._url + '/' + this._key;\r\n }\r\n\r\n _checkExists() {\r\n return this._ogapi.Napi.get(this._urlWithKey()).then(function(response) {\r\n return response.statusCode === HttpStatus.OK;\r\n }).catch((err) => {\r\n console.warn(err);\r\n return false;\r\n });\r\n }\r\n\r\n\r\n execute(defered, percentage) {\r\n let defer = q.defer();\r\n let _this = this;\r\n this._checkExists().then((exists) => {\r\n if (!exists) {\r\n create(defered, defer, percentage);\r\n } else {\r\n defer.resolve({\r\n message: 'OGAPI_ENTITY_ALREADY_CREATED',\r\n entity: _this._key\r\n });\r\n }\r\n }).catch((exists) => {\r\n if (!exists) {\r\n create(defered, defer, percentage);\r\n } else {\r\n defer.resolve({\r\n message: 'OGAPI_ENTITY_ALREADY_CREATED',\r\n entity: _this._key\r\n });\r\n }\r\n });\r\n return defer.promise;\r\n\r\n function create(defered, defer, percentage) {\r\n _this._obj['provision.administration.serviceGroup'] = \"emptyServiceGroup\"\r\n _this._ogapi.Napi.post(_this._url, _this._obj, null, null, {\r\n flattened: true\r\n })\r\n .then((res) => {\r\n _this._created = true;\r\n defered.notify({\r\n entity: _this._key,\r\n message: 'OGAPI_ENTITY_CREATED',\r\n type: 'success',\r\n percentage: percentage\r\n });\r\n defer.resolve({\r\n message: 'OGAPI_ENTITY_CREATED',\r\n entity: _this._key\r\n });\r\n }).catch((err) => {\r\n console.error(err);\r\n defered.notify({\r\n entity: _this._key,\r\n message: 'OGAPI_ENTITY_CREATED',\r\n type: 'warning',\r\n percentage: percentage\r\n });\r\n defer.reject({\r\n entity: _this._key,\r\n message: 'OGAPI_SOMETHING_WRONG_CREATING',\r\n });\r\n });\r\n }\r\n }\r\n\r\n delete(defered, percentage) {\r\n let _this = this;\r\n if (this._created) {\r\n return this._ogapi.Napi.delete(this._urlWithKey())\r\n .then((res) => {\r\n defered.notify({\r\n entity: _this._key,\r\n message: 'OGAPI_ENTITY_DELETED',\r\n type: 'warning',\r\n percentage: percentage\r\n });\r\n });\r\n }\r\n return Q.fcall(() => {\r\n return;\r\n });\r\n }\r\n\r\n}\r\n\r\n/**\r\n * Device builder. This builder give you the necessary tools to create a device using our OpenGate REST.\r\n */\r\nexport default class DeviceBuilder extends ComplexBuilder {\r\n\r\n /**\r\n * @param {!InternalOpenGateAPI} ogapi - this is ogapi instance\r\n * @param {!string} organization - this is the organization name where device will be created\r\n * @param {!array} [allowedDatastreams] - Allowed datastreams to add into the new device\r\n * @param {!array} [definedSchemas] - Jsonschema about all OpenGate specific types\r\n * @param {!Validator} [jsonSchemaValidator] - Json schema validator tool\r\n * @param {number} ms - timeout in milliseconds \r\n */\r\n\r\n constructor(ogapi, organization, allowedDatastreams, definedSchemas, jsonSchemaValidator, timeout) {\r\n super(ogapi, organization + '/devices', allowedDatastreams, definedSchemas, jsonSchemaValidator, timeout);\r\n this._organization = organization;\r\n }\r\n\r\n /**\r\n * This invoke a request to OpenGate North API and the callback is managed by promises\r\n * This function create a entity of provision\r\n * @return {Promise}\r\n * @property {function (result:object, statusCode:number)} then - When request it is OK\r\n * @property {function (error:string)} catch - When request it is NOK\r\n * @example\r\n * ogapi.organizationsBuilder().create()\r\n */\r\n create() {\r\n this._checkRequiredParameters();\r\n return (new BoxBuilder(this._ogapi, this._composeElement(), this._resource, this._getEntityKey(), this._getUrlParameters(), this._timeout)).create();\r\n }\r\n\r\n /**\r\n * This invoke a request to OpenGate North API and the callback is managed by promises\r\n * This function updates a entity of provision and check if any subscriber/subscription exits or no. \r\n * If a subscriber/subscription not exists then this entities will be created and after that will be added to entity box.\r\n * @return {Promise}\r\n * @property {function (result:object, statusCode:number)} then - When request it is OK\r\n * @property {function (error:string)} catch - When request it is NOK\r\n * @example\r\n * ogapi.entityBuilder.devicesBuilder().update()\r\n */\r\n update() {\r\n return (new BoxBuilder(this._ogapi, this._composeElement(), this._buildURL(), this._getEntityKey(), this._getUrlParameters(), this._timeout)).update();\r\n }\r\n\r\n _getEntityKey() {\r\n return this._entity[ID];\r\n }\r\n\r\n}",
42939
+ "content": "'use strict';\r\n\r\nimport ComplexBuilder from './ComplexBuilder';\r\nimport {\r\n SubscriberID\r\n} from './SubscriberBuilder';\r\nimport {\r\n SubscriptionID\r\n} from './SubscriptionBuilder';\r\nimport HttpStatus from 'http-status-codes';\r\nimport q from 'q';\r\nimport jp from 'jsonpath';\r\n\r\nconst ID = 'provision.device.identifier';\r\n\r\nclass BoxBuilder {\r\n constructor(ogapi, obj, url, key, urlParameters, timeout) {\r\n let _this = this;\r\n let subscribers = {};\r\n let subscriptions = {};\r\n\r\n this._obj = obj;\r\n this._url = url;\r\n this._ogapi = ogapi;\r\n this._key = key;\r\n this._timeout = timeout || null;\r\n this._objClone = Object.assign({}, obj);\r\n\r\n this._deviceKeys = Object.keys(obj).filter(function(dsName) {\r\n return dsName.indexOf('subscriber') === -1 && dsName.indexOf('subscription') === -1;\r\n });\r\n this._subscriberKeys = Object.keys(obj).filter(function(dsName) {\r\n return dsName.indexOf('subscriber') !== -1;\r\n });\r\n this._subscriptionKeys = Object.keys(obj).filter(function(dsName) {\r\n return dsName.indexOf('subscription') !== -1;\r\n });\r\n this._administrationKeys = Object.keys(obj).filter(function(dsName) {\r\n return dsName.indexOf('provision.administration') !== -1;\r\n });\r\n this._wrappers = [];\r\n this._urlParameters = urlParameters;\r\n\r\n\r\n this._subscriberKeys.forEach((key) => {\r\n _this._obj[key].forEach((value) => {\r\n if (!subscribers[value._index.value]) {\r\n subscribers[value._index.value] = {};\r\n }\r\n subscribers[value._index.value][key] = {\r\n _value: value._value\r\n };\r\n });\r\n });\r\n\r\n this._subscriptionKeys.forEach((key) => {\r\n _this._obj[key].forEach((value) => {\r\n if (!subscriptions[value._index.value]) {\r\n subscriptions[value._index.value] = {};\r\n }\r\n subscriptions[value._index.value][key] = {\r\n _value: value._value\r\n };\r\n });\r\n });\r\n\r\n Object.keys(subscriptions).forEach((commsId) => {\r\n let obj = subscriptions[commsId];\r\n _this._administrationKeys.forEach((key) => {\r\n obj[key] = _this._obj[key];\r\n });\r\n _this._wrappers.push(new WrapperBuilder(_this._ogapi, obj, _this._url.replace('devices', 'subscriptions').replace('/' + _this._key._value._current.value, ''), obj[SubscriptionID]._value._current.value));\r\n });\r\n Object.keys(subscribers).forEach((commsId) => {\r\n let obj = subscribers[commsId];\r\n _this._administrationKeys.forEach((key) => {\r\n obj[key] = _this._obj[key];\r\n });\r\n _this._wrappers.push(new WrapperBuilder(_this._ogapi, obj, _this._url.replace('devices', 'subscribers').replace('/' + _this._key._value._current.value, ''), obj[SubscriberID]._value._current.value));\r\n });\r\n }\r\n\r\n _urlWithKey() {\r\n return this._url + '/' + this._key._value._current.value;\r\n }\r\n\r\n _getUrlParameters() {\r\n return this._urlParameters;\r\n }\r\n\r\n _setUrlParameters(parameters) {\r\n if (this._urlParameters) {\r\n var keys = Object.keys(parameters);\r\n for (var i = 0; i < keys.length; i++) {\r\n var key = keys[i];\r\n this._urlParameters[key] = parameters[key];\r\n }\r\n } else {\r\n this._urlParameters = parameters;\r\n }\r\n }\r\n\r\n\r\n create() {\r\n let defer = q.defer();\r\n let postObj = {};\r\n let putObj = this._objClone;\r\n let childEntityPromises = [];\r\n let _this = this;\r\n\r\n Object.keys(putObj).filter((key) => {\r\n return key.indexOf('subscriber') === -1 && key.indexOf('subscription') === -1;\r\n }).forEach((deviceKey) => {\r\n postObj[deviceKey] = putObj[deviceKey];\r\n }); \r\n\r\n this._wrappers.forEach((wrapper) => {\r\n childEntityPromises.push({\r\n wrapper: wrapper,\r\n promise: wrapper.execute(defer, 10)\r\n });\r\n });\r\n\r\n q.allSettled(\r\n childEntityPromises.reduce(function(previousValue, current) {\r\n previousValue.push(current.promise);\r\n return previousValue;\r\n }, [])).then(() => {\r\n defer.notify({\r\n message: 'OGAPI_201_ENTITIES_CREATED',\r\n type: 'success',\r\n percentage: 20\r\n });\r\n defer.notify({\r\n entity: _this._key._value._current.value,\r\n message: 'OGAPI_CREATING_DEVICE',\r\n type: 'success',\r\n percentage: 25\r\n });\r\n return _this._ogapi.Napi.post(_this._url, postObj, null, null, {\r\n flattened: true\r\n })\r\n .then((res) => {\r\n if (_this._wrappers.length > 0) {\r\n defer.notify({\r\n message: 'OGAPI_ADDING_RELATED_ENTITIES',\r\n type: 'success',\r\n percentage: 55\r\n });\r\n this._setUrlParameters({\r\n 'flattened': true\r\n });\r\n return _this._ogapi.Napi.put(_this._urlWithKey(), putObj, this._timeout, null, this._getUrlParameters())\r\n .then((res) => {\r\n if (res.statusCode === HttpStatus.OK) {\r\n if (typeof _this._onCreated === \"function\") {\r\n _this._onCreated(res.header.location);\r\n }\r\n defer.notify({\r\n entity: _this._key._value._current.value,\r\n message: 'OGAPI_DEVICE_CREATED ',\r\n type: 'success',\r\n percentage: 75\r\n });\r\n defer.resolve({\r\n location: res.header.location,\r\n statusCode: res.statusCode\r\n });\r\n } else {\r\n defer.reject({\r\n errors: res.errors,\r\n statusCode: res.statusCode\r\n });\r\n }\r\n });\r\n } else {\r\n if (res.statusCode === HttpStatus.CREATED) {\r\n if (typeof _this._onCreated === \"function\") {\r\n _this._onCreated(res.header.location);\r\n }\r\n defer.notify({\r\n entity: _this._key._value._current.value,\r\n message: 'OGAPI_DEVICE_CREATED',\r\n type: 'success',\r\n percentage: 75\r\n });\r\n defer.resolve({\r\n location: res.header.location,\r\n statusCode: res.statusCode\r\n });\r\n } else {\r\n defer.reject({\r\n errors: res.errors,\r\n statusCode: res.statusCode\r\n });\r\n }\r\n }\r\n\r\n })\r\n .catch((errores) => {\r\n \r\n if (errores.statusCode === HttpStatus.BAD_REQUEST) {\r\n let ms = jp.query(errores, '$..message')[0];\r\n\r\n if (ms.includes('Entity duplicated')) {\r\n defer.reject({\r\n errors: errores.data.errors,\r\n statusCode: errores.statusCode\r\n });\r\n } else {\r\n defer.reject({\r\n errors: errores.data.errors,\r\n statusCode: errores.statusCode\r\n });\r\n }\r\n\r\n } else {\r\n defer.reject({\r\n errors: errores.data,\r\n statusCode: errores.statusCode\r\n });\r\n }\r\n });\r\n }).catch((err) => {\r\n err.data.errors.forEach((err) => {\r\n var error = err.description;\r\n if (err.label)\r\n error += \":\" + err.label;\r\n defer.notify({\r\n message: 'Error: ' + error,\r\n type: 'error',\r\n percentage: 80\r\n });\r\n });\r\n let deletePromises = [_this.delete(defer, 90)];\r\n childEntityPromises.forEach((item) => {\r\n deletePromises.push(item.wrapper.delete(defer, 90));\r\n });\r\n q.allSettled(deletePromises).then(() => {\r\n defer.reject(err);\r\n }).catch(() => {\r\n defer.reject(err);\r\n });\r\n });\r\n return defer.promise;\r\n }\r\n \r\n update() {\r\n let defer = q.defer();\r\n let putObj = this._objClone;\r\n let childEntityPromises = [];\r\n let _this = this;\r\n\r\n this._wrappers.forEach((wrapper) => {\r\n childEntityPromises.push({\r\n wrapper: wrapper,\r\n promise: wrapper.execute(defer, 20)\r\n });\r\n });\r\n\r\n q.allSettled(\r\n childEntityPromises.reduce(function(previousValue, current) {\r\n previousValue.push(current.promise);\r\n return previousValue;\r\n }, [])).then(() => {\r\n defer.notify({\r\n message: 'OGAPI_201_ENTITIES_CREATED',\r\n type: 'success',\r\n percentage: 40\r\n });\r\n defer.notify({\r\n entity: _this._key._value._current.value,\r\n message: 'OGAPI_ADDING_RELATED_ENTITIES',\r\n type: 'success',\r\n percentage: 45\r\n });\r\n this._setUrlParameters({\r\n 'flattened': true\r\n });\r\n \r\n return _this._ogapi.Napi.put(_this._url, putObj, this._timeout, null, this._getUrlParameters())\r\n .then((res) => {\r\n if (res.statusCode === HttpStatus.OK) {\r\n if (typeof _this._onCreated === \"function\") {\r\n _this._onCreated(res.header.location);\r\n }\r\n defer.notify({\r\n entity: _this._key._value._current.value,\r\n message: 'OGAPI_DEVICE_UPDATED',\r\n type: 'success',\r\n percentage: 90\r\n });\r\n defer.resolve({\r\n location: res.header.location,\r\n statusCode: res.statusCode\r\n });\r\n } else {\r\n defer.reject({\r\n errors: res.errors,\r\n statusCode: res.statusCode\r\n });\r\n }\r\n });\r\n }).catch((err) => {\r\n defer.notify('OGAPI_SOMETHING_WRONG_UPDATING_DEVICE');\r\n defer.reject(err);\r\n });\r\n return defer.promise;\r\n }\r\n\r\n patch() {\r\n let defer = q.defer();\r\n let putObj = this._obj;\r\n let childEntityPromises = [];\r\n let _this = this;\r\n\r\n this._wrappers.forEach((wrapper) => {\r\n childEntityPromises.push({\r\n wrapper: wrapper,\r\n promise: wrapper.execute(defer, 20)\r\n });\r\n });\r\n\r\n q.allSettled(\r\n childEntityPromises.reduce(function(previousValue, current) {\r\n previousValue.push(current.promise);\r\n return previousValue;\r\n }, [])).then(() => {\r\n defer.notify({\r\n message: 'OGAPI_201_ENTITIES_CREATED',\r\n type: 'success',\r\n percentage: 40\r\n });\r\n defer.notify({\r\n entity: _this._key._value._current.value,\r\n message: 'OGAPI_ADDING_RELATED_ENTITIES',\r\n type: 'success',\r\n percentage: 45\r\n });\r\n this._setUrlParameters({\r\n 'flattened': true\r\n });\r\n \r\n return _this._ogapi.Napi.patch(_this._url, putObj, this._timeout, null, this._getUrlParameters())\r\n .then((res) => {\r\n if (res.statusCode === HttpStatus.OK) {\r\n if (typeof _this._onCreated === \"function\") {\r\n _this._onCreated(res.header.location);\r\n }\r\n defer.notify({\r\n entity: _this._key._value._current.value,\r\n message: 'OGAPI_DEVICE_UPDATED',\r\n type: 'success',\r\n percentage: 90\r\n });\r\n defer.resolve({\r\n location: res.header.location,\r\n statusCode: res.statusCode\r\n });\r\n } else {\r\n defer.reject({\r\n errors: res.errors,\r\n statusCode: res.statusCode\r\n });\r\n }\r\n });\r\n }).catch((err) => {\r\n defer.notify('OGAPI_SOMETHING_WRONG_UPDATING_DEVICE');\r\n defer.reject(err);\r\n });\r\n return defer.promise;\r\n }\r\n\r\n delete(defered, percentage) {\r\n let _this = this;\r\n return this._ogapi.Napi.delete(this._urlWithKey())\r\n .then((res) => {\r\n defered.notify({\r\n entity: _this._key,\r\n message: 'OGAPI_ENTITY_DELETED',\r\n type: 'warning',\r\n percentage: percentage\r\n });\r\n });\r\n }\r\n}\r\n\r\nclass WrapperBuilder {\r\n constructor(ogapi, obj, url, key) {\r\n this._obj = obj;\r\n this._url = url;\r\n this._ogapi = ogapi;\r\n this._key = key;\r\n this._created = false;\r\n }\r\n\r\n _urlWithKey() {\r\n return this._url + '/' + this._key;\r\n }\r\n\r\n _checkExists() {\r\n return this._ogapi.Napi.get(this._urlWithKey()).then(function(response) {\r\n return response.statusCode === HttpStatus.OK;\r\n }).catch((err) => {\r\n console.warn(err);\r\n return false;\r\n });\r\n }\r\n\r\n\r\n execute(defered, percentage) {\r\n let defer = q.defer();\r\n let _this = this;\r\n this._checkExists().then((exists) => {\r\n if (!exists) {\r\n create(defered, defer, percentage);\r\n } else {\r\n defer.resolve({\r\n message: 'OGAPI_ENTITY_ALREADY_CREATED',\r\n entity: _this._key\r\n });\r\n }\r\n }).catch((exists) => {\r\n if (!exists) {\r\n create(defered, defer, percentage);\r\n } else {\r\n defer.resolve({\r\n message: 'OGAPI_ENTITY_ALREADY_CREATED',\r\n entity: _this._key\r\n });\r\n }\r\n });\r\n return defer.promise;\r\n\r\n function create(defered, defer, percentage) {\r\n _this._obj['provision.administration.serviceGroup'] = { \"_value\" : { \"_current\" : { \"value\" : \"emptyServiceGroup\" }}};\r\n _this._ogapi.Napi.post(_this._url, _this._obj, null, null, {\r\n flattened: true\r\n })\r\n .then((res) => {\r\n _this._created = true;\r\n defered.notify({\r\n entity: _this._key,\r\n message: 'OGAPI_ENTITY_CREATED',\r\n type: 'success',\r\n percentage: percentage\r\n });\r\n defer.resolve({\r\n message: 'OGAPI_ENTITY_CREATED',\r\n entity: _this._key\r\n });\r\n }).catch((err) => {\r\n console.error(err);\r\n defered.notify({\r\n entity: _this._key,\r\n message: 'OGAPI_ENTITY_CREATED',\r\n type: 'warning',\r\n percentage: percentage\r\n });\r\n defer.reject({\r\n entity: _this._key,\r\n message: 'OGAPI_SOMETHING_WRONG_CREATING',\r\n });\r\n });\r\n }\r\n }\r\n\r\n delete(defered, percentage) {\r\n let _this = this;\r\n if (this._created) {\r\n return this._ogapi.Napi.delete(this._urlWithKey())\r\n .then((res) => {\r\n defered.notify({\r\n entity: _this._key,\r\n message: 'OGAPI_ENTITY_DELETED',\r\n type: 'warning',\r\n percentage: percentage\r\n });\r\n });\r\n }\r\n return Q.fcall(() => {\r\n return;\r\n });\r\n }\r\n\r\n}\r\n\r\n/**\r\n * Device builder. This builder give you the necessary tools to create a device using our OpenGate REST.\r\n */\r\nexport default class DeviceBuilder extends ComplexBuilder {\r\n\r\n /**\r\n * @param {!InternalOpenGateAPI} ogapi - this is ogapi instance\r\n * @param {!string} organization - this is the organization name where device will be created\r\n * @param {!array} [allowedDatastreams] - Allowed datastreams to add into the new device\r\n * @param {!array} [definedSchemas] - Jsonschema about all OpenGate specific types\r\n * @param {!Validator} [jsonSchemaValidator] - Json schema validator tool\r\n * @param {number} ms - timeout in milliseconds \r\n */\r\n\r\n constructor(ogapi, organization, allowedDatastreams, definedSchemas, jsonSchemaValidator, timeout) {\r\n super(ogapi, organization + '/devices', allowedDatastreams, definedSchemas, jsonSchemaValidator, timeout);\r\n this._organization = organization;\r\n }\r\n\r\n /**\r\n * This invoke a request to OpenGate North API and the callback is managed by promises\r\n * This function create a entity of provision\r\n * @return {Promise}\r\n * @property {function (result:object, statusCode:number)} then - When request it is OK\r\n * @property {function (error:string)} catch - When request it is NOK\r\n * @example\r\n * ogapi.organizationsBuilder().create()\r\n */\r\n create() {\r\n this._checkRequiredParameters();\r\n return (new BoxBuilder(this._ogapi, this._composeElement(), this._resource, this._getEntityKey(), this._getUrlParameters(), this._timeout)).create();\r\n }\r\n\r\n /**\r\n * This invoke a request to OpenGate North API and the callback is managed by promises\r\n * This function updates a entity of provision and check if any subscriber/subscription exits or no. \r\n * If a subscriber/subscription not exists then this entities will be created and after that will be added to entity box.\r\n * @return {Promise}\r\n * @property {function (result:object, statusCode:number)} then - When request it is OK\r\n * @property {function (error:string)} catch - When request it is NOK\r\n * @example\r\n * ogapi.entityBuilder.devicesBuilder().update()\r\n */\r\n update() {\r\n return (new BoxBuilder(this._ogapi, this._composeElement(), this._buildURL(), this._getEntityKey(), this._getUrlParameters(), this._timeout)).update();\r\n }\r\n\r\n _getEntityKey() {\r\n return this._entity[ID];\r\n }\r\n\r\n}\r\n",
42940
42940
  "static": true,
42941
42941
  "longname": "src/provision/entities/DeviceBuilder.js",
42942
42942
  "access": null,
@@ -714,7 +714,7 @@ class WrapperBuilder {
714
714
  return defer.promise;
715
715
 
716
716
  function create(defered, defer, percentage) {
717
- _this._obj[&apos;provision.administration.serviceGroup&apos;] = &quot;emptyServiceGroup&quot;
717
+ _this._obj[&apos;provision.administration.serviceGroup&apos;] = { &quot;_value&quot; : { &quot;_current&quot; : { &quot;value&quot; : &quot;emptyServiceGroup&quot; }}};
718
718
  _this._ogapi.Napi.post(_this._url, _this._obj, null, null, {
719
719
  flattened: true
720
720
  })
@@ -817,7 +817,8 @@ export default class DeviceBuilder extends ComplexBuilder {
817
817
  return this._entity[ID];
818
818
  }
819
819
 
820
- }</code></pre>
820
+ }
821
+ </code></pre>
821
822
 
822
823
  </div>
823
824
 
package/docs/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opengate-js",
3
- "version": "12.7.0",
3
+ "version": "12.7.1",
4
4
  "description": "",
5
5
  "main": "dist/opengate-api-npm.js",
6
6
  "repository": {