@ndustrial/contxt-sdk 4.3.6 → 5.2.0

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.
Files changed (167) hide show
  1. package/.gitattributes +1 -0
  2. package/CHANGELOG.md +8 -0
  3. package/CODEOWNERS +1 -0
  4. package/docs/Facilities.md +0 -144
  5. package/docs/FacilityGroupings.md +0 -160
  6. package/docs/Nionic.md +63 -0
  7. package/docs/README.md +3 -29
  8. package/docs/Typedefs.md +0 -101
  9. package/esm/config/audiences.js +13 -13
  10. package/esm/config/audiences.js.map +1 -1
  11. package/esm/index.js +4 -6
  12. package/esm/index.js.map +1 -1
  13. package/esm/nionic/index.js +126 -0
  14. package/esm/nionic/index.js.map +1 -0
  15. package/lib/config/audiences.js +13 -13
  16. package/lib/config/audiences.js.map +1 -1
  17. package/lib/index.js +10 -15
  18. package/lib/index.js.map +1 -1
  19. package/lib/nionic/index.js +130 -0
  20. package/lib/nionic/index.js.map +1 -0
  21. package/meta.yaml +11 -0
  22. package/package.json +9 -10
  23. package/src/bus/index.spec.js +3 -2
  24. package/src/bus/webSocketConnection.spec.js +31 -31
  25. package/src/config/audiences.js +13 -13
  26. package/src/config/index.spec.js +6 -6
  27. package/src/coordinator/applications.spec.js +8 -8
  28. package/src/coordinator/consent.spec.js +1 -1
  29. package/src/coordinator/edgeNodes.spec.js +3 -3
  30. package/src/coordinator/organizations.spec.js +4 -4
  31. package/src/coordinator/permissions.spec.js +4 -4
  32. package/src/coordinator/roles.spec.js +3 -3
  33. package/src/coordinator/users.spec.js +14 -15
  34. package/src/events/index.spec.js +16 -16
  35. package/src/files/index.spec.js +7 -7
  36. package/src/index.js +4 -6
  37. package/src/index.spec.js +8 -22
  38. package/src/iot/feeds.spec.js +4 -4
  39. package/src/iot/fieldCategories.spec.js +4 -4
  40. package/src/iot/fieldGroupings.spec.js +5 -5
  41. package/src/iot/outputs.spec.js +6 -6
  42. package/src/nionic/index.js +151 -0
  43. package/src/request.spec.js +3 -3
  44. package/src/sessionTypes/auth0WebAuth.spec.js +2 -2
  45. package/src/sessionTypes/index.spec.js +3 -3
  46. package/src/sessionTypes/machineAuth.spec.js +3 -3
  47. package/src/sessionTypes/passwordGrantAuth.spec.js +1 -1
  48. package/src/utils/iot/formatOutputFieldDataFromServer.spec.js +6 -6
  49. package/src/utils/iot/parseOutputFieldNextPageUrlMetadata.spec.js +2 -2
  50. package/src/utils/pagination/formatPaginatedDataFromServer.spec.js +2 -2
  51. package/src/utils/url/stringifyParams.spec.js +2 -3
  52. package/support/fixtures/factories/IOSDevice.js +3 -3
  53. package/support/fixtures/factories/applicationGrouping.js +4 -4
  54. package/support/fixtures/factories/applicationModule.js +2 -2
  55. package/support/fixtures/factories/asset.js +1 -1
  56. package/support/fixtures/factories/assetAttribute.js +2 -2
  57. package/support/fixtures/factories/assetAttributeValue.js +2 -2
  58. package/support/fixtures/factories/assetMetric.js +1 -1
  59. package/support/fixtures/factories/assetMetricValue.js +3 -3
  60. package/support/fixtures/factories/assetMetricValueCompact.js +2 -2
  61. package/support/fixtures/factories/assetType.js +1 -1
  62. package/support/fixtures/factories/authResults.js +3 -3
  63. package/support/fixtures/factories/channel.js +1 -1
  64. package/support/fixtures/factories/contxtApplication.js +5 -5
  65. package/support/fixtures/factories/contxtOrganization.js +2 -2
  66. package/support/fixtures/factories/contxtOrganizationFeaturedApplication.js +3 -3
  67. package/support/fixtures/factories/contxtProject.js +2 -2
  68. package/support/fixtures/factories/contxtProjectEnvironment.js +2 -2
  69. package/support/fixtures/factories/contxtRole.js +3 -3
  70. package/support/fixtures/factories/contxtRoleApplication.js +1 -1
  71. package/support/fixtures/factories/contxtRoleProjectEnvironment.js +1 -1
  72. package/support/fixtures/factories/contxtUser.js +2 -2
  73. package/support/fixtures/factories/contxtUserApplication.js +1 -1
  74. package/support/fixtures/factories/contxtUserFavoriteApplication.js +2 -2
  75. package/support/fixtures/factories/contxtUserPermissions.js +5 -5
  76. package/support/fixtures/factories/contxtUserProjectEnvironment.js +1 -1
  77. package/support/fixtures/factories/contxtUserRole.js +2 -2
  78. package/support/fixtures/factories/costCenter.js +2 -2
  79. package/support/fixtures/factories/costCenterFacility.js +3 -3
  80. package/support/fixtures/factories/edgeNode.js +1 -1
  81. package/support/fixtures/factories/event.js +5 -5
  82. package/support/fixtures/factories/eventType.js +4 -4
  83. package/support/fixtures/factories/eventUser.js +5 -5
  84. package/support/fixtures/factories/facility.js +4 -4
  85. package/support/fixtures/factories/facilityGrouping.js +3 -3
  86. package/support/fixtures/factories/facilityGroupingFacility.js +3 -3
  87. package/support/fixtures/factories/facilityInfo.js +2 -2
  88. package/support/fixtures/factories/facilityTag.js +1 -1
  89. package/support/fixtures/factories/feedStatus.js +1 -1
  90. package/support/fixtures/factories/feedType.js +1 -1
  91. package/support/fixtures/factories/feeds.js +5 -5
  92. package/support/fixtures/factories/fieldCategory.js +3 -3
  93. package/support/fixtures/factories/fieldGrouping.js +4 -4
  94. package/support/fixtures/factories/fieldGroupingField.js +3 -3
  95. package/support/fixtures/factories/fieldGroupingStatus.js +1 -1
  96. package/support/fixtures/factories/file.js +1 -1
  97. package/support/fixtures/factories/fileUploadInfo.js +1 -1
  98. package/support/fixtures/factories/organization.js +1 -1
  99. package/support/fixtures/factories/outputField.js +10 -10
  100. package/support/fixtures/factories/outputFieldData.js +1 -1
  101. package/support/fixtures/factories/owner.js +2 -2
  102. package/support/fixtures/factories/paginationMetadata.js +2 -2
  103. package/support/fixtures/factories/triggeredEvent.js +4 -4
  104. package/support/fixtures/factories/userEventSubscription.js +1 -1
  105. package/support/fixtures/factories/userMobileNumber.js +1 -1
  106. package/support/fixtures/factories/userPermissionsMap.js +2 -2
  107. package/.huskyrc.js +0 -11
  108. package/.nycrc +0 -12
  109. package/esm/assets/assetAttributes.js +0 -574
  110. package/esm/assets/assetAttributes.js.map +0 -1
  111. package/esm/assets/assetMetrics.js +0 -690
  112. package/esm/assets/assetMetrics.js.map +0 -1
  113. package/esm/assets/assetTypes.js +0 -273
  114. package/esm/assets/assetTypes.js.map +0 -1
  115. package/esm/assets/index.js +0 -283
  116. package/esm/assets/index.js.map +0 -1
  117. package/esm/facilities/costCenters.js +0 -306
  118. package/esm/facilities/costCenters.js.map +0 -1
  119. package/esm/facilities/groupings.js +0 -323
  120. package/esm/facilities/groupings.js.map +0 -1
  121. package/esm/facilities/index.js +0 -343
  122. package/esm/facilities/index.js.map +0 -1
  123. package/esm/health/index.js +0 -229
  124. package/esm/health/index.js.map +0 -1
  125. package/esm/utils/facilities/formatFacilityWithInfoFromServer.js +0 -65
  126. package/esm/utils/facilities/formatFacilityWithInfoFromServer.js.map +0 -1
  127. package/esm/utils/facilities/index.js +0 -4
  128. package/esm/utils/facilities/index.js.map +0 -1
  129. package/lib/assets/assetAttributes.js +0 -588
  130. package/lib/assets/assetAttributes.js.map +0 -1
  131. package/lib/assets/assetMetrics.js +0 -705
  132. package/lib/assets/assetMetrics.js.map +0 -1
  133. package/lib/assets/assetTypes.js +0 -287
  134. package/lib/assets/assetTypes.js.map +0 -1
  135. package/lib/assets/index.js +0 -302
  136. package/lib/assets/index.js.map +0 -1
  137. package/lib/facilities/costCenters.js +0 -316
  138. package/lib/facilities/costCenters.js.map +0 -1
  139. package/lib/facilities/groupings.js +0 -333
  140. package/lib/facilities/groupings.js.map +0 -1
  141. package/lib/facilities/index.js +0 -360
  142. package/lib/facilities/index.js.map +0 -1
  143. package/lib/health/index.js +0 -232
  144. package/lib/health/index.js.map +0 -1
  145. package/lib/utils/facilities/formatFacilityWithInfoFromServer.js +0 -71
  146. package/lib/utils/facilities/formatFacilityWithInfoFromServer.js.map +0 -1
  147. package/lib/utils/facilities/index.js +0 -15
  148. package/lib/utils/facilities/index.js.map +0 -1
  149. package/src/assets/assetAttributes.js +0 -619
  150. package/src/assets/assetAttributes.spec.js +0 -1143
  151. package/src/assets/assetMetrics.js +0 -736
  152. package/src/assets/assetMetrics.spec.js +0 -1180
  153. package/src/assets/assetTypes.js +0 -260
  154. package/src/assets/assetTypes.spec.js +0 -556
  155. package/src/assets/index.js +0 -271
  156. package/src/assets/index.spec.js +0 -515
  157. package/src/facilities/costCenters.js +0 -295
  158. package/src/facilities/costCenters.spec.js +0 -529
  159. package/src/facilities/groupings.js +0 -320
  160. package/src/facilities/groupings.spec.js +0 -567
  161. package/src/facilities/index.js +0 -343
  162. package/src/facilities/index.spec.js +0 -551
  163. package/src/health/index.js +0 -200
  164. package/src/health/index.spec.js +0 -501
  165. package/src/utils/facilities/formatFacilityWithInfoFromServer.js +0 -62
  166. package/src/utils/facilities/formatFacilityWithInfoFromServer.spec.js +0 -73
  167. package/src/utils/facilities/index.js +0 -3
