@or-sdk/contacts 3.2.2-beta.1753.0 → 3.2.2-beta.1755.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.
- package/dist/cjs/api/batchProcessApi.js +76 -0
- package/dist/cjs/api/batchProcessApi.js.map +1 -1
- package/dist/cjs/api/contactApi.js +87 -34
- package/dist/cjs/api/contactApi.js.map +1 -1
- package/dist/cjs/apiError.js +13 -1
- package/dist/cjs/apiError.js.map +1 -1
- package/dist/cjs/constants.js +1 -1
- package/dist/esm/api/batchProcessApi.js +31 -0
- package/dist/esm/api/batchProcessApi.js.map +1 -1
- package/dist/esm/api/contactApi.js +46 -19
- package/dist/esm/api/contactApi.js.map +1 -1
- package/dist/esm/apiError.js +8 -0
- package/dist/esm/apiError.js.map +1 -1
- package/dist/esm/constants.js +1 -1
- package/dist/types/api/batchProcessApi.d.ts +4 -1
- package/dist/types/api/batchProcessApi.d.ts.map +1 -1
- package/dist/types/api/contactApi.d.ts +2 -0
- package/dist/types/api/contactApi.d.ts.map +1 -1
- package/dist/types/apiError.d.ts +6 -0
- package/dist/types/apiError.d.ts.map +1 -1
- package/dist/types/constants.d.ts +1 -1
- package/dist/types/types.d.ts +15 -1
- package/dist/types/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/api/batchProcessApi.ts +41 -3
- package/src/api/contactApi.ts +72 -21
- package/src/apiError.ts +11 -0
- package/src/constants.ts +1 -1
- package/src/types.ts +18 -1
|
@@ -14,8 +14,56 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
14
14
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
15
|
};
|
|
16
16
|
})();
|
|
17
|
+
var __assign = (this && this.__assign) || function () {
|
|
18
|
+
__assign = Object.assign || function(t) {
|
|
19
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
+
s = arguments[i];
|
|
21
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
+
t[p] = s[p];
|
|
23
|
+
}
|
|
24
|
+
return t;
|
|
25
|
+
};
|
|
26
|
+
return __assign.apply(this, arguments);
|
|
27
|
+
};
|
|
28
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
29
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
30
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
31
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
32
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
33
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
34
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
38
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
39
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
40
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
41
|
+
function step(op) {
|
|
42
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
43
|
+
while (_) try {
|
|
44
|
+
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;
|
|
45
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
46
|
+
switch (op[0]) {
|
|
47
|
+
case 0: case 1: t = op; break;
|
|
48
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
49
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
50
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
51
|
+
default:
|
|
52
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
53
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
54
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
55
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
56
|
+
if (t[2]) _.ops.pop();
|
|
57
|
+
_.trys.pop(); continue;
|
|
58
|
+
}
|
|
59
|
+
op = body.call(thisArg, _);
|
|
60
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
61
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
62
|
+
}
|
|
63
|
+
};
|
|
17
64
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
65
|
var baseApi_1 = require("./baseApi");
|
|
66
|
+
var types_contacts_api_1 = require("@onereach/types-contacts-api");
|
|
19
67
|
var BatchProcessApi = (function (_super) {
|
|
20
68
|
__extends(BatchProcessApi, _super);
|
|
21
69
|
function BatchProcessApi() {
|
|
@@ -29,6 +77,34 @@ var BatchProcessApi = (function (_super) {
|
|
|
29
77
|
route: "".concat(this.apiBasePath, "/").concat(id),
|
|
30
78
|
});
|
|
31
79
|
};
|
|
80
|
+
BatchProcessApi.prototype.getPendingBatchProcesses = function () {
|
|
81
|
+
return this.apiCall({
|
|
82
|
+
method: 'GET',
|
|
83
|
+
route: "".concat(this.apiBasePath, "/pending-batches"),
|
|
84
|
+
});
|
|
85
|
+
};
|
|
86
|
+
BatchProcessApi.prototype.trackBatchProcess = function (id) {
|
|
87
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
88
|
+
var batchProcess, result;
|
|
89
|
+
return __generator(this, function (_a) {
|
|
90
|
+
switch (_a.label) {
|
|
91
|
+
case 0: return [4, this.getBatchProcess(id)];
|
|
92
|
+
case 1:
|
|
93
|
+
batchProcess = _a.sent();
|
|
94
|
+
result = {
|
|
95
|
+
status: batchProcess.status,
|
|
96
|
+
};
|
|
97
|
+
if (batchProcess.status === types_contacts_api_1.BatchProcessStatus.success) {
|
|
98
|
+
result = __assign(__assign({}, result), { contactsIds: batchProcess.results.flatMap(function (i) { return JSON.parse(i); }) });
|
|
99
|
+
}
|
|
100
|
+
if (batchProcess.status === types_contacts_api_1.BatchProcessStatus.failed) {
|
|
101
|
+
result = __assign(__assign({}, result), { errors: batchProcess.messages });
|
|
102
|
+
}
|
|
103
|
+
return [2, result];
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
};
|
|
32
108
|
return BatchProcessApi;
|
|
33
109
|
}(baseApi_1.BaseApi));
|
|
34
110
|
exports.default = BatchProcessApi;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"batchProcessApi.js","sourceRoot":"","sources":["../../../src/api/batchProcessApi.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"batchProcessApi.js","sourceRoot":"","sources":["../../../src/api/batchProcessApi.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,qCAAoC;AAEpC,mEAAsH;AAEtH;IAA6C,mCAAO;IAApD;QAAA,qEAgDC;QA/CkB,iBAAW,GAAG,eAAe,CAAC;;IA+CjD,CAAC;IA7CC,yCAAe,GAAf,UAAgB,EAAU;QACxB,OAAO,IAAI,CAAC,OAAO,CAAC;YAClB,MAAM,EAAE,KAAK;YACb,KAAK,EAAE,UAAG,IAAI,CAAC,WAAW,cAAI,EAAE,CAAE;SACnC,CAAC,CAAC;IACL,CAAC;IAKD,kDAAwB,GAAxB;QACE,OAAO,IAAI,CAAC,OAAO,CAAC;YAClB,MAAM,EAAE,KAAK;YACb,KAAK,EAAE,UAAG,IAAI,CAAC,WAAW,qBAAkB;SAC7C,CAAC,CAAC;IACL,CAAC;IAMK,2CAAiB,GAAvB,UAAwB,EAAU;;;;;4BACX,WAAM,IAAI,CAAC,eAAe,CAAC,EAAE,CAAC,EAAA;;wBAA7C,YAAY,GAAG,SAA8B;wBAE/C,MAAM,GAA8B;4BACtC,MAAM,EAAE,YAAY,CAAC,MAAM;yBAC5B,CAAC;wBAEF,IAAI,YAAY,CAAC,MAAM,KAAK,uCAAkB,CAAC,OAAO,EAAE;4BACtD,MAAM,yBACD,MAAM,KACT,WAAW,EAAE,YAAY,CAAC,OAAO,CAAC,OAAO,CAAS,UAAA,CAAC,IAAI,OAAA,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAb,CAAa,CAAC,GACtE,CAAC;yBACH;wBAED,IAAI,YAAY,CAAC,MAAM,KAAK,uCAAkB,CAAC,MAAM,EAAE;4BACrD,MAAM,yBACD,MAAM,KACT,MAAM,EAAE,YAAY,CAAC,QAAQ,GAC9B,CAAC;yBACH;wBAED,WAAO,MAAM,EAAC;;;;KACf;IAEH,sBAAC;AAAD,CAAC,AAhDD,CAA6C,iBAAO,GAgDnD"}
|
|
@@ -86,6 +86,7 @@ var types_contacts_api_1 = require("@onereach/types-contacts-api");
|
|
|
86
86
|
var baseApi_1 = require("./baseApi");
|
|
87
87
|
var utils_1 = require("../utils");
|
|
88
88
|
var constants_1 = require("../constants");
|
|
89
|
+
var apiError_1 = require("../apiError");
|
|
89
90
|
var ContactApi = (function (_super) {
|
|
90
91
|
__extends(ContactApi, _super);
|
|
91
92
|
function ContactApi(apiCall, batchProcessApi) {
|
|
@@ -143,16 +144,56 @@ var ContactApi = (function (_super) {
|
|
|
143
144
|
};
|
|
144
145
|
ContactApi.prototype.bulkCreateContacts = function (data) {
|
|
145
146
|
return __awaiter(this, void 0, void 0, function () {
|
|
146
|
-
var contacts, rest, contactsMaxSize, contactsChunks;
|
|
147
|
+
var contacts, rest, contactsMaxSize, contactsChunks, results, _a;
|
|
148
|
+
return __generator(this, function (_b) {
|
|
149
|
+
switch (_b.label) {
|
|
150
|
+
case 0:
|
|
151
|
+
console.log('contacts: ', data.contacts.length);
|
|
152
|
+
contacts = data.contacts, rest = __rest(data, ["contacts"]);
|
|
153
|
+
contactsMaxSize = constants_1.REQUEST_PAYLOAD_MAX_BYTES - (0, utils_1.getObjectSizeInBytes)(__assign({}, rest));
|
|
154
|
+
contactsChunks = (0, utils_1.chunkArrByMaxSize)(contacts, contactsMaxSize);
|
|
155
|
+
console.log('chunks: ', contactsChunks.length);
|
|
156
|
+
if (!(contactsChunks.length === 1)) return [3, 2];
|
|
157
|
+
return [4, this.createContactsInSingleBatch(data)];
|
|
158
|
+
case 1:
|
|
159
|
+
_a = [_b.sent()];
|
|
160
|
+
return [3, 4];
|
|
161
|
+
case 2: return [4, this.createContactsInMultiBatches(contactsChunks, rest)];
|
|
162
|
+
case 3:
|
|
163
|
+
_a = _b.sent();
|
|
164
|
+
_b.label = 4;
|
|
165
|
+
case 4:
|
|
166
|
+
results = _a;
|
|
167
|
+
return [2, results.reduce(function (acc, _a) {
|
|
168
|
+
var contactsIds = _a.contactsIds;
|
|
169
|
+
return (__spreadArray(__spreadArray([], acc, true), contactsIds, true));
|
|
170
|
+
}, [])];
|
|
171
|
+
}
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
};
|
|
175
|
+
ContactApi.prototype.initCreateBatch = function (data) {
|
|
176
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
177
|
+
var contacts, rest, contactsMaxSize, contactsChunks, batchProcess;
|
|
147
178
|
return __generator(this, function (_a) {
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
179
|
+
switch (_a.label) {
|
|
180
|
+
case 0:
|
|
181
|
+
contacts = data.contacts, rest = __rest(data, ["contacts"]);
|
|
182
|
+
contactsMaxSize = constants_1.REQUEST_PAYLOAD_MAX_BYTES - (0, utils_1.getObjectSizeInBytes)(__assign({}, rest));
|
|
183
|
+
contactsChunks = (0, utils_1.chunkArrByMaxSize)(contacts, contactsMaxSize);
|
|
184
|
+
return [4, this.apiCall({
|
|
185
|
+
method: 'POST',
|
|
186
|
+
route: "".concat(this.apiBasePath, "/bulk"),
|
|
187
|
+
data: __assign({ contacts: contactsChunks[0] }, rest),
|
|
188
|
+
})];
|
|
189
|
+
case 1:
|
|
190
|
+
batchProcess = _a.sent();
|
|
191
|
+
return [2, {
|
|
192
|
+
totalChunks: contactsChunks.length,
|
|
193
|
+
firstChunkSize: contactsChunks[0].length,
|
|
194
|
+
batchId: batchProcess.id,
|
|
195
|
+
}];
|
|
196
|
+
}
|
|
156
197
|
});
|
|
157
198
|
});
|
|
158
199
|
};
|
|
@@ -211,11 +252,9 @@ var ContactApi = (function (_super) {
|
|
|
211
252
|
});
|
|
212
253
|
});
|
|
213
254
|
};
|
|
214
|
-
ContactApi.prototype.createContactsInSingleBatch = function (data
|
|
215
|
-
if (repeats === void 0) { repeats = 0; }
|
|
255
|
+
ContactApi.prototype.createContactsInSingleBatch = function (data) {
|
|
216
256
|
return __awaiter(this, void 0, void 0, function () {
|
|
217
257
|
var s, batchProcess, batchProcessResult, e;
|
|
218
|
-
var _this = this;
|
|
219
258
|
return __generator(this, function (_a) {
|
|
220
259
|
switch (_a.label) {
|
|
221
260
|
case 0:
|
|
@@ -230,23 +269,19 @@ var ContactApi = (function (_super) {
|
|
|
230
269
|
return [4, this.polling(batchProcess.id)];
|
|
231
270
|
case 2:
|
|
232
271
|
batchProcessResult = _a.sent();
|
|
233
|
-
if (repeats === 2) {
|
|
234
|
-
throw new Error('Can not complete contacts creation batch');
|
|
235
|
-
}
|
|
236
|
-
if (!(batchProcessResult.status === types_contacts_api_1.BatchProcessStatus.failed)) return [3, 4];
|
|
237
|
-
return [4, (0, utils_1.debouncePromise)(function () { return _this.createContactsInSingleBatch(data, repeats + 1); }, 10000)];
|
|
238
|
-
case 3: return [2, _a.sent()];
|
|
239
|
-
case 4:
|
|
240
272
|
e = performance.now();
|
|
241
273
|
console.log('single batch time: ', (e - s) / 1000, 's');
|
|
242
|
-
return [2,
|
|
274
|
+
return [2, {
|
|
275
|
+
batchId: batchProcess.id,
|
|
276
|
+
contactsIds: batchProcessResult.results.flatMap(function (i) { return JSON.parse(i); }),
|
|
277
|
+
}];
|
|
243
278
|
}
|
|
244
279
|
});
|
|
245
280
|
});
|
|
246
281
|
};
|
|
247
282
|
ContactApi.prototype.createContactsInMultiBatches = function (contactsChunks, data) {
|
|
248
283
|
return __awaiter(this, void 0, void 0, function () {
|
|
249
|
-
var batchPromises, promisesBatchSize, results, i, s, chunk, chunkResults, e;
|
|
284
|
+
var batchPromises, promisesBatchSize, results, i, s, chunk, chunkResults, e_1, e;
|
|
250
285
|
var _this = this;
|
|
251
286
|
return __generator(this, function (_a) {
|
|
252
287
|
switch (_a.label) {
|
|
@@ -262,20 +297,34 @@ var ContactApi = (function (_super) {
|
|
|
262
297
|
i = 0;
|
|
263
298
|
_a.label = 1;
|
|
264
299
|
case 1:
|
|
265
|
-
if (!(i < batchPromises.length)) return [3,
|
|
300
|
+
if (!(i < batchPromises.length)) return [3, 7];
|
|
266
301
|
s = performance.now();
|
|
302
|
+
_a.label = 2;
|
|
303
|
+
case 2:
|
|
304
|
+
_a.trys.push([2, 4, , 5]);
|
|
267
305
|
chunk = batchPromises.slice(i, i + promisesBatchSize);
|
|
268
306
|
return [4, Promise.all(chunk.map(function (p) { return p(); }))];
|
|
269
|
-
case
|
|
307
|
+
case 3:
|
|
270
308
|
chunkResults = _a.sent();
|
|
271
|
-
results.push.apply(results, chunkResults
|
|
309
|
+
results.push.apply(results, chunkResults);
|
|
310
|
+
return [3, 5];
|
|
311
|
+
case 4:
|
|
312
|
+
e_1 = _a.sent();
|
|
313
|
+
if (e_1 instanceof apiError_1.CreateContactsBatchError) {
|
|
314
|
+
e_1.processedBatchIds = results.map(function (_a) {
|
|
315
|
+
var batchId = _a.batchId;
|
|
316
|
+
return batchId;
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
throw e_1;
|
|
320
|
+
case 5:
|
|
272
321
|
e = performance.now();
|
|
273
322
|
console.log('awaiting ', promisesBatchSize, ' batches: ', (e - s) / 1000, 's');
|
|
274
|
-
_a.label =
|
|
275
|
-
case
|
|
323
|
+
_a.label = 6;
|
|
324
|
+
case 6:
|
|
276
325
|
i += promisesBatchSize;
|
|
277
326
|
return [3, 1];
|
|
278
|
-
case
|
|
327
|
+
case 7: return [2, results];
|
|
279
328
|
}
|
|
280
329
|
});
|
|
281
330
|
});
|
|
@@ -283,7 +332,7 @@ var ContactApi = (function (_super) {
|
|
|
283
332
|
ContactApi.prototype.polling = function (batchId, repeats) {
|
|
284
333
|
if (repeats === void 0) { repeats = 0; }
|
|
285
334
|
return __awaiter(this, void 0, void 0, function () {
|
|
286
|
-
var
|
|
335
|
+
var batchProcess, e_2;
|
|
287
336
|
var _this = this;
|
|
288
337
|
return __generator(this, function (_a) {
|
|
289
338
|
switch (_a.label) {
|
|
@@ -291,17 +340,21 @@ var ContactApi = (function (_super) {
|
|
|
291
340
|
_a.trys.push([0, 2, , 3]);
|
|
292
341
|
return [4, (0, utils_1.debouncePromise)(function () { return _this.batchProcessApi.getBatchProcess(batchId); }, 1000)];
|
|
293
342
|
case 1:
|
|
294
|
-
|
|
343
|
+
batchProcess = _a.sent();
|
|
295
344
|
return [3, 3];
|
|
296
345
|
case 2:
|
|
297
|
-
|
|
346
|
+
e_2 = _a.sent();
|
|
298
347
|
if (repeats < constants_1.FAILED_REQUEST_REPEATS) {
|
|
299
348
|
return [2, this.polling(batchId, repeats + 1)];
|
|
300
349
|
}
|
|
301
|
-
throw
|
|
302
|
-
case 3:
|
|
303
|
-
|
|
304
|
-
|
|
350
|
+
throw new apiError_1.CreateContactsBatchError(e_2.message, batchId);
|
|
351
|
+
case 3:
|
|
352
|
+
if (batchProcess.status === types_contacts_api_1.BatchProcessStatus.failed) {
|
|
353
|
+
throw new apiError_1.CreateContactsBatchError('Could not complete batch process', batchId, batchProcess.messages);
|
|
354
|
+
}
|
|
355
|
+
return [2, batchProcess.status === types_contacts_api_1.BatchProcessStatus.pending
|
|
356
|
+
? this.polling(batchId)
|
|
357
|
+
: batchProcess];
|
|
305
358
|
}
|
|
306
359
|
});
|
|
307
360
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contactApi.js","sourceRoot":"","sources":["../../../src/api/contactApi.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mEAiBsC;AAEtC,qCAAoC;AAEpC,kCAAoF;
|
|
1
|
+
{"version":3,"file":"contactApi.js","sourceRoot":"","sources":["../../../src/api/contactApi.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,mEAiBsC;AAEtC,qCAAoC;AAEpC,kCAAoF;AAEpF,0CAA6G;AAC7G,wCAAuD;AAEvD;IAAwC,8BAAO;IAC7C,oBACqB,OAAgD,EAC3D,eAAgC;QAF1C,YAIE,kBAAM,OAAO,CAAC,SACf;QAJoB,aAAO,GAAP,OAAO,CAAyC;QAC3D,qBAAe,GAAf,eAAe,CAAiB;QAKzB,iBAAW,GAAG,SAAS,CAAC;;IAFzC,CAAC;IASD,+BAAU,GAAV,UAAW,EAAU,EAAE,MAAwB;QAC7C,OAAO,IAAI,CAAC,OAAO,CAAC;YAClB,MAAM,EAAE,KAAK;YACb,KAAK,EAAE,UAAG,IAAI,CAAC,WAAW,cAAI,EAAE,CAAE;YAClC,MAAM,QAAA;SACP,CAAC,CAAC;IACL,CAAC;IAMD,gCAAW,GAAX,UAAY,IAA2B;QAA3B,qBAAA,EAAA,SAA2B;QACrC,OAAO,IAAI,CAAC,OAAO,CAAC;YAClB,MAAM,EAAE,MAAM;YACd,KAAK,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AY,GAAG,SAAgF,CAAC;;;;wBAEhG,IAAI,OAAO,GAAG,kCAAsB,EAAE;4BACpC,WAAO,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,GAAG,CAAC,CAAC,EAAC;yBAC3C;wBACD,MAAM,IAAI,mCAAwB,CAAE,GAAsB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;;wBAG/E,IAAI,YAAY,CAAC,MAAM,KAAK,uCAAkB,CAAC,MAAM,EAAE;4BACrD,MAAM,IAAI,mCAAwB,CAChC,kCAAkC,EAClC,OAAO,EACP,YAAY,CAAC,QAAQ,CACtB,CAAC;yBACH;wBAED,WAAO,YAAY,CAAC,MAAM,KAAK,uCAAkB,CAAC,OAAO;gCACvD,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC;gCACvB,CAAC,CAAC,YAAY,EAAC;;;;KAClB;IACH,iBAAC;AAAD,CAAC,AAnSD,CAAwC,iBAAO,GAmS9C"}
|
package/dist/cjs/apiError.js
CHANGED
|
@@ -15,7 +15,7 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
15
15
|
};
|
|
16
16
|
})();
|
|
17
17
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
-
exports.ApiError = void 0;
|
|
18
|
+
exports.CreateContactsBatchError = exports.ApiError = void 0;
|
|
19
19
|
var ApiError = (function (_super) {
|
|
20
20
|
__extends(ApiError, _super);
|
|
21
21
|
function ApiError(statusCode, message, stack, translationKey) {
|
|
@@ -28,4 +28,16 @@ var ApiError = (function (_super) {
|
|
|
28
28
|
return ApiError;
|
|
29
29
|
}(Error));
|
|
30
30
|
exports.ApiError = ApiError;
|
|
31
|
+
var CreateContactsBatchError = (function (_super) {
|
|
32
|
+
__extends(CreateContactsBatchError, _super);
|
|
33
|
+
function CreateContactsBatchError(message, failedBatchId, batchMessages, processedBatchIds) {
|
|
34
|
+
var _this = _super.call(this, message) || this;
|
|
35
|
+
_this.failedBatchId = failedBatchId;
|
|
36
|
+
_this.batchMessages = batchMessages;
|
|
37
|
+
_this.processedBatchIds = processedBatchIds;
|
|
38
|
+
return _this;
|
|
39
|
+
}
|
|
40
|
+
return CreateContactsBatchError;
|
|
41
|
+
}(Error));
|
|
42
|
+
exports.CreateContactsBatchError = CreateContactsBatchError;
|
|
31
43
|
//# sourceMappingURL=apiError.js.map
|
package/dist/cjs/apiError.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apiError.js","sourceRoot":"","sources":["../../src/apiError.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAGA;IAA8B,4BAAK;IACjC,kBACkB,UAAe,EAC/B,OAAe,EACC,KAAc,EACd,cAAuB;QAJzC,YAME,kBAAM,OAAO,CAAC,SACf;QANiB,gBAAU,GAAV,UAAU,CAAK;QAEf,WAAK,GAAL,KAAK,CAAS;QACd,oBAAc,GAAd,cAAc,CAAS;;IAGzC,CAAC;IACH,eAAC;AAAD,CAAC,AATD,CAA8B,KAAK,GASlC;AATY,4BAAQ"}
|
|
1
|
+
{"version":3,"file":"apiError.js","sourceRoot":"","sources":["../../src/apiError.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;AAGA;IAA8B,4BAAK;IACjC,kBACkB,UAAe,EAC/B,OAAe,EACC,KAAc,EACd,cAAuB;QAJzC,YAME,kBAAM,OAAO,CAAC,SACf;QANiB,gBAAU,GAAV,UAAU,CAAK;QAEf,WAAK,GAAL,KAAK,CAAS;QACd,oBAAc,GAAd,cAAc,CAAS;;IAGzC,CAAC;IACH,eAAC;AAAD,CAAC,AATD,CAA8B,KAAK,GASlC;AATY,4BAAQ;AAWrB;IAA8C,4CAAK;IACjD,kCACE,OAAe,EACC,aAAqB,EACrB,aAAwB,EACjC,iBAA4B;QAJrC,YAME,kBAAM,OAAO,CAAC,SACf;QALiB,mBAAa,GAAb,aAAa,CAAQ;QACrB,mBAAa,GAAb,aAAa,CAAW;QACjC,uBAAiB,GAAjB,iBAAiB,CAAW;;IAGrC,CAAC;IACH,+BAAC;AAAD,CAAC,AATD,CAA8C,KAAK,GASlD;AATY,4DAAwB"}
|
package/dist/cjs/constants.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.CONTACTS_DELETE_MAX_AMOUNT = exports.FAILED_REQUEST_REPEATS = exports.REQUEST_PAYLOAD_MAX_BYTES = exports.CONTACTS_SERVICE_KEY = void 0;
|
|
4
4
|
exports.CONTACTS_SERVICE_KEY = 'contacts-api';
|
|
5
|
-
exports.REQUEST_PAYLOAD_MAX_BYTES =
|
|
5
|
+
exports.REQUEST_PAYLOAD_MAX_BYTES = 200000;
|
|
6
6
|
exports.FAILED_REQUEST_REPEATS = 3;
|
|
7
7
|
exports.CONTACTS_DELETE_MAX_AMOUNT = 5;
|
|
8
8
|
//# sourceMappingURL=constants.js.map
|
|
@@ -1,4 +1,14 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
1
10
|
import { BaseApi } from './baseApi';
|
|
11
|
+
import { BatchProcessStatus } from '@onereach/types-contacts-api';
|
|
2
12
|
export default class BatchProcessApi extends BaseApi {
|
|
3
13
|
constructor() {
|
|
4
14
|
super(...arguments);
|
|
@@ -10,5 +20,26 @@ export default class BatchProcessApi extends BaseApi {
|
|
|
10
20
|
route: `${this.apiBasePath}/${id}`,
|
|
11
21
|
});
|
|
12
22
|
}
|
|
23
|
+
getPendingBatchProcesses() {
|
|
24
|
+
return this.apiCall({
|
|
25
|
+
method: 'GET',
|
|
26
|
+
route: `${this.apiBasePath}/pending-batches`,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
trackBatchProcess(id) {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
const batchProcess = yield this.getBatchProcess(id);
|
|
32
|
+
let result = {
|
|
33
|
+
status: batchProcess.status,
|
|
34
|
+
};
|
|
35
|
+
if (batchProcess.status === BatchProcessStatus.success) {
|
|
36
|
+
result = Object.assign(Object.assign({}, result), { contactsIds: batchProcess.results.flatMap(i => JSON.parse(i)) });
|
|
37
|
+
}
|
|
38
|
+
if (batchProcess.status === BatchProcessStatus.failed) {
|
|
39
|
+
result = Object.assign(Object.assign({}, result), { errors: batchProcess.messages });
|
|
40
|
+
}
|
|
41
|
+
return result;
|
|
42
|
+
});
|
|
43
|
+
}
|
|
13
44
|
}
|
|
14
45
|
//# sourceMappingURL=batchProcessApi.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"batchProcessApi.js","sourceRoot":"","sources":["../../../src/api/batchProcessApi.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"batchProcessApi.js","sourceRoot":"","sources":["../../../src/api/batchProcessApi.ts"],"names":[],"mappings":";;;;;;;;;AACA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,EAA2B,kBAAkB,EAA6B,MAAM,8BAA8B,CAAC;AAEtH,MAAM,CAAC,OAAO,OAAO,eAAgB,SAAQ,OAAO;IAApD;;QACmB,gBAAW,GAAG,eAAe,CAAC;IA+CjD,CAAC;IA7CC,eAAe,CAAC,EAAU;QACxB,OAAO,IAAI,CAAC,OAAO,CAAC;YAClB,MAAM,EAAE,KAAK;YACb,KAAK,EAAE,GAAG,IAAI,CAAC,WAAW,IAAI,EAAE,EAAE;SACnC,CAAC,CAAC;IACL,CAAC;IAKD,wBAAwB;QACtB,OAAO,IAAI,CAAC,OAAO,CAAC;YAClB,MAAM,EAAE,KAAK;YACb,KAAK,EAAE,GAAG,IAAI,CAAC,WAAW,kBAAkB;SAC7C,CAAC,CAAC;IACL,CAAC;IAMK,iBAAiB,CAAC,EAAU;;YAChC,MAAM,YAAY,GAAG,MAAM,IAAI,CAAC,eAAe,CAAC,EAAE,CAAC,CAAC;YAEpD,IAAI,MAAM,GAA8B;gBACtC,MAAM,EAAE,YAAY,CAAC,MAAM;aAC5B,CAAC;YAEF,IAAI,YAAY,CAAC,MAAM,KAAK,kBAAkB,CAAC,OAAO,EAAE;gBACtD,MAAM,mCACD,MAAM,KACT,WAAW,EAAE,YAAY,CAAC,OAAO,CAAC,OAAO,CAAS,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GACtE,CAAC;aACH;YAED,IAAI,YAAY,CAAC,MAAM,KAAK,kBAAkB,CAAC,MAAM,EAAE;gBACrD,MAAM,mCACD,MAAM,KACT,MAAM,EAAE,YAAY,CAAC,QAAQ,GAC9B,CAAC;aACH;YAED,OAAO,MAAM,CAAC;QAChB,CAAC;KAAA;CAEF"}
|
|
@@ -22,6 +22,7 @@ import { BatchProcessStatus, } from '@onereach/types-contacts-api';
|
|
|
22
22
|
import { BaseApi } from './baseApi';
|
|
23
23
|
import { getObjectSizeInBytes, chunkArrByMaxSize, debouncePromise } from '../utils';
|
|
24
24
|
import { REQUEST_PAYLOAD_MAX_BYTES, FAILED_REQUEST_REPEATS, CONTACTS_DELETE_MAX_AMOUNT } from '../constants';
|
|
25
|
+
import { CreateContactsBatchError } from '../apiError';
|
|
25
26
|
export default class ContactApi extends BaseApi {
|
|
26
27
|
constructor(apiCall, batchProcessApi) {
|
|
27
28
|
super(apiCall);
|
|
@@ -81,9 +82,27 @@ export default class ContactApi extends BaseApi {
|
|
|
81
82
|
const contactsMaxSize = REQUEST_PAYLOAD_MAX_BYTES - getObjectSizeInBytes(Object.assign({}, rest));
|
|
82
83
|
const contactsChunks = chunkArrByMaxSize(contacts, contactsMaxSize);
|
|
83
84
|
console.log('chunks: ', contactsChunks.length);
|
|
84
|
-
|
|
85
|
-
? this.createContactsInSingleBatch(data)
|
|
86
|
-
: this.createContactsInMultiBatches(contactsChunks, rest);
|
|
85
|
+
const results = contactsChunks.length === 1
|
|
86
|
+
? [yield this.createContactsInSingleBatch(data)]
|
|
87
|
+
: yield this.createContactsInMultiBatches(contactsChunks, rest);
|
|
88
|
+
return results.reduce((acc, { contactsIds }) => ([...acc, ...contactsIds]), []);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
initCreateBatch(data) {
|
|
92
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
93
|
+
const { contacts } = data, rest = __rest(data, ["contacts"]);
|
|
94
|
+
const contactsMaxSize = REQUEST_PAYLOAD_MAX_BYTES - getObjectSizeInBytes(Object.assign({}, rest));
|
|
95
|
+
const contactsChunks = chunkArrByMaxSize(contacts, contactsMaxSize);
|
|
96
|
+
const batchProcess = yield this.apiCall({
|
|
97
|
+
method: 'POST',
|
|
98
|
+
route: `${this.apiBasePath}/bulk`,
|
|
99
|
+
data: Object.assign({ contacts: contactsChunks[0] }, rest),
|
|
100
|
+
});
|
|
101
|
+
return {
|
|
102
|
+
totalChunks: contactsChunks.length,
|
|
103
|
+
firstChunkSize: contactsChunks[0].length,
|
|
104
|
+
batchId: batchProcess.id,
|
|
105
|
+
};
|
|
87
106
|
});
|
|
88
107
|
}
|
|
89
108
|
mergeContacts(id, data) {
|
|
@@ -131,7 +150,7 @@ export default class ContactApi extends BaseApi {
|
|
|
131
150
|
yield this.polling(batchProcess.id);
|
|
132
151
|
});
|
|
133
152
|
}
|
|
134
|
-
createContactsInSingleBatch(data
|
|
153
|
+
createContactsInSingleBatch(data) {
|
|
135
154
|
return __awaiter(this, void 0, void 0, function* () {
|
|
136
155
|
const s = performance.now();
|
|
137
156
|
const batchProcess = yield this.apiCall({
|
|
@@ -140,15 +159,12 @@ export default class ContactApi extends BaseApi {
|
|
|
140
159
|
data,
|
|
141
160
|
});
|
|
142
161
|
const batchProcessResult = yield this.polling(batchProcess.id);
|
|
143
|
-
if (repeats === 2) {
|
|
144
|
-
throw new Error('Can not complete contacts creation batch');
|
|
145
|
-
}
|
|
146
|
-
if (batchProcessResult.status === BatchProcessStatus.failed) {
|
|
147
|
-
return yield debouncePromise(() => this.createContactsInSingleBatch(data, repeats + 1), 10000);
|
|
148
|
-
}
|
|
149
162
|
const e = performance.now();
|
|
150
163
|
console.log('single batch time: ', (e - s) / 1000, 's');
|
|
151
|
-
return
|
|
164
|
+
return {
|
|
165
|
+
batchId: batchProcess.id,
|
|
166
|
+
contactsIds: batchProcessResult.results.flatMap(i => JSON.parse(i)),
|
|
167
|
+
};
|
|
152
168
|
});
|
|
153
169
|
}
|
|
154
170
|
createContactsInMultiBatches(contactsChunks, data) {
|
|
@@ -161,9 +177,17 @@ export default class ContactApi extends BaseApi {
|
|
|
161
177
|
const results = [];
|
|
162
178
|
for (let i = 0; i < batchPromises.length; i += promisesBatchSize) {
|
|
163
179
|
const s = performance.now();
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
180
|
+
try {
|
|
181
|
+
const chunk = batchPromises.slice(i, i + promisesBatchSize);
|
|
182
|
+
const chunkResults = yield Promise.all(chunk.map((p) => p()));
|
|
183
|
+
results.push(...chunkResults);
|
|
184
|
+
}
|
|
185
|
+
catch (e) {
|
|
186
|
+
if (e instanceof CreateContactsBatchError) {
|
|
187
|
+
e.processedBatchIds = results.map(({ batchId }) => batchId);
|
|
188
|
+
}
|
|
189
|
+
throw e;
|
|
190
|
+
}
|
|
167
191
|
const e = performance.now();
|
|
168
192
|
console.log('awaiting ', promisesBatchSize, ' batches: ', (e - s) / 1000, 's');
|
|
169
193
|
}
|
|
@@ -172,19 +196,22 @@ export default class ContactApi extends BaseApi {
|
|
|
172
196
|
}
|
|
173
197
|
polling(batchId, repeats = 0) {
|
|
174
198
|
return __awaiter(this, void 0, void 0, function* () {
|
|
175
|
-
let
|
|
199
|
+
let batchProcess;
|
|
176
200
|
try {
|
|
177
|
-
|
|
201
|
+
batchProcess = yield debouncePromise(() => this.batchProcessApi.getBatchProcess(batchId), 1000);
|
|
178
202
|
}
|
|
179
203
|
catch (e) {
|
|
180
204
|
if (repeats < FAILED_REQUEST_REPEATS) {
|
|
181
205
|
return this.polling(batchId, repeats + 1);
|
|
182
206
|
}
|
|
183
|
-
throw e;
|
|
207
|
+
throw new CreateContactsBatchError(e.message, batchId);
|
|
208
|
+
}
|
|
209
|
+
if (batchProcess.status === BatchProcessStatus.failed) {
|
|
210
|
+
throw new CreateContactsBatchError('Could not complete batch process', batchId, batchProcess.messages);
|
|
184
211
|
}
|
|
185
|
-
return
|
|
212
|
+
return batchProcess.status === BatchProcessStatus.pending
|
|
186
213
|
? this.polling(batchId)
|
|
187
|
-
:
|
|
214
|
+
: batchProcess;
|
|
188
215
|
});
|
|
189
216
|
}
|
|
190
217
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contactApi.js","sourceRoot":"","sources":["../../../src/api/contactApi.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,OAAO,EAEL,kBAAkB,GAenB,MAAM,8BAA8B,CAAC;AAEtC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;
|
|
1
|
+
{"version":3,"file":"contactApi.js","sourceRoot":"","sources":["../../../src/api/contactApi.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA,OAAO,EAEL,kBAAkB,GAenB,MAAM,8BAA8B,CAAC;AAEtC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,EAAE,oBAAoB,EAAE,iBAAiB,EAAE,eAAe,E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AAE,CAAC;gBAE5B,OAAO,CAAC,GAAG,CAAC,WAAW,EAAE,iBAAiB,EAAE,YAAY,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,IAAI,EAAE,GAAG,CAAC,CAAC;aAChF;YAED,OAAO,OAAO,CAAC;QACjB,CAAC;KAAA;IAKa,OAAO,CAAC,OAAe,EAAE,OAAO,GAAG,CAAC;;YAChD,IAAI,YAAqC,CAAC;YAC1C,IAAI;gBACF,YAAY,GAAG,MAAM,eAAe,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,eAAe,CAAC,eAAe,CAAC,OAAO,CAAC,EAAE,IAAI,CAAC,CAAC;aACjG;YAAC,OAAO,CAAC,EAAE;gBACV,IAAI,OAAO,GAAG,sBAAsB,EAAE;oBACpC,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,GAAG,CAAC,CAAC,CAAC;iBAC3C;gBACD,MAAM,IAAI,wBAAwB,CAAE,CAAsB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;aAC9E;YAED,IAAI,YAAY,CAAC,MAAM,KAAK,kBAAkB,CAAC,MAAM,EAAE;gBACrD,MAAM,IAAI,wBAAwB,CAChC,kCAAkC,EAClC,OAAO,EACP,YAAY,CAAC,QAAQ,CACtB,CAAC;aACH;YAED,OAAO,YAAY,CAAC,MAAM,KAAK,kBAAkB,CAAC,OAAO;gBACvD,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC;gBACvB,CAAC,CAAC,YAAY,CAAC;QACnB,CAAC;KAAA;CACF"}
|
package/dist/esm/apiError.js
CHANGED
|
@@ -6,4 +6,12 @@ export class ApiError extends Error {
|
|
|
6
6
|
this.translationKey = translationKey;
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
|
+
export class CreateContactsBatchError extends Error {
|
|
10
|
+
constructor(message, failedBatchId, batchMessages, processedBatchIds) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.failedBatchId = failedBatchId;
|
|
13
|
+
this.batchMessages = batchMessages;
|
|
14
|
+
this.processedBatchIds = processedBatchIds;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
9
17
|
//# sourceMappingURL=apiError.js.map
|
package/dist/esm/apiError.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apiError.js","sourceRoot":"","sources":["../../src/apiError.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,QAAS,SAAQ,KAAK;IACjC,YACkB,UAAe,EAC/B,OAAe,EACC,KAAc,EACd,cAAuB;QAEvC,KAAK,CAAC,OAAO,CAAC,CAAC;QALC,eAAU,GAAV,UAAU,CAAK;QAEf,UAAK,GAAL,KAAK,CAAS;QACd,mBAAc,GAAd,cAAc,CAAS;IAGzC,CAAC;CACF"}
|
|
1
|
+
{"version":3,"file":"apiError.js","sourceRoot":"","sources":["../../src/apiError.ts"],"names":[],"mappings":"AAGA,MAAM,OAAO,QAAS,SAAQ,KAAK;IACjC,YACkB,UAAe,EAC/B,OAAe,EACC,KAAc,EACd,cAAuB;QAEvC,KAAK,CAAC,OAAO,CAAC,CAAC;QALC,eAAU,GAAV,UAAU,CAAK;QAEf,UAAK,GAAL,KAAK,CAAS;QACd,mBAAc,GAAd,cAAc,CAAS;IAGzC,CAAC;CACF;AAED,MAAM,OAAO,wBAAyB,SAAQ,KAAK;IACjD,YACE,OAAe,EACC,aAAqB,EACrB,aAAwB,EACjC,iBAA4B;QAEnC,KAAK,CAAC,OAAO,CAAC,CAAC;QAJC,kBAAa,GAAb,aAAa,CAAQ;QACrB,kBAAa,GAAb,aAAa,CAAW;QACjC,sBAAiB,GAAjB,iBAAiB,CAAW;IAGrC,CAAC;CACF"}
|
package/dist/esm/constants.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export const CONTACTS_SERVICE_KEY = 'contacts-api';
|
|
2
|
-
export const REQUEST_PAYLOAD_MAX_BYTES =
|
|
2
|
+
export const REQUEST_PAYLOAD_MAX_BYTES = 200000;
|
|
3
3
|
export const FAILED_REQUEST_REPEATS = 3;
|
|
4
4
|
export const CONTACTS_DELETE_MAX_AMOUNT = 5;
|
|
5
5
|
//# sourceMappingURL=constants.js.map
|
|
@@ -1,7 +1,10 @@
|
|
|
1
|
+
import { TrackBatchProcessResponse } from '../types';
|
|
1
2
|
import { BaseApi } from './baseApi';
|
|
2
|
-
import { BatchProcessResponseDto } from '@onereach/types-contacts-api';
|
|
3
|
+
import { BatchProcessResponseDto, PendingBatchesResponseDto } from '@onereach/types-contacts-api';
|
|
3
4
|
export default class BatchProcessApi extends BaseApi {
|
|
4
5
|
private readonly apiBasePath;
|
|
5
6
|
getBatchProcess(id: string): Promise<BatchProcessResponseDto>;
|
|
7
|
+
getPendingBatchProcesses(): Promise<PendingBatchesResponseDto>;
|
|
8
|
+
trackBatchProcess(id: string): Promise<TrackBatchProcessResponse>;
|
|
6
9
|
}
|
|
7
10
|
//# sourceMappingURL=batchProcessApi.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"batchProcessApi.d.ts","sourceRoot":"","sources":["../../../src/api/batchProcessApi.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,
|
|
1
|
+
{"version":3,"file":"batchProcessApi.d.ts","sourceRoot":"","sources":["../../../src/api/batchProcessApi.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,UAAU,CAAC;AACrD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,OAAO,EAAE,uBAAuB,EAAsB,yBAAyB,EAAE,MAAM,8BAA8B,CAAC;AAEtH,MAAM,CAAC,OAAO,OAAO,eAAgB,SAAQ,OAAO;IAClD,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAmB;IAE/C,eAAe,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,uBAAuB,CAAC;IAU7D,wBAAwB,IAAI,OAAO,CAAC,yBAAyB,CAAC;IAWxD,iBAAiB,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,yBAAyB,CAAC;CAwBxE"}
|
|
@@ -2,6 +2,7 @@ import { ContactParamsDto, ContactResponseDto, CreateContactDto, CreateMultipleC
|
|
|
2
2
|
import { CalApiParams, List } from '@or-sdk/base';
|
|
3
3
|
import { BaseApi } from './baseApi';
|
|
4
4
|
import BatchProcessApi from './batchProcessApi';
|
|
5
|
+
import { InitCreateBatchResponse } from '../types';
|
|
5
6
|
export default class ContactApi extends BaseApi {
|
|
6
7
|
protected readonly apiCall: <T>(params: CalApiParams) => Promise<T>;
|
|
7
8
|
private batchProcessApi;
|
|
@@ -14,6 +15,7 @@ export default class ContactApi extends BaseApi {
|
|
|
14
15
|
deleteMulti(data: DeleteContactMultiParamsDto): Promise<void>;
|
|
15
16
|
createContact(data: CreateContactDto): Promise<ContactResponseDto>;
|
|
16
17
|
bulkCreateContacts(data: CreateMultipleContactsDto): Promise<string[]>;
|
|
18
|
+
initCreateBatch(data: CreateMultipleContactsDto): Promise<InitCreateBatchResponse>;
|
|
17
19
|
mergeContacts(id: string, data: MergeContactParamsDto): Promise<ContactResponseDto>;
|
|
18
20
|
linkContact(id: string, data: LinkContactParamsDto): Promise<void>;
|
|
19
21
|
linkContactsMulti(data: LinkContactMultiParamsDto): Promise<void>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contactApi.d.ts","sourceRoot":"","sources":["../../../src/api/contactApi.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,gBAAgB,EAEhB,kBAAkB,EAClB,gBAAgB,EAChB,yBAAyB,EACzB,2BAA2B,EAC3B,sBAAsB,EACtB,gBAAgB,EAChB,yBAAyB,EACzB,oBAAoB,EACpB,qBAAqB,EACrB,2BAA2B,EAC3B,sBAAsB,EACtB,gBAAgB,EACjB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC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,aAAa,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,sBAAsB,GAAG,OAAO,CAAC,IAAI,CAAC;IAatE,WAAW,CAAC,IAAI,EAAE,2BAA2B,GAAG,OAAO,CAAC,IAAI,CAAC;IAe7D,aAAa,CAAC,IAAI,EAAE,gBAAgB,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAW5D,kBAAkB,CAAC,IAAI,EAAE,yBAAyB,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IA2BtE,eAAe,CAAC,IAAI,EAAE,yBAAyB,GAAG,OAAO,CAAC,uBAAuB,CAAC;IA2BxF,aAAa,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,qBAAqB,GAAG,OAAO,CAAC,kBAAkB,CAAC;IAanF,WAAW,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,oBAAoB,GAAG,OAAO,CAAC,IAAI,CAAC;IAYlE,iBAAiB,CAAC,IAAI,EAAE,yBAAyB,GAAG,OAAO,CAAC,IAAI,CAAC;IAajE,aAAa,CAAC,EAAE,EAAE,MAAM,EAAE,IAAI,EAAE,sBAAsB,GAAG,OAAO,CAAC,IAAI,CAAC;IAYtE,mBAAmB,CAAC,IAAI,EAAE,2BAA2B,GAAG,OAAO,CAAC,IAAI,CAAC;YAQvD,kBAAkB;YASlB,2BAA2B;YAiB3B,4BAA4B;YAsC5B,OAAO;CAuBtB"}
|
package/dist/types/apiError.d.ts
CHANGED
|
@@ -4,4 +4,10 @@ export declare class ApiError extends Error {
|
|
|
4
4
|
readonly translationKey?: string | undefined;
|
|
5
5
|
constructor(statusCode: any, message: string, stack?: string | undefined, translationKey?: string | undefined);
|
|
6
6
|
}
|
|
7
|
+
export declare class CreateContactsBatchError extends Error {
|
|
8
|
+
readonly failedBatchId: string;
|
|
9
|
+
readonly batchMessages?: string[] | undefined;
|
|
10
|
+
processedBatchIds?: string[] | undefined;
|
|
11
|
+
constructor(message: string, failedBatchId: string, batchMessages?: string[] | undefined, processedBatchIds?: string[] | undefined);
|
|
12
|
+
}
|
|
7
13
|
//# sourceMappingURL=apiError.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apiError.d.ts","sourceRoot":"","sources":["../../src/apiError.ts"],"names":[],"mappings":"AAGA,qBAAa,QAAS,SAAQ,KAAK;aAEf,UAAU,EAAE,GAAG;aAEf,KAAK,CAAC;aACN,cAAc,CAAC;gBAHf,UAAU,EAAE,GAAG,EAC/B,OAAO,EAAE,MAAM,EACC,KAAK,CAAC,oBAAQ,EACd,cAAc,CAAC,oBAAQ;CAI1C"}
|
|
1
|
+
{"version":3,"file":"apiError.d.ts","sourceRoot":"","sources":["../../src/apiError.ts"],"names":[],"mappings":"AAGA,qBAAa,QAAS,SAAQ,KAAK;aAEf,UAAU,EAAE,GAAG;aAEf,KAAK,CAAC;aACN,cAAc,CAAC;gBAHf,UAAU,EAAE,GAAG,EAC/B,OAAO,EAAE,MAAM,EACC,KAAK,CAAC,oBAAQ,EACd,cAAc,CAAC,oBAAQ;CAI1C;AAED,qBAAa,wBAAyB,SAAQ,KAAK;aAG/B,aAAa,EAAE,MAAM;aACrB,aAAa,CAAC;IACvB,iBAAiB,CAAC;gBAHzB,OAAO,EAAE,MAAM,EACC,aAAa,EAAE,MAAM,EACrB,aAAa,CAAC,sBAAU,EACjC,iBAAiB,CAAC,sBAAU;CAItC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export declare const CONTACTS_SERVICE_KEY = "contacts-api";
|
|
2
|
-
export declare const REQUEST_PAYLOAD_MAX_BYTES =
|
|
2
|
+
export declare const REQUEST_PAYLOAD_MAX_BYTES = 200000;
|
|
3
3
|
export declare const FAILED_REQUEST_REPEATS = 3;
|
|
4
4
|
export declare const CONTACTS_DELETE_MAX_AMOUNT = 5;
|
|
5
5
|
//# sourceMappingURL=constants.d.ts.map
|
package/dist/types/types.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Token } from '@or-sdk/base';
|
|
2
|
-
import { ContactBookParamsDto, ListApiParams, OrderParams } from '@onereach/types-contacts-api';
|
|
2
|
+
import { BatchProcessStatus, ContactBookParamsDto, ListApiParams, OrderParams } from '@onereach/types-contacts-api';
|
|
3
3
|
import { OrderOptions, PaginationOptions } from '@or-sdk/base';
|
|
4
4
|
export * from '@onereach/types-contacts-api';
|
|
5
5
|
export { OrderDirection, PaginationOptions, OrderOptions } from '@or-sdk/base';
|
|
@@ -13,4 +13,18 @@ export interface ContactBookParams extends AdaptedListParams<ContactBookParamsDt
|
|
|
13
13
|
orderProperty?: 'label' | 'created_at' | 'updated_at';
|
|
14
14
|
}
|
|
15
15
|
export declare type AdaptedListParams<T extends ListApiParams & OrderParams> = Omit<T, 'order' | 'skip' | 'take'> & Partial<PaginationOptions & OrderOptions>;
|
|
16
|
+
export declare type CreateContactsBatchResults = {
|
|
17
|
+
batchId: string;
|
|
18
|
+
contactsIds: string[];
|
|
19
|
+
};
|
|
20
|
+
export declare type InitCreateBatchResponse = {
|
|
21
|
+
totalChunks: number;
|
|
22
|
+
firstChunkSize: number;
|
|
23
|
+
batchId: string;
|
|
24
|
+
};
|
|
25
|
+
export declare type TrackBatchProcessResponse = {
|
|
26
|
+
status: BatchProcessStatus;
|
|
27
|
+
errors?: string[];
|
|
28
|
+
contactsIds?: string[];
|
|
29
|
+
};
|
|
16
30
|
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACrC,OAAO,EAAE,oBAAoB,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,8BAA8B,CAAC;
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AACrC,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2.2-beta.1755.0",
|
|
3
3
|
"name": "@or-sdk/contacts",
|
|
4
4
|
"main": "dist/cjs/index.js",
|
|
5
5
|
"module": "dist/esm/index.js",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
"access": "public"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@onereach/types-contacts-api": "4.
|
|
27
|
+
"@onereach/types-contacts-api": "4.7.2-beta.2230.0",
|
|
28
28
|
"@or-sdk/base": "^0.28.3"
|
|
29
29
|
}
|
|
30
30
|
}
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
+
import { TrackBatchProcessResponse } from '../types';
|
|
1
2
|
import { BaseApi } from './baseApi';
|
|
2
3
|
|
|
3
|
-
import {
|
|
4
|
-
BatchProcessResponseDto,
|
|
5
|
-
} from '@onereach/types-contacts-api';
|
|
4
|
+
import { BatchProcessResponseDto, BatchProcessStatus, PendingBatchesResponseDto } from '@onereach/types-contacts-api';
|
|
6
5
|
|
|
7
6
|
export default class BatchProcessApi extends BaseApi {
|
|
8
7
|
private readonly apiBasePath = 'batch-process';
|
|
@@ -13,4 +12,43 @@ export default class BatchProcessApi extends BaseApi {
|
|
|
13
12
|
route: `${this.apiBasePath}/${id}`,
|
|
14
13
|
});
|
|
15
14
|
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Returns the count of pending batch processes and their ids.
|
|
18
|
+
*/
|
|
19
|
+
getPendingBatchProcesses(): Promise<PendingBatchesResponseDto> {
|
|
20
|
+
return this.apiCall({
|
|
21
|
+
method: 'GET',
|
|
22
|
+
route: `${this.apiBasePath}/pending-batches`,
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Provides information about create contacts batch process: its status, errors if
|
|
28
|
+
* the status is failed, and created contacts ids if the status is success.
|
|
29
|
+
*/
|
|
30
|
+
async trackBatchProcess(id: string): Promise<TrackBatchProcessResponse> {
|
|
31
|
+
const batchProcess = await this.getBatchProcess(id);
|
|
32
|
+
|
|
33
|
+
let result: TrackBatchProcessResponse = {
|
|
34
|
+
status: batchProcess.status,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
if (batchProcess.status === BatchProcessStatus.success) {
|
|
38
|
+
result = {
|
|
39
|
+
...result,
|
|
40
|
+
contactsIds: batchProcess.results.flatMap<string>(i => JSON.parse(i)),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (batchProcess.status === BatchProcessStatus.failed) {
|
|
45
|
+
result = {
|
|
46
|
+
...result,
|
|
47
|
+
errors: batchProcess.messages,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return result;
|
|
52
|
+
}
|
|
53
|
+
|
|
16
54
|
}
|
package/src/api/contactApi.ts
CHANGED
|
@@ -20,7 +20,9 @@ import { CalApiParams, List } from '@or-sdk/base';
|
|
|
20
20
|
import { BaseApi } from './baseApi';
|
|
21
21
|
import BatchProcessApi from './batchProcessApi';
|
|
22
22
|
import { getObjectSizeInBytes, chunkArrByMaxSize, debouncePromise } from '../utils';
|
|
23
|
+
import { InitCreateBatchResponse, CreateContactsBatchResults } from '../types';
|
|
23
24
|
import { REQUEST_PAYLOAD_MAX_BYTES, FAILED_REQUEST_REPEATS, CONTACTS_DELETE_MAX_AMOUNT } from '../constants';
|
|
25
|
+
import { CreateContactsBatchError } from '../apiError';
|
|
24
26
|
|
|
25
27
|
export default class ContactApi extends BaseApi {
|
|
26
28
|
constructor(
|
|
@@ -122,12 +124,47 @@ export default class ContactApi extends BaseApi {
|
|
|
122
124
|
const contactsChunks = chunkArrByMaxSize(contacts, contactsMaxSize);
|
|
123
125
|
// eslint-disable-next-line no-console
|
|
124
126
|
console.log('chunks: ', contactsChunks.length);
|
|
127
|
+
const results = contactsChunks.length === 1
|
|
128
|
+
? [await this.createContactsInSingleBatch(data)]
|
|
129
|
+
: await this.createContactsInMultiBatches(contactsChunks, rest);
|
|
125
130
|
|
|
126
|
-
return
|
|
127
|
-
? this.createContactsInSingleBatch(data)
|
|
128
|
-
: this.createContactsInMultiBatches(contactsChunks, rest);
|
|
131
|
+
return results.reduce<string[]>((acc, { contactsIds }) => ([...acc, ...contactsIds]), []);
|
|
129
132
|
}
|
|
130
133
|
|
|
134
|
+
/**
|
|
135
|
+
* Breaks the contacts to chunks of maximum allowed size (200 KB) and launches create batch
|
|
136
|
+
* only for the first chunk.
|
|
137
|
+
* Returns batch id in response by which the batch can be tracked using batch process API.
|
|
138
|
+
* Note: since create contact batch is a very expensive operations from terms of consuming
|
|
139
|
+
* DB connections, it is highly recommended to check amount of pending (running) batches
|
|
140
|
+
* prior to this method execution, and if the amount is greater of 2 the batch initiation
|
|
141
|
+
* should be deferred.
|
|
142
|
+
*
|
|
143
|
+
* @see batchProcessApi.trackBatchProcess
|
|
144
|
+
* @see batchProcessApi.getPendingBatchProcesses
|
|
145
|
+
*/
|
|
146
|
+
async initCreateBatch(data: CreateMultipleContactsDto): Promise<InitCreateBatchResponse> {
|
|
147
|
+
const { contacts, ...rest } = data;
|
|
148
|
+
const contactsMaxSize = REQUEST_PAYLOAD_MAX_BYTES - getObjectSizeInBytes({ ...rest });
|
|
149
|
+
const contactsChunks = chunkArrByMaxSize(contacts, contactsMaxSize);
|
|
150
|
+
|
|
151
|
+
const batchProcess = await this.apiCall<BatchProcessResponseDto>({
|
|
152
|
+
method: 'POST',
|
|
153
|
+
route: `${this.apiBasePath}/bulk`,
|
|
154
|
+
data: {
|
|
155
|
+
contacts: contactsChunks[0],
|
|
156
|
+
...rest,
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
return {
|
|
161
|
+
totalChunks: contactsChunks.length,
|
|
162
|
+
firstChunkSize: contactsChunks[0].length,
|
|
163
|
+
batchId: batchProcess.id,
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
|
|
131
168
|
/**
|
|
132
169
|
* @description Merge two Contacts into one
|
|
133
170
|
* @param id Contact id TO which the data will be merged
|
|
@@ -200,7 +237,7 @@ export default class ContactApi extends BaseApi {
|
|
|
200
237
|
await this.polling(batchProcess.id);
|
|
201
238
|
}
|
|
202
239
|
|
|
203
|
-
private async createContactsInSingleBatch(data: CreateMultipleContactsDto
|
|
240
|
+
private async createContactsInSingleBatch(data: CreateMultipleContactsDto): Promise<CreateContactsBatchResults> {
|
|
204
241
|
const s = performance.now();
|
|
205
242
|
const batchProcess = await this.apiCall<BatchProcessResponseDto>({
|
|
206
243
|
method: 'POST',
|
|
@@ -208,22 +245,19 @@ export default class ContactApi extends BaseApi {
|
|
|
208
245
|
data,
|
|
209
246
|
});
|
|
210
247
|
const batchProcessResult = await this.polling(batchProcess.id);
|
|
211
|
-
if (repeats === 2) {
|
|
212
|
-
throw new Error('Can not complete contacts creation batch');
|
|
213
|
-
}
|
|
214
|
-
if (batchProcessResult.status === BatchProcessStatus.failed) {
|
|
215
|
-
return await debouncePromise(() => this.createContactsInSingleBatch(data, repeats + 1), 10000);
|
|
216
|
-
}
|
|
217
248
|
const e = performance.now();
|
|
218
249
|
// eslint-disable-next-line no-console
|
|
219
250
|
console.log('single batch time: ', (e - s) / 1000, 's');
|
|
220
|
-
return
|
|
251
|
+
return {
|
|
252
|
+
batchId: batchProcess.id,
|
|
253
|
+
contactsIds: batchProcessResult.results.flatMap<string>(i => JSON.parse(i)),
|
|
254
|
+
};
|
|
221
255
|
}
|
|
222
256
|
|
|
223
257
|
private async createContactsInMultiBatches(
|
|
224
258
|
contactsChunks: ContactRequestDto[][],
|
|
225
259
|
data: Omit<CreateMultipleContactsDto, 'contacts'>
|
|
226
|
-
): Promise<
|
|
260
|
+
): Promise<CreateContactsBatchResults[]> {
|
|
227
261
|
const batchPromises = contactsChunks
|
|
228
262
|
.map((chunkContacts) => async () => this.createContactsInSingleBatch({
|
|
229
263
|
contacts: chunkContacts,
|
|
@@ -231,13 +265,22 @@ export default class ContactApi extends BaseApi {
|
|
|
231
265
|
}));
|
|
232
266
|
|
|
233
267
|
const promisesBatchSize = 2;
|
|
234
|
-
const results:
|
|
268
|
+
const results: CreateContactsBatchResults[] = [];
|
|
235
269
|
|
|
236
270
|
for (let i = 0;i < batchPromises.length;i += promisesBatchSize) {
|
|
237
271
|
const s = performance.now();
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
272
|
+
try {
|
|
273
|
+
const chunk = batchPromises.slice(i, i + promisesBatchSize);
|
|
274
|
+
const chunkResults = await Promise.all(chunk.map((p) => p()));
|
|
275
|
+
// results.push(...chunkResults.reduce((acc, r) => ([...acc, ...r]), []));
|
|
276
|
+
results.push(...chunkResults);
|
|
277
|
+
} catch (e) {
|
|
278
|
+
if (e instanceof CreateContactsBatchError) {
|
|
279
|
+
e.processedBatchIds = results.map(({ batchId }) => batchId);
|
|
280
|
+
}
|
|
281
|
+
throw e;
|
|
282
|
+
}
|
|
283
|
+
|
|
241
284
|
const e = performance.now();
|
|
242
285
|
// eslint-disable-next-line no-console
|
|
243
286
|
console.log('awaiting ', promisesBatchSize, ' batches: ', (e - s) / 1000, 's');
|
|
@@ -250,18 +293,26 @@ export default class ContactApi extends BaseApi {
|
|
|
250
293
|
* Pols specific batch process until status of it isn't turned from 'pending to something else.
|
|
251
294
|
*/
|
|
252
295
|
private async polling(batchId: string, repeats = 0): Promise<BatchProcessResponseDto> {
|
|
253
|
-
let
|
|
296
|
+
let batchProcess: BatchProcessResponseDto;
|
|
254
297
|
try {
|
|
255
|
-
|
|
298
|
+
batchProcess = await debouncePromise(() => this.batchProcessApi.getBatchProcess(batchId), 1000);
|
|
256
299
|
} catch (e) {
|
|
257
300
|
if (repeats < FAILED_REQUEST_REPEATS) {
|
|
258
301
|
return this.polling(batchId, repeats + 1);
|
|
259
302
|
}
|
|
260
|
-
throw e;
|
|
303
|
+
throw new CreateContactsBatchError((e as unknown as Error).message, batchId);
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
if (batchProcess.status === BatchProcessStatus.failed) {
|
|
307
|
+
throw new CreateContactsBatchError(
|
|
308
|
+
'Could not complete batch process',
|
|
309
|
+
batchId,
|
|
310
|
+
batchProcess.messages
|
|
311
|
+
);
|
|
261
312
|
}
|
|
262
313
|
|
|
263
|
-
return
|
|
314
|
+
return batchProcess.status === BatchProcessStatus.pending
|
|
264
315
|
? this.polling(batchId)
|
|
265
|
-
:
|
|
316
|
+
: batchProcess;
|
|
266
317
|
}
|
|
267
318
|
}
|
package/src/apiError.ts
CHANGED
|
@@ -11,3 +11,14 @@ export class ApiError extends Error {
|
|
|
11
11
|
super(message);
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
|
+
|
|
15
|
+
export class CreateContactsBatchError extends Error {
|
|
16
|
+
constructor(
|
|
17
|
+
message: string,
|
|
18
|
+
public readonly failedBatchId: string,
|
|
19
|
+
public readonly batchMessages?: string[],
|
|
20
|
+
public processedBatchIds?: string[],
|
|
21
|
+
) {
|
|
22
|
+
super(message);
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/constants.ts
CHANGED
package/src/types.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Token } from '@or-sdk/base';
|
|
2
|
-
import { ContactBookParamsDto, ListApiParams, OrderParams } from '@onereach/types-contacts-api';
|
|
2
|
+
import { BatchProcessStatus, ContactBookParamsDto, ListApiParams, OrderParams } from '@onereach/types-contacts-api';
|
|
3
3
|
import { OrderOptions, PaginationOptions } from '@or-sdk/base';
|
|
4
4
|
export * from '@onereach/types-contacts-api';
|
|
5
5
|
|
|
@@ -30,3 +30,20 @@ export interface ContactBookParams extends AdaptedListParams<ContactBookParamsDt
|
|
|
30
30
|
|
|
31
31
|
export type AdaptedListParams<T extends ListApiParams & OrderParams> =
|
|
32
32
|
Omit<T, 'order' | 'skip' | 'take'> & Partial<PaginationOptions & OrderOptions>;
|
|
33
|
+
|
|
34
|
+
export type CreateContactsBatchResults ={
|
|
35
|
+
batchId: string;
|
|
36
|
+
contactsIds: string[];
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export type InitCreateBatchResponse = {
|
|
40
|
+
totalChunks: number;
|
|
41
|
+
firstChunkSize: number;
|
|
42
|
+
batchId: string;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type TrackBatchProcessResponse = {
|
|
46
|
+
status: BatchProcessStatus;
|
|
47
|
+
errors?: string[];
|
|
48
|
+
contactsIds?: string[];
|
|
49
|
+
};
|