@bringg/dashboard-sdk 9.77.1 → 9.77.2

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.
@@ -3069,18 +3069,19 @@ var BringgDashboardSDK = /** @class */ (function () {
3069
3069
  exports.BringgDashboardSDK = BringgDashboardSDK;
3070
3070
  function init(session) {
3071
3071
  return __awaiter(this, void 0, void 0, function () {
3072
- var bringg, workflows, taskRejectReasons;
3072
+ var bringg, customersApi, workflows, taskRejectReasons;
3073
3073
  return __generator(this, function (_a) {
3074
3074
  switch (_a.label) {
3075
3075
  case 0:
3076
3076
  bringg = new BringgDashboardSDK(session);
3077
+ customersApi = new customers_api_1.default(session);
3077
3078
  bringg.v2 = {
3078
3079
  vehicles: new vehicles_api_1.default(session),
3079
3080
  relay: new RelayApi_1.default(session),
3080
- tasks: new tasks_api_1.default(session),
3081
+ tasks: new tasks_api_1.default(session, customersApi),
3081
3082
  users: new users_api_1.default(session),
3082
3083
  tags: new tags_api_1.default(session),
3083
- customers: new customers_api_1.default(session),
3084
+ customers: customersApi,
3084
3085
  plannedDeliveryWindows: new planned_delivery_windows_api_1.default(session),
3085
3086
  optimizationResultApi: new optimization_result_api_1.default(session),
3086
3087
  exclusionWindow: new exclusion_window_api_1.ExclusionWindowApi(session),
@@ -8269,6 +8270,17 @@ exports.objectMerger = { deepMerge: deepMerge };
8269
8270
 
8270
8271
  "use strict";
8271
8272
 
8273
+ var __assign = (this && this.__assign) || function () {
8274
+ __assign = Object.assign || function(t) {
8275
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
8276
+ s = arguments[i];
8277
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
8278
+ t[p] = s[p];
8279
+ }
8280
+ return t;
8281
+ };
8282
+ return __assign.apply(this, arguments);
8283
+ };
8272
8284
  var __values = (this && this.__values) || function(o) {
8273
8285
  var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
8274
8286
  if (m) return m.call(o);
@@ -8340,11 +8352,75 @@ var StorableRealtimeSubscriptions = /** @class */ (function () {
8340
8352
  }
8341
8353
  var _loop_2 = function (subEntityEvent) {
8342
8354
  this_2.realtimeSubscriptions.subscribe(subEntityEvent.name, function (subEntityEventData) {
8355
+ var e_3, _a;
8356
+ var _b, _c;
8343
8357
  subEntityEvent.ignoreFields.forEach(function (field) { return delete subEntityEventData[field]; });
8358
+ var parentId = subEntityEventData[subEntityEvent.parentField];
8359
+ var entityId = subEntityEventData[subEntityEvent.idField || 'id'];
8360
+ var getParent = function () { return _this.dataStore.get(parentId); };
8361
+ var pendingEnrichers = [];
8362
+ if (subEntityEvent.enrich) {
8363
+ var activeEnrichers = subEntityEvent.enrich.enrichers.filter(function (e) {
8364
+ return e.shouldEnrich(subEntityEventData, getParent);
8365
+ });
8366
+ try {
8367
+ for (var activeEnrichers_1 = (e_3 = void 0, __values(activeEnrichers)), activeEnrichers_1_1 = activeEnrichers_1.next(); !activeEnrichers_1_1.done; activeEnrichers_1_1 = activeEnrichers_1.next()) {
8368
+ var enricher = activeEnrichers_1_1.value;
8369
+ var result = (_c = (_b = enricher.resolveSync) === null || _b === void 0 ? void 0 : _b.call(enricher, subEntityEventData, getParent)) !== null && _c !== void 0 ? _c : null;
8370
+ if (result !== null) {
8371
+ Object.assign(subEntityEventData, result);
8372
+ }
8373
+ else {
8374
+ pendingEnrichers.push(enricher);
8375
+ }
8376
+ }
8377
+ }
8378
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
8379
+ finally {
8380
+ try {
8381
+ if (activeEnrichers_1_1 && !activeEnrichers_1_1.done && (_a = activeEnrichers_1.return)) _a.call(activeEnrichers_1);
8382
+ }
8383
+ finally { if (e_3) throw e_3.error; }
8384
+ }
8385
+ }
8344
8386
  var eventChangeData = _this.changeSubEntity(subEntityEventData, subEntityEvent.idField || 'id', subEntityEvent.parentField, subEntityEvent.field, subEntityEvent.isDelete);
8345
8387
  if (eventChangeData === null || eventChangeData === void 0 ? void 0 : eventChangeData.current) {
8346
8388
  _this.runEventCallbacks(subEntityEvent.eventToRaise, eventChangeData);
8347
8389
  }
8390
+ if (pendingEnrichers.length > 0) {
8391
+ var applyEnrichment_1 = function (data) {
8392
+ var _a;
8393
+ var _b;
8394
+ var parent = _this.dataStore.get(parentId);
8395
+ if (!parent) {
8396
+ return;
8397
+ }
8398
+ var subEntity = (_b = parent[subEntityEvent.field]) === null || _b === void 0 ? void 0 : _b.find(function (e) { return Number(e.id) === Number(entityId); });
8399
+ if (!subEntity) {
8400
+ return;
8401
+ }
8402
+ var oldValues = Object.fromEntries(Object.keys(data).map(function (key) { return [key, subEntity[key]]; }));
8403
+ Object.assign(subEntity, data);
8404
+ _this.runEventCallbacks(subEntityEvent.eventToRaise, {
8405
+ current: parent,
8406
+ diff: (_a = {}, _a[subEntityEvent.field] = [__assign({ id: entityId }, oldValues)], _a)
8407
+ });
8408
+ };
8409
+ // Promise.all is fine here because today there is only one async enricher per event.
8410
+ // If multiple async enrichers are ever added, switch to Promise.allSettled so a
8411
+ // single rejection does not suppress the results of the others.
8412
+ Promise.all(pendingEnrichers.map(function (e) { return e.resolveAsync(subEntityEventData, getParent); }))
8413
+ .then(function (results) {
8414
+ var asyncData = results.reduce(function (acc, result) { return (result !== null ? Object.assign(acc, result) : acc); }, {});
8415
+ if (Object.keys(asyncData).length > 0) {
8416
+ applyEnrichment_1(asyncData);
8417
+ }
8418
+ })
8419
+ .catch(function (err) {
8420
+ console.error('async enrichment failed for', subEntityEvent.field, 'id', entityId);
8421
+ console.error(err);
8422
+ });
8423
+ }
8348
8424
  });
8349
8425
  };
8350
8426
  var this_2 = this;
@@ -23765,7 +23841,7 @@ var lodash_1 = __webpack_require__(96486);
23765
23841
  var BringgException_1 = __webpack_require__(43605);
23766
23842
  var Logger_1 = __importDefault(__webpack_require__(55860));
23767
23843
  var abort_1 = __webpack_require__(34179);
23768
- var version = '9.77.1';
23844
+ var version = '9.77.2';
23769
23845
  function logErrorResponse(response) {
23770
23846
  var data = response.data, status = response.status;
23771
23847
  try {
@@ -26565,6 +26641,42 @@ exports.taskMerger = { mergeTaskIntoExisting: mergeTaskIntoExisting };
26565
26641
 
26566
26642
  "use strict";
26567
26643
 
26644
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26645
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
26646
+ return new (P || (P = Promise))(function (resolve, reject) {
26647
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
26648
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
26649
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
26650
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
26651
+ });
26652
+ };
26653
+ var __generator = (this && this.__generator) || function (thisArg, body) {
26654
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
26655
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
26656
+ function verb(n) { return function (v) { return step([n, v]); }; }
26657
+ function step(op) {
26658
+ if (f) throw new TypeError("Generator is already executing.");
26659
+ while (g && (g = 0, op[0] && (_ = 0)), _) try {
26660
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
26661
+ if (y = 0, t) op = [op[0] & 2, t.value];
26662
+ switch (op[0]) {
26663
+ case 0: case 1: t = op; break;
26664
+ case 4: _.label++; return { value: op[1], done: false };
26665
+ case 5: _.label++; y = op[1]; op = [0]; continue;
26666
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
26667
+ default:
26668
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
26669
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
26670
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
26671
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
26672
+ if (t[2]) _.ops.pop();
26673
+ _.trys.pop(); continue;
26674
+ }
26675
+ op = body.call(thisArg, _);
26676
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
26677
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
26678
+ }
26679
+ };
26568
26680
  var __values = (this && this.__values) || function(o) {
26569
26681
  var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
26570
26682
  if (m) return m.call(o);
@@ -26580,11 +26692,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26580
26692
  return (mod && mod.__esModule) ? mod : { "default": mod };
26581
26693
  };
26582
26694
  Object.defineProperty(exports, "__esModule", ({ value: true }));
26695
+ var cloneDeep_1 = __importDefault(__webpack_require__(50361));
26583
26696
  var storable_realtime_subscriptions_1 = __importDefault(__webpack_require__(1227));
26584
26697
  var Tasks_consts_1 = __webpack_require__(24976);
26585
26698
  var TaskRealtimeSubscriptions = /** @class */ (function () {
26586
- function TaskRealtimeSubscriptions(session, tasksStore, storableDataService) {
26699
+ function TaskRealtimeSubscriptions(session, tasksStore, storableDataService, customersApi) {
26587
26700
  this.tasksStore = tasksStore;
26701
+ this.customersApi = customersApi;
26588
26702
  this.storableRealtimeSubscriptions = new storable_realtime_subscriptions_1.default(session, tasksStore, storableDataService);
26589
26703
  }
26590
26704
  TaskRealtimeSubscriptions.prototype.updateStoreOnEvents = function () {
@@ -26635,7 +26749,61 @@ var TaskRealtimeSubscriptions = /** @class */ (function () {
26635
26749
  parentField: 'task_id',
26636
26750
  field: 'way_points',
26637
26751
  ignoreFields: ['way_points'],
26638
- isDelete: false
26752
+ isDelete: false,
26753
+ enrich: {
26754
+ enrichers: [
26755
+ /**
26756
+ * Updating customer on a WayPoint is pretty much an edge case
26757
+ * Customer enrichment can be fully removed if way_point.customer is deprecated and marked as such
26758
+ * that must not be used within dSDK data consumers and all existing usages are removed.
26759
+ * This is what we have to do if we want to avoid unnecessary complications on dSDK side.
26760
+ */
26761
+ {
26762
+ shouldEnrich: function (payload, getTask) {
26763
+ var _a, _b, _c;
26764
+ if (!('customer_id' in payload) || 'customer' in payload) {
26765
+ return false;
26766
+ }
26767
+ var wp = (_b = (_a = getTask()) === null || _a === void 0 ? void 0 : _a.way_points) === null || _b === void 0 ? void 0 : _b.find(function (wp) { return Number(wp.id) === Number(payload.id); });
26768
+ return payload.customer_id !== ((_c = wp === null || wp === void 0 ? void 0 : wp.customer) === null || _c === void 0 ? void 0 : _c.id);
26769
+ },
26770
+ resolveSync: function (payload) {
26771
+ if (payload.customer_id === null) {
26772
+ return { customer: null };
26773
+ }
26774
+ var cached = _this.customersApi.get(payload.customer_id);
26775
+ return cached ? { customer: (0, cloneDeep_1.default)(cached) } : null;
26776
+ },
26777
+ resolveAsync: function (payload, getTask) { return __awaiter(_this, void 0, void 0, function () {
26778
+ var payloadCustomerId, wpId, shouldStop, findWp, customer;
26779
+ return __generator(this, function (_a) {
26780
+ switch (_a.label) {
26781
+ case 0:
26782
+ if (payload.customer_id === null) {
26783
+ return [2 /*return*/, null];
26784
+ }
26785
+ payloadCustomerId = Number(payload.customer_id);
26786
+ wpId = Number(payload.id);
26787
+ shouldStop = function (wp) {
26788
+ return !wp || Number(wp.customer_id) !== payloadCustomerId;
26789
+ };
26790
+ findWp = function () { var _a, _b; return (_b = (_a = getTask()) === null || _a === void 0 ? void 0 : _a.way_points) === null || _b === void 0 ? void 0 : _b.find(function (wp) { return wp.id === wpId; }); };
26791
+ if (shouldStop(findWp())) {
26792
+ return [2 /*return*/, null];
26793
+ }
26794
+ return [4 /*yield*/, this.customersApi.load(payload.customer_id)];
26795
+ case 1:
26796
+ customer = _a.sent();
26797
+ if (shouldStop(findWp())) {
26798
+ return [2 /*return*/, null];
26799
+ }
26800
+ return [2 /*return*/, { customer: (0, cloneDeep_1.default)(customer) }];
26801
+ }
26802
+ });
26803
+ }); }
26804
+ }
26805
+ ]
26806
+ }
26639
26807
  },
26640
26808
  {
26641
26809
  name: Tasks_consts_1.TaskRealTimeEvents.WAYPOINT_DELETED,
@@ -26796,7 +26964,7 @@ var tasks_service_1 = __importDefault(__webpack_require__(8159));
26796
26964
  // hard coded fields to take advantage of the light data store cache
26797
26965
  var READY_TO_EXECUTE_FIELDS = ['ready_to_execute'];
26798
26966
  var TasksApi = /** @class */ (function () {
26799
- function TasksApi(session) {
26967
+ function TasksApi(session, customersApi) {
26800
26968
  var _this = this;
26801
26969
  var groupFilters = [
26802
26970
  {
@@ -26837,7 +27005,7 @@ var TasksApi = /** @class */ (function () {
26837
27005
  params: params
26838
27006
  });
26839
27007
  }, [lightDataStore]);
26840
- this.taskRealtimeSubscriptions = new task_realtime_subscriptions_1.default(session, this.tasksStore, this.storableDataService);
27008
+ this.taskRealtimeSubscriptions = new task_realtime_subscriptions_1.default(session, this.tasksStore, this.storableDataService, customersApi);
26841
27009
  }
26842
27010
  TasksApi.prototype.updateStoreOnEvents = function () {
26843
27011
  this.taskRealtimeSubscriptions.updateStoreOnEvents();