@@ -1,736 +0,0 @@
1
- import has from 'lodash.has';
2
- import isPlainObject from 'lodash.isplainobject';
3
- import { toCamelCase, toSnakeCase } from '../utils/objects';
4
- import { stringifyParamsWithCommaSeparatedArrays } from '../utils/url';
5
- import { formatPaginatedDataFromServer } from '../utils/pagination';
6
-
7
- /**
8
- * @typedef {Object} AssetMetric
9
- * @property {string} assetTypeId UUID corresponding with the asset type
10
- * @property {string} createdAt ISO 8601 Extended Format date/time string
11
- * @property {string} description
12
- * @property {string} id UUID
13
- * @property {string} label
14
- * @property {string} organizationId UUID corresponding with the organization
15
- * @property {string} timeInterval Options are "hourly", "daily", "weekly", "monthly", "yearly"
16
- * @property {string} [units]
17
- * @property {string} updatedAt ISO 8601 Extended Format date/time string
18
- */
19
-
20
- /**
21
- * @typedef {Object} AssetMetricsFromServer
22
- * @property {Object} _metadata Metadata about the pagination settings
23
- * @property {number} _metadata.offset Offset of records in subsequent queries
24
- * @property {number} _metadata.totalRecords Total number of asset types found
25
- * @property {AssetMetric[]} records
26
- */
27
-
28
- /**
29
- * @typedef {Object} AssetMetricValue
30
- * @property {string} assetId UUID corresponding to the asset
31
- * @property {string} assetMetricId UUID corresponding to the asset metric
32
- * @property {string} createdAt ISO 8601 Extended Format date/time string
33
- * @property {string} effectiveEndDate ISO 8601 Extended Format date/time string
34
- * @property {string} effectiveStartDate ISO 8601 Extended Format date/time string
35
- * @property {string} id UUID
36
- * @property {string} notes
37
- * @property {string} updatedAt ISO 8601 Extended Format date/time string
38
- * @property {string} value
39
- * @property {boolean} isEstimated Whether the value is an estimation or a true reading
40
- */
41
-
42
- /**
43
- * @typedef {Object} AssetMetricValuesFromServer
44
- * @property {Object} _metadata Metadata about the pagination settings
45
- * @property {number} _metadata.offset Offset of records in subsequent queries
46
- * @property {number} _metadata.totalRecords Total number of asset types found
47
- * @property {AssetMetricValue[]} records
48
- */
49
-
50
- /**
51
- * @typedef {Object} AssetMetricValueCompact
52
- * @property {string} id the UUID corresponding to the asset metric value id
53
- * @property {string} value
54
- * @property {boolean} is_estimated
55
- * @property {string} effectiveEndDate ISO 8601 Extended Format date/time string
56
- * @property {string} effectiveStartDate ISO 8601 Extended Format date/time string
57
- */
58
-
59
- /**
60
- * @typedef {Object.<AssetMetric.label, AssetMetricValueCompact[]>} AssetMetricsKeyedByMetricId
61
- */
62
-
63
- /**
64
- * An object with asset ids as keys to another object with asset metric labels as keys.
65
- * At this final level the object contains arrays of {@link AssetMetricValueCompact}
66
- * @typedef {Object.<Asset.id, AssetMetricsKeyedByMetricId>} AssetMetricValuesByAssetIdMetricId
67
- * @example
68
- * {
69
- * "b47e45af-3e18-408a-8070-008f9e6d7b42" : {
70
- * "facility_daily_cuft": [
71
- * {
72
- * "id": "cf7e45af-3e18-408a-8070-008f9e6d7b3j",
73
- * "value": 10,
74
- * "isEstimated": false,
75
- * "effectiveEndDate": "2018-07-10T11:04:24.631Z",
76
- * "effectiveStartDate" "2018-07-10T11:04:24.631Z"
77
- * }
78
- * ]
79
- * }
80
- * }
81
- */
82
-
83
- /**
84
- * Module that provides access to, and the manipulation of, information about different asset metrics
85
- *
86
- * @typicalname contxtSdk.assets.metrics
87
- */
88
- class AssetMetrics {
89
- /**
90
- * @param {Object} sdk An instance of the SDK so the module can communicate with other modules.
91
- * @param {Object} request An instance of the request module tied to this module's audience.
92
- * @param {string} baseUrl The base URL provided by the parent module
93
- */
94
- constructor(sdk, request, baseUrl) {
95
- this._baseUrl = baseUrl;
96
- this._request = request;
97
- this._sdk = sdk;
98
- }
99
-
100
- /**
101
- * Creates a new asset metric
102
- *
103
- * API Endpoint: '/assets/types/:assetTypeId/metrics'
104
- * Method: POST
105
- *
106
- * @param {string} assetTypeId The UUID formatted ID of the asset type
107
- * @param {Object} assetMetric
108
- * @param {string} assetMetric.description
109
- * @param {string} assetMetric.label
110
- * @param {string} assetMetric.organizationId Organization ID (UUID) to which the metric belongs
111
- * @param {string} assetMetric.timeInterval Options are "hourly", "daily", "weekly", "monthly", "yearly"
112
- * @param {string} [assetMetric.units] Units of the metric
113
- *
114
- * @returns {Promise}
115
- * @fulfill {AssetMetric} Information about the new asset metric
116
- * @reject {Error}
117
- *
118
- * @example
119
- * contxtSdk.assets.metrics
120
- * .create('4f0e51c6-728b-4892-9863-6d002e61204d', {
121
- * description: 'Number of injuries which occur in the facility each month',
122
- * label: 'Facility Injuries',
123
- * organizationId: 'b47e45af-3e18-408a-8070-008f9e6d7b42',
124
- * timeInterval: 'monthly',
125
- * units: 'injuries'
126
- * })
127
- * .then((assetMetric) => console.log(assetMetric))
128
- * .catch((err) => console.log(err));
129
- */
130
- create(assetTypeId, assetMetric = {}) {
131
- if (!assetTypeId) {
132
- return Promise.reject(
133
- new Error('An asset type ID is required to create an asset metric.')
134
- );
135
- }
136
-
137
- const hasFieldFns = {
138
- default: (object, key) => !!object[key],
139
- organizationId: (object, key) => has(object, key)
140
- };
141
- const requiredFields = [
142
- 'description',
143
- 'label',
144
- 'organizationId',
145
- 'timeInterval'
146
- ];
147
-
148
- for (let i = 0; i < requiredFields.length; i++) {
149
- const field = requiredFields[i];
150
- const hasField = hasFieldFns[field] || hasFieldFns.default;
151
-
152
- if (!hasField(assetMetric, field)) {
153
- return Promise.reject(
154
- new Error(`A ${field} is required to create a new asset metric.`)
155
- );
156
- }
157
- }
158
-
159
- return this._request
160
- .post(
161
- `${this._baseUrl}/assets/types/${assetTypeId}/metrics`,
162
- toSnakeCase(assetMetric)
163
- )
164
- .then((assetMetric) => toCamelCase(assetMetric));
165
- }
166
-
167
- /**
168
- * Deletes an asset metric
169
- *
170
- * API Endpoint: '/assets/metrics/:assetMetricId'
171
- * Method: DELETE
172
- *
173
- * @param {string} assetMetricId The UUID formatted ID of the asset metric
174
- *
175
- * @returns {Promise}
176
- * @fulfill {undefined}
177
- * @reject {Error}
178
- *
179
- * @example
180
- * contxtSdk.assets.metrics.delete('4f0e51c6-728b-4892-9863-6d002e61204d');
181
- */
182
- delete(assetMetricId) {
183
- if (!assetMetricId) {
184
- return Promise.reject(
185
- new Error(
186
- 'An asset metric ID is required for deleting an asset metric.'
187
- )
188
- );
189
- }
190
-
191
- return this._request.delete(
192
- `${this._baseUrl}/assets/metrics/${assetMetricId}`
193
- );
194
- }
195
-
196
- /**
197
- * Gets information about an asset metric
198
- *
199
- * API Endpoint: '/assets/metrics/:assetMetricId'
200
- * Method: GET
201
- *
202
- * @param {string} assetMetricId The UUID formatted ID of the asset metric
203
- *
204
- * @returns {Promise}
205
- * @fulfill {AssetMetric} Information about the asset metric
206
- * @reject {Error}
207
- *
208
- * @example
209
- * contxtSdk.assets.metrics
210
- * .get('4f0e51c6-728b-4892-9863-6d002e61204d')
211
- * .then((assetMetric) => console.log(assetMetric))
212
- * .catch((err) => console.log(err));
213
- */
214
- get(assetMetricId) {
215
- if (!assetMetricId) {
216
- return Promise.reject(
217
- new Error(
218
- 'An asset metric ID is required for getting information about an asset metric.'
219
- )
220
- );
221
- }
222
-
223
- return this._request
224
- .get(`${this._baseUrl}/assets/metrics/${assetMetricId}`)
225
- .then((assetMetric) => toCamelCase(assetMetric));
226
- }
227
-
228
- /**
229
- * Gets a list of all asset metrics that belong to a given asset
230
- *
231
- * API Endpoint: '/assets/:assetId/metrics
232
- * Method: GET
233
- *
234
- * @param {string} assetId The UUID formatted ID of the asset type
235
- * @param {Object} [assetMetricsFilters] Specific information that is used to
236
- * filter the list of asset metrics
237
- * @param {String} [assetMetricsFilters.assetMetricLabel] The label of the
238
- * associated asset metrics
239
- * @param {Number} [assetMetricsFilters.limit] Maximum number of records to
240
- * return per query
241
- * @param {Number} [assetMetricsFilters.offset] How many records from the first
242
- * record to start the query
243
- *
244
- * @returns {Promise}
245
- * @fulfill {AssetMetricsFromServer}
246
- * @reject {Error}
247
- *
248
- * @example
249
- * contxtSdk.assets.metrics
250
- * .getByAssetId(
251
- * 'f3be81fd-4494-443b-87a3-320b1c9aa495',
252
- * {
253
- * assetMetricLabel: 'Square Footage',
254
- * limit: 50,
255
- * offset: 150
256
- * }
257
- * )
258
- * .then((assetMetricData) => console.log(assetMetricData))
259
- * .catch((err) => console.log(err));
260
- */
261
- getByAssetId(assetId, assetMetricsFilters) {
262
- if (!assetId) {
263
- return Promise.reject(
264
- new Error('An asset ID is required to get a list of all asset metrics.')
265
- );
266
- }
267
-
268
- return this._request
269
- .get(`${this._baseUrl}/assets/${assetId}/metrics`, {
270
- params: toSnakeCase(assetMetricsFilters)
271
- })
272
- .then((assetMetricsData) =>
273
- formatPaginatedDataFromServer(assetMetricsData)
274
- );
275
- }
276
-
277
- /**
278
- * Gets a list of all asset metrics that belong to a given type
279
- *
280
- * API Endpoint: '/assets/types/:assetTypeId/metrics
281
- * Method: GET
282
- *
283
- * @param {string} assetTypeId The UUID formatted ID of the asset type
284
- * @param {Object} [assetMetricsFilters] Specific information that is used to
285
- * filter the list of asset metrics
286
- * @param {Number} [assetMetricsFilters.limit] Maximum number of records to
287
- * return per query
288
- * @param {Number} [assetMetricsFilters.offset] How many records from the first
289
- * record to start the query
290
- * @param {String} [assetMetricsFilters.organizationId] The UUID formatted ID
291
- * of the organization to filter asset metrics by
292
- *
293
- * @returns {Promise}
294
- * @fulfill {AssetMetricsFromServer}
295
- * @reject {Error}
296
- *
297
- * @example
298
- * contxtSdk.assets.metrics
299
- * .getByAssetTypeId(
300
- * '4f0e51c6-728b-4892-9863-6d002e61204d'
301
- * {
302
- * limit: 50,
303
- * offset: 150
304
- * }
305
- * )
306
- * .then((assetMetrics) => console.log(assetMetrics))
307
- * .catch((err) => console.log(err));
308
- */
309
- getByAssetTypeId(assetTypeId, assetMetricsFilters) {
310
- if (!assetTypeId) {
311
- return Promise.reject(
312
- new Error(
313
- 'An asset type ID is required to get a list of all asset metrics.'
314
- )
315
- );
316
- }
317
-
318
- return this._request
319
- .get(`${this._baseUrl}/assets/types/${assetTypeId}/metrics`, {
320
- params: toSnakeCase(assetMetricsFilters)
321
- })
322
- .then((assetMetricsData) =>
323
- formatPaginatedDataFromServer(assetMetricsData)
324
- );
325
- }
326
-
327
- /**
328
- * Updates an asset metric's data
329
- *
330
- * API Endpoint: '/assets/metrics/:assetMetricId'
331
- * Method: PUT
332
- *
333
- * @param {string} assetMetricId The ID of the asset metric to update (formatted as a UUID)
334
- * @param {Object} update An object containing the updated data for the asset metric
335
- * @param {string} [update.description]
336
- * @param {string} [update.label]
337
- * @param {string} [update.timeInterval]
338
- * @param {string} [update.units]
339
- *
340
- * @returns {Promise}
341
- * @fulfill {undefined}
342
- * @reject {Error}
343
- *
344
- * @example
345
- * contxtSdk.assets.metrics
346
- * .update('5f310899-d8f9-4dac-ae82-cedb2048a8ef', {
347
- * description: 'An updated description of this metric'
348
- * });
349
- */
350
- update(assetMetricId, update) {
351
- if (!assetMetricId) {
352
- return Promise.reject(
353
- new Error('An asset metric ID is required to update an asset metric.')
354
- );
355
- }
356
-
357
- if (!update) {
358
- return Promise.reject(
359
- new Error('An update is required to update an asset metric.')
360
- );
361
- }
362
-
363
- if (!isPlainObject(update)) {
364
- return Promise.reject(
365
- new Error(
366
- 'The asset metric update must be a well-formed object with the data you wish to update.'
367
- )
368
- );
369
- }
370
-
371
- const formattedUpdate = toSnakeCase(update, {
372
- excludeKeys: ['createdAt', 'id', 'label', 'organizationId', 'updatedAt']
373
- });
374
-
375
- return this._request.put(
376
- `${this._baseUrl}/assets/metrics/${assetMetricId}`,
377
- formattedUpdate
378
- );
379
- }
380
-
381
- /**
382
- * Creates a new asset metric value
383
- *
384
- * API Endpoint: '/assets/:assetId/metrics/:assetMetricId/values'
385
- * Method: POST
386
- *
387
- * @param {string} assetId The ID of the asset (formatted as a UUID)
388
- * @param {Object} assetMetricValue
389
- * @param {string} assetMetricValue.assetMetricId UUID corresponding to the asset metric
390
- * @param {string} assetMetricValue.effectiveEndDate ISO 8601 Extended Format date/time string
391
- * @param {string} assetMetricValue.effectiveStartDate ISO 8601 Extended Format date/time string
392
- * @param {string} [assetMetricValue.notes]
393
- * @param {string} assetMetricValue.value
394
- * @param {boolean} [assetMetricValue.isEstimated] Whether the value is an estimation or a true reading
395
- *
396
- * @returns {Promise}
397
- * @fulfill {AssetMetricValue}
398
- * @reject {Error}
399
- *
400
- * @example
401
- * contxtSdk.assets.metrics
402
- * .createValue('1140cc2e-6d13-42ee-9941-487fe98f8e2d', {
403
- * assetMetricId: 'cca11baa-cf7d-44c0-9d0a-6ad73d5f30cb',
404
- * effectiveEndDate: '2018-08-28T18:18:18.264Z',
405
- * effectiveStartDate: '2018-08-27T18:18:03.175Z',
406
- * notes: 'Iure delectus non sunt a voluptates pariatur fuga.',
407
- * value: '2000',
408
- * isEstimated: true
409
- * })
410
- * .then((newAssetMetricValue) => {
411
- * console.log(newAssetMetricValue);
412
- * })
413
- * .catch((error) => {
414
- * console.error(error);
415
- * });
416
- */
417
- createValue(assetId, assetMetricValue = {}) {
418
- const requiredFields = [
419
- 'assetMetricId',
420
- 'effectiveEndDate',
421
- 'effectiveStartDate',
422
- 'value'
423
- ];
424
-
425
- if (!assetId) {
426
- return Promise.reject(
427
- new Error('An asset ID is required to create a new asset metric value.')
428
- );
429
- }
430
-
431
- for (let i = 0; i < requiredFields.length; i++) {
432
- const field = requiredFields[i];
433
-
434
- if (!assetMetricValue[field]) {
435
- return Promise.reject(
436
- new Error(
437
- `A ${field} is required to create a new asset metric value.`
438
- )
439
- );
440
- }
441
- }
442
-
443
- return this._request
444
- .post(
445
- `${this._baseUrl}/assets/${assetId}/metrics/${
446
- assetMetricValue.assetMetricId
447
- }/values`,
448
- toSnakeCase(assetMetricValue)
449
- )
450
- .then((assetMetricValue) => toCamelCase(assetMetricValue));
451
- }
452
-
453
- /**
454
- * Deletes an asset metric value
455
- *
456
- * API Endpoint: '/assets/metrics/values/:assetMetricValueId'
457
- * Method: DELETE
458
- *
459
- * @param {string} assetMetricValueId The ID of the asset metric value (formatted as a UUID)
460
- *
461
- * @returns {Promise}
462
- * @fulfill {undefined}
463
- * @reject {Error}
464
- *
465
- * @example
466
- * contxtSdk.assets.metrics.deleteValue(
467
- * 'f4cd0d84-6c61-4d19-9322-7c1ab226dc83'
468
- * );
469
- */
470
- deleteValue(assetMetricValueId) {
471
- if (!assetMetricValueId) {
472
- return Promise.reject(
473
- new Error(
474
- 'An asset metric value ID is required for deleting an asset metric value.'
475
- )
476
- );
477
- }
478
-
479
- return this._request.delete(
480
- `${this._baseUrl}/assets/metrics/values/${assetMetricValueId}`
481
- );
482
- }
483
-
484
- /**
485
- * Gets asset metric values for a particular asset
486
- *
487
- * API Endpoint: '/assets/:assetId/metrics/values'
488
- * Method: GET
489
- *
490
- * @param {String} assetId The ID of the asset (formatted as a UUID)
491
- * @param {Object} [assetMetricValuesFilters] Specific information that is
492
- * used to filter the list of asset metric values
493
- * @param {String} [assetMetricValuesFilters.assetMetricLabel] The label of
494
- * the associated asset metrics
495
- * @param {String} [assetMetricValuesFilters.effectiveEndDate] Effective end
496
- * date (ISO 8601 Extended formatted) of the asset metric values
497
- * @param {String} [assetMetricValuesFilters.effectiveStartDate] Effective
498
- * start date (ISO 8601 Extended formatted) of the asset metric values
499
- * @param {Number} [assetMetricValuesFilters.limit] Maximum number of records
500
- * to return per query
501
- * @param {Number} [assetMetricValuesFilters.offset] How many records from the
502
- * first record to start the query
503
- *
504
- * @returns {Promise}
505
- * @fulfill {AssetMetricValuesFromServer}
506
- * @rejects {Error}
507
- *
508
- * @example
509
- * contxtSdk.assets.metrics
510
- * .getValuesByAssetId(
511
- * 'f9c606f3-d270-4623-bf3b-b085424d9a8b',
512
- * {
513
- * assetMetricLabel: 'Square Footage',
514
- * effectiveEndDate: '2018-04-13T15:44:51.943Z'
515
- * effectiveStartDate: '2017-12-13T15:42:01.376Z'
516
- * limit: 10,
517
- * offset: 200
518
- * }
519
- * )
520
- * .then((assetMetricValuesData) => console.log(assetMetricValuesData))
521
- * .catch((err) => console.log(err));
522
- */
523
- getValuesByAssetId(assetId, assetMetricValuesFilters) {
524
- if (!assetId) {
525
- return Promise.reject(
526
- new Error(
527
- 'An asset ID is required to get a list of asset metric values.'
528
- )
529
- );
530
- }
531
-
532
- return this._request
533
- .get(`${this._baseUrl}/assets/${assetId}/metrics/values`, {
534
- params: toSnakeCase(assetMetricValuesFilters)
535
- })
536
- .then((assetMetricValueData) =>
537
- formatPaginatedDataFromServer(assetMetricValueData)
538
- );
539
- }
540
-
541
- /**
542
- * Gets asset metric values for a particular asset and metric
543
- *
544
- * API Endpoint: '/assets/:assetId/metrics/:assetMetricId/values'
545
- * Method: GET
546
- *
547
- * @param {String} assetId The ID of the asset (formatted as a UUID)
548
- * @param {String} assetMetricId The ID of the asset metric (formatted as a
549
- * UUID)
550
- * @param {Object} [assetMetricValuesFilters] Specific information that is
551
- * used to filter the list of asset metric values
552
- * @param {String} [assetMetricValuesFilters.effectiveEndDate] Effective end
553
- * date (ISO 8601 Extended formatted) of the asset metric values
554
- * @param {String} [assetMetricValuesFilters.effectiveStartDate] Effective
555
- * start date (ISO 8601 Extended formatted) of the asset metric values
556
- * @param {Number} [assetMetricValuesFilters.limit] Maximum number of records
557
- * to return per query
558
- * @param {Number} [assetMetricValuesFilters.offset] How many records from the
559
- * first record to start the query
560
- *
561
- * @returns {Promise}
562
- * @fulfill {AssetMetricValuesFromServer}
563
- * @rejects {Error}
564
- *
565
- * @example
566
- * contxtSdk.assets.metrics
567
- * .getValuesByMetricId(
568
- * 'd7329ef3-ca63-4ad5-bb3e-632b702584f8',
569
- * 'a1329ef3-ca63-4ad5-bb3e-632b702584f8',
570
- * {
571
- * limit: 10,
572
- * effectiveStartDate: '2018-07-11T19:14:49.715Z'
573
- * }
574
- * )
575
- * .then((assetMetricValuesData) => {
576
- * console.log(assetMetricValuesData);
577
- * })
578
- * .catch((err) => console.log(err));
579
- */
580
- getValuesByMetricId(assetId, assetMetricId, assetMetricValuesFilters) {
581
- if (!assetId) {
582
- return Promise.reject(
583
- new Error(
584
- 'An asset ID is required to get a list of asset metric values.'
585
- )
586
- );
587
- }
588
-
589
- if (!assetMetricId) {
590
- return Promise.reject(
591
- new Error(
592
- 'An asset metric ID is required to get a list of asset metric values.'
593
- )
594
- );
595
- }
596
-
597
- return this._request
598
- .get(
599
- `${this._baseUrl}/assets/${assetId}/metrics/${assetMetricId}/values`,
600
- {
601
- params: toSnakeCase(assetMetricValuesFilters)
602
- }
603
- )
604
- .then((assetMetricValueData) =>
605
- formatPaginatedDataFromServer(assetMetricValueData)
606
- );
607
- }
608
-
609
- /**
610
- * Gets asset metric values for a particular asset and metric
611
- *
612
- * API Endpoint: '/assets/metrics/values'
613
- * Method: GET
614
- *
615
- * @param {Object} [assetMetricFilters] Specific information that is
616
- * used to filter the list of asset metric values
617
- * @param {String[]} assetMetricFilters.assetIds an array of asset ids
618
- * to request the metrics for
619
- * @param {String[]} assetMetricFilters.labels an array of metric labels
620
- * to request the metrics for
621
- * @param {String} [assetMetricFilters.effectiveEndDate] Effective end
622
- * date (ISO 8601 Extended formatted) of the asset metric values
623
- * @param {String} [assetMetricFilters.effectiveStartDate] Effective
624
- * start date (ISO 8601 Extended formatted) of the asset metric values
625
- * @returns {Promise}
626
- * @fulfill {AssetMetricValuesByAssetIdMetricId}
627
- * @rejects {Error}
628
- *
629
- * @example
630
- * contxtSdk.assets.metrics
631
- * .getValuesByMetricIdsAssetIds(
632
- * {
633
- * effectiveStartDate: '2018-07-11T19:14:49.715Z',
634
- * effectiveEndDate: '2018-07-11T19:14:49.715Z',
635
- * assetIds: ['2140cc2e-6d13-42ee-9941-487fe98f8e2d', '5540cc2e-6d13-42ee-9941-487fe98f8efc'],
636
- * labels: ['facility_kwh', 'facility_cuft'],
637
- * }
638
- * )
639
- * .then((assetMetricValuesByAssetIdMetricId) => {
640
- * console.log(assetMetricValuesByAssetIdMetricId);
641
- * })
642
- * .catch((err) => console.log(err));
643
- */
644
- getValuesByMetricIdsAssetIds(assetMetricFilters) {
645
- const requiredFields = ['assetIds', 'labels'];
646
-
647
- for (let i = 0; i < requiredFields.length; i++) {
648
- const field = requiredFields[i];
649
-
650
- if (!assetMetricFilters[field]) {
651
- return Promise.reject(
652
- new Error(`The ${field} param is required to fetch data.`)
653
- );
654
- }
655
- }
656
-
657
- return this._request
658
- .get(`${this._baseUrl}/assets/metrics/values`, {
659
- params: toSnakeCase(assetMetricFilters),
660
- paramsSerializer: stringifyParamsWithCommaSeparatedArrays
661
- })
662
- .then((assetMetricValues) => {
663
- const result = toCamelCase(assetMetricValues, {
664
- excludeTransform: [
665
- ...assetMetricFilters.labels,
666
- ...assetMetricFilters.assetIds
667
- ]
668
- });
669
-
670
- return result;
671
- });
672
- }
673
-
674
- /**
675
- * Updates an asset metric value
676
- *
677
- * API Endpoint: '/assets/metrics/values/:assetMetricValueId'
678
- * Method: PUT
679
- *
680
- * @param {string} assetMetricValueId The ID of the asset metric value to update (formatted as a UUID)
681
- * @param {Object} update An object containing the updated data for the asset metric value
682
- * @param {string} [update.effectiveEndDate] ISO 8601 Extended Format date/time string
683
- * @param {string} [update.effectiveStartDate] ISO 8601 Extended Format date/time string
684
- * @param {string} [update.notes]
685
- * @param {string} [update.value]
686
- * @param {boolean} [update.isEstimated] Whether the value is an estimation or a true reading
687
- *
688
- * @returns {Promise}
689
- * @fulfill {undefined}
690
- * @reject {Error}
691
- *
692
- * @example
693
- * contxtSdk.assets.metrics
694
- * .updateValue('2140cc2e-6d13-42ee-9941-487fe98f8e2d', {
695
- * effectiveEndDate: '2018-07-10T11:04:24.631Z',
696
- * notes: 'Dolores et sapiente sunt doloribus aut in.',
697
- * value: '61456',
698
- * isEstimated: true
699
- * })
700
- * .catch((err) => console.log(err));
701
- */
702
- updateValue(assetMetricValueId, update) {
703
- if (!assetMetricValueId) {
704
- return Promise.reject(
705
- new Error(
706
- 'An asset metric value ID is required to update an asset metric value.'
707
- )
708
- );
709
- }
710
-
711
- if (!update) {
712
- return Promise.reject(
713
- new Error('An update is required to update an asset metric value.')
714
- );
715
- }
716
-
717
- if (!isPlainObject(update)) {
718
- return Promise.reject(
719
- new Error(
720
- 'The asset metric value update must be a well-formed object with the data you wish to update.'
721
- )
722
- );
723
- }
724
-
725
- const formattedUpdate = toSnakeCase(update, {
726
- excludeKeys: ['assetId', 'assetMetricId', 'id']
727
- });
728
-
729
- return this._request.put(
730
- `${this._baseUrl}/assets/metrics/values/${assetMetricValueId}`,
731
- formattedUpdate
732
- );
733
- }
734
- }
735
-
736
- export default AssetMetrics;