@c8y/ngx-components 1018.0.191 → 1018.0.194

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.
@@ -117,7 +117,7 @@ const PRODUCT_EXPERIENCE = {
117
117
  };
118
118
 
119
119
  class RepositoryService {
120
- constructor(inventory, inventoryBinary, operation, alert, event, operationRealtime, eventBinary, advancedSoftwareService) {
120
+ constructor(inventory, inventoryBinary, operation, alert, event, operationRealtime, eventBinary, advancedSoftwareService, globalConfigService) {
121
121
  this.inventory = inventory;
122
122
  this.inventoryBinary = inventoryBinary;
123
123
  this.operation = operation;
@@ -126,6 +126,7 @@ class RepositoryService {
126
126
  this.operationRealtime = operationRealtime;
127
127
  this.eventBinary = eventBinary;
128
128
  this.advancedSoftwareService = advancedSoftwareService;
129
+ this.globalConfigService = globalConfigService;
129
130
  this.dateFrom = new Date(0);
130
131
  this.dateTo = new Date(Date.now() + 86400000); // 1 day in the future
131
132
  this.queriesUtil = new QueriesUtil();
@@ -165,50 +166,35 @@ class RepositoryService {
165
166
  };
166
167
  return this.inventory.list(filters);
167
168
  }
168
- // TODO: merge with create()
169
- async save(data, type, mo = {}) {
169
+ async create(modal, type, mo = {}) {
170
170
  switch (type) {
171
- case RepositoryType.CONFIGURATION: {
172
- Object.assign(mo, {
173
- type: RepositoryType.CONFIGURATION,
174
- configurationType: data.selected ? data.selected.configurationType : undefined,
175
- name: data.version,
176
- description: data.description,
177
- deviceType: data.deviceType,
178
- c8y_Global: {}
171
+ case RepositoryType.FIRMWARE:
172
+ case RepositoryType.SOFTWARE:
173
+ return this.createRepositoryObject(modal, type);
174
+ case RepositoryType.CONFIGURATION:
175
+ Object.assign(modal, {
176
+ selected: {
177
+ id: modal.selected?.id || mo.id,
178
+ name: modal.version || modal.selected?.name
179
+ },
180
+ configurationType: modal.selected ? modal.selected.configurationType : undefined,
181
+ name: modal.version
179
182
  });
180
- if (!data.deviceType && mo.id) {
181
- mo.deviceType = null;
183
+ if (!modal.deviceType && mo.id) {
184
+ modal.deviceType = null;
182
185
  }
183
- if (!data.selected && mo.id) {
184
- mo.configurationType = null;
186
+ if (!modal.selected && mo.id) {
187
+ modal.configurationType = null;
185
188
  }
186
- break;
187
- }
188
- }
189
- const existingUrl = mo.url;
190
- if (data.binary.url) {
191
- mo.url = data.binary.url;
192
- }
193
- else if (data.binary.file) {
194
- const response = await this.inventoryBinary.create(data.binary.file, {
195
- c8y_Global: {}
196
- });
197
- mo.url = response.data.self;
198
- }
199
- if (mo.id) {
200
- return this.updateEntry(mo, existingUrl);
201
- }
202
- return this.createEntry(mo);
203
- }
204
- async create(modal, type) {
205
- switch (type) {
206
- case RepositoryType.FIRMWARE:
207
- case RepositoryType.SOFTWARE:
208
- return this.createFirmwareOrSoftware(modal, type);
189
+ const repositoryObject = this.createRepositoryObject(modal, type);
190
+ if (mo.url) {
191
+ const newBinaryUrl = (await repositoryObject).url;
192
+ this.removeOutdatedBinary(newBinaryUrl, mo.url);
193
+ }
194
+ return repositoryObject;
209
195
  }
210
196
  }
211
- async createFirmwareOrSoftware(modal, type) {
197
+ async createRepositoryObject(modal, type) {
212
198
  let binary;
213
199
  let binaryURL;
214
200
  let repositoryEntry;
@@ -216,22 +202,28 @@ class RepositoryService {
216
202
  const mos = [];
217
203
  const { selected: { id: selectedId }, binary: { file, url } } = modal;
218
204
  try {
205
+ const globalParam = await this.getGlobalFragment(type);
219
206
  if (file) {
220
- ({ data: binary } = await this.saveBinary(file));
207
+ ({ data: binary } = await this.saveBinary(file, globalParam));
221
208
  ({ self: binaryURL } = binary);
209
+ if (type === RepositoryType.CONFIGURATION) {
210
+ modal.binary.url = binaryURL;
211
+ }
222
212
  mos.push(binary);
223
213
  }
224
214
  else {
225
215
  binaryURL = url;
226
216
  }
227
- ({ data: repositoryEntry } = await this.createOrUpdateRepositoryEntry(modal, type));
217
+ ({ data: repositoryEntry } = await this.createOrUpdateRepositoryEntry({ ...modal, ...globalParam }, type));
228
218
  if (isNil(selectedId)) {
229
219
  mos.push(repositoryEntry);
230
220
  }
231
- ({ data: repositoryBinary } = await this.createRepositoryBinary(modal, binaryURL, type, repositoryEntry));
232
- mos.push(repositoryBinary);
221
+ if (type !== RepositoryType.CONFIGURATION) {
222
+ ({ data: repositoryBinary } = await this.createRepositoryBinary({ ...modal, ...globalParam }, binaryURL, type, repositoryEntry));
223
+ mos.push(repositoryBinary);
224
+ }
233
225
  if (file) {
234
- await this.linkBinary(repositoryBinary, binary);
226
+ await this.linkBinary(repositoryBinary, binary, repositoryEntry);
235
227
  }
236
228
  return repositoryEntry;
237
229
  }
@@ -242,24 +234,33 @@ class RepositoryService {
242
234
  throw error;
243
235
  }
244
236
  }
245
- saveBinary(file) {
246
- return this.inventoryBinary.create(file, { c8y_Global: {} });
237
+ saveBinary(file, global) {
238
+ return this.inventoryBinary.create(file, global);
247
239
  }
248
- createOrUpdateRepositoryEntry(modal, type) {
249
- const { selected: { id, name }, description, deviceType } = modal;
240
+ async createOrUpdateRepositoryEntry(modal, type) {
241
+ const { selected: { id, name }, description, deviceType, c8y_Global, binary } = modal;
250
242
  const mo = {
251
243
  id,
252
- name: id ? undefined : name,
244
+ name,
253
245
  description,
254
- type: id ? undefined : type,
255
- c8y_Global: {}
246
+ type,
247
+ c8y_Global
256
248
  };
257
- if (deviceType) {
249
+ if (deviceType && type !== RepositoryType.CONFIGURATION) {
258
250
  set(mo, 'c8y_Filter.type', deviceType);
259
251
  }
252
+ if (deviceType && type === RepositoryType.CONFIGURATION) {
253
+ set(mo, 'deviceType', deviceType);
254
+ }
260
255
  if (modal.softwareType) {
261
256
  set(mo, 'softwareType', modal.softwareType.softwareType);
262
257
  }
258
+ if (modal.configurationType) {
259
+ set(mo, 'configurationType', modal.configurationType);
260
+ }
261
+ if (type === RepositoryType.CONFIGURATION) {
262
+ set(mo, 'url', binary?.url);
263
+ }
263
264
  return id
264
265
  ? this.inventory.update(mo)
265
266
  : this.inventory.create(mo);
@@ -269,13 +270,13 @@ class RepositoryService {
269
270
  return this.inventory.childAdditionsCreate(mo, parent);
270
271
  }
271
272
  prepareRepositoryBinaryMO(modal, binaryURL, type) {
272
- const { version, patchVersion, dependency } = modal;
273
+ const { version, patchVersion, dependency, c8y_Global } = modal;
273
274
  const result = {
274
275
  type: REPOSITORY_BINARY_TYPES[type],
275
276
  [type]: {
276
277
  url: binaryURL
277
278
  },
278
- c8y_Global: {}
279
+ c8y_Global
279
280
  };
280
281
  if (dependency) {
281
282
  set(result, [type, 'version'], patchVersion);
@@ -290,12 +291,16 @@ class RepositoryService {
290
291
  }
291
292
  return result;
292
293
  }
293
- async linkBinary(repositoryBinary, binary) {
294
- const { id: repositoryBinaryId } = repositoryBinary;
295
- if (binary) {
296
- const { id: binaryId } = binary;
297
- return this.inventory.childAdditionsAdd(binaryId, repositoryBinaryId);
294
+ async linkBinary(repositoryBinary, binary, repositoryEntry) {
295
+ if (repositoryBinary) {
296
+ const { id: repositoryBinaryId } = repositoryBinary;
297
+ if (binary) {
298
+ const { id: binaryId } = binary;
299
+ return this.inventory.childAdditionsAdd(binaryId, repositoryBinaryId);
300
+ }
298
301
  }
302
+ else
303
+ return this.inventory.childAdditionsAdd(binary, repositoryEntry);
299
304
  }
300
305
  cleanUp(mosToDelete) {
301
306
  mosToDelete.forEach(mo => {
@@ -1081,26 +1086,6 @@ class RepositoryService {
1081
1086
  }
1082
1087
  return res;
1083
1088
  }
1084
- async createEntry(mo) {
1085
- const binaryId = await this.inventoryBinary.getIdFromUrl(mo.url);
1086
- const newMo = await this.inventory.create(mo);
1087
- if (binaryId) {
1088
- await this.inventory.childAdditionsAdd(binaryId, newMo.data);
1089
- }
1090
- return newMo;
1091
- }
1092
- async updateEntry(mo, url) {
1093
- const existingBinaryId = await this.inventoryBinary.getIdFromUrl(url);
1094
- const newBinaryId = await this.inventoryBinary.getIdFromUrl(mo.url);
1095
- if (existingBinaryId && existingBinaryId !== newBinaryId) {
1096
- const id = this.inventoryBinary.getIdFromUrl(url);
1097
- await this.inventoryBinary.delete(id);
1098
- }
1099
- if (newBinaryId) {
1100
- await this.inventory.childAdditionsAdd(newBinaryId, mo);
1101
- }
1102
- return this.inventory.update(mo);
1103
- }
1104
1089
  getBaseVersionResultListForLegacyEntry(entry) {
1105
1090
  return Promise.resolve({
1106
1091
  res: {},
@@ -1192,12 +1177,22 @@ class RepositoryService {
1192
1177
  softwareListToLegacy(list) {
1193
1178
  return (list || []).reduce((prev, curr) => ({ ...prev, [curr.name]: curr.version }), {});
1194
1179
  }
1180
+ async getGlobalFragment(type) {
1181
+ return (await this.globalConfigService.getGlobalParam(type)) ? { c8y_Global: {} } : undefined;
1182
+ }
1183
+ async removeOutdatedBinary(newBinaryURL, oldBinaryURL) {
1184
+ const existingBinaryId = await this.inventoryBinary.getIdFromUrl(oldBinaryURL);
1185
+ const newBinaryId = await this.inventoryBinary.getIdFromUrl(newBinaryURL);
1186
+ if (existingBinaryId && existingBinaryId !== newBinaryId) {
1187
+ await this.inventoryBinary.delete(existingBinaryId);
1188
+ }
1189
+ }
1195
1190
  }
1196
- RepositoryService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.6", ngImport: i0, type: RepositoryService, deps: [{ token: i1.InventoryService }, { token: i1.InventoryBinaryService }, { token: i1.OperationService }, { token: i2.AlertService }, { token: i1.EventService }, { token: i2.OperationRealtimeService }, { token: i1.EventBinaryService }, { token: AdvancedSoftwareService }], target: i0.ɵɵFactoryTarget.Injectable });
1191
+ RepositoryService.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "14.0.6", ngImport: i0, type: RepositoryService, deps: [{ token: i1.InventoryService }, { token: i1.InventoryBinaryService }, { token: i1.OperationService }, { token: i2.AlertService }, { token: i1.EventService }, { token: i2.OperationRealtimeService }, { token: i1.EventBinaryService }, { token: AdvancedSoftwareService }, { token: i2.GlobalConfigService }], target: i0.ɵɵFactoryTarget.Injectable });
1197
1192
  RepositoryService.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "14.0.6", ngImport: i0, type: RepositoryService });
1198
1193
  i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "14.0.6", ngImport: i0, type: RepositoryService, decorators: [{
1199
1194
  type: Injectable
1200
- }], ctorParameters: function () { return [{ type: i1.InventoryService }, { type: i1.InventoryBinaryService }, { type: i1.OperationService }, { type: i2.AlertService }, { type: i1.EventService }, { type: i2.OperationRealtimeService }, { type: i1.EventBinaryService }, { type: AdvancedSoftwareService }]; } });
1195
+ }], ctorParameters: function () { return [{ type: i1.InventoryService }, { type: i1.InventoryBinaryService }, { type: i1.OperationService }, { type: i2.AlertService }, { type: i1.EventService }, { type: i2.OperationRealtimeService }, { type: i1.EventBinaryService }, { type: AdvancedSoftwareService }, { type: i2.GlobalConfigService }]; } });
1201
1196
 
1202
1197
  var LinkRenderType;
1203
1198
  (function (LinkRenderType) {